-
Notifications
You must be signed in to change notification settings - Fork 44
/
text-input.js
682 lines (540 loc) · 14.8 KB
/
text-input.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
import { Container, Graphics, Text, TextStyle, TextMetrics } from 'pixi.js'
export default class TextInput extends Container{
constructor(styles){
super()
this._input_style = Object.assign(
{
position: 'absolute',
background: 'none',
border: 'none',
outline: 'none',
transformOrigin: '0 0',
lineHeight: '1'
},
styles.input
)
if(styles.box)
this._box_generator = typeof styles.box === 'function' ? styles.box : new DefaultBoxGenerator(styles.box)
else
this._box_generator = null
if(this._input_style.hasOwnProperty('multiline')){
this._multiline = !!this._input_style.multiline
delete this._input_style.multiline
}else
this._multiline = false
this._box_cache = {}
this._previous = {}
this._dom_added = false
this._dom_visible = true
this._placeholder = ''
this._placeholderColor = 0xa9a9a9
this._selection = [0,0]
this._restrict_value = ''
this._createDOMInput()
this.substituteText = true
this._setState('DEFAULT')
this._addListeners()
}
// GETTERS & SETTERS
get substituteText(){
return this._substituted
}
set substituteText(substitute){
if(this._substituted==substitute)
return
this._substituted = substitute
if(substitute){
this._createSurrogate()
this._dom_visible = false
}else{
this._destroySurrogate()
this._dom_visible = true
}
this.placeholder = this._placeholder
this._update()
}
get placeholder(){
return this._placeholder
}
set placeholder(text){
this._placeholder = text
if(this._substituted){
this._updateSurrogate()
this._dom_input.placeholder = ''
}else{
this._dom_input.placeholder = text
}
}
get disabled(){
return this._disabled
}
set disabled(disabled){
this._disabled = disabled
this._dom_input.disabled = disabled
this._setState(disabled ? 'DISABLED' : 'DEFAULT')
}
get maxLength(){
return this._max_length
}
set maxLength(length){
this._max_length = length
this._dom_input.setAttribute('maxlength', length)
}
get restrict(){
return this._restrict_regex
}
set restrict(regex){
if(regex instanceof RegExp){
regex = regex.toString().slice(1,-1)
if(regex.charAt(0) !== '^')
regex = '^'+regex
if(regex.charAt(regex.length-1) !== '$')
regex = regex+'$'
regex = new RegExp(regex)
}else{
regex = new RegExp('^['+regex+']*$')
}
this._restrict_regex = regex
}
get text(){
return this._dom_input.value
}
set text(text){
this._dom_input.value = text
if(this._substituted) this._updateSurrogate()
}
get htmlInput(){
return this._dom_input
}
focus(){
if(this._substituted && !this.dom_visible)
this._setDOMInputVisible(true)
this._dom_input.focus()
}
blur(){
this._dom_input.blur()
}
select(){
this.focus()
this._dom_input.select()
}
setInputStyle(key,value){
this._input_style[key] = value
this._dom_input.style[key] = value
if(this._substituted && (key==='fontFamily' || key==='fontSize'))
this._updateFontMetrics()
if(this._last_renderer)
this._update()
}
destroy(options){
this._destroyBoxCache()
super.destroy(options)
}
// SETUP
_createDOMInput(){
if(this._multiline){
this._dom_input = document.createElement('textarea')
this._dom_input.style.resize = 'none'
}else{
this._dom_input = document.createElement('input')
this._dom_input.type = 'text'
}
for(let key in this._input_style){
this._dom_input.style[key] = this._input_style[key]
}
}
_addListeners(){
this.on('added',this._onAdded.bind(this))
this.on('removed',this._onRemoved.bind(this))
this._dom_input.addEventListener('keydown', this._onInputKeyDown.bind(this))
this._dom_input.addEventListener('input', this._onInputInput.bind(this))
this._dom_input.addEventListener('keyup', this._onInputKeyUp.bind(this))
this._dom_input.addEventListener('focus', this._onFocused.bind(this))
this._dom_input.addEventListener('blur', this._onBlurred.bind(this))
}
_onInputKeyDown(e){
this._selection = [
this._dom_input.selectionStart,
this._dom_input.selectionEnd
]
this.emit('keydown',e.keyCode)
}
_onInputInput(e){
if(this._restrict_regex)
this._applyRestriction()
if(this._substituted)
this._updateSubstitution()
this.emit('input',this.text)
}
_onInputKeyUp(e){
this.emit('keyup',e.keyCode)
}
_onFocused(){
this._setState('FOCUSED')
this.emit('focus')
}
_onBlurred(){
this._setState('DEFAULT')
this.emit('blur')
}
_onAdded(){
document.body.appendChild(this._dom_input)
this._dom_input.style.display = 'none'
this._dom_added = true
}
_onRemoved(){
document.body.removeChild(this._dom_input)
this._dom_added = false
}
_setState(state){
this.state = state
this._updateBox()
if(this._substituted)
this._updateSubstitution()
}
// RENDER & UPDATE
// for pixi v4
renderWebGL(renderer){
super.renderWebGL(renderer)
this._renderInternal(renderer)
}
// for pixi v4
renderCanvas(renderer){
super.renderCanvas(renderer)
this._renderInternal(renderer)
}
// for pixi v5
render(renderer){
super.render(renderer)
this._renderInternal(renderer)
}
_renderInternal(renderer){
this._resolution = renderer.resolution
this._last_renderer = renderer
this._canvas_bounds = this._getCanvasBounds()
if(this._needsUpdate())
this._update()
}
_update(){
this._updateDOMInput()
if(this._substituted) this._updateSurrogate()
this._updateBox()
}
_updateBox(){
if(!this._box_generator)
return
if(this._needsNewBoxCache())
this._buildBoxCache()
if(this.state==this._previous.state
&& this._box==this._box_cache[this.state])
return
if(this._box)
this.removeChild(this._box)
this._box = this._box_cache[this.state]
this.addChildAt(this._box,0)
this._previous.state = this.state
}
_updateSubstitution(){
if(this.state==='FOCUSED'){
this._dom_visible = true
this._surrogate.visible = this.text.length===0
}else{
this._dom_visible = false
this._surrogate.visible = true
}
this._updateDOMInput()
this._updateSurrogate()
}
_updateDOMInput(){
if(!this._canvas_bounds)
return
this._dom_input.style.top = (this._canvas_bounds.top || 0)+'px'
this._dom_input.style.left = (this._canvas_bounds.left || 0)+'px'
this._dom_input.style.transform = this._pixiMatrixToCSS(this._getDOMRelativeWorldTransform())
this._dom_input.style.opacity = this.worldAlpha
this._setDOMInputVisible(this.worldVisible && this._dom_visible)
this._previous.canvas_bounds = this._canvas_bounds
this._previous.world_transform = this.worldTransform.clone()
this._previous.world_alpha = this.worldAlpha
this._previous.world_visible = this.worldVisible
}
_applyRestriction(){
if(this._restrict_regex.test(this.text)){
this._restrict_value = this.text
}else{
this.text = this._restrict_value
this._dom_input.setSelectionRange(
this._selection[0],
this._selection[1]
)
}
}
// STATE COMPAIRSON (FOR PERFORMANCE BENEFITS)
_needsUpdate(){
return (
!this._comparePixiMatrices(this.worldTransform,this._previous.world_transform)
|| !this._compareClientRects(this._canvas_bounds,this._previous.canvas_bounds)
|| this.worldAlpha != this._previous.world_alpha
|| this.worldVisible != this._previous.world_visible
)
}
_needsNewBoxCache(){
let input_bounds = this._getDOMInputBounds()
return (
!this._previous.input_bounds
|| input_bounds.width != this._previous.input_bounds.width
|| input_bounds.height != this._previous.input_bounds.height
)
}
// INPUT SUBSTITUTION
_createSurrogate(){
this._surrogate_hitbox = new Graphics()
this._surrogate_hitbox.alpha = 0
this._surrogate_hitbox.interactive = true
this._surrogate_hitbox.cursor = 'text'
this._surrogate_hitbox.on('pointerdown',this._onSurrogateFocus.bind(this))
this.addChild(this._surrogate_hitbox)
this._surrogate_mask = new Graphics()
this.addChild(this._surrogate_mask)
this._surrogate = new Text('',{})
this.addChild(this._surrogate)
this._surrogate.mask = this._surrogate_mask
this._updateFontMetrics()
this._updateSurrogate()
}
_updateSurrogate(){
let padding = this._deriveSurrogatePadding()
let input_bounds = this._getDOMInputBounds()
this._surrogate.style = this._deriveSurrogateStyle()
this._surrogate.style.padding = Math.max.apply(Math,padding)
this._surrogate.y = this._multiline ? padding[0] : (input_bounds.height-this._surrogate.height)/2
this._surrogate.x = padding[3]
this._surrogate.text = this._deriveSurrogateText()
switch (this._surrogate.style.align){
case 'left':
this._surrogate.x = padding[3]
break
case 'center':
this._surrogate.x = input_bounds.width * 0.5 - this._surrogate.width * 0.5
break
case 'right':
this._surrogate.x = input_bounds.width - padding[1] - this._surrogate.width
break
}
this._updateSurrogateHitbox(input_bounds)
this._updateSurrogateMask(input_bounds,padding)
}
_updateSurrogateHitbox(bounds){
this._surrogate_hitbox.clear()
this._surrogate_hitbox.beginFill(0)
this._surrogate_hitbox.drawRect(0,0,bounds.width,bounds.height)
this._surrogate_hitbox.endFill()
this._surrogate_hitbox.interactive = !this._disabled
}
_updateSurrogateMask(bounds,padding){
this._surrogate_mask.clear()
this._surrogate_mask.beginFill(0)
this._surrogate_mask.drawRect(padding[3],0,bounds.width-padding[3]-padding[1],bounds.height)
this._surrogate_mask.endFill()
}
_destroySurrogate(){
if(!this._surrogate) return
this.removeChild(this._surrogate)
this.removeChild(this._surrogate_mask)
this.removeChild(this._surrogate_hitbox)
this._surrogate.destroy()
this._surrogate_hitbox.destroy()
this._surrogate = null
this._surrogate_hitbox = null
}
_onSurrogateFocus(){
this._setDOMInputVisible(true)
//sometimes the input is not being focused by the mouseclick
setTimeout(this._ensureFocus.bind(this),10)
}
_ensureFocus(){
if(!this._hasFocus())
this.focus()
}
_deriveSurrogateStyle(){
let style = new TextStyle()
for(var key in this._input_style){
switch(key){
case 'color':
style.fill = this._input_style.color
break
case 'fontFamily':
case 'fontSize':
case 'fontWeight':
case 'fontVariant':
case 'fontStyle':
style[key] = this._input_style[key]
break
case 'letterSpacing':
style.letterSpacing = parseFloat(this._input_style.letterSpacing)
break
case 'textAlign':
style.align = this._input_style.textAlign
break
}
}
if(this._multiline){
style.lineHeight = parseFloat(style.fontSize)
style.wordWrap = true
style.wordWrapWidth = this._getDOMInputBounds().width
}
if(this._dom_input.value.length === 0)
style.fill = this._placeholderColor
return style
}
_deriveSurrogatePadding(){
let indent = this._input_style.textIndent ? parseFloat(this._input_style.textIndent) : 0
if(this._input_style.padding && this._input_style.padding.length>0){
let components = this._input_style.padding.trim().split(' ')
if(components.length==1){
let padding = parseFloat(components[0])
return [padding,padding,padding,padding+indent]
}else if(components.length==2){
let paddingV = parseFloat(components[0])
let paddingH = parseFloat(components[1])
return [paddingV,paddingH,paddingV,paddingH+indent]
}else if(components.length==4){
let padding = components.map(component => {
return parseFloat(component)
})
padding[3] += indent
return padding
}
}
return [0,0,0,indent]
}
_deriveSurrogateText(){
if(this._dom_input.value.length === 0)
return this._placeholder
if(this._dom_input.type == 'password')
return '•'.repeat(this._dom_input.value.length)
return this._dom_input.value
}
_updateFontMetrics(){
const style = this._deriveSurrogateStyle()
const font = style.toFontString()
this._font_metrics = TextMetrics.measureFont(font)
}
// CACHING OF INPUT BOX GRAPHICS
_buildBoxCache(){
this._destroyBoxCache()
let states = ['DEFAULT','FOCUSED','DISABLED']
let input_bounds = this._getDOMInputBounds()
for(let state of states){
this._box_cache[state] = this._box_generator(
input_bounds.width,
input_bounds.height,
state
)
}
this._previous.input_bounds = input_bounds
}
_destroyBoxCache(){
if(this._box){
this.removeChild(this._box)
this._box = null
}
for(let i in this._box_cache){
this._box_cache[i].destroy()
this._box_cache[i] = null
delete this._box_cache[i]
}
}
// HELPER FUNCTIONS
_hasFocus(){
return document.activeElement===this._dom_input
}
_setDOMInputVisible(visible){
this._dom_input.style.display = visible ? 'block' : 'none'
}
_getCanvasBounds(){
let rect = this._last_renderer.view.getBoundingClientRect()
let bounds = {top: rect.top, left: rect.left, width: rect.width, height: rect.height}
bounds.left += window.scrollX
bounds.top += window.scrollY
return bounds
}
_getDOMInputBounds(){
let remove_after = false
if(!this._dom_added){
document.body.appendChild(this._dom_input)
remove_after = true
}
let org_transform = this._dom_input.style.transform
let org_display = this._dom_input.style.display
this._dom_input.style.transform = ''
this._dom_input.style.display = 'block'
let bounds = this._dom_input.getBoundingClientRect()
this._dom_input.style.transform = org_transform
this._dom_input.style.display = org_display
if(remove_after)
document.body.removeChild(this._dom_input)
return bounds
}
_getDOMRelativeWorldTransform(){
let canvas_bounds = this._last_renderer.view.getBoundingClientRect()
let matrix = this.worldTransform.clone()
matrix.scale(this._resolution,this._resolution)
matrix.scale(canvas_bounds.width/this._last_renderer.width,
canvas_bounds.height/this._last_renderer.height)
return matrix
}
_pixiMatrixToCSS(m){
return 'matrix('+[m.a,m.b,m.c,m.d,m.tx,m.ty].join(',')+')'
}
_comparePixiMatrices(m1,m2){
if(!m1 || !m2) return false
return (
m1.a == m2.a
&& m1.b == m2.b
&& m1.c == m2.c
&& m1.d == m2.d
&& m1.tx == m2.tx
&& m1.ty == m2.ty
)
}
_compareClientRects(r1,r2){
if(!r1 || !r2) return false
return (
r1.left == r2.left
&& r1.top == r2.top
&& r1.width == r2.width
&& r1.height == r2.height
)
}
}
function DefaultBoxGenerator(styles){
styles = styles || {fill: 0xcccccc}
if(styles.default){
styles.focused = styles.focused || styles.default
styles.disabled = styles.disabled || styles.default
}else{
let temp_styles = styles
styles = {}
styles.default = styles.focused = styles.disabled = temp_styles
}
return function(w,h,state){
let style = styles[state.toLowerCase()]
let box = new Graphics()
if(style.fill)
box.beginFill(style.fill)
if(style.stroke)
box.lineStyle(
style.stroke.width || 1,
style.stroke.color || 0,
style.stroke.alpha || 1
)
if(style.rounded)
box.drawRoundedRect(0,0,w,h,style.rounded)
else
box.drawRect(0,0,w,h)
box.endFill()
box.closePath()
return box
}
}