forked from scripcrypt/textarea-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsweEditor.js
354 lines (251 loc) · 8.48 KB
/
sweEditor.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
window.onload = (e) => {
const sedt = document.getElementById("fugafuga");
if (sedt) {
new sweEditor(sedt);
}
};
class sweEditor {
frNode; // frame node
lnNode; // line number frame node
txNode; // textarea node
txWidth; // textarea width
letterWidth = {};
sele = {start:0,end:0}; // selection letter number {start,end}
lineNum = {start:0,end:0}; // target line number
deleteString;
compositStatus = false;
composit = {};
caLineNum;
lineHeight;
lineArr = [];
/*--------------------------------------------------
コンストラクタ
--------------------------------------------------*/
constructor (txNode) {
this.txNode = txNode;
// FrameやLineNumber及び高さ計算用要素を追加する
this.attachFrame();
// 文字の幅を計測する
this.measureLetterSize();
// textareaの高さを内容に合わせて設定する
this.adjustSize();
setTimeout(()=>{
// 行番号を全行に付加する
this.lineNumbers_all();
// イベントを付加
this.attachEvents();
});
}
/*--------------------------------------------------
イベントを付加
--------------------------------------------------*/
attachEvents = () => {
// 日本語文字入力終了
this.txNode.addEventListener("compositionstart",(e)=>{
this.composit = {deleteString:this.deleteString,sele:{start:this.sele.start,end:this.sele.end},line:{start:this.lineNum.start,end:this.lineNum.end}};
});
// キーダウン
this.txNode.addEventListener("keydown",(e)=>{
// キャレット位置
this.sele.start = this.txNode.selectionStart;
this.sele.end = this.txNode.selectionEnd;
// 選択行番号の取得
this.lineNum.start = this.getLineNum(this.sele.start);
this.lineNum.end = this.getLineNum(this.sele.end);
// タブ挿入
if (e.key == "Tab") {
e.preventDefault();
document.execCommand("insertText", false, "\t");
}
// Backspace
else if (e.key == "Backspace") {
// キャレットの前の1文字
if (this.sele.start == this.sele.end) {
this.deleteString = this.txNode.value.substr(this.sele.start-1,1);
}
// 選択された範囲
else {
this.deleteString = this.txNode.value.slice(this.sele.start,this.sele.end);
}
}
// Delete
else if (e.key == "Delete") {
// キャレットの後の1文字
if (this.sele.start == this.sele.end) {
this.deleteString = this.txNode.value.substr(this.sele.start,1);
}
// 選択された範囲
else {
this.deleteString = this.txNode.value.slice(this.sele.start,this.sele.end);
}
}
// 入力
else {
// キャレットの後の1文字
if (this.sele.start != this.sele.end) {
this.deleteString = this.txNode.value.slice(this.sele.start,this.sele.end);
}
}
});
// 入力
this.txNode.addEventListener("input",(e)=>{
e.insertData = e.data;
// 入力値がEnterだった場合
if (e.inputType == "insertLineBreak") {
e.insertData = "\n";
}
// 行番号調整
this.adjustLineNumber(e);
});
};
/*--------------------------------------------------
行番号調整
--------------------------------------------------*/
adjustLineNumber = (e) => {
// 対象行番号を消す
for (let i=this.lineNum.start; i<=this.lineNum.end; i++) {
// 一行分削除
this.lnNode.removeChild(this.lnNode.children[this.lineNum.start]);
}
// 入力データが複数行ある
let targetLine = {
start: this.lineNum.start,
end: this.getLineNum(this.txNode.selectionEnd)
};
if (e.insertData) {
targetLine.end = targetLine.start + (e.insertData.match(/\n/) ? e.insertData.match(/\n/g).length : 0);
}
// 対象行番号を追加
this.lineArr = [];
for (let i=targetLine.start; i<=targetLine.end; i++) {
// 行番号を一行追加
this.addLineNumber(i,this.pickSpecLine(i));
}
// 後ろの行番号を合わせる
for (let i=targetLine.end; i<this.txNode.value.match(/\n/gm).length; i++) {
if (typeof this.lnNode.children[i] != "undefined") {
this.lnNode.children[i].innerText = i + 1;
}
}
// textareaの高さを内容に合わせて設定する
this.adjustSize();
};
/*--------------------------------------------------
特定の行を抜き出す
--------------------------------------------------*/
pickSpecLine = (lineNum) => {
if (this.lineArr.length === 0) {
this.lineArr = this.txNode.value.split(/\n/);
}
return this.lineArr[lineNum];
};
/*--------------------------------------------------
行番号の取得
--------------------------------------------------*/
getLineNum = (letterNum) => {
return this.txNode.value.slice(0,letterNum).split(/\n/).length - 1;
};
/*--------------------------------------------------
行番号を一行追加
--------------------------------------------------*/
addLineNumber = (i,line=null) => {
let tLLC = 0; // the line letter count
let tLLB = 0; // the line letter byte
let tLW = 0; // the line width
let ci = line == null ? 0 : line.length; // letter amount of the line
for (; tLLC<ci; tLLC++) {
let byte = line.charCodeAt(tLLC) < 300 ? 1 : 2;
// タブの場合の幅計測
if (line.charCodeAt(tLLC)===9) {
tLLB = Math.ceil(tLLB / 8) * 8;
tLW = Math.ceil(tLLB / 8) * 8 * this.letterWidth[1];
}
else {
tLLB += byte;
tLW += this.letterWidth[byte];
}
}
let height = ci===0 ? this.lineHeight : Math.ceil(tLW / this.txWidth) * this.lineHeight;
const lineNumber = this.lineNumber.cloneNode(false);
lineNumber.innerText = i+1;
lineNumber.style.height = height+"px";
if (this.lnNode.children[i]) {
this.lnNode.insertBefore(lineNumber,this.lnNode.children[i]);
}
else {
this.lnNode.append(lineNumber);
}
};
/*--------------------------------------------------
textareaの高さを内容に合わせて設定する
--------------------------------------------------*/
adjustSize = () => {
const height = this.txNode.scrollHeight - 10;
this.txNode.style.height = height+"px";
this.lnNode.style.height = (height+40)+"px";
};
/*--------------------------------------------------
FrameやLineNumber及び高さ計算用要素を追加する
--------------------------------------------------*/
attachFrame = () => {
// Frame
this.frNode = document.createElement("div");
this.frNode.classList.add("sweEditor_frame");
// Line Number Frame
this.lnNode =document.createElement("div");
this.lnNode.classList.add("line_number_frame");
// Append
this.txNode.parentElement.insertBefore(this.frNode,this.txNode);
this.frNode.append(this.lnNode);
this.frNode.append(this.txNode);
// Size
const txStyle = getComputedStyle(this.txNode);
this.frNode.style.width = txStyle.width;
this.frNode.style.height = txStyle.height;
// Investigate
let txStyle2 = getComputedStyle(this.txNode);
this.txNode.style.width = txStyle2.width;
setTimeout(()=>{
this.txWidth = Number(txStyle2.width.replace(/px/,""));
this.frNode.append(this.txNode);
this.lineHeight = Number(window.getComputedStyle(this.txNode)["lineHeight"].replace(/px$/,""));
this.lineNumber = document.createElement("div");
this.lineNumber.classList.add("line_number");
});
};
/*--------------------------------------------------
1文字の文字幅を計測しておく
--------------------------------------------------*/
measureLetterSize = () => {
let letterSizeMeasure = document.createElement('div');
letterSizeMeasure.classList.add("letter_size_measure");
this.frNode.insertBefore(letterSizeMeasure,this.txNode);
letterSizeMeasure.textContent = "あ";
let lsmStyle = getComputedStyle(letterSizeMeasure);
this.letterWidth[2] = Number(lsmStyle.width.replace(/px/,""));
letterSizeMeasure.textContent = "A";
lsmStyle = getComputedStyle(letterSizeMeasure);
this.letterWidth[1] = Number(lsmStyle.width.replace(/px/,""));
letterSizeMeasure.remove();
};
/*--------------------------------------------------
行番号を全行に付加する
--------------------------------------------------*/
lineNumbers_all = () => {
// 全行削除
this.lineNumbers_deleteAll();
this.virtLine = this.txNode.value.split(/\n/);
for (let i=0,ci=this.virtLine.length; i<ci; i++) {
// 行番号を一行追加
this.addLineNumber(i,this.virtLine[i]);
}
};
/*--------------------------------------------------
全行削除
--------------------------------------------------*/
lineNumbers_deleteAll = () => {
while(this.lnNode.firstChild){
this.lnNode.removeChild(this.lnNode.firstChild);
}
};
}