Skip to content

Commit

Permalink
Make the emitter work with multiple documents
Browse files Browse the repository at this point in the history
  • Loading branch information
edave64 committed Jul 19, 2024
1 parent ea7ff14 commit 7742b1c
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions packages/quill/src/core/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ import logger from './logger.js';
const debug = logger('quill:events');
const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];

EVENTS.forEach((eventName) => {
document.addEventListener(eventName, (...args) => {
Array.from(document.querySelectorAll('.ql-container')).forEach((node) => {
const quill = instances.get(node);
if (quill && quill.emitter) {
quill.emitter.handleDOM(...args);
}
});
});
});

class Emitter extends EventEmitter<string> {
static events = {
EDITOR_CHANGE: 'editor-change',
Expand All @@ -39,6 +28,24 @@ class Emitter extends EventEmitter<string> {
USER: 'user',
} as const;

private static registeredDocuments = new WeakMap<Document, boolean>();

static registerEventsOnDocument(doc: Document) {
if (Emitter.registeredDocuments.get(doc)) return;
Emitter.registeredDocuments.set(doc, true);

EVENTS.forEach((eventName) => {
doc.addEventListener(eventName, (...args) => {
Array.from(doc.querySelectorAll('.ql-container')).forEach((node) => {
const quill = instances.get(node);
if (quill && quill.emitter) {
quill.emitter.handleDOM(...args);
}
});
});
});
}

protected domListeners: Record<string, { node: Node; handler: Function }[]>;

constructor() {
Expand All @@ -62,6 +69,7 @@ class Emitter extends EventEmitter<string> {
}

listenDOM(eventName: string, node: Node, handler: EventListener) {
Emitter.registerEventsOnDocument(node.ownerDocument ?? document);
if (!this.domListeners[eventName]) {
this.domListeners[eventName] = [];
}
Expand Down

0 comments on commit 7742b1c

Please sign in to comment.