-
Notifications
You must be signed in to change notification settings - Fork 342
/
area.ts
147 lines (119 loc) · 3.07 KB
/
area.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
/**
* @module plugins/source
*/
import type { IJodit, ISourceEditor } from 'jodit/types';
import { Dom } from 'jodit/core/dom/dom';
import { css } from 'jodit/core/helpers/utils/css';
import { SourceEditor } from 'jodit/plugins/source/editor/sourceEditor';
export class TextAreaEditor
extends SourceEditor<HTMLTextAreaElement>
implements ISourceEditor
{
private autosize = this.j.async.debounce(() => {
this.instance.style.height = 'auto';
this.instance.style.height = this.instance.scrollHeight + 'px';
}, this.j.defaultTimeout);
init(editor: IJodit): any {
this.instance = editor.c.element('textarea', {
class: 'jodit-source__mirror'
});
this.container.appendChild(this.instance);
editor.e
.on(
this.instance,
'mousedown keydown touchstart input',
editor.async.debounce(this.toWYSIWYG, editor.defaultTimeout)
)
.on('setMinHeight.source', (minHeightD: number) => {
css(this.instance, 'minHeight', minHeightD);
})
.on(
this.instance,
'change keydown mousedown touchstart input',
this.autosize
)
.on('afterSetMode.source', this.autosize)
.on(this.instance, 'mousedown focus', (e: Event) => {
editor.e.fire(e.type, e);
});
this.autosize();
this.onReady();
}
destruct(): any {
Dom.safeRemove(this.instance);
}
getValue(): string {
return this.instance.value;
}
setValue(raw: string): void {
this.instance.value = raw;
}
insertRaw(raw: string): void {
const value = this.getValue();
if (this.getSelectionStart() >= 0) {
const startPos = this.getSelectionStart(),
endPos = this.getSelectionEnd();
this.setValue(
value.substring(0, startPos) +
raw +
value.substring(endPos, value.length)
);
} else {
this.setValue(value + raw);
}
}
getSelectionStart(): number {
return this.instance.selectionStart;
}
getSelectionEnd(): number {
return this.instance.selectionEnd;
}
setSelectionRange(start: number, end: number = start): void {
this.instance.setSelectionRange(start, end);
}
get isFocused(): boolean {
return this.instance === this.j.od.activeElement;
}
focus(): void {
this.instance.focus();
}
blur(): void {
this.instance.blur();
}
setPlaceHolder(title: string): void {
this.instance.setAttribute('placeholder', title);
}
setReadOnly(isReadOnly: boolean): void {
if (isReadOnly) {
this.instance.setAttribute('readonly', 'true');
} else {
this.instance.removeAttribute('readonly');
}
}
selectAll(): void {
this.instance.select();
}
replaceUndoManager(): void {
const { history } = this.jodit;
this.j.e.on(
this.instance,
'keydown',
(e: KeyboardEvent): false | void => {
if ((e.ctrlKey || e.metaKey) && e.key === 'z') {
if (e.shiftKey) {
history.redo();
} else {
history.undo();
}
this.setSelectionRange(this.getValue().length);
return false;
}
}
);
}
}