1 | import Embed from '../blots/embed.js';
|
2 | import Emitter from './emitter.js';
|
3 | class 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 |
|
19 |
|
20 |
|
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 | }
|
43 | export default Composition;
|
44 |
|
\ | No newline at end of file |