Skip to content

Commit 4253057

Browse files
committed
Make Trix compatible with morphing
Morphing the trix editor will leave it in an unusuable state because morphing will replace the bootstrapping elements without triggering the connection logic. The solution is based on adding an attribute to flag the editor as initialized and detect when that attribute is modified. See hotwired/turbo-rails#533 (comment)
1 parent 7bf3e5a commit 4253057

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/trix/elements/trix_editor_element.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ class LegacyDelegate {
348348
export default class TrixEditorElement extends HTMLElement {
349349
static formAssociated = "ElementInternals" in window
350350

351+
static get observedAttributes() {
352+
return [ "initialized" ]
353+
}
354+
351355
#delegate
352356

353357
constructor() {
@@ -453,6 +457,14 @@ export default class TrixEditorElement extends HTMLElement {
453457
this.editor?.loadHTML(this.defaultValue)
454458
}
455459

460+
// Element callbacks
461+
462+
attributeChangedCallback(name, oldValue, newValue) {
463+
if (name === "initialized" && oldValue && oldValue !== newValue) {
464+
requestAnimationFrame(() => this.reconnect())
465+
}
466+
}
467+
456468
// Controller delegate methods
457469

458470
notify(message, data) {
@@ -485,6 +497,8 @@ export default class TrixEditorElement extends HTMLElement {
485497
}
486498
this.editorController.registerSelectionManager()
487499
this.#delegate.connectedCallback()
500+
501+
this.setAttribute("initialized", "true")
488502
autofocus(this)
489503
}
490504
}
@@ -494,6 +508,11 @@ export default class TrixEditorElement extends HTMLElement {
494508
this.#delegate.disconnectedCallback()
495509
}
496510

511+
async reconnect() {
512+
this.disconnectedCallback()
513+
this.connectedCallback()
514+
}
515+
497516
// Form support
498517

499518
checkValidity() {

0 commit comments

Comments
 (0)