|
| 1 | +import type { Ref } from 'vue' |
| 2 | +import { onMounted, onUnmounted, ref } from 'vue' |
| 3 | + |
| 4 | +interface SmartShortcutOptions { |
| 5 | + // Array of keys that make up the shortcut |
| 6 | + keys: string[] |
| 7 | + // CSS selector for the context in which the shortcut should work |
| 8 | + contextSelector: string |
| 9 | + // Callback function to run when shortcut is triggered |
| 10 | + onTrigger: (e: KeyboardEvent) => void |
| 11 | + // Whether to prevent default browser behavior |
| 12 | + preventDefault?: boolean |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Prevents conflicts with other global shortcuts |
| 17 | + */ |
| 18 | +export function useSmartShortcut(options: SmartShortcutOptions): { keysPressed: Ref<Set<string>> } { |
| 19 | + const { |
| 20 | + keys, |
| 21 | + contextSelector, |
| 22 | + onTrigger, |
| 23 | + preventDefault = true, |
| 24 | + } = options |
| 25 | + |
| 26 | + const keysPressed = ref<Set<string>>(new Set()) |
| 27 | + const controller = new AbortController() |
| 28 | + |
| 29 | + const normalizeKey = (key: string): string => { |
| 30 | + const keyMap: Record<string, string> = { |
| 31 | + Ctrl: 'Control', |
| 32 | + Cmd: 'Meta', |
| 33 | + Alt: 'Alt', |
| 34 | + Shift: 'Shift', |
| 35 | + } |
| 36 | + return keyMap[key] || key |
| 37 | + } |
| 38 | + |
| 39 | + const normalizedKeys = keys.map(k => |
| 40 | + normalizeKey(k.charAt(0).toUpperCase() + k.slice(1).toLowerCase()), |
| 41 | + ) |
| 42 | + |
| 43 | + const handleKeyDown = (e: KeyboardEvent): void => { |
| 44 | + const key = e.key === ' ' ? 'Space' : e.key |
| 45 | + const currentKey = key.length === 1 ? key.toLowerCase() : key |
| 46 | + |
| 47 | + // Track pressed key |
| 48 | + keysPressed.value.add(currentKey) |
| 49 | + |
| 50 | + // Build currently pressed keys |
| 51 | + const activeKeys = new Set<string>() |
| 52 | + if (e.ctrlKey) |
| 53 | + activeKeys.add('Control') |
| 54 | + if (e.metaKey) |
| 55 | + activeKeys.add('Meta') |
| 56 | + if (e.altKey) |
| 57 | + activeKeys.add('Alt') |
| 58 | + if (e.shiftKey) |
| 59 | + activeKeys.add('Shift') |
| 60 | + activeKeys.add(currentKey) |
| 61 | + |
| 62 | + // Check if the exact combination is matched |
| 63 | + const isMatch |
| 64 | + = normalizedKeys.length === activeKeys.size |
| 65 | + && normalizedKeys.every(k => activeKeys.has(k) || activeKeys.has(k.toLowerCase())) |
| 66 | + |
| 67 | + if (isMatch) { |
| 68 | + const targetElement = e.target as HTMLElement |
| 69 | + const isInContext = targetElement.closest(contextSelector) !== null |
| 70 | + if (isInContext) { |
| 71 | + e.stopPropagation() |
| 72 | + if (preventDefault) |
| 73 | + e.preventDefault() |
| 74 | + onTrigger(e) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const handleKeyUp = (e: KeyboardEvent): void => { |
| 80 | + const key = e.key === ' ' ? 'Space' : e.key |
| 81 | + const currentKey = key.length === 1 ? key.toLowerCase() : key |
| 82 | + keysPressed.value.delete(currentKey) |
| 83 | + |
| 84 | + // Also clear modifier keys when they're lifted |
| 85 | + if (!e.ctrlKey) |
| 86 | + keysPressed.value.delete('Control') |
| 87 | + if (!e.metaKey) |
| 88 | + keysPressed.value.delete('Meta') |
| 89 | + if (!e.altKey) |
| 90 | + keysPressed.value.delete('Alt') |
| 91 | + if (!e.shiftKey) |
| 92 | + keysPressed.value.delete('Shift') |
| 93 | + } |
| 94 | + |
| 95 | + onMounted(() => { |
| 96 | + document.addEventListener('keydown', handleKeyDown, { signal: controller.signal, capture: true }) |
| 97 | + document.addEventListener('keyup', handleKeyUp, { signal: controller.signal, capture: true }) |
| 98 | + window.addEventListener('blur', () => keysPressed.value.clear(), { signal: controller.signal }) |
| 99 | + }) |
| 100 | + |
| 101 | + onUnmounted(() => { |
| 102 | + controller.abort() |
| 103 | + }) |
| 104 | + |
| 105 | + return { keysPressed } |
| 106 | +} |
0 commit comments