-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
BasicEditorDriver.ts
128 lines (99 loc) · 3.06 KB
/
BasicEditorDriver.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import getCaretCoordinates from 'textarea-caret';
import insertText from './insertText';
import ItemList from './ItemList';
import type EditorDriverInterface from './EditorDriverInterface';
import { type EditorDriverParams } from './EditorDriverInterface';
export default class BasicEditorDriver implements EditorDriverInterface {
el: HTMLTextAreaElement;
constructor(dom: HTMLElement, params: EditorDriverParams) {
this.el = document.createElement('textarea');
this.build(dom, params);
}
protected build(dom: HTMLElement, params: EditorDriverParams) {
this.el.className = params.classNames.join(' ');
this.el.disabled = params.disabled;
this.el.placeholder = params.placeholder;
this.el.value = params.value;
const callInputListeners = (e: Event) => {
params.inputListeners.forEach((listener) => {
listener();
});
e.redraw = false;
};
this.el.oninput = (e) => {
params.oninput(this.el.value);
callInputListeners(e);
};
this.el.onclick = callInputListeners;
this.el.onkeyup = callInputListeners;
this.el.addEventListener('keydown', (e) => {
this.keyHandlers(params)
.toArray()
.forEach((handler) => handler(e));
});
dom.append(this.el);
}
protected keyHandlers(params: EditorDriverParams) {
const items = new ItemList<(e: KeyboardEvent) => void>();
items.add('submit', function (e: KeyboardEvent) {
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
params.onsubmit();
}
});
return items;
}
moveCursorTo(position: number) {
this.setSelectionRange(position, position);
}
getSelectionRange(): Array<number> {
return [this.el.selectionStart, this.el.selectionEnd];
}
getLastNChars(n: number): string {
const value = this.el.value;
return value.slice(Math.max(0, this.el.selectionStart - n), this.el.selectionStart);
}
insertAtCursor(text: string) {
this.insertAt(this.el.selectionStart, text);
}
insertAt(pos: number, text: string) {
this.insertBetween(pos, pos, text);
}
insertBetween(selectionStart: number, selectionEnd: number, text: string) {
this.setSelectionRange(selectionStart, selectionEnd);
const cursorPos = selectionStart + text.length;
insertText(this.el, { text, selectionStart: cursorPos, selectionEnd: cursorPos });
}
replaceBeforeCursor(start: number, text: string) {
this.insertBetween(start, this.el.selectionStart, text);
}
protected setSelectionRange(start: number, end: number) {
this.el.setSelectionRange(start, end);
this.focus();
}
getCaretCoordinates(position: number) {
const relCoords = getCaretCoordinates(this.el, position);
return {
top: relCoords.top - this.el.scrollTop,
left: relCoords.left,
};
}
// DOM Interactions
/**
* Set the disabled status of the editor.
*/
disabled(disabled: boolean) {
this.el.disabled = disabled;
}
/**
* Focus on the editor.
*/
focus() {
this.el.focus();
}
/**
* Destroy the editor
*/
destroy() {
this.el.remove();
}
}