-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (100 loc) · 3.4 KB
/
Copy pathindex.js
File metadata and controls
124 lines (100 loc) · 3.4 KB
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
class GlobalInput {
constructor(onChange, onSubmit, options = {}){
if (!document) throw Error('`document` not found')
this.options = Object.assign({
submitKey: 'Enter',
debug: false,
excludeNodes: [],
excludeNodeNames: [],
preventDefault: false,
mountInitial: true,
}, options)
this.onChange = onChange
this.onSubmit = onSubmit
this.inputCache = ''
this.mounted = false
this.mount = this.mount.bind(this)
this.unmount = this.unmount.bind(this)
this.setValue = this.setValue.bind(this)
this.action = this.action.bind(this)
if (!window.GlobalInput) {
window.GlobalInput = {
isMasterDisabled: false
}
}
if (this.options.mountInitial) this.mount()
}
handle(method, value) {
if (this.options.debug) {
console.info('Method: ' + method)
console.info('Input Value: ' + this.inputCache)
console.log('')
}
this['on' + method](value)
}
mount() {
if (this.mounted) return console.warn('Input already mounted')
document.body.addEventListener('keydown', this.action)
this.mounted = true
if (this.options.debug) console.warn('mounted')
}
unmount() {
if (!this.mounted) return console.warn('Input already unmounted')
document.body.removeEventListener('keydown', this.action)
this.mounted = false
if (this.options.debug) console.warn('unmounted')
}
setValue(value) {
this.inputCache = value
}
masterDisable() {
window.GlobalInput.isMasterDisabled = true
}
masterEnable() {
window.GlobalInput.isMasterDisabled = false
}
action(e) {
if (window.GlobalInput.isMasterDisabled) {
return
}
if (
this.options.excludeNodes.includes(document.activeElement) ||
this.options.excludeNodeNames.includes(document.activeElement.nodeName) ||
typeof e.getModifierState === 'function' && (
e.getModifierState('Alt') ||
e.getModifierState('Control') ||
e.getModifierState('OS') ||
e.getModifierState('Fn') ||
e.getModifierState('Meta') ||
e.getModifierState('Win') ||
e.getModifierState('Hyper') ||
e.getModifierState('Super')
)
) {
return
}
if (this.options.preventDefault) e.preventDefault()
switch(e.key) {
case this.options.submitKey:
const value = String(this.inputCache);
this.handle('Submit', value)
this.inputCache = ''
return value
break
case 'Backspace':
this.inputCache = this.inputCache.slice(0, -1)
break
default:
const caps = e.getModifierState && (
e.getModifierState('CapsLock') ||
e.getModifierState('Shift')
)
this.inputCache += (
e.key.length === 1 ? e.key : ''
)[caps ? 'toUpperCase' : 'toLowerCase']()
break
}
this.handle('Change', this.inputCache)
}
}
module.exports = GlobalInput