UNPKG

1.47 kBJavaScriptView Raw
1import Embed from '../blots/embed.js';
2import Emitter from './emitter.js';
3class Composition {
4 isComposing = false;
5 constructor(scroll, emitter) {
6 this.scroll = scroll;
7 this.emitter = emitter;
8 this.setupListeners();
9 }
10 setupListeners() {
11 this.scroll.domNode.addEventListener('compositionstart', event => {
12 if (!this.isComposing) {
13 this.handleCompositionStart(event);
14 }
15 });
16 this.scroll.domNode.addEventListener('compositionend', event => {
17 if (this.isComposing) {
18 // Webkit makes DOM changes after compositionend, so we use microtask to
19 // ensure the order.
20 // https://bugs.webkit.org/show_bug.cgi?id=31902
21 queueMicrotask(() => {
22 this.handleCompositionEnd(event);
23 });
24 }
25 });
26 }
27 handleCompositionStart(event) {
28 const blot = event.target instanceof Node ? this.scroll.find(event.target, true) : null;
29 if (blot && !(blot instanceof Embed)) {
30 this.emitter.emit(Emitter.events.COMPOSITION_BEFORE_START, event);
31 this.scroll.batchStart();
32 this.emitter.emit(Emitter.events.COMPOSITION_START, event);
33 this.isComposing = true;
34 }
35 }
36 handleCompositionEnd(event) {
37 this.emitter.emit(Emitter.events.COMPOSITION_BEFORE_END, event);
38 this.scroll.batchEnd();
39 this.emitter.emit(Emitter.events.COMPOSITION_END, event);
40 this.isComposing = false;
41 }
42}
43export default Composition;
44//# sourceMappingURL=composition.js.map
\No newline at end of file