-
Notifications
You must be signed in to change notification settings - Fork 3
Optimized the interaction of shortcut key modifications #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| export { useConfirm } from './useConfirm'; | ||
| export type { UseConfirmOptions } from './useConfirm'; | ||
|
|
||
| export { useKeyRecorder } from './useKeyRecorder'; | ||
| export type { KeyRecorderState, UseKeyRecorderOptions, UseKeyRecorderReturn } from './useKeyRecorder'; | ||
|
|
||
| export { usePolling } from './usePolling'; | ||
| export type { UsePollingOptions, UsePollingReturn } from './usePolling'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { ref, onUnmounted, readonly, type Ref, type DeepReadonly } from 'vue'; | ||
|
|
||
| export interface KeyRecorderState { | ||
| /** The binding ID currently being recorded */ | ||
| bindingId: number; | ||
| /** Internal key string result (e.g. "Mod-Shift-k") */ | ||
| result: string; | ||
| /** Display parts for UI (e.g. ["Ctrl", "Shift", "K"]) */ | ||
| displayParts: string[]; | ||
| /** Whether a non-modifier key has been captured (recording complete) */ | ||
| completed: boolean; | ||
| } | ||
|
|
||
| export interface UseKeyRecorderOptions { | ||
| isMacOS: boolean; | ||
| onComplete?: (bindingId: number, keyString: string) => void; | ||
| onCancel?: (bindingId: number) => void; | ||
| } | ||
|
|
||
| export interface UseKeyRecorderReturn { | ||
| recording: DeepReadonly<Ref<KeyRecorderState | null>>; | ||
| isRecording: (bindingId: number) => boolean; | ||
| startRecording: (bindingId: number) => void; | ||
| stopRecording: () => void; | ||
| } | ||
|
|
||
| const MODIFIER_KEYS = new Set(['Control', 'Shift', 'Alt', 'Meta']); | ||
|
|
||
| const KEY_DISPLAY_MAP: Record<string, string> = { | ||
| 'ArrowUp': '↑', | ||
| 'ArrowDown': '↓', | ||
| 'ArrowLeft': '←', | ||
| 'ArrowRight': '→', | ||
| ' ': 'Space', | ||
| }; | ||
|
|
||
| const MAC_MODIFIER_DISPLAY: Record<string, string> = { | ||
| 'Mod': '⌘', | ||
| 'Alt': '⌥', | ||
| 'Shift': '⇧', | ||
| 'Ctrl': '⌃', | ||
| }; | ||
|
|
||
| function normalizeKey(key: string): string { | ||
| if (key === ' ') return 'Space'; | ||
| if (key.length === 1) return key.toLowerCase(); | ||
| return key; | ||
| } | ||
|
|
||
| function getDisplayKey(key: string, isMacOS: boolean): string { | ||
| if (isMacOS && MAC_MODIFIER_DISPLAY[key]) return MAC_MODIFIER_DISPLAY[key]; | ||
| if (KEY_DISPLAY_MAP[key]) return KEY_DISPLAY_MAP[key]; | ||
| if (key.length === 1) return key.toUpperCase(); | ||
| return key; | ||
| } | ||
|
|
||
| /** | ||
| * Composable for capturing keyboard shortcuts via keydown events. | ||
| * Captures the full key combination (modifiers + main key) in a single interaction. | ||
| */ | ||
| export function useKeyRecorder(options: UseKeyRecorderOptions): UseKeyRecorderReturn { | ||
| const { isMacOS, onComplete, onCancel } = options; | ||
|
Check warning on line 62 in frontend/src/composables/useKeyRecorder.ts
|
||
|
|
||
| const recording = ref<KeyRecorderState | null>(null); | ||
|
|
||
| const buildParts = (e: KeyboardEvent): { internalParts: string[]; displayParts: string[] } => { | ||
| const internalParts: string[] = []; | ||
| const displayParts: string[] = []; | ||
|
|
||
| const hasCtrlOrMeta = e.ctrlKey || e.metaKey; | ||
| if (hasCtrlOrMeta) { | ||
| internalParts.push('Mod'); | ||
| displayParts.push(getDisplayKey('Mod', isMacOS)); | ||
| } | ||
|
|
||
| if (e.altKey) { | ||
| internalParts.push('Alt'); | ||
| displayParts.push(getDisplayKey('Alt', isMacOS)); | ||
| } | ||
|
|
||
| if (e.shiftKey) { | ||
| internalParts.push('Shift'); | ||
| displayParts.push(getDisplayKey('Shift', isMacOS)); | ||
| } | ||
|
|
||
| return { internalParts, displayParts }; | ||
| }; | ||
|
|
||
| const onKeyDown = (e: KeyboardEvent) => { | ||
| if (!recording.value) return; | ||
|
|
||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
|
|
||
| const { internalParts, displayParts } = buildParts(e); | ||
|
|
||
| if (MODIFIER_KEYS.has(e.key)) { | ||
| recording.value = { | ||
| ...recording.value, | ||
| result: '', | ||
| displayParts, | ||
| completed: false, | ||
| }; | ||
| return; | ||
| } | ||
|
|
||
| const mainKey = normalizeKey(e.key); | ||
| internalParts.push(mainKey); | ||
| displayParts.push(getDisplayKey(e.key, isMacOS)); | ||
|
|
||
| const keyString = internalParts.join('-'); | ||
|
|
||
| recording.value = { | ||
| ...recording.value, | ||
| result: keyString, | ||
| displayParts, | ||
| completed: true, | ||
| }; | ||
|
|
||
| const bindingId = recording.value.bindingId; | ||
| cleanup(); | ||
| recording.value = null; | ||
| onComplete?.(bindingId, keyString); | ||
| }; | ||
|
|
||
| const onKeyUp = (e: KeyboardEvent) => { | ||
| if (!recording.value) return; | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| }; | ||
|
|
||
| const preventDefaults = (e: KeyboardEvent) => { | ||
|
Check warning on line 132 in frontend/src/composables/useKeyRecorder.ts
|
||
| if (!recording.value) return; | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| }; | ||
|
|
||
| const cleanup = () => { | ||
| document.removeEventListener('keydown', onKeyDown, true); | ||
| document.removeEventListener('keyup', onKeyUp, true); | ||
| document.removeEventListener('keypress', preventDefaults, true); | ||
| }; | ||
|
|
||
| const startRecording = (bindingId: number) => { | ||
| cleanup(); | ||
|
|
||
| recording.value = { | ||
| bindingId, | ||
| result: '', | ||
| displayParts: [], | ||
| completed: false, | ||
| }; | ||
|
|
||
| document.addEventListener('keydown', onKeyDown, true); | ||
| document.addEventListener('keyup', onKeyUp, true); | ||
| document.addEventListener('keypress', preventDefaults, true); | ||
| }; | ||
|
|
||
| const stopRecording = () => { | ||
| cleanup(); | ||
| recording.value = null; | ||
| }; | ||
|
|
||
| const isRecording = (bindingId: number): boolean => { | ||
| return recording.value?.bindingId === bindingId; | ||
| }; | ||
|
|
||
| onUnmounted(cleanup); | ||
|
|
||
| return { | ||
| recording: readonly(recording) as DeepReadonly<Ref<KeyRecorderState | null>>, | ||
| isRecording, | ||
| startRecording, | ||
| stopRecording, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.