-
Notifications
You must be signed in to change notification settings - Fork 97
/
utils.ts
66 lines (59 loc) · 1.81 KB
/
utils.ts
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
export function isFormField(element: Node): boolean {
if (!(element instanceof HTMLElement)) {
return false
}
const name = element.nodeName.toLowerCase()
const type = (element.getAttribute('type') || '').toLowerCase()
return (
name === 'select' ||
name === 'textarea' ||
(name === 'input' &&
type !== 'submit' &&
type !== 'reset' &&
type !== 'checkbox' &&
type !== 'radio' &&
type !== 'file') ||
element.isContentEditable
)
}
export function fireDeterminedAction(el: HTMLElement, path: readonly string[]): void {
const delegateEvent = new CustomEvent('hotkey-fire', {cancelable: true, detail: {path}})
const cancelled = !el.dispatchEvent(delegateEvent)
if (cancelled) return
if (isFormField(el)) {
el.focus()
} else {
el.click()
}
}
export function expandHotkeyToEdges(hotkey: string): string[][] {
// NOTE: we can't just split by comma, since comma is a valid hotkey character!
const output = []
let acc = ['']
let commaIsSeparator = false
for (let i = 0; i < hotkey.length; i++) {
if (commaIsSeparator && hotkey[i] === ',') {
output.push(acc)
acc = ['']
commaIsSeparator = false
continue
}
if (hotkey[i] === ' ') {
// Spaces are used to separate key sequences, so a following comma is
// part of the sequence, not a separator.
acc.push('')
commaIsSeparator = false
continue
} else if (hotkey[i] === '+') {
// If the current character is a +, a following comma is part of the
// shortcut and not a separator.
commaIsSeparator = false
} else {
commaIsSeparator = true
}
acc[acc.length - 1] += hotkey[i]
}
output.push(acc)
// Remove any empty hotkeys/sequences
return output.map(h => h.filter(k => k !== '')).filter(h => h.length > 0)
}