-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhotkeys.js
56 lines (49 loc) · 1.41 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
const events = {}
export default {
init () {
window.addEventListener('keydown', event => {
if (event.isComposing) return
const key = event.key
const ctrl = event.ctrlKey
const alt = event.altKey
const meta = event.metaKey
if (key === 'Escape' && isEscapeHide(document.activeElement)) {
this.trigger('escape-hide', document.activeElement)
}
if (activeFocusedInput()) return
if (!ctrl && !alt && !meta) {
if (key === 'f') {
this.trigger('filter')
event.preventDefault()
} else if (key === 'o') {
this.trigger('uncollapse-all')
event.preventDefault()
}
}
})
},
on (name, callback) {
if (!events[name]) events[name] = []
events[name].push(callback)
},
trigger (name, payload = undefined) {
const callbacks = events[name] || []
callbacks.forEach(callback => {
callback(payload)
})
}
}
function activeFocusedInput () {
const elem = document.activeElement
if (elem.isContentEditable) return true
const active = ['INPUT', 'TEXTAREA'].includes(elem.tagName) &&
!['submit', 'checkbox', 'radio'].includes(elem.type) &&
!elem.disabled
if (!active) return false
if (document.activeElement.classList.contains('stepper-value')) return false
return true
}
function isEscapeHide (elem) {
if (!elem) return
return Boolean(elem.dataset.escapehide)
}