Skip to content

Commit aa5388e

Browse files
web-padawansujoykd
andauthored
fix: re-initialize Quill on detach and re-attach and keep htmlValue (#6506)
Co-authored-by: sujoykd <106066145+sujoykd@users.noreply.github.com>
1 parent 4468b75 commit aa5388e

2 files changed

Lines changed: 69 additions & 11 deletions

File tree

‎packages/rich-text-editor/src/vaadin-rich-text-editor.js‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,20 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
533533
}
534534
}
535535

536+
/** @protected */
537+
disconnectedCallback() {
538+
super.disconnectedCallback();
539+
540+
// Ensure that htmlValue property set before attach
541+
// gets applied in case of detach and re-attach.
542+
if (this.__debounceSetValue && this.__debounceSetValue.isActive()) {
543+
this.__debounceSetValue.flush();
544+
}
545+
546+
this._editor.emitter.removeAllListeners();
547+
this._editor.emitter.listeners = {};
548+
}
549+
536550
/** @private */
537551
__setDirection(dir) {
538552
// Needed for proper `ql-align` class to be set and activate the toolbar align button
@@ -554,18 +568,14 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
554568
}
555569

556570
/** @protected */
557-
ready() {
558-
super.ready();
571+
connectedCallback() {
572+
super.connectedCallback();
559573

560574
const editor = this.shadowRoot.querySelector('[part="content"]');
561-
const toolbarConfig = this._prepareToolbar();
562-
this._toolbar = toolbarConfig.container;
563-
564-
this._addToolbarListeners();
565575

566576
this._editor = new Quill(editor, {
567577
modules: {
568-
toolbar: toolbarConfig,
578+
toolbar: this._toolbarConfig,
569579
},
570580
});
571581

@@ -577,10 +587,6 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
577587
this.__patchFirefoxFocus();
578588
}
579589

580-
this.$.linkDialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-open', () => {
581-
this.$.linkUrl.focus();
582-
});
583-
584590
const editorContent = editor.querySelector('.ql-editor');
585591

586592
editorContent.setAttribute('role', 'textbox');
@@ -632,6 +638,20 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
632638
this._editor.on('selection-change', this.__announceFormatting.bind(this));
633639
}
634640

641+
/** @protected */
642+
ready() {
643+
super.ready();
644+
645+
this._toolbarConfig = this._prepareToolbar();
646+
this._toolbar = this._toolbarConfig.container;
647+
648+
this._addToolbarListeners();
649+
650+
this.$.linkDialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-open', () => {
651+
this.$.linkUrl.focus();
652+
});
653+
}
654+
635655
/** @private */
636656
_prepareToolbar() {
637657
const clean = Quill.imports['modules/toolbar'].DEFAULTS.handlers.clean;

‎packages/rich-text-editor/test/basic.test.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,42 @@ describe('rich text editor', () => {
928928
);
929929
});
930930
});
931+
932+
describe('detach and re-attach', () => {
933+
it('should not have active listeners once detached', () => {
934+
expect(editor.emitter).to.not.equal(null);
935+
expect(editor.emitter._events).to.not.be.empty;
936+
expect(editor.emitter._eventsCount).to.greaterThan(0);
937+
expect(editor.emitter.listeners).to.not.be.empty;
938+
939+
rte.parentNode.removeChild(rte);
940+
941+
expect(editor.emitter._events).to.be.empty;
942+
expect(editor.emitter._eventsCount).to.be.equal(0);
943+
expect(editor.emitter.listeners).to.be.empty;
944+
});
945+
946+
it('should have the listeners when removed and added back again', () => {
947+
const parent = rte.parentNode;
948+
949+
parent.removeChild(rte);
950+
parent.appendChild(rte);
951+
952+
// previous `editor` reference is now stale as a new editor is created in the connectedCallback
953+
expect(rte._editor.emitter).to.not.equal(null);
954+
expect(rte._editor.emitter._events).to.not.be.empty;
955+
expect(rte._editor.emitter._eventsCount).to.greaterThan(0);
956+
expect(rte._editor.emitter.listeners).to.not.be.empty;
957+
});
958+
959+
it('should keep htmlValue when detached and immediately re-attached', () => {
960+
rte.dangerouslySetHtmlValue('<h1>Foo</h1>');
961+
962+
const parent = rte.parentNode;
963+
parent.removeChild(rte);
964+
parent.appendChild(rte);
965+
966+
expect(rte.htmlValue).to.equal('<h1>Foo</h1>');
967+
});
968+
});
931969
});

0 commit comments

Comments
 (0)