Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trix editor breaks after page morph #1142

Closed
pinzonjulian opened this issue Jan 28, 2024 · 1 comment
Closed

Trix editor breaks after page morph #1142

pinzonjulian opened this issue Jan 28, 2024 · 1 comment
Assignees

Comments

@pinzonjulian
Copy link

pinzonjulian commented Jan 28, 2024

Example repo

Fresh Rails app built to showcase this behaviour
https://github.com/pinzonjulian/trix_morphing_issue_turbo_8_beta_4

Rails 7.1.3
turbo-rails 8.0.0.beta4

Issue

In a new Rails app using the latest beta (4) for Turbo 8, a trix-editor breaks after morphing.

At first I thought this might be related to #1133 and #1140 but upon further inspection it seems that this is not related to stylesheets but to the initialisation of the Trix custom element.

Step by step

(low quality because GitHub requires 10mb max. See screenshots below for hi-def)

trix_morph_issue_480.mov

First load

  • Page is loaded
  • Trix adds styles to the head
  • Refreshes are set to use morph and to preserve scroll
1

After submitting the form

  • Page refreshes
  • Trix editor breaks: Toolbar disappears from the dom, rich text area does not allow any input.
2

Verifying that morph is actually happening

This screenshot was taken after reloading the browser manually. Notice how, right after morphing, only the affected nodes light up. More specifically, the element with ID comment_1 is not morphed.
3

Workarounds

❌data-turbo-permanent in form helper

If I add data-turbo-permanent to the form helper form.rich_text_area :body, data: { turbo_permanent: true } the editor keeps being interactive but the toolbar disappears.

✅ data-turbo-permanent wrapping form helper

This does work. Is this the suggested way to make Trix work with morhping?

@pinzonjulian pinzonjulian changed the title 8.0.0.beta4 - Trix editor breaks after page morph 8.0.0.pre.beta4 - Trix editor breaks after page morph Jan 28, 2024
@afcapel afcapel changed the title 8.0.0.pre.beta4 - Trix editor breaks after page morph Trix editor breaks after page morph Jan 29, 2024
@afcapel
Copy link
Collaborator

afcapel commented Jan 29, 2024

Unfortunately this is a limitation of morphing. It's a common practice for libraries to assume they can be in charge of some part of the DOM, and that part will only change using the library public APIs. For example, Trix assumes it will have full control of the contenteditable area and the toolbar. When those elements change in a morph update Trix loses its state.

There's not much Trix can do about this, mutating the editor content bypassing Trix API will always have unpredictable consequences.

Using data-turbo-permanent is the preferred approach here. You need to ensure data-turbo-permanent wraps both the editor and the toolbar, that's why you need to put it in a wrapper element.

@afcapel afcapel closed this as completed Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants