Loading Trix javascript after `trix-editor` is in the DOM: Setting `Trix.config.lang` does not set custom text in toolbar
See original GitHub issueExpected: Toolbar texts are set according to those specified in Trix.config.lang
Actual:
We are running trix/dist/trix.js
AFTER the trix-editor
is already in the DOM.
Using Rails 6.0 and Webpacker.
Steps to Reproduce
javascript code, which is loaded asynchronously (not placed in the DOM in a script tag before the trix-editor
element).
import 'trix/dist/trix.css'
import Trix from 'trix/dist/trix.js'
Trix.config.lang = {
bold: 'Gras',
bullets: 'Liste',
byte: 'Octet',
bytes: 'Octets',
captionPlaceholder: 'Ajouter légende…',
code: 'Code',
heading1: 'Titre',
indent: 'Agrandir retrait',
italic: 'Italique',
link: 'Lien',
numbers: 'Liste numérotée',
outdent: 'Réduire retrait',
quote: 'Citation',
redo: 'Rétablir',
remove: 'Supprimer',
strike: 'Barré',
undo: 'Annuler',
unlink: 'Supprimer lien',
url: 'Lien',
urlPlaceholder: 'Saisir adresse URL…',
GB: 'Go',
KB: 'Ko',
MB: 'Mo',
PB: 'Po',
TB: 'To'
}
The trix-editor
element is placed in the DOM via ActionText rich_text_area
, which is in a server-rendered Rails template.
Details
- Trix version: https://github.com/basecamp/trix/commit/520cc50afff2b90120f49038945eb0cb73d82535
- Browser name and version: Chrome 87
- Operating system: macOS 11.1
- Rails version: 6.0.3.4 (with ActiveText)
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
How to customize the Trix toolbar in Rails 7 when using ...
js appears to be loading correctly via my import map because I'm getting a ReferenceError: Can't find variable: require in the console. So...
Read more >Modifying the default toolbar in Trix - DEV Community
The first thing you may be tempted to do when exploring with Trix is to change the default toolbar. It's not obvious how...
Read more >Build a Table Editor with Trix and Turbo Frames in Rails
Let's build a table editor in a Rails application using ActionText's Trix editor, and use Turbo Frames to customize your table.
Read more >Prism.js
css and prism.js, use proper HTML5 code tags ( code.language-xxxx ), done! Intuitive Language classes are inherited so you can only define ......
Read more >Trix: A Rich Text Editor for Everyday Writing - Morioh
Trix won't load any HTML content inside a <trix-editor>…</trix-editor> ... trix-attachment-add fires after an attachment is added to the document. You can ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve just experienced a similar bug. You cannot set
Trix.config.lang
directly (LikeTrix.config.lang = translations
). Instead, you need to update each individual key of the object.My working implementation using Turbolinks + Webpacker looks like this:
It is also possible to change the caption placeholder in the
trix-editor
element this way.Update: Clarify I’m using Turbolinks and webpacker
Update: We have changed our implementation to get around this issue:
trix-editor
element still rendered withrich_text_area
via server-side rendered Rails template_trix_editor.html.haml
). We are usingjavascript_pack_tag
from Webpacker to include the Trix script within the same template and to make sure it runs before the rest of the ._trix_editor.html.haml
:app/javascript/pack/trix.js
:This fixes the issue where the text wasn’t being set in time before the editor was rendered, on page loads where the script does run before the editor is rendered. This desired behavior occurs when we enter the page URL directly in the browser or refresh the page.
However, we still run into the issue that if the page is loaded by turbolinks, the Webpacker packed script included via
javascript_pack_tag
is NOT executed in time. The editor is rendered in the DOM before the script runs. I think this is related to https://github.com/turbolinks/turbolinks/issues/107Therefore, we’ve had to disable turbolinks on the pages where we show a Trix editor 😦
@javan Please let us know if we can provide any further info to help debug and/or fix this issue! Does it make sense to support running the
trix.js
script asynchronously potentially after the DOM element has been rendered?