Repository navigation
fix(power-k): shortcut matching, badge rendering, i18n and dead-code cleanup #9874
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
Changes from all commits
1ff894f
055198b
04e4ed7
3b4a95e
25e0465
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,39 +7,147 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { IPowerKCommandRegistry } from "./registry"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { TPowerKCommandConfig, TPowerKContext } from "./types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** `KeyboardEvent.code` -> unshifted character on the active OS keyboard layout */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type TKeyboardLayoutMap = ReadonlyMap<string, string>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type TNavigatorWithKeyboard = Navigator & { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keyboard?: { getLayoutMap?: () => Promise<TKeyboardLayoutMap> }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Reads the active keyboard layout where the browser exposes it (Chromium, secure top-level | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * contexts). Resolves to null elsewhere, in which case matching falls back to the US layout. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export async function loadKeyboardLayoutMap(): Promise<TKeyboardLayoutMap | null> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof navigator === "undefined") return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const keyboard = (navigator as TNavigatorWithKeyboard).keyboard; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof keyboard?.getLayoutMap !== "function") return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await keyboard.getLayoutMap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * US layout: the base character under each shifted glyph and the physical key it lives on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Used only without a layout map, and only when `e.code` agrees, so a layout that puts a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * different character on that key can never fire a shortcut it does not advertise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const US_SHIFTED_GLYPHS: Record<string, { base: string; code: string }> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "~": { base: "`", code: "Backquote" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "!": { base: "1", code: "Digit1" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@": { base: "2", code: "Digit2" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "#": { base: "3", code: "Digit3" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $: { base: "4", code: "Digit4" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "%": { base: "5", code: "Digit5" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "^": { base: "6", code: "Digit6" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "&": { base: "7", code: "Digit7" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "*": { base: "8", code: "Digit8" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "(": { base: "9", code: "Digit9" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ")": { base: "0", code: "Digit0" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _: { base: "-", code: "Minus" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "+": { base: "=", code: "Equal" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "{": { base: "[", code: "BracketLeft" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "}": { base: "]", code: "BracketRight" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "|": { base: "\\", code: "Backslash" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ":": { base: ";", code: "Semicolon" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '"': { base: "'", code: "Quote" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "<": { base: ",", code: "Comma" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ">": { base: ".", code: "Period" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "?": { base: "/", code: "Slash" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isAsciiPrintable = (char: string): boolean => char >= " " && char <= "~"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Letter or digit printed on a physical key in the US layout ("KeyB" -> "b", "Digit2" -> "2") */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const usKeyForCode = (code: string): string | undefined => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const letter = /^Key([A-Z])$/.exec(code); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (letter) return letter[1].toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const digit = /^Digit([0-9])$/.exec(code); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (digit) return digit[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The layout-independent key a shortcut is registered against: the unshifted character on the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * pressed physical key ("," for Shift+Comma), lowercased; named keys ("backspace") as-is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Non-Latin layouts resolve letters and digits by physical position, as browsers do for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * their own shortcuts, so Cmd+B still toggles the sidebar on a Cyrillic or Greek layout. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function resolveShortcutKey(e: KeyboardEvent, layoutMap: TKeyboardLayoutMap | null): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.key === " ") return "space"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.key.length !== 1) return e.key.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const fromLayout = e.code ? layoutMap?.get(e.code) : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fromLayout) return isAsciiPrintable(fromLayout) ? fromLayout.toLowerCase() : (usKeyForCode(e.code) ?? fromLayout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const key = e.key.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isAsciiPrintable(key)) return usKeyForCode(e.code) ?? key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!e.shiftKey) return key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shifted = US_SHIFTED_GLYPHS[e.key]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return shifted && shifted.code === e.code ? shifted.base : key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Formats a keyboard event into a modifier shortcut string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * e.g., "cmd+k", "cmd+shift+,", "cmd+delete" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * e.g., "cmd+k", "cmd+shift+,", "cmd+backspace" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function formatModifierShortcut(e: KeyboardEvent): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function formatModifierShortcut(e: KeyboardEvent, layoutMap: TKeyboardLayoutMap | null): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parts: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.ctrlKey || e.metaKey) parts.push("cmd"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.altKey) parts.push("alt"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.shiftKey) parts.push("shift"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const key = e.key.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parts.push(key === " " ? "space" : key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parts.push(resolveShortcutKey(e, layoutMap)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return parts.join("+"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** ARIA roles whose keyboard handling (typing, typeahead) must win over global shortcuts */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const TYPING_ROLES = new Set([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "textbox", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "searchbox", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "combobox", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "spinbutton", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "listbox", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "option", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "menu", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "menuitem", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "menuitemcheckbox", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "menuitemradio", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Overlays and editors that own keyboard input while focus is inside them */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const KEYBOARD_OWNING_ANCESTOR = '[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"], .ProseMirror'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Checks if the event target is a typing-focused element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Whether keystrokes on this target belong to a widget: form controls, editable content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * (including `plaintext-only` and inherited editability), and ARIA widgets or overlays with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * their own keyboard handling. Global shortcuts neither fire from nor linger across them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function isTypingInInput(target: EventTarget | null): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!target) return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!(target instanceof Element)) return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (target instanceof HTMLInputElement) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (target instanceof HTMLTextAreaElement) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (target instanceof HTMLSelectElement) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (target instanceof HTMLElement && target.isContentEditable) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const element = target as Element; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (element.classList?.contains("ProseMirror")) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (element.getAttribute?.("contenteditable") === "true") return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const role = target.getAttribute("role"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (role && TYPING_ROLES.has(role)) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return target.closest(KEYBOARD_OWNING_ANCESTOR) !== null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Whether any registered sequence starts with `prefix` (equality included) */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hasSequencePrefix = (sequenceMap: ReadonlyMap<string, string>, prefix: string): boolean => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const sequence of sequenceMap.keys()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sequence.startsWith(prefix)) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Global shortcut handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Handles all keyboard shortcuts: single keys, sequences, and modifiers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -50,12 +158,30 @@ export class ShortcutHandler { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private registry: IPowerKCommandRegistry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private getContext: () => TPowerKContext; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private openPalette: () => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private togglePalette: () => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private layoutMap: TKeyboardLayoutMap | null = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private isEnabled = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor(registry: IPowerKCommandRegistry, getContext: () => TPowerKContext, openPalette: () => void) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registry: IPowerKCommandRegistry, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getContext: () => TPowerKContext, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| openPalette: () => void, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| togglePalette: () => void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.registry = registry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.getContext = getContext; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.openPalette = openPalette; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.togglePalette = togglePalette; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void this.loadLayoutMap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Resolves the OS keyboard layout; until then, and on browsers without the API, the US | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * fallback applies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private async loadLayoutMap(): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const layoutMap = await loadKeyboardLayoutMap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.isEnabled) this.layoutMap = layoutMap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+175
to
185
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Refresh the keyboard layout map. A stale map resolves keys to the wrong shortcut.
Example: the page loads on a US layout, and the user then switches to German. On the German layout, the Make two changes:
🐛 Proposed staleness guard const fromLayout = e.code ? layoutMap?.get(e.code) : undefined;
- if (fromLayout) return isAsciiPrintable(fromLayout) ? fromLayout.toLowerCase() : (usKeyForCode(e.code) ?? fromLayout);
+ const unshiftedKey = !e.shiftKey && !e.altKey ? e.key.toLowerCase() : undefined;
+ const isStale =
+ fromLayout !== undefined &&
+ unshiftedKey !== undefined &&
+ isAsciiPrintable(unshiftedKey) &&
+ unshiftedKey !== fromLayout.toLowerCase();
+ if (fromLayout && !isStale)
+ return isAsciiPrintable(fromLayout) ? fromLayout.toLowerCase() : (usKeyForCode(e.code) ?? fromLayout); constructor(
@@
this.togglePalette = togglePalette;
void this.loadLayoutMap();
+ window.addEventListener("focus", this.handleWindowFocus);
}
+
+ private handleWindowFocus = (): void => {
+ void this.loadLayoutMap();
+ };
@@
destroy(): void {
this.resetSequence();
this.isEnabled = false;
+ window.removeEventListener("focus", this.handleWindowFocus);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -71,36 +197,48 @@ export class ShortcutHandler { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleKeyDown = (e: KeyboardEvent): void => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!this.isEnabled) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const key = e.key.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hasModifier = e.metaKey || e.ctrlKey || e.altKey || e.shiftKey; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Auto-repeat must neither re-fire a shortcut nor touch the sequence buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.repeat) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Special: Cmd+K always opens command palette | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((e.metaKey || e.ctrlKey) && key === "k") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Cmd+K toggles the palette from anywhere, including inputs. It runs before the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `defaultPrevented` check because the palette itself prevents the default to keep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // cmdk's Ctrl+K vim binding from moving the selection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((e.metaKey || e.ctrlKey) && resolveShortcutKey(e, this.layoutMap) === "k") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.openPalette(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.togglePalette(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A component that already handled the key (editor keymaps, cmdk navigation) wins, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and the key cannot be part of a sequence either | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.defaultPrevented) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Don't handle shortcuts when typing in inputs (except Cmd+K) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Keys typed into inputs and widgets never trigger shortcuts, nor continue a sequence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isTypingInInput(e.target)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle modifier shortcuts (Cmd+Delete, Cmd+Shift+,, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hasModifier) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Any modified key cancels a pending sequence, whether or not it matches a shortcut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.handleModifierShortcut(e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle single key shortcuts and sequences (c, p, gm, op, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.handleKeyOrSequence(e, key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle single key shortcuts and sequences (s, gm, op, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.handleKeyOrSequence(e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Handle modifier shortcuts (Cmd+X, Cmd+Shift+X, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private handleModifierShortcut(e: KeyboardEvent): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shortcut = formatModifierShortcut(e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shortcut = formatModifierShortcut(e, this.layoutMap); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const command = this.registry.findByModifierShortcut(this.getContext(), shortcut); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (command && this.canExecuteCommand(command)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -110,34 +248,58 @@ export class ShortcutHandler { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Handle single key shortcuts or build sequences (c, gm, op, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Handle single key shortcuts or build sequences (s, gm, op, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private handleKeyOrSequence(e: KeyboardEvent, key: string): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Add key to sequence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private handleKeyOrSequence(e: KeyboardEvent): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Non-printable keys (Escape, Tab, Enter, arrows, ...) cancel a pending sequence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.key.length !== 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-printable keys now cancel a pending sequence, but modifier keys and executed modifier shortcuts still don't. A stale prefix survives Cmd+B, Shift+E and similar, and can complete a navigation afterwards. User presses "g" (buffer "g"), changes their mind and presses Cmd+B to toggle the sidebar, then presses "m" within 1s, for example to start typing. The buffer still holds "g", so "gm" matches and the app navigates to Modules. handleModifierShortcut never calls resetSequence().
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 3b4a95e — modified keys reset the buffer before dispatch, matched or not. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const key = resolveShortcutKey(e, this.layoutMap); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ctx = this.getContext(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Visible commands are computed on every call outside a reactive context, so fetch the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // map once and answer both the exact and the prefix lookup from it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sequenceMap = this.registry.getKeySequenceMap(ctx); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.sequence += key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if sequence matches a command (e.g., "gm", "op") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sequenceCommand = this.registry.findByKeySequence(this.getContext(), this.sequence); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sequenceCommandId = sequenceMap.get(this.sequence); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sequenceCommand = sequenceCommandId ? this.registry.getCommand(sequenceCommandId) : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sequenceCommand && this.canExecuteCommand(sequenceCommand)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.executeCommand(sequenceCommand); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If sequence is one character, check for single-key shortcut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The buffer can still become a sequence - wait for the next key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hasSequencePrefix(sequenceMap, this.sequence)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.scheduleSequenceReset(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A fresh key that starts no sequence is a single-key shortcut, or nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.sequence.length === 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const singleKeyCommand = this.registry.findByShortcut(this.getContext(), key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (singleKeyCommand && this.canExecuteCommand(singleKeyCommand)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shortcutCommandId = this.registry.getShortcutMap(ctx).get(key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shortcutCommand = shortcutCommandId ? this.registry.getCommand(shortcutCommandId) : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (shortcutCommand && this.canExecuteCommand(shortcutCommand)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.executeCommand(singleKeyCommand); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.executeCommand(shortcutCommand); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Reset sequence after 1 second of no typing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.scheduleSequenceReset(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A pending prefix this key does not extend: the key may start a new sequence ("g", "g", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // "m"), otherwise the stray keystroke is dropped rather than re-read as a single-key shortcut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resetSequence(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hasSequencePrefix(sequenceMap, key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.sequence = key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.scheduleSequenceReset(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
focus_top_nav_search is always enabled, so the global handler preventDefaults Cmd/Ctrl+F even when no top-nav input is mounted, and the simplified action silently no-ops.
On a route where TopNavPowerK is not mounted (topNavInputRef is null), Cmd+F matches cmd+f. canExecuteCommand returns true because isEnabled is () => true, so e.preventDefault() suppresses the browser's Find bar and
topNavInputRef?.current?.focus()does nothing. The user loses Find with no feedback. Gate it with isEnabled/isVisible: () => Boolean(topNavInputRef?.current).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gated in 25e0465 —
isVisible/isEnabledareBoolean(topNavInputRef?.current).