|
| 1 | +import hotkeys, { type HotkeysEvent } from 'hotkeys-js' |
| 2 | +import { type Ref, onMounted, onUnmounted, unref, watch } from 'vue' |
| 3 | + |
| 4 | +type MaybeRef<T> = T | Ref<T> |
| 5 | + |
| 6 | +export interface UseKeyOptions { |
| 7 | + prevent?: MaybeRef<boolean> |
| 8 | + stop?: MaybeRef<boolean> |
| 9 | + repeat?: MaybeRef<boolean> |
| 10 | + input?: MaybeRef<boolean> |
| 11 | + source?: (() => boolean) | Ref<boolean> |
| 12 | +} |
| 13 | + |
| 14 | +export type UseKeyCallback = (event: KeyboardEvent, hotkeysEvent: HotkeysEvent) => void |
| 15 | + |
| 16 | +const getLast = <T>(arr: T[]) => arr[arr.length - 1] |
| 17 | + |
| 18 | +const isInputing = () => |
| 19 | + document.activeElement instanceof HTMLTextAreaElement |
| 20 | + || document.activeElement?.hasAttribute('contenteditable') |
| 21 | + || document.activeElement instanceof HTMLInputElement |
| 22 | + || document.activeElement instanceof HTMLSelectElement |
| 23 | + |
| 24 | +hotkeys.filter = () => true |
| 25 | + |
| 26 | +const bindings = new Map<string, UseKeyCallback[]>() |
| 27 | + |
| 28 | +/** |
| 29 | + * @source https://github.com/kadiryazici/use-key-composable-vue3/blob/main/src/composables/useKey.ts |
| 30 | + */ |
| 31 | +export function useKey( |
| 32 | + keys: string, |
| 33 | + callback: UseKeyCallback, |
| 34 | + { |
| 35 | + source, // |
| 36 | + input = false, |
| 37 | + prevent = false, |
| 38 | + repeat = false, |
| 39 | + stop = false, |
| 40 | + }: UseKeyOptions = {}, |
| 41 | +) { |
| 42 | + let initialized = false |
| 43 | + |
| 44 | + const keyList = keys |
| 45 | + .split(',') |
| 46 | + .map(key => key.trim()) |
| 47 | + .filter(Boolean) |
| 48 | + |
| 49 | + const handler: UseKeyCallback = (event, hotkeysEvent) => { |
| 50 | + if (!unref(input) && isInputing()) |
| 51 | + return |
| 52 | + if (unref(prevent)) |
| 53 | + event.preventDefault() |
| 54 | + if (unref(stop)) |
| 55 | + event.stopPropagation() |
| 56 | + if (!unref(repeat) && event.repeat) |
| 57 | + return |
| 58 | + |
| 59 | + callback(event, hotkeysEvent) |
| 60 | + } |
| 61 | + |
| 62 | + const init = () => { |
| 63 | + if (initialized) |
| 64 | + return |
| 65 | + |
| 66 | + initialized = true |
| 67 | + |
| 68 | + for (const key of keyList) { |
| 69 | + if (bindings.has(key)) { |
| 70 | + bindings.set(key, [...bindings.get(key)!, handler]) |
| 71 | + } |
| 72 | + else { |
| 73 | + bindings.set(key, [handler]) |
| 74 | + hotkeys(key, (...args) => { |
| 75 | + const func = getLast(bindings.get(key)!) |
| 76 | + func(...args) |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const destroy = () => { |
| 83 | + if (!initialized) |
| 84 | + return |
| 85 | + |
| 86 | + initialized = false |
| 87 | + |
| 88 | + for (const key of keyList) { |
| 89 | + bindings.set( |
| 90 | + key, |
| 91 | + bindings.get(key)!.filter(cb => cb !== handler), |
| 92 | + ) |
| 93 | + if (bindings.get(key)!.length === 0) { |
| 94 | + bindings.delete(key) |
| 95 | + hotkeys.unbind(key) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (source) { |
| 101 | + watch( |
| 102 | + source, |
| 103 | + (newSourceValue) => { |
| 104 | + if (newSourceValue) |
| 105 | + init() |
| 106 | + else destroy() |
| 107 | + }, |
| 108 | + { immediate: true, flush: 'post' }, |
| 109 | + ) |
| 110 | + } |
| 111 | + else { |
| 112 | + onMounted(init) |
| 113 | + } |
| 114 | + |
| 115 | + onUnmounted(destroy) |
| 116 | +} |
0 commit comments