-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
TextEditingManager.ts
70 lines (60 loc) · 1.71 KB
/
TextEditingManager.ts
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
64
65
66
67
68
69
70
import type { TPointerEvent } from '../EventTypeDefs';
import type { ITextBehavior } from '../shapes/IText/ITextBehavior';
import { removeFromArray } from '../util/internals';
import { isInteractiveTextObject } from '../util/typeAssertions';
import type { Canvas } from './Canvas';
/**
* In charge of synchronizing all interactive text instances of a canvas
*/
export class TextEditingManager {
private targets: ITextBehavior[] = [];
private declare target?: ITextBehavior;
private __disposer: VoidFunction;
constructor(canvas: Canvas) {
const cb = () => {
const activeObject = canvas.getActiveObject();
if (isInteractiveTextObject(activeObject)) {
activeObject.hiddenTextarea && activeObject.hiddenTextarea.focus();
}
};
const el = canvas.upperCanvasEl;
el.addEventListener('click', cb);
this.__disposer = () => el.removeEventListener('click', cb);
}
exitTextEditing() {
this.target = undefined;
this.targets.forEach((target) => {
if (target.isEditing) {
target.exitEditing();
}
});
}
add(target: ITextBehavior) {
this.targets.push(target);
}
remove(target: ITextBehavior) {
this.unregister(target);
removeFromArray(this.targets, target);
}
register(target: ITextBehavior) {
this.target = target;
}
unregister(target: ITextBehavior) {
if (target === this.target) {
this.target = undefined;
}
}
onMouseMove(e: TPointerEvent) {
this.target?.isEditing && this.target.updateSelectionOnMouseMove(e);
}
clear() {
this.targets = [];
this.target = undefined;
}
dispose() {
this.clear();
this.__disposer();
// @ts-expect-error disposing
delete this.__disposer;
}
}