-
Notifications
You must be signed in to change notification settings - Fork 2
/
HotKeys.js
174 lines (159 loc) · 3.11 KB
/
HotKeys.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
import _ from 'lodash'
import $ from 'jquery'
const TASK_CHAR_CODES = [
81,
87,
69,
82,
84,
89,
85,
73,
79,
80,
219,
221,
65,
83,
68,
70,
71,
72,
74,
75,
76,
186,
222,
90,
88,
67,
86,
66,
78,
77,
188,
190,
191,
]
// const TASK_LIST_ELEMENTS_QUERY = '#task-list .task'
const getTaskQueryByKeyCode = keyCode => $(`.task[char-code="${keyCode}"]`)
export default class HotKeys {
isEnabled = false
taskCharCodes = TASK_CHAR_CODES
actions = {
tab: _.noop,
arrowUp: _.noop,
arrowDown: _.noop,
del: _.noop,
shiftA: _.noop,
shiftS: _.noop,
shiftR: _.noop,
shiftArrowUp: _.noop,
shiftArrowDown: _.noop,
}
keyCodeActions = {
// [shift] key is not triggered
0: {
38: 'arrowUp',
40: 'arrowDown',
46: 'del',
9: 'tab',
},
// [shift] key is triggered
1: {
38: 'shiftArrowUp',
40: 'shiftArrowDown',
65: 'shiftA',
82: 'shiftR',
83: 'shiftS',
},
}
clearifyEvent = event => {
// event.keyCode = 0
// event.ctrlKey = false
// event.cancelBubble = true
event.preventDefault()
event.stopPropagation()
}
onKeyClick = event => {
const {
shiftKey,
keyCode,
target: { tagName },
} = event
if (_.indexOf(['INPUT', 'TEXTAREA'], tagName) === -1) {
if (shiftKey) {
this.clearifyEvent(event)
}
const actionName = this.keyCodeActions[+shiftKey][keyCode]
if (actionName) {
this.actions[actionName](event)
} else if (!shiftKey) {
getTaskQueryByKeyCode(keyCode).trigger('click')
}
}
}
triggerEnabled(isEnabled) {
this.isEnabled = !!isEnabled
if (this.isEnabled) {
this.connect()
} else {
this.disconnect()
}
}
connect() {
window.addEventListener('keydown', this.onKeyClick, false)
localStorage.setItem('hotkeys', true)
document.body.setAttribute('hotkeys', 'true')
// setTimeout(() => {
// _.each(
// document.querySelectorAll(TASK_LIST_ELEMENTS_QUERY),
// (element, keycodeIndex) => {
// element.setAttribute('char-code', this.taskCharCodes[keycodeIndex])
// }
// )
// })
}
disconnect() {
window.removeEventListener('keydown', this.onKeyClick, false)
delete localStorage.hotkeys
document.body.removeAttribute('hotkeys')
// setTimeout(() => {
// _.each(document.querySelectorAll(TASK_LIST_ELEMENTS_QUERY), element => {
// element.removeAttribute('char-code')
// })
// })
}
connectTaskButton = (taskEl, index) => {
taskEl.setAttribute('char-code', this.taskCharCodes[index])
}
constructor({
tab,
arrowUp,
arrowDown,
del,
shiftA,
shiftS,
shiftR,
shiftArrowUp,
shiftArrowDown,
} = {}) {
// this.triggerEnabled(true)
_.each(
{
tab,
arrowUp,
arrowDown,
del,
shiftA,
shiftS,
shiftR,
shiftArrowUp,
shiftArrowDown,
},
(handler, handlerName) => {
this.actions[handlerName] = handler
}
)
}
}