Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 16 additions & 8 deletions packages/quill/src/core/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ 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);
}
const registeredDocuments = new WeakSet<Document>();

function ensureDocumentListeners(doc: Document) {
Comment thread
brrichards marked this conversation as resolved.
if (registeredDocuments.has(doc)) return;
registeredDocuments.add(doc);

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);
}
});
});
});
});
}

class Emitter extends EventEmitter<string> {
static events = {
Expand Down Expand Up @@ -72,4 +79,5 @@ class Emitter extends EventEmitter<string> {
export type EmitterSource =
(typeof Emitter.sources)[keyof typeof Emitter.sources];

export { ensureDocumentListeners };
export default Emitter;
3 changes: 2 additions & 1 deletion packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type History from '../modules/history.js';
import type Keyboard from '../modules/keyboard.js';
import type Uploader from '../modules/uploader.js';
import Editor from './editor.js';
import Emitter from './emitter.js';
import Emitter, { ensureDocumentListeners } from './emitter.js';
import type { EmitterSource } from './emitter.js';
import instances from './instances.js';
import logger from './logger.js';
Expand Down Expand Up @@ -209,6 +209,7 @@ class Quill {
this.container.classList.add('ql-container');
this.container.innerHTML = '';
instances.set(this.container, this);
ensureDocumentListeners(this.container.ownerDocument);
this.root = this.addContainer('ql-editor');
this.root.classList.add('ql-blank');
this.emitter = new Emitter();
Expand Down
15 changes: 9 additions & 6 deletions packages/quill/src/modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ const debug = logger('quill:clipboard');
type Selector = string | Node['TEXT_NODE'] | Node['ELEMENT_NODE'];
type Matcher = (node: Node, delta: Delta, scroll: ScrollBlot) => Delta;

const TEXT_NODE = 3;
const ELEMENT_NODE = 1;
Comment thread
brrichards marked this conversation as resolved.
Outdated

const CLIPBOARD_CONFIG: [Selector, Matcher][] = [
[Node.TEXT_NODE, matchText],
[Node.TEXT_NODE, matchNewline],
[TEXT_NODE, matchText],
[TEXT_NODE, matchNewline],
['br', matchBreak],
[Node.ELEMENT_NODE, matchNewline],
[Node.ELEMENT_NODE, matchBlot],
[Node.ELEMENT_NODE, matchAttributor],
[Node.ELEMENT_NODE, matchStyles],
[ELEMENT_NODE, matchNewline],
[ELEMENT_NODE, matchBlot],
[ELEMENT_NODE, matchAttributor],
[ELEMENT_NODE, matchStyles],
['li', matchIndent],
['ol, ul', matchList],
['pre', matchCodeBlock],
Expand Down
5 changes: 4 additions & 1 deletion packages/quill/src/modules/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import type { Range } from '../core/selection.js';

const debug = logger('quill:keyboard');

const SHORTKEY = /Mac/i.test(navigator.platform) ? 'metaKey' : 'ctrlKey';
const SHORTKEY =
typeof navigator !== 'undefined' && /Mac/i.test(navigator.platform)
? 'metaKey'
: 'ctrlKey';

export interface Context {
collapsed: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/quill/src/modules/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class Syntax extends Module<SyntaxOptions> {
}
Syntax.DEFAULTS = {
hljs: (() => {
return window.hljs;
return typeof window !== 'undefined' ? window.hljs : null;
})(),
interval: 1000,
languages: [
Expand Down
3 changes: 2 additions & 1 deletion packages/quill/src/modules/uiNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ParentBlot } from 'parchment';
import Module from '../core/module.js';
import Quill from '../core/quill.js';

const isMac = /Mac/i.test(navigator.platform);
const isMac =
typeof navigator !== 'undefined' && /Mac/i.test(navigator.platform);

// Export for testing
export const TTL_FOR_VALID_SELECTION_CHANGE = 100;
Expand Down