@tanstack/hotkeys 0.7.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/dist/{manager.utils.js → _event-target.cjs} +5 -41
- package/dist/_event-target.cjs.map +1 -0
- package/dist/{manager.utils.cjs → _event-target.js} +3 -47
- package/dist/_event-target.js.map +1 -0
- package/dist/_keyboard-event.cjs +83 -0
- package/dist/_keyboard-event.cjs.map +1 -0
- package/dist/_keyboard-event.js +76 -0
- package/dist/_keyboard-event.js.map +1 -0
- package/dist/_match.cjs +49 -0
- package/dist/_match.cjs.map +1 -0
- package/dist/_match.js +49 -0
- package/dist/_match.js.map +1 -0
- package/dist/_named-keys.cjs +68 -0
- package/dist/_named-keys.cjs.map +1 -0
- package/dist/_named-keys.d.cts +8 -0
- package/dist/_named-keys.d.ts +8 -0
- package/dist/_named-keys.js +66 -0
- package/dist/_named-keys.js.map +1 -0
- package/dist/_recorder-chord.cjs +44 -0
- package/dist/_recorder-chord.cjs.map +1 -0
- package/dist/_recorder-chord.js +43 -0
- package/dist/_recorder-chord.js.map +1 -0
- package/dist/_recording-guard.cjs +59 -0
- package/dist/_recording-guard.cjs.map +1 -0
- package/dist/_recording-guard.js +56 -0
- package/dist/_recording-guard.js.map +1 -0
- package/dist/_registration.cjs +54 -0
- package/dist/_registration.cjs.map +1 -0
- package/dist/_registration.js +50 -0
- package/dist/_registration.js.map +1 -0
- package/dist/conflicts.cjs +55 -0
- package/dist/conflicts.cjs.map +1 -0
- package/dist/conflicts.d.cts +39 -0
- package/dist/conflicts.d.ts +39 -0
- package/dist/conflicts.js +55 -0
- package/dist/conflicts.js.map +1 -0
- package/dist/constants.cjs +46 -171
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +4 -129
- package/dist/constants.d.ts +4 -129
- package/dist/constants.js +48 -164
- package/dist/constants.js.map +1 -1
- package/dist/display-labels.cjs +119 -0
- package/dist/display-labels.cjs.map +1 -0
- package/dist/display-labels.d.cts +93 -0
- package/dist/display-labels.d.ts +93 -0
- package/dist/display-labels.js +113 -0
- package/dist/display-labels.js.map +1 -0
- package/dist/format.cjs +30 -36
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.cts +46 -7
- package/dist/format.d.ts +46 -7
- package/dist/format.js +31 -37
- package/dist/format.js.map +1 -1
- package/dist/hint.cjs +24 -0
- package/dist/hint.cjs.map +1 -0
- package/dist/hint.d.cts +23 -0
- package/dist/hint.d.ts +23 -0
- package/dist/hint.js +24 -0
- package/dist/hint.js.map +1 -0
- package/dist/hotkey-manager.cjs +53 -22
- package/dist/hotkey-manager.cjs.map +1 -1
- package/dist/hotkey-manager.d.cts +6 -2
- package/dist/hotkey-manager.d.ts +6 -2
- package/dist/hotkey-manager.js +49 -18
- package/dist/hotkey-manager.js.map +1 -1
- package/dist/hotkey-recorder.cjs +67 -11
- package/dist/hotkey-recorder.cjs.map +1 -1
- package/dist/hotkey-recorder.d.cts +6 -3
- package/dist/hotkey-recorder.d.ts +6 -3
- package/dist/hotkey-recorder.js +66 -10
- package/dist/hotkey-recorder.js.map +1 -1
- package/dist/hotkey-sequence-recorder.cjs +85 -22
- package/dist/hotkey-sequence-recorder.cjs.map +1 -1
- package/dist/hotkey-sequence-recorder.d.cts +9 -2
- package/dist/hotkey-sequence-recorder.d.ts +9 -2
- package/dist/hotkey-sequence-recorder.js +84 -21
- package/dist/hotkey-sequence-recorder.js.map +1 -1
- package/dist/{hotkey.d.ts → hotkey.types.d.cts} +55 -120
- package/dist/{hotkey.d.cts → hotkey.types.d.ts} +55 -120
- package/dist/index.cjs +18 -10
- package/dist/index.d.cts +14 -9
- package/dist/index.d.ts +14 -9
- package/dist/index.js +10 -6
- package/dist/key-state-tracker.cjs +24 -24
- package/dist/key-state-tracker.cjs.map +1 -1
- package/dist/key-state-tracker.js +24 -24
- package/dist/key-state-tracker.js.map +1 -1
- package/dist/key.types.d.cts +82 -0
- package/dist/key.types.d.ts +82 -0
- package/dist/match.cjs +21 -30
- package/dist/match.cjs.map +1 -1
- package/dist/match.d.cts +19 -3
- package/dist/match.d.ts +19 -3
- package/dist/match.js +19 -29
- package/dist/match.js.map +1 -1
- package/dist/parse.cjs +43 -26
- package/dist/parse.cjs.map +1 -1
- package/dist/parse.d.cts +10 -4
- package/dist/parse.d.ts +10 -4
- package/dist/parse.js +35 -19
- package/dist/parse.js.map +1 -1
- package/dist/platform.cjs +55 -0
- package/dist/platform.cjs.map +1 -0
- package/dist/platform.d.cts +44 -0
- package/dist/platform.d.ts +44 -0
- package/dist/platform.js +53 -0
- package/dist/platform.js.map +1 -0
- package/dist/recorder-options.d.cts +34 -0
- package/dist/recorder-options.d.ts +34 -0
- package/dist/sequence-manager.cjs +54 -28
- package/dist/sequence-manager.cjs.map +1 -1
- package/dist/sequence-manager.d.cts +18 -3
- package/dist/sequence-manager.d.ts +18 -3
- package/dist/sequence-manager.js +49 -23
- package/dist/sequence-manager.js.map +1 -1
- package/dist/validate.cjs +6 -10
- package/dist/validate.cjs.map +1 -1
- package/dist/validate.d.cts +13 -2
- package/dist/validate.d.ts +13 -2
- package/dist/validate.js +7 -11
- package/dist/validate.js.map +1 -1
- package/package.json +2 -2
- package/src/{manager.utils.ts → _event-target.ts} +0 -74
- package/src/_keyboard-event.ts +101 -0
- package/src/_match.ts +96 -0
- package/src/_named-keys.ts +62 -0
- package/src/_recorder-chord.ts +76 -0
- package/src/_recording-guard.ts +63 -0
- package/src/_registration.ts +79 -0
- package/src/conflicts.ts +125 -0
- package/src/constants.ts +48 -196
- package/src/display-labels.ts +123 -0
- package/src/format.ts +111 -52
- package/src/hint.ts +42 -0
- package/src/hotkey-manager.ts +95 -40
- package/src/hotkey-recorder.ts +92 -16
- package/src/hotkey-sequence-recorder.ts +110 -28
- package/src/{hotkey.ts → hotkey.types.ts} +57 -224
- package/src/index.ts +11 -5
- package/src/key-state-tracker.ts +36 -24
- package/src/key.types.ts +268 -0
- package/src/match.ts +41 -90
- package/src/parse.ts +67 -26
- package/src/platform.ts +66 -0
- package/src/recorder-options.ts +34 -0
- package/src/sequence-manager.ts +77 -42
- package/src/validate.ts +29 -21
- package/dist/manager.utils.cjs.map +0 -1
- package/dist/manager.utils.d.cts +0 -13
- package/dist/manager.utils.d.ts +0 -13
- package/dist/manager.utils.js.map +0 -1
- package/dist/recorder-chord.cjs +0 -18
- package/dist/recorder-chord.cjs.map +0 -1
- package/dist/recorder-chord.js +0 -18
- package/dist/recorder-chord.js.map +0 -1
- package/src/recorder-chord.ts +0 -25
package/dist/match.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"match.js","names":[],"sources":["../src/match.ts"],"sourcesContent":["import {\n PUNCTUATION_CODE_MAP,\n detectPlatform,\n isSingleLetterKey,\n normalizeKeyName,\n} from './constants'\nimport { parseHotkey } from './parse'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n} from './hotkey'\n\n/**\n * Checks if a KeyboardEvent matches a hotkey.\n *\n * Uses the `key` property from KeyboardEvent for matching, with a fallback to `code`\n * for letter keys, digit keys (0-9), and punctuation keys when `key` produces special\n * characters (e.g., macOS Option+letter, Shift+number, or Option+punctuation).\n * Letter keys are matched case-insensitively.\n *\n * Also handles \"dead key\" events where `event.key` is `'Dead'` instead of the expected\n * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,\n * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.\n * In these cases, `event.code` is used to determine the physical key.\n *\n * @param event - The KeyboardEvent to check\n * @param hotkey - The hotkey string or ParsedHotkey to match against\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns True if the event matches the hotkey\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * if (matchesKeyboardEvent(event, 'Mod+S')) {\n * event.preventDefault()\n * handleSave()\n * }\n * })\n * ```\n */\nexport function matchesKeyboardEvent(\n event: KeyboardEvent,\n hotkey: Hotkey | ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check modifiers\n if (event.ctrlKey !== parsed.ctrl) {\n return false\n }\n if (event.shiftKey !== parsed.shift) {\n return false\n }\n if (event.altKey !== parsed.alt) {\n return false\n }\n if (event.metaKey !== parsed.meta) {\n return false\n }\n\n // Check key (case-insensitive for letters)\n const eventKey = normalizeKeyName(event.key)\n const hotkeyKey = parsed.key\n\n // For single-character keys (not dead keys), try direct event.key match first\n if (eventKey !== 'Dead' && eventKey.length === 1 && hotkeyKey.length === 1) {\n if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) {\n return true\n }\n\n // If event.key is a letter, we usually trust the keyboard layout.\n // For ASCII letters: always trust layout (Dvorak, Colemak, AZERTY support).\n // For non-ASCII letters without Alt: trust layout (e.g., Cyrillic keyboards).\n //\n // For non-ASCII letters WITH Alt held down: fall through to the event.code\n // fallback. macOS Option+letter combinations produce non-ASCII letters as\n // event.key (e.g., Option+A → 'å', Option+' → 'æ', Option+P → 'π'), but\n // event.code still reflects the physical key. We want Alt+A to match even\n // when Option+A fires event.key='å'.\n if (\n isSingleLetterKey(eventKey) &&\n (/^[A-Za-z]$/.test(eventKey) || !event.altKey)\n ) {\n return false\n }\n }\n\n // Fallback to event.code for dead keys or single-char mismatches where\n // event.key is a non-letter special character.\n // Dead keys: Option+letter on macOS, international layouts produce event.key === 'Dead'\n // Single-char mismatches: Cmd+Option+T gives '†' instead of 'T', Shift+4 gives '$'\n if (\n event.code &&\n (eventKey === 'Dead' || (eventKey.length === 1 && hotkeyKey.length === 1))\n ) {\n // fallback for letter keys (common with mac option + letter)\n if (event.code.startsWith('Key')) {\n const codeLetter = event.code.slice(3)\n if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) {\n return codeLetter.toUpperCase() === hotkeyKey.toUpperCase()\n }\n }\n\n // fallback for number keys (common with mac option + num)\n if (event.code.startsWith('Digit')) {\n const codeDigit = event.code.slice(5)\n if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) {\n return codeDigit === hotkeyKey\n }\n }\n // Fallback for punctuation keys (e.g., Minus, Slash, BracketLeft).\n // On macOS, Option+punctuation produces composed characters (e.g., Option+- → '–'),\n // but event.code still reports the physical key.\n if (event.code in PUNCTUATION_CODE_MAP) {\n return PUNCTUATION_CODE_MAP[event.code] === hotkeyKey\n }\n\n return false\n }\n\n // For special keys, compare exactly (after normalization)\n return eventKey === hotkeyKey\n}\n\n/**\n * Options for creating a hotkey handler.\n */\nexport interface CreateHotkeyHandlerOptions {\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n}\n\n/**\n * Creates a keyboard event handler that calls the callback when the hotkey matches.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to match\n * @param callback - The function to call when the hotkey matches\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {\n * console.log(`${hotkey} was pressed`)\n * handleSave()\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createHotkeyHandler(\n hotkey: Hotkey | ParsedHotkey,\n callback: HotkeyCallback,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n const hotkeyString: Hotkey =\n typeof hotkey === 'string' ? hotkey : formatParsedHotkey(hotkey)\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, resolvedPlatform) : hotkey\n\n const context: HotkeyCallbackContext = {\n hotkey: hotkeyString,\n parsedHotkey: parsed,\n }\n\n return (event: KeyboardEvent) => {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n callback(event, context)\n }\n }\n}\n\ntype MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback }\n\n/**\n * Creates a handler that matches multiple hotkeys.\n *\n * @param handlers - A map of hotkey strings to their handlers\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createMultiHotkeyHandler({\n * 'Mod+S': (event, { hotkey }) => handleSave(),\n * 'Mod+Z': (event, { hotkey }) => handleUndo(),\n * 'Mod+Shift+Z': (event, { hotkey }) => handleRedo(),\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createMultiHotkeyHandler(\n handlers: MultiHotkeyHandler,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n // Pre-parse all hotkeys for efficiency\n const parsedHandlers = Object.entries(handlers)\n .filter((entry): entry is [string, HotkeyCallback] => Boolean(entry[1]))\n .map(([hotkey, handler]) => {\n const parsed = parseHotkey(hotkey as Hotkey, resolvedPlatform)\n const context: HotkeyCallbackContext = {\n hotkey: hotkey as Hotkey,\n parsedHotkey: parsed,\n }\n return { parsed, handler, context }\n })\n\n return (event: KeyboardEvent) => {\n for (const { parsed, handler, context } of parsedHandlers) {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n handler(event, context)\n return // Only handle the first match\n }\n }\n }\n}\n\n/**\n * Formats a ParsedHotkey back to a hotkey string.\n * Used internally to provide the hotkey string in callback context.\n */\nfunction formatParsedHotkey(parsed: ParsedHotkey): Hotkey {\n const parts: Array<string> = []\n\n if (parsed.ctrl) parts.push('Control')\n if (parsed.alt) parts.push('Alt')\n if (parsed.shift) parts.push('Shift')\n if (par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K,OAAO;AACnC,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI"}
|
|
1
|
+
{"version":3,"file":"match.js","names":[],"sources":["../src/match.ts"],"sourcesContent":["import { matchKeyboardEvent } from './_match'\nimport { parseHotkey, parseRegisterableHotkey } from './parse'\nimport { detectPlatform } from './platform'\nimport { keysEqual } from './_keyboard-event'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey.types'\n\nexport interface KeyboardEventMatch {\n matched: boolean\n score: 0 | 1 | 2 | 3\n source?: 'key' | 'code' | 'fallback'\n identity?: { key: string; code: string }\n}\n\n/**\n * Checks if a KeyboardEvent matches a hotkey.\n *\n * Physical bindings such as `Mod+[KeyS]` match `event.code` exactly.\n * Logical bindings use `event.key`, with a fallback to `code`\n * for letter keys, digit keys (0-9), and punctuation keys when `key` produces special\n * characters (e.g., macOS Option+letter, Shift+number, or Option+punctuation).\n * Letter keys are matched case-insensitively.\n *\n * Also handles \"dead key\" events where `event.key` is `'Dead'` instead of the expected\n * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,\n * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.\n * In these cases, `event.code` is used to determine the physical key.\n *\n * @param event - The KeyboardEvent to check\n * @param hotkey - The hotkey string or ParsedHotkey to match against\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns True if the event matches the hotkey\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * if (matchesKeyboardEvent(event, 'Mod+S')) {\n * event.preventDefault()\n * handleSave()\n * }\n * })\n * ```\n */\nexport function matchesKeyboardEvent(\n event: KeyboardEvent,\n hotkey: Hotkey | ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n return matchKeyboardEvent(event, hotkey, platform).matched\n}\n\n/**\n * Options for creating a hotkey handler.\n */\nexport interface CreateHotkeyHandlerOptions {\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n}\n\n/**\n * Creates a keyboard event handler that calls the callback when the hotkey matches.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to match\n * @param callback - The function to call when the hotkey matches\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {\n * console.log(`${hotkey} was pressed`)\n * handleSave()\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createHotkeyHandler(\n hotkey: Hotkey | ParsedHotkey,\n callback: HotkeyCallback,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n const hotkeyString: Hotkey =\n typeof hotkey === 'string' ? hotkey : formatParsedHotkey(hotkey)\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, resolvedPlatform) : hotkey\n\n const context: HotkeyCallbackContext = {\n hotkey: hotkeyString,\n parsedHotkey: parsed,\n }\n\n return (event: KeyboardEvent) => {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n callback(event, context)\n }\n }\n}\n\nexport type MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback }\n\n/**\n * Creates a handler that matches multiple hotkeys.\n *\n * @param handlers - A map of hotkey strings to their handlers\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createMultiHotkeyHandler({\n * 'Mod+S': (event, { hotkey }) => handleSave(),\n * 'Mod+Z': (event, { hotkey }) => handleUndo(),\n * 'Mod+Shift+Z': (event, { hotkey }) => handleRedo(),\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createMultiHotkeyHandler(\n handlers: MultiHotkeyHandler,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n // Pre-parse all hotkeys for efficiency\n const parsedHandlers = Object.entries(handlers)\n .filter((entry): entry is [string, HotkeyCallback] => Boolean(entry[1]))\n .map(([hotkey, handler]) => {\n const parsed = parseHotkey(hotkey as Hotkey, resolvedPlatform)\n const context: HotkeyCallbackContext = {\n hotkey: hotkey as Hotkey,\n parsedHotkey: parsed,\n }\n return { parsed, handler, context }\n })\n\n return (event: KeyboardEvent) => {\n for (const { parsed, handler, context } of parsedHandlers) {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n handler(event, context)\n return // Only handle the first match\n }\n }\n }\n}\n\n/**\n * Formats a ParsedHotkey back to a hotkey string.\n * Used internally to provide the hotkey string in callback context.\n */\nfunction formatParsedHotkey(parsed: ParsedHotkey): Hotkey {\n const parts: Array<string> = []\n\n if (parsed.ctrl) parts.push('Control')\n if (parsed.alt) parts.push('Alt')\n if (parsed.shift) parts.push('Shift')\n if (parsed.meta) parts.push('Meta')\n parts.push(parsed.code !== undefined ? `[${parsed.code}]` : parsed.key)\n\n return parts.join('+') as Hotkey\n}\n\n/**\n * Compares normalized binding identity, including all four modifiers.\n * Aliases resolve on the supplied platform. A physical code and a logical key\n * remain distinct even when one keyboard event could match both.\n */\nexport function areHotkeysEqual(\n left: RegisterableHotkey,\n right: RegisterableHotkey,\n platform = detectPlatform(),\n): boolean {\n const a = parseRegisterableHotkey(left, platform)\n const b = parseRegisterableHotkey(right, platform)\n return (\n a.ctrl === b.ctrl &&\n a.alt === b.alt &&\n a.shift === b.shift &&\n a.meta === b.meta &&\n (a.code !== undefined || b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AAI,gBACF,OAAM,iBAAiB;AAEzB,WAAQ,OAAO,QAAQ;AACvB;;;;;;;;AAUR,SAAS,mBAAmB,QAA8B;CACxD,MAAM,QAAuB,EAAE;AAE/B,KAAI,OAAO,KAAM,OAAM,KAAK,UAAU;AACtC,KAAI,OAAO,IAAK,OAAM,KAAK,MAAM;AACjC,KAAI,OAAO,MAAO,OAAM,KAAK,QAAQ;AACrC,KAAI,OAAO,KAAM,OAAM,KAAK,OAAO;AACnC,OAAM,KAAK,OAAO,SAAS,SAAY,IAAI,OAAO,KAAK,KAAK,OAAO,IAAI;AAEvE,QAAO,MAAM,KAAK,IAAI;;;;;;;AAQxB,SAAgB,gBACd,MACA,OACA,WAAW,gBAAgB,EAClB;CACT,MAAM,IAAI,wBAAwB,MAAM,SAAS;CACjD,MAAM,IAAI,wBAAwB,OAAO,SAAS;AAClD,QACE,EAAE,SAAS,EAAE,QACb,EAAE,QAAQ,EAAE,OACZ,EAAE,UAAU,EAAE,SACd,EAAE,SAAS,EAAE,SACZ,EAAE,SAAS,UAAa,EAAE,SAAS,SAChC,EAAE,SAAS,EAAE,OACb,UAAU,EAAE,KAAK,EAAE,IAAI"}
|
package/dist/parse.cjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_platform = require('./platform.cjs');
|
|
3
|
+
const require__keyboard_event = require('./_keyboard-event.cjs');
|
|
2
4
|
|
|
3
5
|
//#region src/parse.ts
|
|
4
6
|
/**
|
|
@@ -6,7 +8,7 @@ const require_constants = require('./constants.cjs');
|
|
|
6
8
|
*
|
|
7
9
|
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
10
|
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
-
* @returns A ParsedHotkey
|
|
11
|
+
* @returns A ParsedHotkey with either logical key or physical code and modifier flags
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
@@ -15,8 +17,8 @@ const require_constants = require('./constants.cjs');
|
|
|
15
17
|
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
16
18
|
* ```
|
|
17
19
|
*/
|
|
18
|
-
function parseHotkey(hotkey, platform =
|
|
19
|
-
const parts =
|
|
20
|
+
function parseHotkey(hotkey, platform = require_platform.detectPlatform()) {
|
|
21
|
+
const parts = require__keyboard_event.splitHotkeyParts(hotkey);
|
|
20
22
|
const modifiers = /* @__PURE__ */ new Set();
|
|
21
23
|
let key = "";
|
|
22
24
|
for (let i = 0; i < parts.length; i++) {
|
|
@@ -25,14 +27,15 @@ function parseHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
|
25
27
|
else {
|
|
26
28
|
const alias = require_constants.MODIFIER_ALIASES[part] ?? require_constants.MODIFIER_ALIASES[part.toLowerCase()];
|
|
27
29
|
if (alias) {
|
|
28
|
-
const resolved =
|
|
30
|
+
const resolved = require_platform.resolveModifier(alias, platform);
|
|
29
31
|
modifiers.add(resolved);
|
|
30
32
|
} else if (parts.length === 1) key = require_constants.normalizeKeyName(part);
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
if (!key && parts.length > 0) key = require_constants.normalizeKeyName(parts[parts.length - 1].trim());
|
|
36
|
+
const code = /^\[([A-Za-z][A-Za-z0-9]*)\]$/.exec(key)?.[1];
|
|
34
37
|
return {
|
|
35
|
-
key,
|
|
38
|
+
...code !== void 0 ? { code } : { key },
|
|
36
39
|
ctrl: modifiers.has("Control"),
|
|
37
40
|
shift: modifiers.has("Shift"),
|
|
38
41
|
alt: modifiers.has("Alt"),
|
|
@@ -61,12 +64,13 @@ function parseHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
|
61
64
|
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
62
65
|
* ```
|
|
63
66
|
*/
|
|
64
|
-
function rawHotkeyToParsedHotkey(raw, platform =
|
|
67
|
+
function rawHotkeyToParsedHotkey(raw, platform = require_platform.detectPlatform()) {
|
|
68
|
+
if (raw.code === void 0) require__keyboard_event.assertLogicalKey(raw.key);
|
|
65
69
|
let ctrl = raw.ctrl ?? false;
|
|
66
70
|
const shift = raw.shift ?? false;
|
|
67
71
|
const alt = raw.alt ?? false;
|
|
68
72
|
let meta = raw.meta ?? false;
|
|
69
|
-
if (raw.mod) if (
|
|
73
|
+
if (raw.mod) if (require_platform.resolveModifier("Mod", platform) === "Control") ctrl = true;
|
|
70
74
|
else meta = true;
|
|
71
75
|
const modifiers = require_constants.MODIFIER_ORDER.filter((m) => {
|
|
72
76
|
switch (m) {
|
|
@@ -78,7 +82,7 @@ function rawHotkeyToParsedHotkey(raw, platform = require_constants.detectPlatfor
|
|
|
78
82
|
}
|
|
79
83
|
});
|
|
80
84
|
return {
|
|
81
|
-
key: raw.key,
|
|
85
|
+
...raw.code !== void 0 ? { code: raw.code } : { key: require_constants.normalizeKeyName(raw.key) },
|
|
82
86
|
ctrl,
|
|
83
87
|
shift,
|
|
84
88
|
alt,
|
|
@@ -92,6 +96,7 @@ function rawHotkeyToParsedHotkey(raw, platform = require_constants.detectPlatfor
|
|
|
92
96
|
* otherwise full `Control+Alt+Shift+Meta` order.
|
|
93
97
|
*/
|
|
94
98
|
function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
99
|
+
if (parsed.code === void 0) require__keyboard_event.assertLogicalKey(parsed.key);
|
|
95
100
|
const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
|
|
96
101
|
const parts = [];
|
|
97
102
|
if (canUseMod) {
|
|
@@ -99,7 +104,7 @@ function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
|
99
104
|
if (parsed.alt) parts.push("Alt");
|
|
100
105
|
if (parsed.shift) parts.push("Shift");
|
|
101
106
|
} else for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
102
|
-
parts.push(require_constants.normalizeKeyName(parsed.key));
|
|
107
|
+
parts.push(parsed.code !== void 0 ? `[${parsed.code}]` : require_constants.normalizeKeyName(parsed.key));
|
|
103
108
|
return parts.join("+");
|
|
104
109
|
}
|
|
105
110
|
/**
|
|
@@ -122,20 +127,20 @@ function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
|
122
127
|
* normalizeHotkey('esc') // 'Escape'
|
|
123
128
|
* ```
|
|
124
129
|
*/
|
|
125
|
-
function normalizeHotkey(hotkey, platform =
|
|
130
|
+
function normalizeHotkey(hotkey, platform = require_platform.detectPlatform()) {
|
|
126
131
|
return normalizedHotkeyStringFromParsed(parseHotkey(hotkey, platform), platform);
|
|
127
132
|
}
|
|
128
133
|
/**
|
|
129
134
|
* Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
|
|
130
135
|
*/
|
|
131
|
-
function normalizeHotkeyFromParsed(parsed, platform =
|
|
136
|
+
function normalizeHotkeyFromParsed(parsed, platform = require_platform.detectPlatform()) {
|
|
132
137
|
return normalizedHotkeyStringFromParsed(parsed, platform);
|
|
133
138
|
}
|
|
134
139
|
/**
|
|
135
140
|
* Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
|
|
136
141
|
* Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
|
|
137
142
|
*/
|
|
138
|
-
function normalizeRegisterableHotkey(hotkey, platform =
|
|
143
|
+
function normalizeRegisterableHotkey(hotkey, platform = require_platform.detectPlatform()) {
|
|
139
144
|
return typeof hotkey === "string" ? normalizeHotkey(hotkey, platform) : normalizeHotkeyFromParsed(rawHotkeyToParsedHotkey(hotkey, platform), platform);
|
|
140
145
|
}
|
|
141
146
|
/**
|
|
@@ -168,19 +173,22 @@ function isModifierKey(key) {
|
|
|
168
173
|
* })
|
|
169
174
|
* ```
|
|
170
175
|
*/
|
|
171
|
-
function parseKeyboardEvent(event) {
|
|
172
|
-
const
|
|
176
|
+
function parseKeyboardEvent(event, platform) {
|
|
177
|
+
const normalized = require__keyboard_event.normalizeKeyboardEvent(event, platform);
|
|
178
|
+
const ctrl = normalized.altGraph ? false : normalized.ctrl;
|
|
179
|
+
const alt = normalized.altGraph ? false : normalized.alt;
|
|
180
|
+
const shift = normalized.shift;
|
|
173
181
|
const modifiers = [];
|
|
174
|
-
if (
|
|
175
|
-
if (
|
|
176
|
-
if (
|
|
177
|
-
if (
|
|
182
|
+
if (ctrl) modifiers.push("Control");
|
|
183
|
+
if (alt) modifiers.push("Alt");
|
|
184
|
+
if (shift) modifiers.push("Shift");
|
|
185
|
+
if (normalized.meta) modifiers.push("Meta");
|
|
178
186
|
return {
|
|
179
|
-
key:
|
|
180
|
-
ctrl
|
|
181
|
-
shift
|
|
182
|
-
alt
|
|
183
|
-
meta:
|
|
187
|
+
key: normalized.key,
|
|
188
|
+
ctrl,
|
|
189
|
+
shift,
|
|
190
|
+
alt,
|
|
191
|
+
meta: normalized.meta,
|
|
184
192
|
modifiers
|
|
185
193
|
};
|
|
186
194
|
}
|
|
@@ -190,8 +198,8 @@ function parseKeyboardEvent(event) {
|
|
|
190
198
|
* @param event - The keyboard event (typically `keydown`)
|
|
191
199
|
* @param platform - Target platform for `Mod` eligibility
|
|
192
200
|
*/
|
|
193
|
-
function normalizeHotkeyFromEvent(event, platform =
|
|
194
|
-
return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform);
|
|
201
|
+
function normalizeHotkeyFromEvent(event, platform = require_platform.detectPlatform()) {
|
|
202
|
+
return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event, platform), platform);
|
|
195
203
|
}
|
|
196
204
|
/**
|
|
197
205
|
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
@@ -210,10 +218,18 @@ function normalizeHotkeyFromEvent(event, platform = require_constants.detectPlat
|
|
|
210
218
|
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
211
219
|
* ```
|
|
212
220
|
*/
|
|
213
|
-
function hasNonModifierKey(hotkey, platform =
|
|
221
|
+
function hasNonModifierKey(hotkey, platform = require_platform.detectPlatform()) {
|
|
214
222
|
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
|
|
223
|
+
if (parsed.code !== void 0) return parsed.code.length > 0 && !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code);
|
|
215
224
|
return !isModifierKey(parsed.key) && parsed.key.length > 0;
|
|
216
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Parses a string or raw binding, resolving Mod for the supplied platform.
|
|
228
|
+
* Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
|
|
229
|
+
*/
|
|
230
|
+
function parseRegisterableHotkey(hotkey, platform = require_platform.detectPlatform()) {
|
|
231
|
+
return typeof hotkey === "string" ? parseHotkey(hotkey, platform) : rawHotkeyToParsedHotkey(hotkey, platform);
|
|
232
|
+
}
|
|
217
233
|
|
|
218
234
|
//#endregion
|
|
219
235
|
exports.hasNonModifierKey = hasNonModifierKey;
|
|
@@ -224,5 +240,6 @@ exports.normalizeHotkeyFromParsed = normalizeHotkeyFromParsed;
|
|
|
224
240
|
exports.normalizeRegisterableHotkey = normalizeRegisterableHotkey;
|
|
225
241
|
exports.parseHotkey = parseHotkey;
|
|
226
242
|
exports.parseKeyboardEvent = parseKeyboardEvent;
|
|
243
|
+
exports.parseRegisterableHotkey = parseRegisterableHotkey;
|
|
227
244
|
exports.rawHotkeyToParsedHotkey = rawHotkeyToParsedHotkey;
|
|
228
245
|
//# sourceMappingURL=parse.cjs.map
|
package/dist/parse.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.cjs","names":["detectPlatform","normalizeKeyName","MODIFIER_ALIASES","resolveModifier","MODIFIER_ORDER"],"sources":["../src/parse.ts"],"sourcesContent":["import {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n detectPlatform,\n normalizeKeyName,\n resolveModifier,\n} from './constants'\nimport type {\n CanonicalModifier,\n Hotkey,\n Key,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey object with the key and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = hotkey.split('+')\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n return {\n key,\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n key: raw.key,\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(normalizeKeyName(parsed.key))\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey {\n const normalizedKey = normalizeKeyName(event.key)\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (event.ctrlKey) modifiers.push('Control')\n if (event.altKey) modifiers.push('Alt')\n if (event.shiftKey) modifiers.push('Shift')\n if (event.metaKey) modifiers.push('Meta')\n\n return {\n key: normalizedKey,\n ctrl: event.ctrlKey,\n shift: event.shiftKey,\n alt: event.altKey,\n meta: event.metaKey,\n modifiers,\n }\n}\n\n/**\n * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform)\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifierKey(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8BA,SAAgB,YACd,QACA,WAAwCA,kCAAgB,EAC1C;CACd,MAAM,QAAQ,OAAO,MAAM,IAAI;CAC/B,MAAM,4BAAoC,IAAI,KAAK;CACnD,IAAI,MAAM;AAEV,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACrC,MAAM,OAAO,MAAM,GAAI,MAAM;AAE7B,MAAI,MAAM,MAAM,SAAS,EAEvB,OAAMC,mCAAiB,KAAK;OACvB;GAEL,MAAM,QACJC,mCAAiB,SAASA,mCAAiB,KAAK,aAAa;AAE/D,OAAI,OAAO;IACT,MAAM,WAAWC,kCAAgB,OAAO,SAAS;AACjD,cAAU,IAAI,SAAS;cAInB,MAAM,WAAW,EACnB,OAAMF,mCAAiB,KAAK;;;AAOpC,KAAI,CAAC,OAAO,MAAM,SAAS,EACzB,OAAMA,mCAAiB,MAAM,MAAM,SAAS,GAAI,MAAM,CAAC;AAGzD,QAA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L,YAAY,QAAQ,SAAS,EAC7B,SACD;;;;;AAMH,SAAgB,0BACd,QACA,WAAwCA,kCAAgB,EAChD;AACR,QAAO,iCAAiC,QAAQ,SAAS;;;;;;AAO3D,SAAgB,4BACd,QACA,WAAwCA,kCAAgB,EAChD;AACR,QAAO,OAAO,WAAW,WACrB,gBAAgB,QAAQ,SAAS,GACjC,0BACE,wBAAwB,QAAQ,SAAS,EACzC,SACD;;;;;;;;;;;AAYP,SAAgB,cACd,KACsC;AACtC,QAAO,OAAOE,sCAAoB,IAAI,aAAa,IAAIA;;;;;;;;;;;;;;;;;;;;AAqBzD,SAAgB,mBAAmB,OAAoC;CACrE,MAAM,gBAAgBD,mCAAiB,MAAM,IAAI;CAGjD,MAAM,YAAsC,EAAE;AAC9C,KAAI,MAAM,QAAS,WAAU,KAAK,UAAU;AAC5C,KAAI,MAAM,OAAQ,WAAU,KAAK,MAAM;AACvC,KAAI,MAAM,SAAU,WAAU,KAAK,QAAQ;AAC3C,KAAI,MAAM,QAAS,WAAU,KAAK,OAAO;AAEzC,QAAO;EACL,KAAK;EACL,MAAM,MAAM;EACZ,OAAO,MAAM;EACb,KAAK,MAAM;EACX,MAAM,MAAM;EACZ;EACD;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwCD,kCAAgB,EAChD;AACR,QAAO,iCAAiC,mBAAmB,MAAM,EAAE,SAAS;;;;;;;;;;;;;;;;;;;AAoB9E,SAAgB,kBACd,QACA,WAAwCA,kCAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS"}
|
|
1
|
+
{"version":3,"file":"parse.cjs","names":["detectPlatform","splitHotkeyParts","normalizeKeyName","MODIFIER_ALIASES","resolveModifier","MODIFIER_ORDER","normalizeKeyboardEvent"],"sources":["../src/parse.ts"],"sourcesContent":["import {\n assertLogicalKey,\n normalizeKeyboardEvent,\n splitHotkeyParts,\n} from './_keyboard-event'\nimport { MODIFIER_ALIASES, MODIFIER_ORDER, normalizeKeyName } from './constants'\nimport { detectPlatform, resolveModifier } from './platform'\nimport type { CanonicalModifier, Key } from './key.types'\nimport type {\n Hotkey,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey.types'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey with either logical key or physical code and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = splitHotkeyParts(hotkey)\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n const code = /^\\[([A-Za-z][A-Za-z0-9]*)\\]$/.exec(key)?.[1]\n return {\n ...(code !== undefined ? { code } : { key }),\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n if (raw.code === undefined) assertLogicalKey(raw.key)\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n ...(raw.code !== undefined\n ? { code: raw.code }\n : { key: normalizeKeyName(raw.key) }),\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n if (parsed.code === undefined) assertLogicalKey(parsed.key)\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(\n parsed.code !== undefined\n ? `[${parsed.code}]`\n : normalizeKeyName(parsed.key),\n )\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(\n event: KeyboardEvent,\n platform?: 'mac' | 'windows' | 'linux',\n): ParsedHotkey {\n const normalized = normalizeKeyboardEvent(event, platform)\n // AltGraph is a character-production modifier, not an ordinary Control+Alt\n // shortcut. Record the produced glyph instead of a synthetic Ctrl+Alt chord.\n const ctrl = normalized.altGraph ? false : normalized.ctrl\n const alt = normalized.altGraph ? false : normalized.alt\n // Shift is a real modifier even during AltGraph input; preserve it for replay.\n const shift = normalized.shift\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (ctrl) modifiers.push('Control')\n if (alt) modifiers.push('Alt')\n if (shift) modifiers.push('Shift')\n if (normalized.meta) modifiers.push('Meta')\n\n return {\n key: normalized.key,\n ctrl,\n shift,\n alt,\n meta: normalized.meta,\n modifiers,\n }\n}\n\n/**\n * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseKeyboardEvent(event, platform),\n platform,\n )\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n if (parsed.code !== undefined) {\n // Physical modifier codes identify a side, but still are not action keys.\n return (\n parsed.code.length > 0 &&\n !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code)\n )\n }\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifierKey(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n\n/**\n * Parses a string or raw binding, resolving Mod for the supplied platform.\n * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.\n */\nexport function parseRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform = detectPlatform(),\n): ParsedHotkey {\n return typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)\n : rawHotkeyToParsedHotkey(hotkey, platform)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,YACd,QACA,WAAwCA,iCAAgB,EAC1C;CACd,MAAM,QAAQC,yCAAiB,OAAO;CACtC,MAAM,4BAAoC,IAAI,KAAK;CACnD,IAAI,MAAM;AAEV,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACrC,MAAM,OAAO,MAAM,GAAI,MAAM;AAE7B,MAAI,MAAM,MAAM,SAAS,EAEvB,OAAMC,mCAAiB,KAAK;OACvB;GAEL,MAAM,QACJC,mCAAiB,SAASA,mCAAiB,KAAK,aAAa;AAE/D,OAAI,OAAO;IACT,MAAM,WAAWC,iCAAgB,OAAO,SAAS;AACjD,cAAU,IAAI,SAAS;cAInB,MAAM,WAAW,EACnB,OAAMF,mCAAiB,KAAK;;;AAOpC,KAAI,CAAC,OAAO,MAAM,SAAS,EACzB,OAAMA,mCAAiB,MAAM,MAAM,SAAS,GAAI,MAAM,CAAC;CAGzD,MAAM,OAAO,+BAA+B,KAAK,IAAI,GAAG;AACxD,QAAO;EACL,GAAI,SAAS,SAAY,EAAE,MAAM,GAAG,EAAE,KAAK;EAC3C,MAAM,UAAU,IAAI,UAAU;EAC9B,OAAO,UAAU,IAAI,QAAQ;EAC7B,KAAK,UAAU,IAAI,MAAM;EACzB,MAAM,UAAU,IAAI,OAAO;EAC3B,WAAWG,iCAAe,QAAQ,MAAM,UAAU,IAAI,EAAE,CAAC;EAC1D;;;;;;;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,SAAgB,wBACd,QACA,WAAWA,iCAAgB,EACb;AACd,QAAO,OAAO,WAAW,WACrB,YAAY,QAAQ,SAAS,GAC7B,wBAAwB,QAAQ,SAAS"}
|
package/dist/parse.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Key } from "./key.types.cjs";
|
|
2
2
|
import { MODIFIER_ALIASES } from "./constants.cjs";
|
|
3
|
+
import { Hotkey, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.types.cjs";
|
|
3
4
|
|
|
4
5
|
//#region src/parse.d.ts
|
|
5
6
|
/**
|
|
@@ -7,7 +8,7 @@ import { MODIFIER_ALIASES } from "./constants.cjs";
|
|
|
7
8
|
*
|
|
8
9
|
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
9
10
|
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
10
|
-
* @returns A ParsedHotkey
|
|
11
|
+
* @returns A ParsedHotkey with either logical key or physical code and modifier flags
|
|
11
12
|
*
|
|
12
13
|
* @example
|
|
13
14
|
* ```ts
|
|
@@ -97,7 +98,7 @@ declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MO
|
|
|
97
98
|
* })
|
|
98
99
|
* ```
|
|
99
100
|
*/
|
|
100
|
-
declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
|
|
101
|
+
declare function parseKeyboardEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
101
102
|
/**
|
|
102
103
|
* Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
|
|
103
104
|
*
|
|
@@ -123,6 +124,11 @@ declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac'
|
|
|
123
124
|
* ```
|
|
124
125
|
*/
|
|
125
126
|
declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Parses a string or raw binding, resolving Mod for the supplied platform.
|
|
129
|
+
* Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
|
|
130
|
+
*/
|
|
131
|
+
declare function parseRegisterableHotkey(hotkey: RegisterableHotkey, platform?: "mac" | "windows" | "linux"): ParsedHotkey;
|
|
126
132
|
//#endregion
|
|
127
|
-
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
133
|
+
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey };
|
|
128
134
|
//# sourceMappingURL=parse.d.cts.map
|
package/dist/parse.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Key } from "./key.types.js";
|
|
2
2
|
import { MODIFIER_ALIASES } from "./constants.js";
|
|
3
|
+
import { Hotkey, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.types.js";
|
|
3
4
|
|
|
4
5
|
//#region src/parse.d.ts
|
|
5
6
|
/**
|
|
@@ -7,7 +8,7 @@ import { MODIFIER_ALIASES } from "./constants.js";
|
|
|
7
8
|
*
|
|
8
9
|
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
9
10
|
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
10
|
-
* @returns A ParsedHotkey
|
|
11
|
+
* @returns A ParsedHotkey with either logical key or physical code and modifier flags
|
|
11
12
|
*
|
|
12
13
|
* @example
|
|
13
14
|
* ```ts
|
|
@@ -97,7 +98,7 @@ declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MO
|
|
|
97
98
|
* })
|
|
98
99
|
* ```
|
|
99
100
|
*/
|
|
100
|
-
declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
|
|
101
|
+
declare function parseKeyboardEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
|
|
101
102
|
/**
|
|
102
103
|
* Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
|
|
103
104
|
*
|
|
@@ -123,6 +124,11 @@ declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac'
|
|
|
123
124
|
* ```
|
|
124
125
|
*/
|
|
125
126
|
declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Parses a string or raw binding, resolving Mod for the supplied platform.
|
|
129
|
+
* Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
|
|
130
|
+
*/
|
|
131
|
+
declare function parseRegisterableHotkey(hotkey: RegisterableHotkey, platform?: "mac" | "windows" | "linux"): ParsedHotkey;
|
|
126
132
|
//#endregion
|
|
127
|
-
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
133
|
+
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey };
|
|
128
134
|
//# sourceMappingURL=parse.d.ts.map
|
package/dist/parse.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { MODIFIER_ALIASES, MODIFIER_ORDER,
|
|
1
|
+
import { MODIFIER_ALIASES, MODIFIER_ORDER, normalizeKeyName } from "./constants.js";
|
|
2
|
+
import { detectPlatform, resolveModifier } from "./platform.js";
|
|
3
|
+
import { assertLogicalKey, normalizeKeyboardEvent, splitHotkeyParts } from "./_keyboard-event.js";
|
|
2
4
|
|
|
3
5
|
//#region src/parse.ts
|
|
4
6
|
/**
|
|
@@ -6,7 +8,7 @@ import { MODIFIER_ALIASES, MODIFIER_ORDER, detectPlatform, normalizeKeyName, res
|
|
|
6
8
|
*
|
|
7
9
|
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
10
|
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
-
* @returns A ParsedHotkey
|
|
11
|
+
* @returns A ParsedHotkey with either logical key or physical code and modifier flags
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
@@ -16,7 +18,7 @@ import { MODIFIER_ALIASES, MODIFIER_ORDER, detectPlatform, normalizeKeyName, res
|
|
|
16
18
|
* ```
|
|
17
19
|
*/
|
|
18
20
|
function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
19
|
-
const parts = hotkey
|
|
21
|
+
const parts = splitHotkeyParts(hotkey);
|
|
20
22
|
const modifiers = /* @__PURE__ */ new Set();
|
|
21
23
|
let key = "";
|
|
22
24
|
for (let i = 0; i < parts.length; i++) {
|
|
@@ -31,8 +33,9 @@ function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
if (!key && parts.length > 0) key = normalizeKeyName(parts[parts.length - 1].trim());
|
|
36
|
+
const code = /^\[([A-Za-z][A-Za-z0-9]*)\]$/.exec(key)?.[1];
|
|
34
37
|
return {
|
|
35
|
-
key,
|
|
38
|
+
...code !== void 0 ? { code } : { key },
|
|
36
39
|
ctrl: modifiers.has("Control"),
|
|
37
40
|
shift: modifiers.has("Shift"),
|
|
38
41
|
alt: modifiers.has("Alt"),
|
|
@@ -62,6 +65,7 @@ function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
|
62
65
|
* ```
|
|
63
66
|
*/
|
|
64
67
|
function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
|
|
68
|
+
if (raw.code === void 0) assertLogicalKey(raw.key);
|
|
65
69
|
let ctrl = raw.ctrl ?? false;
|
|
66
70
|
const shift = raw.shift ?? false;
|
|
67
71
|
const alt = raw.alt ?? false;
|
|
@@ -78,7 +82,7 @@ function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
|
|
|
78
82
|
}
|
|
79
83
|
});
|
|
80
84
|
return {
|
|
81
|
-
key: raw.key,
|
|
85
|
+
...raw.code !== void 0 ? { code: raw.code } : { key: normalizeKeyName(raw.key) },
|
|
82
86
|
ctrl,
|
|
83
87
|
shift,
|
|
84
88
|
alt,
|
|
@@ -92,6 +96,7 @@ function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
|
|
|
92
96
|
* otherwise full `Control+Alt+Shift+Meta` order.
|
|
93
97
|
*/
|
|
94
98
|
function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
99
|
+
if (parsed.code === void 0) assertLogicalKey(parsed.key);
|
|
95
100
|
const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
|
|
96
101
|
const parts = [];
|
|
97
102
|
if (canUseMod) {
|
|
@@ -99,7 +104,7 @@ function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
|
99
104
|
if (parsed.alt) parts.push("Alt");
|
|
100
105
|
if (parsed.shift) parts.push("Shift");
|
|
101
106
|
} else for (const modifier of MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
102
|
-
parts.push(normalizeKeyName(parsed.key));
|
|
107
|
+
parts.push(parsed.code !== void 0 ? `[${parsed.code}]` : normalizeKeyName(parsed.key));
|
|
103
108
|
return parts.join("+");
|
|
104
109
|
}
|
|
105
110
|
/**
|
|
@@ -168,19 +173,22 @@ function isModifierKey(key) {
|
|
|
168
173
|
* })
|
|
169
174
|
* ```
|
|
170
175
|
*/
|
|
171
|
-
function parseKeyboardEvent(event) {
|
|
172
|
-
const
|
|
176
|
+
function parseKeyboardEvent(event, platform) {
|
|
177
|
+
const normalized = normalizeKeyboardEvent(event, platform);
|
|
178
|
+
const ctrl = normalized.altGraph ? false : normalized.ctrl;
|
|
179
|
+
const alt = normalized.altGraph ? false : normalized.alt;
|
|
180
|
+
const shift = normalized.shift;
|
|
173
181
|
const modifiers = [];
|
|
174
|
-
if (
|
|
175
|
-
if (
|
|
176
|
-
if (
|
|
177
|
-
if (
|
|
182
|
+
if (ctrl) modifiers.push("Control");
|
|
183
|
+
if (alt) modifiers.push("Alt");
|
|
184
|
+
if (shift) modifiers.push("Shift");
|
|
185
|
+
if (normalized.meta) modifiers.push("Meta");
|
|
178
186
|
return {
|
|
179
|
-
key:
|
|
180
|
-
ctrl
|
|
181
|
-
shift
|
|
182
|
-
alt
|
|
183
|
-
meta:
|
|
187
|
+
key: normalized.key,
|
|
188
|
+
ctrl,
|
|
189
|
+
shift,
|
|
190
|
+
alt,
|
|
191
|
+
meta: normalized.meta,
|
|
184
192
|
modifiers
|
|
185
193
|
};
|
|
186
194
|
}
|
|
@@ -191,7 +199,7 @@ function parseKeyboardEvent(event) {
|
|
|
191
199
|
* @param platform - Target platform for `Mod` eligibility
|
|
192
200
|
*/
|
|
193
201
|
function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
|
|
194
|
-
return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform);
|
|
202
|
+
return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event, platform), platform);
|
|
195
203
|
}
|
|
196
204
|
/**
|
|
197
205
|
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
@@ -212,9 +220,17 @@ function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
|
|
|
212
220
|
*/
|
|
213
221
|
function hasNonModifierKey(hotkey, platform = detectPlatform()) {
|
|
214
222
|
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
|
|
223
|
+
if (parsed.code !== void 0) return parsed.code.length > 0 && !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code);
|
|
215
224
|
return !isModifierKey(parsed.key) && parsed.key.length > 0;
|
|
216
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Parses a string or raw binding, resolving Mod for the supplied platform.
|
|
228
|
+
* Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.
|
|
229
|
+
*/
|
|
230
|
+
function parseRegisterableHotkey(hotkey, platform = detectPlatform()) {
|
|
231
|
+
return typeof hotkey === "string" ? parseHotkey(hotkey, platform) : rawHotkeyToParsedHotkey(hotkey, platform);
|
|
232
|
+
}
|
|
217
233
|
|
|
218
234
|
//#endregion
|
|
219
|
-
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
235
|
+
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, parseRegisterableHotkey, rawHotkeyToParsedHotkey };
|
|
220
236
|
//# sourceMappingURL=parse.js.map
|
package/dist/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","names":[],"sources":["../src/parse.ts"],"sourcesContent":["import {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n detectPlatform,\n normalizeKeyName,\n resolveModifier,\n} from './constants'\nimport type {\n CanonicalModifier,\n Hotkey,\n Key,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey object with the key and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = hotkey.split('+')\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n return {\n key,\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n key: raw.key,\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(normalizeKeyName(parsed.key))\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey {\n const normalizedKey = normalizeKeyName(event.key)\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (event.ctrlKey) modifiers.push('Control')\n if (event.altKey) modifiers.push('Alt')\n if (event.shiftKey) modifiers.push('Shift')\n if (event.metaKey) modifiers.push('Meta')\n\n return {\n key: normalizedKey,\n ctrl: event.ctrlKey,\n shift: event.shiftKey,\n alt: event.altKey,\n meta: event.metaKey,\n modifiers,\n }\n}\n\n/**\n * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform)\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifierKey(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8BA,SAAgB,YACd,QACA,WAAwC,gBAAgB,EAC1C;CACd,MAAM,QAAQ,OAAO,MAAM,IAAI;CAC/B,MAAM,4BAAoC,IAAI,KAAK;CACnD,IAAI,MAAM;AAEV,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACrC,MAAM,OAAO,MAAM,GAAI,MAAM;AAE7B,MAAI,MAAM,MAAM,SAAS,EAEvB,OAAM,iBAAiB,KAAK;OACvB;GAEL,MAAM,QACJ,iBAAiB,SAAS,iBAAiB,KAAK,aAAa;AAE/D,OAAI,OAAO;IACT,MAAM,WAAW,gBAAgB,OAAO,SAAS;AACjD,cAAU,IAAI,SAAS;cAInB,MAAM,WAAW,EACnB,OAAM,iBAAiB,KAAK;;;AAOpC,KAAI,CAAC,OAAO,MAAM,SAAS,EACzB,OAAM,iBAAiB,MAAM,MAAM,SAAS,GAAI,MAAM,CAAC;AAGzD,QAAO;EACL;EACA,MAAM,UAAU,IAAI,UAAU;EAC9B,OAAO,UAAU,IAAI,QAAQ;EAC7B,KAAK,UAAU,IAAI,MAAM;EACzB,MAAM,UAAU,IAAI,OAAO;EAC3B,WAAW,eAAe,QAAQ,MAAM,UAAU,IAAI,EAAE,CAAC;EAC1D;;;;;;;;;;;;;;;;;;;;;;;AAwBH,SAAgB,wBACd,KACA,WAAwC,gBAAgB,EAC1C;CACd,IAAI,OAAO,IAAI,QAAQ;CACvB,MAAM,QAAQ,IAAI,SAAS;CAC3B,MAAM,MA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;;;;;;;;AAqBzD,SAAgB,mBAAmB,OAAoC;CACrE,MAAM,gBAAgB,iBAAiB,MAAM,IAAI;CAGjD,MAAM,YAAsC,EAAE;AAC9C,KAAI,MAAM,QAAS,WAAU,KAAK,UAAU;AAC5C,KAAI,MAAM,OAAQ,WAAU,KAAK,MAAM;AACvC,KAAI,MAAM,SAAU,WAAU,KAAK,QAAQ;AAC3C,KAAI,MAAM,QAAS,WAAU,KAAK,OAAO;AAEzC,QAAO;EACL,KAAK;EACL,MAAM,MAAM;EACZ,OAAO,MAAM;EACb,KAAK,MAAM;EACX,MAAM,MAAM;EACZ;EACD;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,iCAAiC,mBAAmB,MAAM,EAAE,SAAS;;;;;;;;;;;;;;;;;;;AAoB9E,SAAgB,kBACd,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS"}
|
|
1
|
+
{"version":3,"file":"parse.js","names":[],"sources":["../src/parse.ts"],"sourcesContent":["import {\n assertLogicalKey,\n normalizeKeyboardEvent,\n splitHotkeyParts,\n} from './_keyboard-event'\nimport { MODIFIER_ALIASES, MODIFIER_ORDER, normalizeKeyName } from './constants'\nimport { detectPlatform, resolveModifier } from './platform'\nimport type { CanonicalModifier, Key } from './key.types'\nimport type {\n Hotkey,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey.types'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey with either logical key or physical code and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = splitHotkeyParts(hotkey)\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n const code = /^\\[([A-Za-z][A-Za-z0-9]*)\\]$/.exec(key)?.[1]\n return {\n ...(code !== undefined ? { code } : { key }),\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n if (raw.code === undefined) assertLogicalKey(raw.key)\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n ...(raw.code !== undefined\n ? { code: raw.code }\n : { key: normalizeKeyName(raw.key) }),\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n if (parsed.code === undefined) assertLogicalKey(parsed.key)\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(\n parsed.code !== undefined\n ? `[${parsed.code}]`\n : normalizeKeyName(parsed.key),\n )\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(\n event: KeyboardEvent,\n platform?: 'mac' | 'windows' | 'linux',\n): ParsedHotkey {\n const normalized = normalizeKeyboardEvent(event, platform)\n // AltGraph is a character-production modifier, not an ordinary Control+Alt\n // shortcut. Record the produced glyph instead of a synthetic Ctrl+Alt chord.\n const ctrl = normalized.altGraph ? false : normalized.ctrl\n const alt = normalized.altGraph ? false : normalized.alt\n // Shift is a real modifier even during AltGraph input; preserve it for replay.\n const shift = normalized.shift\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (ctrl) modifiers.push('Control')\n if (alt) modifiers.push('Alt')\n if (shift) modifiers.push('Shift')\n if (normalized.meta) modifiers.push('Meta')\n\n return {\n key: normalized.key,\n ctrl,\n shift,\n alt,\n meta: normalized.meta,\n modifiers,\n }\n}\n\n/**\n * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseKeyboardEvent(event, platform),\n platform,\n )\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n if (parsed.code !== undefined) {\n // Physical modifier codes identify a side, but still are not action keys.\n return (\n parsed.code.length > 0 &&\n !/^(Control|Shift|Alt|Meta)(Left|Right)$/.test(parsed.code)\n )\n }\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifierKey(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n\n/**\n * Parses a string or raw binding, resolving Mod for the supplied platform.\n * Physical tokens such as `[KeyQ]` retain `code`; they never become logical Q.\n */\nexport function parseRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform = detectPlatform(),\n): ParsedHotkey {\n return typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)\n : rawHotkeyToParsedHotkey(hotkey, platform)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,YACd,QACA,WAAwC,gBAAgB,EAC1C;CACd,MAAM,QAAQ,iBAAiB,OAAO;CACtC,MAAM,4BAAoC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