-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathuseKeyboard.js
247 lines (197 loc) · 5.88 KB
/
useKeyboard.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
import { toRefs, getCurrentInstance } from 'vue'
import toRef from './../utils/toRef'
export default function useKeyboard (props, context, dep)
{
const {
mode, addTagOn, openDirection, searchable,
showOptions, valueProp, groups: groupped,
addOptionOn: addOptionOn_, createTag, createOption: createOption_,
reverse,
} = toRefs(props)
const $this = getCurrentInstance().proxy
// ============ DEPENDENCIES ============
const iv = dep.iv
const update = dep.update
const deselect = dep.deselect
const search = dep.search
const setPointer = dep.setPointer
const selectPointer = dep.selectPointer
const backwardPointer = dep.backwardPointer
const forwardPointer = dep.forwardPointer
const multiselect = dep.multiselect
const wrapper = dep.wrapper
const tags = dep.tags
const isOpen = dep.isOpen
const open = dep.open
const blur = dep.blur
const fo = dep.fo
// ============== COMPUTED ==============
// no export
const createOption = toRef(() => {
return createTag.value || createOption_.value || false
})
// no export
const addOptionOn = toRef(() => {
if (addTagOn.value !== undefined) {
return addTagOn.value
}
else if (addOptionOn_.value !== undefined) {
return addOptionOn_.value
}
return ['enter']
})
// =============== METHODS ==============
// no export
const preparePointer = () => {
// When options are hidden and creating tags is allowed
// no pointer will be set (because options are hidden).
// In such case we need to set the pointer manually to the
// first option, which equals to the option created from
// the search value.
if (mode.value === 'tags' && !showOptions.value && createOption.value && searchable.value && !groupped.value) {
setPointer(fo.value[fo.value.map(o => o[valueProp.value]).indexOf(search.value)])
}
}
const handleKeydown = (e) => {
context.emit('keydown', e, $this)
let tagList
let activeIndex
if (['ArrowLeft', 'ArrowRight', 'Enter'].indexOf(e.key) !== -1 && mode.value === 'tags') {
tagList = [...(multiselect.value.querySelectorAll(`[data-tags] > *`))].filter(e => e !== tags.value)
activeIndex = tagList.findIndex(e => e === document.activeElement)
}
switch (e.key) {
case 'Backspace':
if (mode.value === 'single') {
return
}
if (searchable.value && [null, ''].indexOf(search.value) === -1) {
return
}
if (iv.value.length === 0) {
return
}
let deselectables = iv.value.filter(v=>!v.disabled && v.remove !== false)
if (deselectables.length) {
deselect(deselectables[deselectables.length - 1])
}
break
case 'Enter':
e.preventDefault()
if (e.keyCode === 229) {
// ignore IME confirmation
return
}
if (activeIndex !== -1 && activeIndex !== undefined) {
update([...iv.value].filter((v, k) => k !== activeIndex))
if (activeIndex === tagList.length - 1) {
if (tagList.length - 1) {
tagList[tagList.length - 2].focus()
} else if (searchable.value) {
tags.value.querySelector('input').focus()
} else {
wrapper.value.focus()
}
}
return
}
if (addOptionOn.value.indexOf('enter') === -1 && createOption.value) {
return
}
preparePointer()
selectPointer()
break
case ' ':
if (!createOption.value && !searchable.value) {
e.preventDefault()
preparePointer()
selectPointer()
return
}
if (!createOption.value) {
return false
}
if (addOptionOn.value.indexOf('space') === -1 && createOption.value) {
return
}
e.preventDefault()
preparePointer()
selectPointer()
break
case 'Tab':
case ';':
case ',':
if (addOptionOn.value.indexOf(e.key.toLowerCase()) === -1 || !createOption.value) {
return
}
preparePointer()
selectPointer()
e.preventDefault()
break
case 'Escape':
blur()
break
case 'ArrowUp':
e.preventDefault()
if (!showOptions.value) {
return
}
/* istanbul ignore else */
if (!isOpen.value) {
open()
}
backwardPointer()
break
case 'ArrowDown':
e.preventDefault()
if (!showOptions.value) {
return
}
/* istanbul ignore else */
if (!isOpen.value) {
open()
}
forwardPointer()
break
case 'ArrowLeft':
if (
(searchable.value && tags.value && tags.value.querySelector('input').selectionStart)
|| e.shiftKey || mode.value !== 'tags' || !iv.value || !iv.value.length
) {
return
}
e.preventDefault()
if (activeIndex === -1) {
tagList[tagList.length-1].focus()
}
else if (activeIndex > 0) {
tagList[activeIndex-1].focus()
}
break
case 'ArrowRight':
if (activeIndex === -1 || e.shiftKey || mode.value !== 'tags' || !iv.value || !iv.value.length) {
return
}
e.preventDefault()
/* istanbul ignore else */
if (tagList.length > activeIndex + 1) {
tagList[activeIndex+1].focus()
}
else if (searchable.value) {
tags.value.querySelector('input').focus()
}
else if (!searchable.value) {
wrapper.value.focus()
}
break
}
}
const handleKeyup = (e) => {
context.emit('keyup', e, $this)
}
return {
handleKeydown,
handleKeyup,
preparePointer,
}
}