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

fix: re-initialize Quill on detach and re-attach and keep htmlValue #6506

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>');
});
});
});