forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.js
63 lines (55 loc) · 1.54 KB
/
emitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import EventEmitter from 'eventemitter3';
import instances from './instances';
import logger from './logger';
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 {
constructor() {
super();
this.listeners = {};
this.on('error', debug.error);
}
emit(...args) {
debug.log.call(debug, ...args);
super.emit(...args);
}
handleDOM(event, ...args) {
(this.listeners[event.type] || []).forEach(({ node, handler }) => {
if (event.target === node || node.contains(event.target)) {
handler(event, ...args);
}
});
}
listenDOM(eventName, node, handler) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = [];
}
this.listeners[eventName].push({ node, handler });
}
}
Emitter.events = {
EDITOR_CHANGE: 'editor-change',
SCROLL_BEFORE_UPDATE: 'scroll-before-update',
SCROLL_BLOT_MOUNT: 'scroll-blot-mount',
SCROLL_BLOT_UNMOUNT: 'scroll-blot-unmount',
SCROLL_OPTIMIZE: 'scroll-optimize',
SCROLL_UPDATE: 'scroll-update',
SELECTION_CHANGE: 'selection-change',
TEXT_CHANGE: 'text-change',
};
Emitter.sources = {
API: 'api',
SILENT: 'silent',
USER: 'user',
};
export default Emitter;