Skip to content

Commit

Permalink
Emitter: support event handling when quill is inside of shadowDOM (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
  • Loading branch information
ksercs and marker dao ® authored May 2, 2024
1 parent 20c787f commit 7431fe9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];
if (hasWindow()) {
EVENTS.forEach((eventName) => {
document.addEventListener(eventName, (...args) => {
Array.from(document.querySelectorAll('.ql-container')).forEach((node) => {
const event = args[0];
const shadowRoot = event?.target?.shadowRoot;
const root = shadowRoot ?? document;
const quillContainers = root.querySelectorAll('.ql-container');

Array.from(quillContainers).forEach((node) => {
const quill = instances.get(node);
if (quill && quill.emitter) {
quill.emitter.handleDOM(...args);
Expand Down
1 change: 1 addition & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './unit/blots/block-embed';
import './unit/blots/inline';

import './unit/core/editor';
import './unit/core/emitter';
import './unit/core/selection';
import './unit/core/quill';
import './unit/core/input';
Expand Down
32 changes: 32 additions & 0 deletions test/unit/core/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Quill from '../../../core/quill';

describe('Emitter', function () {
it('handleDOM is called on event handling', function () {
this.quill = this.initialize(Quill, '');
spyOn(this.quill.emitter, 'handleDOM');

const event = new Event('click');
document.dispatchEvent(event);

expect(this.quill.emitter.handleDOM).toHaveBeenCalledWith(event);
});

it('handleDOM is called on event handling if quill container is in shadowDOM', function () {
const shadowContainer = document.body.appendChild(document.createElement('div'));
try {
shadowContainer.attachShadow({ mode: 'open' });
shadowContainer.shadowRoot.innerHTML = '<div></div>';
const quillContainer = shadowContainer.shadowRoot.querySelector('div');

this.quill = this.initialize(Quill, '', quillContainer);
spyOn(this.quill.emitter, 'handleDOM');

const event = new Event('click', { bubbles: true });
shadowContainer.dispatchEvent(event);

expect(this.quill.emitter.handleDOM).toHaveBeenCalledWith(event);
} finally {
shadowContainer.remove();
}
});
});

0 comments on commit 7431fe9

Please sign in to comment.