-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
340 lines (302 loc) · 14.6 KB
/
main.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
/* global HTMLElement IntersectionObserver customElements */
const ROWS = 30
const ROWS_MAG = 10 // > 1.0
const MARGINS = 10 // < ROWS
const MARGINS_MAG = 3 // > 1.0
const LINE_HEIGHT = 1.2 // rem (default value)
const LINE_COUNTS = 10000
const padLength = Math.log10(LINE_COUNTS) + 1
const text = new Array(LINE_COUNTS).fill('').map((_, i) => i.toString().padStart(padLength, '0')).join('\n')
document.getElementById('textarea-original').value = text
const indexOf = (text, target, count, offset = 0) => {
let index = offset - 1
for (let i = 0; i < count; i++) {
index = text.indexOf(target, index + 1)
if (index === -1) return -1
}
return index
}
const lastIndexOf = (text, target, count, offset = text.length - 1) => {
let index = offset + 1
for (let i = 0; i < count; i++) {
index = text.lastIndexOf(target, index - 1)
if (index === -1) return -1
}
return index
}
class TextareaVirtualized extends HTMLElement {
constructor () {
super()
this.lineHeight = LINE_HEIGHT
this.shadow = this.attachShadow({ mode: 'closed' })
const style = document.createElement('style')
style.textContent = `
* {
padding: 0;
margin: 0;
resize: none;
border: none;
outline: none;
}
#container {
overflow-x: hidden;
overflow-y: scroll;
border: solid 1px black;
height: ${ROWS * this.lineHeight}rem;
}
#container::-webkit-scrollbar {
display: none;
}
#textarea {
width: 100%;
overflow: hidden;
vertical-align: top;
}
#upper, #lower {
width: 100%;
height: 1px;
position: relative;
}
#upper {
top: calc(100% * ${MARGINS} / ${ROWS});
background-color: rgb(255, 0, 0);
}
#lower {
bottom: calc(100% * ${MARGINS} / ${ROWS});
background-color: rgb(0, 0, 255);
}
`
this.shadow.appendChild(style)
this.container = document.createElement('div')
this.container.setAttribute('id', 'container')
this.shadow.appendChild(this.container)
this.upper = document.createElement('div')
this.upper.setAttribute('id', 'upper')
this.container.appendChild(this.upper)
this.textarea = document.createElement('textarea')
this.textarea.setAttribute('id', 'textarea')
this.textarea.setAttribute('rows', ROWS * ROWS_MAG)
this.container.appendChild(this.textarea)
this.lower = document.createElement('div')
this.lower.setAttribute('id', 'lower')
this.container.appendChild(this.lower)
// const text = this.value
// const text = text // (use global variable)
const end = indexOf(text, '\n', ROWS * ROWS_MAG)
const indexOfEndTextarea = end !== -1 ? end : text.length
this.upperVirtualizedText = ''
this.textarea.value = text.substring(0, indexOfEndTextarea)
this.lowerVirtualizedText = text.substring(indexOfEndTextarea)
this.selectionStart = 0
this.selectionEnd = 0
this.selectionAlreadyUpdated = 0
this.upperTextareaIntersectionObserver = new IntersectionObserver(this.upperIntersectionCallback.bind(this), { root: this.container })
this.lowerTextareaIntersectionObserver = new IntersectionObserver(this.lowerIntersectionCallback.bind(this), { root: this.container })
this.textarea.addEventListener('select', this.onSelect.bind(this))
this.textarea.addEventListener('keydown', this.onKeyDown.bind(this))
}
upperIntersectionCallback (entries) {
for (const entry of entries) {
if (!entry.isIntersecting) return
if (this.upperVirtualizedText === '') return
const start = lastIndexOf(this.upperVirtualizedText, '\n', MARGINS * MARGINS_MAG)
const indexOfStartTextarea = start !== -1 ? start : 0
const text = this.upperVirtualizedText.substring(indexOfStartTextarea) + this.textarea.value
this.upperVirtualizedText = this.upperVirtualizedText.substring(0, indexOfStartTextarea)
const end = indexOf(text, '\n', ROWS * ROWS_MAG)
const indexOfEndTextarea = end !== -1 ? end : text.length
this.textarea.value = text.substring(0, indexOfEndTextarea)
this.lowerVirtualizedText = text.substring(indexOfEndTextarea) + this.lowerVirtualizedText
this.container.scrollBy(0, this.lineHeight * MARGINS * MARGINS_MAG)
this.textarea.scrollTo(0, 0)
this.textarea.selectionStart = Math.min(Math.max(this.selectionStart - this.upperVirtualizedText.length, 0), this.textarea.value.length)
this.textarea.selectionEnd = Math.min(Math.max(this.selectionEnd - this.upperVirtualizedText.length, 0), this.textarea.value.length)
this.selectionAlreadyUpdated += 2
return
}
}
lowerIntersectionCallback (entries) {
for (const entry of entries) {
if (!entry.isIntersecting) return
if (this.lowerVirtualizedText === '') return
const end = indexOf(this.lowerVirtualizedText, '\n', MARGINS * MARGINS_MAG)
const indexOfEndTextarea = end !== -1 ? end : this.lowerVirtualizedText.length
const text = this.textarea.value + this.lowerVirtualizedText.substring(0, indexOfEndTextarea)
this.lowerVirtualizedText = this.lowerVirtualizedText.substring(indexOfEndTextarea)
const start = lastIndexOf(text, '\n', ROWS * ROWS_MAG)
const indexOfStartTextarea = start !== -1 ? start : 0
this.textarea.value = text.substring(indexOfStartTextarea)
this.upperVirtualizedText = this.upperVirtualizedText + text.substring(0, indexOfStartTextarea)
this.container.scrollBy(0, -1 * this.lineHeight * (MARGINS * MARGINS_MAG - 1))
this.textarea.scrollBy(0, this.lineHeight)
this.textarea.selectionStart = Math.min(Math.max(this.selectionStart - this.upperVirtualizedText.length, 0), this.textarea.value.length)
this.textarea.selectionEnd = Math.min(Math.max(this.selectionEnd - this.upperVirtualizedText.length, 0), this.textarea.value.length)
this.selectionAlreadyUpdated += 2
return
}
}
onSelect (event) {
if (this.selectionAlreadyUpdated > 0) {
this.selectionAlreadyUpdated--
return
}
switch (this.textarea.selectionDirection) {
case 'forward':
this.selectionEnd = this.upperVirtualizedText.length + this.textarea.selectionEnd
break
case 'backward':
this.selectionStart = this.upperVirtualizedText.length + this.textarea.selectionStart
break
case 'none':
this.selectionStart = this.upperVirtualizedText.length + this.textarea.selectionStart
this.selectionEnd = this.upperVirtualizedText.length + this.textarea.selectionEnd
break
default:
break
}
}
onKeyDown (event) {
if (event.metaKey) {
this.onKeyDownWithMetaKey(event)
return
}
switch (event.key) {
case 'ArrowLeft': {
if (this.selectionStart === this.selectionEnd) return
event.preventDefault()
const text = this.upperVirtualizedText + this.textarea.value + this.lowerVirtualizedText
const start = lastIndexOf(text, '\n', Math.floor(ROWS * ROWS_MAG * 0.5), this.selectionStart)
const indexOfStartTextarea = start !== -1 ? start : 0
const end = indexOf(text, '\n', ROWS * ROWS_MAG, indexOfStartTextarea)
const indexOfEndTextarea = end !== -1 ? end : 0
this.upperVirtualizedText = text.substring(0, indexOfStartTextarea)
this.textarea.value = text.substring(indexOfStartTextarea, indexOfEndTextarea)
this.lowerVirtualizedText = text.substring(indexOfEndTextarea)
this.textarea.selectionStart = this.selectionStart - this.upperVirtualizedText.length
this.textarea.selectionEnd = this.textarea.selectionStart
this.selectionStart = this.textarea.selectionStart
this.selectionEnd = this.textarea.selectionEnd
this.container.scrollTo(0, this.lineHeight * Math.floor((this.textarea.value.substring(0, this.textarea.selectionStart).match(/\n/g) || []).length - ROWS * 0.5))
this.textarea.scrollTo(0, 0)
break
}
case 'ArrowRight': {
if (this.selectionStart === this.selectionEnd) return
event.preventDefault()
const text = this.upperVirtualizedText + this.textarea.value + this.lowerVirtualizedText
const end = indexOf(text, '\n', Math.floor(ROWS * ROWS_MAG * 0.5), this.selectionEnd)
const indexOfEndTextarea = end !== -1 ? end : text.length
const start = lastIndexOf(text, '\n', ROWS * ROWS_MAG, indexOfEndTextarea)
const indexOfStartTextarea = start !== -1 ? start : 0
this.upperVirtualizedText = text.substring(0, indexOfStartTextarea)
this.textarea.value = text.substring(indexOfStartTextarea, indexOfEndTextarea)
this.lowerVirtualizedText = text.substring(indexOfEndTextarea)
this.textarea.selectionEnd = this.selectionEnd - this.upperVirtualizedText.length
this.textarea.selectionStart = this.textarea.selectionEnd
this.selectionStart = this.textarea.selectionStart
this.selectionEnd = this.textarea.selectionEnd
this.container.scrollTo(0, this.lineHeight * Math.floor((this.textarea.value.substring(0, this.textarea.selectionStart).match(/\n/g) || []).length - ROWS * 0.5))
this.textarea.scrollTo(0, this.lineHeight)
break
}
default:
break
}
}
onKeyDownWithMetaKey (event) {
switch (event.key) {
case 'a': {
this.selectionStart = 0
this.selectionEnd = this.upperVirtualizedText.length + this.textarea.value.length + this.lowerVirtualizedText.length
break
}
case 'ArrowUp': {
this.selectionStart = 0
this.selectionEnd = event.shiftKey
? this.upperVirtualizedText.length + this.textarea.selectionEnd
: 0
const text = this.upperVirtualizedText + this.textarea.value + this.lowerVirtualizedText
const end = indexOf(text, '\n', ROWS * ROWS_MAG)
const indexOfEndTextarea = end !== -1 ? end : text.length
this.textarea.value = text.substring(0, indexOfEndTextarea)
this.upperVirtualizedText = ''
this.lowerVirtualizedText = text.substring(index)
this.container.scrollTo(0, 0)
this.textarea.scrollTo(0, 0)
this.textarea.selectionStart = this.selectionStart
this.textarea.selectionEnd = event.shiftKey
? Math.min(this.selectionEnd - this.upperVirtualizedText.length, this.textarea.value.length)
: 0
this.selectionAlreadyUpdated += 2
break
}
case 'ArrowDown': {
this.selectionStart = event.shiftKey
? this.upperVirtualizedText.length + this.textarea.selectionStart
: this.upperVirtualizedText.length + this.textarea.value.length + this.lowerVirtualizedText.length
this.selectionEnd = this.upperVirtualizedText.length + this.textarea.value.length + this.lowerVirtualizedText.length
const text = this.upperVirtualizedText + this.textarea.value + this.lowerVirtualizedText
const start = lastIndexOf(text, '\n', ROWS * ROWS_MAG)
const indexOfStartTextarea = start !== -1 ? start : 0
this.textarea.value = text.substring(indexOfStartTextarea)
this.upperVirtualizedText = text.substring(0, indexOfStartTextarea)
this.lowerVirtualizedText = ''
this.container.scrollTo(0, this.lineHeight * (ROWS * ROWS_MAG))
this.textarea.scrollBy(0, this.lineHeight)
this.textarea.selectionStart = event.shiftKey
? Math.max(this.selectionStart - this.upperVirtualizedText.length, 0)
: this.textarea.value.length
this.textarea.selectionEnd = this.textarea.value.length
this.selectionAlreadyUpdated += 2
break
}
case 'ArrowLeft': {
if (this.selectionStart === this.selectionEnd) return
const text = this.upperVirtualizedText + this.textarea.value + this.lowerVirtualizedText
const selectionStartLineTail = text.lastIndexOf('\n', this.selectionStart)
this.selectionStart = selectionStartLineTail !== -1 ? selectionStartLineTail + 1 : 0
this.selectionEnd = this.selectionStart
const start = lastIndexOf(text, '\n', Math.floor(ROWS * ROWS_MAG * 0.5), this.selectionStart)
const indexOfStartTextarea = start === -1 ? start : 0
const end = indexOf(text, '\n', ROWS * ROWS_MAG, indexOfStartTextarea)
const indexOfEndTextarea = end !== -1 ? end : text.length
this.upperVirtualizedText = text.substring(0, indexOfStartTextarea)
this.textarea.value = text.substring(indexOfStartTextarea, indexOfEndTextarea)
this.lowerVirtualizedText = text.substring(indexOfEndTextarea)
this.textarea.selectionStart = this.selectionStart - this.upperVirtualizedText.length
this.textarea.selectionEnd = this.textarea.selectionStart
this.container.scrollTo(0, this.lineHeight * Math.floor((this.textarea.value.substring(0, this.textarea.selectionStart).match(/\n/g) || []).length - ROWS * 0.5))
this.textarea.scrollTo(0, 0)
break
}
case 'ArrowRight': {
if (this.selectionStart === this.selectionEnd) return
const text = this.upperVirtualizedText + this.textarea.value + this.lowerVirtualizedText
const selectionStartLineTail = text.indexOf('\n', this.selectionStart)
this.selectionStart = selectionStartLineTail !== -1 ? selectionStartLineTail : (this.upperVirtualizedText.length + this.textarea.value.length)
this.selectionEnd = this.selectionStart
const start = lastIndexOf(text, '\n', Math.floor(ROWS * ROWS_MAG * 0.5), this.selectionStart)
const indexOfStartTextarea = start !== -1 ? start : 0
const end = indexOf(text, '\n', ROWS * ROWS_MAG, indexOfStartTextarea)
const indexOfEndTextarea = end !== -1 ? end : text.length
this.upperVirtualizedText = text.substring(0, indexOfStartTextarea)
this.textarea.value = text.substring(indexOfStartTextarea, indexOfEndTextarea)
this.lowerVirtualizedText = text.substring(indexOfEndTextarea)
this.textarea.selectionStart = this.selectionStart - this.upperVirtualizedText.length
this.textarea.selectionEnd = this.textarea.selectionStart
this.container.scrollTo(0, this.lineHeight * Math.floor((this.textarea.value.substring(0, this.textarea.selectionStart).match(/\n/g) || []).length - ROWS * 0.5))
this.textarea.scrollTo(0, 0)
break
}
default:
break
}
}
connectedCallback () {
this.lineHeight = this.textarea.getBoundingClientRect().height / (ROWS * ROWS_MAG)
this.container.style.setProperty('height', `${ROWS * this.lineHeight}px`)
this.upperTextareaIntersectionObserver.observe(this.upper)
this.lowerTextareaIntersectionObserver.observe(this.lower)
}
}
customElements.define('textarea-virtualized', TextareaVirtualized)