Skip to content

Commit

Permalink
fix: re-initialize Quill on detach and re-attach and keep htmlValue (#…
Browse files Browse the repository at this point in the history
…6506) (#6513)

Co-authored-by: Serhii Kulykov <[email protected]>
Co-authored-by: sujoykd <[email protected]>
  • Loading branch information
3 people authored Sep 19, 2023
1 parent 33afe5c commit 547ccb0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
42 changes: 31 additions & 11 deletions packages/rich-text-editor/src/vaadin-rich-text-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,20 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
}
}

/** @protected */
disconnectedCallback() {
super.disconnectedCallback();

// Ensure that htmlValue property set before attach
// gets applied in case of detach and re-attach.
if (this.__debounceSetValue && this.__debounceSetValue.isActive()) {
this.__debounceSetValue.flush();
}

this._editor.emitter.removeAllListeners();
this._editor.emitter.listeners = {};
}

/** @private */
__setDirection(dir) {
// Needed for proper `ql-align` class to be set and activate the toolbar align button
Expand All @@ -554,18 +568,14 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
}

/** @protected */
ready() {
super.ready();
connectedCallback() {
super.connectedCallback();

const editor = this.shadowRoot.querySelector('[part="content"]');
const toolbarConfig = this._prepareToolbar();
this._toolbar = toolbarConfig.container;

this._addToolbarListeners();

this._editor = new Quill(editor, {
modules: {
toolbar: toolbarConfig,
toolbar: this._toolbarConfig,
},
});

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

this.$.linkDialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-open', () => {
this.$.linkUrl.focus();
});

const editorContent = editor.querySelector('.ql-editor');

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

/** @protected */
ready() {
super.ready();

this._toolbarConfig = this._prepareToolbar();
this._toolbar = this._toolbarConfig.container;

this._addToolbarListeners();

this.$.linkDialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-open', () => {
this.$.linkUrl.focus();
});
}

/** @private */
_prepareToolbar() {
const clean = Quill.imports['modules/toolbar'].DEFAULTS.handlers.clean;
Expand Down
38 changes: 38 additions & 0 deletions packages/rich-text-editor/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,42 @@ describe('rich text editor', () => {
);
});
});

describe('detach and re-attach', () => {
it('should not have active listeners once detached', () => {
expect(editor.emitter).to.not.equal(null);
expect(editor.emitter._events).to.not.be.empty;
expect(editor.emitter._eventsCount).to.greaterThan(0);
expect(editor.emitter.listeners).to.not.be.empty;

rte.parentNode.removeChild(rte);

expect(editor.emitter._events).to.be.empty;
expect(editor.emitter._eventsCount).to.be.equal(0);
expect(editor.emitter.listeners).to.be.empty;
});

it('should have the listeners when removed and added back again', () => {
const parent = rte.parentNode;

parent.removeChild(rte);
parent.appendChild(rte);

// previous `editor` reference is now stale as a new editor is created in the connectedCallback
expect(rte._editor.emitter).to.not.equal(null);
expect(rte._editor.emitter._events).to.not.be.empty;
expect(rte._editor.emitter._eventsCount).to.greaterThan(0);
expect(rte._editor.emitter.listeners).to.not.be.empty;
});

it('should keep htmlValue when detached and immediately re-attached', () => {
rte.dangerouslySetHtmlValue('<h1>Foo</h1>');

const parent = rte.parentNode;
parent.removeChild(rte);
parent.appendChild(rte);

expect(rte.htmlValue).to.equal('<h1>Foo</h1>');
});
});
});

0 comments on commit 547ccb0

Please sign in to comment.