@tanstack/hotkeys 0.2.0 → 0.3.1

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.
Files changed (47) hide show
  1. package/README.md +2 -1
  2. package/dist/format.cjs +16 -0
  3. package/dist/format.cjs.map +1 -1
  4. package/dist/format.d.cts +14 -1
  5. package/dist/format.d.ts +14 -1
  6. package/dist/format.js +16 -1
  7. package/dist/format.js.map +1 -1
  8. package/dist/hotkey-manager.cjs +7 -75
  9. package/dist/hotkey-manager.cjs.map +1 -1
  10. package/dist/hotkey-manager.d.cts +2 -10
  11. package/dist/hotkey-manager.d.ts +2 -10
  12. package/dist/hotkey-manager.js +5 -73
  13. package/dist/hotkey-manager.js.map +1 -1
  14. package/dist/index.cjs +5 -4
  15. package/dist/index.d.cts +5 -4
  16. package/dist/index.d.ts +5 -4
  17. package/dist/index.js +3 -3
  18. package/dist/manager.utils.cjs +90 -0
  19. package/dist/manager.utils.cjs.map +1 -0
  20. package/dist/manager.utils.d.cts +13 -0
  21. package/dist/manager.utils.d.ts +13 -0
  22. package/dist/manager.utils.js +85 -0
  23. package/dist/manager.utils.js.map +1 -0
  24. package/dist/match.cjs +10 -3
  25. package/dist/match.cjs.map +1 -1
  26. package/dist/match.d.cts +5 -0
  27. package/dist/match.d.ts +5 -0
  28. package/dist/match.js +10 -3
  29. package/dist/match.js.map +1 -1
  30. package/dist/sequence-manager.cjs +391 -0
  31. package/dist/sequence-manager.cjs.map +1 -0
  32. package/dist/{sequence.d.cts → sequence-manager.d.cts} +50 -19
  33. package/dist/{sequence.d.ts → sequence-manager.d.ts} +50 -19
  34. package/dist/sequence-manager.js +389 -0
  35. package/dist/sequence-manager.js.map +1 -0
  36. package/package.json +1 -1
  37. package/src/format.ts +16 -0
  38. package/src/hotkey-manager.ts +18 -156
  39. package/src/index.ts +1 -1
  40. package/src/manager.utils.ts +171 -0
  41. package/src/match.ts +21 -15
  42. package/src/sequence-manager.ts +693 -0
  43. package/dist/sequence.cjs +0 -242
  44. package/dist/sequence.cjs.map +0 -1
  45. package/dist/sequence.js +0 -240
  46. package/dist/sequence.js.map +0 -1
  47. package/src/sequence.ts +0 -391
package/dist/match.cjs CHANGED
@@ -9,6 +9,11 @@ const require_parse = require('./parse.cjs');
9
9
  * for letter keys (A-Z) and digit keys (0-9) when `key` produces special characters
10
10
  * (e.g., macOS Option+letter or Shift+number). Letter keys are matched case-insensitively.
11
11
  *
12
+ * Also handles "dead key" events where `event.key` is `'Dead'` instead of the expected
13
+ * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,
14
+ * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.
15
+ * In these cases, `event.code` is used to determine the physical key.
16
+ *
12
17
  * @param event - The KeyboardEvent to check
13
18
  * @param hotkey - The hotkey string or ParsedHotkey to match against
14
19
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
@@ -32,13 +37,15 @@ function matchesKeyboardEvent(event, hotkey, platform = require_constants.detect
32
37
  if (event.metaKey !== parsed.meta) return false;
33
38
  const eventKey = require_constants.normalizeKeyName(event.key);
34
39
  const hotkeyKey = parsed.key;
35
- if (eventKey.length === 1 && hotkeyKey.length === 1) {
40
+ if (eventKey !== "Dead" && eventKey.length === 1 && hotkeyKey.length === 1) {
36
41
  if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) return true;
37
- if (event.code && event.code.startsWith("Key")) {
42
+ }
43
+ if (eventKey === "Dead" || eventKey.length === 1 && hotkeyKey.length === 1) {
44
+ if (event.code?.startsWith("Key")) {
38
45
  const codeLetter = event.code.slice(3);
39
46
  if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) return codeLetter.toUpperCase() === hotkeyKey.toUpperCase();
40
47
  }
41
- if (event.code && event.code.startsWith("Digit")) {
48
+ if (event.code?.startsWith("Digit")) {
42
49
  const codeDigit = event.code.slice(5);
43
50
  if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) return codeDigit === hotkeyKey;
44
51
  }
@@ -1 +1 @@
1
- {"version":3,"file":"match.cjs","names":["detectPlatform","parseHotkey","normalizeKeyName"],"sources":["../src/match.ts"],"sourcesContent":["import { detectPlatform, normalizeKeyName } 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 (A-Z) and digit keys (0-9) when `key` produces special characters\n * (e.g., macOS Option+letter or Shift+number). Letter keys are matched case-insensitively.\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 letters, compare case-insensitively\n if (eventKey.length === 1 && hotkeyKey.length === 1) {\n // First try matching with event.key\n if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) {\n return true\n }\n\n // Fallback to event.code for letter keys when event.key doesn't match\n // This handles cases like Command+Option+T on macOS where event.key is '†' instead of 'T'\n // event.code format for letter keys is \"KeyA\", \"KeyB\", etc. (always uppercase in browsers)\n if (event.code && event.code.startsWith('Key')) {\n const codeLetter = event.code.slice(3) // Remove \"Key\" prefix\n if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) {\n return codeLetter.toUpperCase() === hotkeyKey.toUpperCase()\n }\n }\n\n // Fallback to event.code for digit keys when event.key doesn't match\n // This handles cases like Shift+4 where event.key is '$' instead of '4'\n // event.code format for digit keys is \"Digit0\", \"Digit1\", etc.\n if (event.code && event.code.startsWith('Digit')) {\n const codeDigit = event.code.slice(5) // Remove \"Digit\" prefix\n if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) {\n return codeDigit === hotkeyKey\n }\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 (parsed.meta) parts.push('Meta')\n parts.push(parsed.key)\n\n return parts.join('+') as Hotkey\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,SAAgB,qBACd,OACA,QACA,WAAwCA,kCAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAWC,0BAAY,QAAQ,SAAS,GAAG;AAG/D,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;AAET,KAAI,MAAM,aAAa,OAAO,MAC5B,QAAO;AAET,KAAI,MAAM,WAAW,OAAO,IAC1B,QAAO;AAET,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;CAIT,MAAM,WAAWC,mCAAiB,MAAM,IAAI;CAC5C,MAAM,YAAY,OAAO;AAGzB,KAAI,SAAS,WAAW,KAAK,UAAU,WAAW,GAAG;AAEnD,MAAI,SAAS,aAAa,KAAK,UAAU,aAAa,CACpD,QAAO;AAMT,MAAI,MAAM,QAAQ,MAAM,KAAK,WAAW,MAAM,EAAE;GAC9C,MAAM,aAAa,MAAM,KAAK,MAAM,EAAE;AACtC,OAAI,WAAW,WAAW,KAAK,aAAa,KAAK,WAAW,CAC1D,QAAO,WAAW,aAAa,KAAK,UAAU,aAAa;;AAO/D,MAAI,MAAM,QAAQ,MAAM,KAAK,WAAW,QAAQ,EAAE;GAChD,MAAM,YAAY,MAAM,KAAK,MAAM,EAAE;AACrC,OAAI,UAAU,WAAW,KAAK,UAAU,KAAK,UAAU,CACrD,QAAO,cAAc;;AAIzB,SAAO;;AAIT,QAAO,aAAa;;;;;;;;;;;;;;;;;;;;AAiCtB,SAAgB,oBACd,QACA,UACA,UAAsC,EAAE,EACR;CAChC,MAAM,EAAE,iBAAiB,MAAM,kBAAkB,MAAM,aAAa;CACpE,MAAM,mBAAmB,YA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AAK,IAAI"}
1
+ {"version":3,"file":"match.cjs","names":["detectPlatform","parseHotkey","normalizeKeyName"],"sources":["../src/match.ts"],"sourcesContent":["import { detectPlatform, normalizeKeyName } 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 (A-Z) and digit keys (0-9) when `key` produces special characters\n * (e.g., macOS Option+letter or Shift+number). 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\n // Fallback to event.code for dead keys or single-char mismatches.\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 eventKey === 'Dead' ||\n (eventKey.length === 1 && hotkeyKey.length === 1)\n ) {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- event.code can be undefined in older browsers/mobile\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 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- event.code can be undefined in older browsers/mobile\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 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 (parsed.meta) parts.push('Meta')\n parts.push(parsed.key)\n\n return parts.join('+') as Hotkey\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,qBACd,OACA,QACA,WAAwCA,kCAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAWC,0BAAY,QAAQ,SAAS,GAAG;AAG/D,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;AAET,KAAI,MAAM,aAAa,OAAO,MAC5B,QAAO;AAET,KAAI,MAAM,WAAW,OAAO,IAC1B,QAAO;AAET,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;CAIT,MAAM,WAAWC,mCAAiB,MAAM,IAAI;CAC5C,MAAM,YAAY,OAAO;AAGzB,KAAI,aAAa,UAAU,SAAS,WAAW,KAAK,UAAU,WAAW,GACvE;MAAI,SAAS,aAAa,KAAK,UAAU,aAAa,CACpD,QAAO;;AAOX,KACE,aAAa,UACZ,SAAS,WAAW,KAAK,UAAU,WAAW,GAC/C;AAEA,MAAI,MAAM,MAAM,WAAW,MAAM,EAAE;GACjC,MAAM,aAAa,MAAM,KAAK,MAAM,EAAE;AACtC,OAAI,WAAW,WAAW,KAAK,aAAa,KAAK,WAAW,CAC1D,QAAO,WAAW,aAAa,KAAK,UAAU,aAAa;;AAI/D,MAAI,MAAM,MAAM,WAAW,QAAQ,EAAE;GACnC,MAAM,YAAY,MAAM,KAAK,MAAM,EAAE;AACrC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D,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,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI"}
package/dist/match.d.cts CHANGED
@@ -8,6 +8,11 @@ import { Hotkey, HotkeyCallback, ParsedHotkey } from "./hotkey.cjs";
8
8
  * for letter keys (A-Z) and digit keys (0-9) when `key` produces special characters
9
9
  * (e.g., macOS Option+letter or Shift+number). Letter keys are matched case-insensitively.
10
10
  *
11
+ * Also handles "dead key" events where `event.key` is `'Dead'` instead of the expected
12
+ * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,
13
+ * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.
14
+ * In these cases, `event.code` is used to determine the physical key.
15
+ *
11
16
  * @param event - The KeyboardEvent to check
12
17
  * @param hotkey - The hotkey string or ParsedHotkey to match against
13
18
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
package/dist/match.d.ts CHANGED
@@ -8,6 +8,11 @@ import { Hotkey, HotkeyCallback, ParsedHotkey } from "./hotkey.js";
8
8
  * for letter keys (A-Z) and digit keys (0-9) when `key` produces special characters
9
9
  * (e.g., macOS Option+letter or Shift+number). Letter keys are matched case-insensitively.
10
10
  *
11
+ * Also handles "dead key" events where `event.key` is `'Dead'` instead of the expected
12
+ * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,
13
+ * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.
14
+ * In these cases, `event.code` is used to determine the physical key.
15
+ *
11
16
  * @param event - The KeyboardEvent to check
12
17
  * @param hotkey - The hotkey string or ParsedHotkey to match against
13
18
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
package/dist/match.js CHANGED
@@ -9,6 +9,11 @@ import { parseHotkey } from "./parse.js";
9
9
  * for letter keys (A-Z) and digit keys (0-9) when `key` produces special characters
10
10
  * (e.g., macOS Option+letter or Shift+number). Letter keys are matched case-insensitively.
11
11
  *
12
+ * Also handles "dead key" events where `event.key` is `'Dead'` instead of the expected
13
+ * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,
14
+ * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.
15
+ * In these cases, `event.code` is used to determine the physical key.
16
+ *
12
17
  * @param event - The KeyboardEvent to check
13
18
  * @param hotkey - The hotkey string or ParsedHotkey to match against
14
19
  * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
@@ -32,13 +37,15 @@ function matchesKeyboardEvent(event, hotkey, platform = detectPlatform()) {
32
37
  if (event.metaKey !== parsed.meta) return false;
33
38
  const eventKey = normalizeKeyName(event.key);
34
39
  const hotkeyKey = parsed.key;
35
- if (eventKey.length === 1 && hotkeyKey.length === 1) {
40
+ if (eventKey !== "Dead" && eventKey.length === 1 && hotkeyKey.length === 1) {
36
41
  if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) return true;
37
- if (event.code && event.code.startsWith("Key")) {
42
+ }
43
+ if (eventKey === "Dead" || eventKey.length === 1 && hotkeyKey.length === 1) {
44
+ if (event.code?.startsWith("Key")) {
38
45
  const codeLetter = event.code.slice(3);
39
46
  if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) return codeLetter.toUpperCase() === hotkeyKey.toUpperCase();
40
47
  }
41
- if (event.code && event.code.startsWith("Digit")) {
48
+ if (event.code?.startsWith("Digit")) {
42
49
  const codeDigit = event.code.slice(5);
43
50
  if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) return codeDigit === hotkeyKey;
44
51
  }
package/dist/match.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"match.js","names":[],"sources":["../src/match.ts"],"sourcesContent":["import { detectPlatform, normalizeKeyName } 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 (A-Z) and digit keys (0-9) when `key` produces special characters\n * (e.g., macOS Option+letter or Shift+number). Letter keys are matched case-insensitively.\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 letters, compare case-insensitively\n if (eventKey.length === 1 && hotkeyKey.length === 1) {\n // First try matching with event.key\n if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) {\n return true\n }\n\n // Fallback to event.code for letter keys when event.key doesn't match\n // This handles cases like Command+Option+T on macOS where event.key is '†' instead of 'T'\n // event.code format for letter keys is \"KeyA\", \"KeyB\", etc. (always uppercase in browsers)\n if (event.code && event.code.startsWith('Key')) {\n const codeLetter = event.code.slice(3) // Remove \"Key\" prefix\n if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) {\n return codeLetter.toUpperCase() === hotkeyKey.toUpperCase()\n }\n }\n\n // Fallback to event.code for digit keys when event.key doesn't match\n // This handles cases like Shift+4 where event.key is '$' instead of '4'\n // event.code format for digit keys is \"Digit0\", \"Digit1\", etc.\n if (event.code && event.code.startsWith('Digit')) {\n const codeDigit = event.code.slice(5) // Remove \"Digit\" prefix\n if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) {\n return codeDigit === hotkeyKey\n }\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 (parsed.meta) parts.push('Meta')\n parts.push(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EACR,cAAc;EACf;AAED,SAAQ,UAAyB;AAC/B,MAAI,qBAAqB,OAAO,QAAQ,iBAAiB,EAAE;AACzD,OAAI,eACF,OAAM,gBAAgB;AAExB,OAAI,gBACF,OAAM,iBAAiB;AAEzB,YAAS,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;;;;AAyB9B,SAAgB,yBACd,UACA,UAAsC,EAAE,EACR;CAChC,MAAM,EAAE,iBAAiB,MAAM,kBAAkB,MAAM,aAAa;CACpE,MAAM,mBAAmB,YAAY,gBAAgB;CAGrD,MAAM,iBAAiB,OAAO,QAAQ,SAAS,CAC5C,QAAQ,UAA6C,QAAQ,MAAM,GAAG,CAAC,CACvE,KAAK,CAAC,QAAQ,aAAa;EAC1B,MAAM,SAAS,YAAY,QAAkB,iBAAiB;AAK9D,SAAO;GAAE;GAAQ;GAAS,SAJa;IAC7B;IACR,cAAc;IACf;GACkC;GACnC;AAEJ,SAAQ,UAAyB;AAC/B,OAAK,MAAM,EAAE,QAAQ,SAAS,aAAa,eACzC,KAAI,qBAAqB,OAAO,QAAQ,iBAAiB,EAAE;AACzD,OAAI,eACF,OAAM,gBAAgB;AAExB,OAAI,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,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI"}
1
+ {"version":3,"file":"match.js","names":[],"sources":["../src/match.ts"],"sourcesContent":["import { detectPlatform, normalizeKeyName } 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 (A-Z) and digit keys (0-9) when `key` produces special characters\n * (e.g., macOS Option+letter or Shift+number). 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\n // Fallback to event.code for dead keys or single-char mismatches.\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 eventKey === 'Dead' ||\n (eventKey.length === 1 && hotkeyKey.length === 1)\n ) {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- event.code can be undefined in older browsers/mobile\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 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- event.code can be undefined in older browsers/mobile\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 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 (parsed.meta) parts.push('Meta')\n parts.push(parsed.key)\n\n return parts.join('+') as Hotkey\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,qBACd,OACA,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAG/D,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;AAET,KAAI,MAAM,aAAa,OAAO,MAC5B,QAAO;AAET,KAAI,MAAM,WAAW,OAAO,IAC1B,QAAO;AAET,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;CAIT,MAAM,WAAW,iBAAiB,MAAM,IAAI;CAC5C,MAAM,YAAY,OAAO;AAGzB,KAAI,aAAa,UAAU,SAAS,WAAW,KAAK,UAAU,WAAW,GACvE;MAAI,SAAS,aAAa,KAAK,UAAU,aAAa,CACpD,QAAO;;AAOX,KACE,aAAa,UACZ,SAAS,WAAW,KAAK,UAAU,WAAW,GAC/C;AAEA,MAAI,MAAM,MAAM,WAAW,MAAM,EAAE;GACjC,MAAM,aAAa,MAAM,KAAK,MAAM,EAAE;AACtC,OAAI,WAAW,WAAW,KAAK,aAAa,KAAK,WAAW,CAC1D,QAAO,WAAW,aAAa,KAAK,UAAU,aAAa;;AAI/D,MAAI,MAAM,MAAM,WAAW,QAAQ,EAAE;GACnC,MAAM,YAAY,MAAM,KAAK,MAAM,EAAE;AACrC,OAAI,UAAU,WAAW,KAAK,UAAU,KAAK,UAAU,CACrD,QAAO,cAAc;;AAGzB,SAAO;;AAIT,QAAO,aAAa;;;;;;;;;;;;;;;;;;;;AAiCtB,SAAgB,oBACd,QACA,UACA,UAAsC,EAAE,EACR;CAChC,MAAM,EAAE,iBAAiB,MAAM,kBAAkB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AO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI"}
@@ -0,0 +1,391 @@
1
+ const require_constants = require('./constants.cjs');
2
+ const require_parse = require('./parse.cjs');
3
+ const require_format = require('./format.cjs');
4
+ const require_match = require('./match.cjs');
5
+ const require_manager_utils = require('./manager.utils.cjs');
6
+ let _tanstack_store = require("@tanstack/store");
7
+
8
+ //#region src/sequence-manager.ts
9
+ /**
10
+ * Default timeout between keys in a sequence (in milliseconds).
11
+ */
12
+ const DEFAULT_SEQUENCE_TIMEOUT = 1e3;
13
+ let sequenceIdCounter = 0;
14
+ /**
15
+ * Generates a unique ID for sequence registrations.
16
+ */
17
+ function generateSequenceId() {
18
+ return `sequence_${++sequenceIdCounter}`;
19
+ }
20
+ /**
21
+ * Returns a canonical string for sequence conflict comparison.
22
+ */
23
+ function sequenceKey(sequence) {
24
+ return sequence.join("|");
25
+ }
26
+ /**
27
+ * Manages keyboard sequence matching for Vim-style shortcuts.
28
+ *
29
+ * This class allows registering multi-key sequences like 'g g' or 'd d'
30
+ * that trigger callbacks when the full sequence is pressed within
31
+ * a configurable timeout.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const matcher = SequenceManager.getInstance()
36
+ *
37
+ * // Register 'g g' to go to top
38
+ * const unregister = matcher.register(['G', 'G'], (event, context) => {
39
+ * scrollToTop()
40
+ * }, { timeout: 500 })
41
+ *
42
+ * // Later, to unregister:
43
+ * unregister()
44
+ * ```
45
+ */
46
+ /**
47
+ * Builds a devtools view from an internal registration.
48
+ */
49
+ function toRegistrationView(reg) {
50
+ return {
51
+ id: reg.id,
52
+ sequence: reg.sequence,
53
+ options: reg.options,
54
+ target: reg.target,
55
+ triggerCount: reg.triggerCount
56
+ };
57
+ }
58
+ var SequenceManager = class SequenceManager {
59
+ static #instance = null;
60
+ #registrations = /* @__PURE__ */ new Map();
61
+ #targetListeners = /* @__PURE__ */ new Map();
62
+ #targetRegistrations = /* @__PURE__ */ new Map();
63
+ #platform;
64
+ constructor() {
65
+ this.registrations = new _tanstack_store.Store(/* @__PURE__ */ new Map());
66
+ this.#platform = require_constants.detectPlatform();
67
+ }
68
+ /**
69
+ * Gets the singleton instance of SequenceManager.
70
+ */
71
+ static getInstance() {
72
+ if (!SequenceManager.#instance) SequenceManager.#instance = new SequenceManager();
73
+ return SequenceManager.#instance;
74
+ }
75
+ /**
76
+ * Resets the singleton instance. Useful for testing.
77
+ */
78
+ static resetInstance() {
79
+ if (SequenceManager.#instance) {
80
+ SequenceManager.#instance.destroy();
81
+ SequenceManager.#instance = null;
82
+ }
83
+ }
84
+ /**
85
+ * Registers a hotkey sequence handler.
86
+ *
87
+ * @param sequence - Array of hotkey strings that form the sequence
88
+ * @param callback - Function to call when the sequence is completed
89
+ * @param options - Options for the sequence behavior
90
+ * @returns A handle to update or unregister the sequence
91
+ */
92
+ register(sequence, callback, options = {}) {
93
+ if (sequence.length === 0) throw new Error("Sequence must contain at least one hotkey");
94
+ const id = generateSequenceId();
95
+ const platform = options.platform ?? this.#platform;
96
+ const parsedSequence = sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform));
97
+ const target = options.target ?? (typeof document !== "undefined" ? document : {});
98
+ const conflictBehavior = options.conflictBehavior ?? "warn";
99
+ const conflictingRegistration = this.#findConflictingSequence(sequence, target);
100
+ if (conflictingRegistration) require_manager_utils.handleConflict(conflictingRegistration.id, require_format.formatHotkeySequence(sequence), conflictBehavior, (regId) => this.#unregister(regId));
101
+ const firstStep = parsedSequence[0];
102
+ const resolvedIgnoreInputs = options.ignoreInputs ?? require_manager_utils.getDefaultIgnoreInputs(firstStep);
103
+ const registration = {
104
+ id,
105
+ sequence,
106
+ parsedSequence,
107
+ callback,
108
+ options: {
109
+ ...require_manager_utils.defaultHotkeyOptions,
110
+ timeout: DEFAULT_SEQUENCE_TIMEOUT,
111
+ ...options,
112
+ platform,
113
+ ignoreInputs: resolvedIgnoreInputs
114
+ },
115
+ target,
116
+ currentIndex: 0,
117
+ lastKeyTime: 0,
118
+ triggerCount: 0
119
+ };
120
+ this.#registrations.set(id, registration);
121
+ this.registrations.setState((prev) => new Map(prev).set(id, toRegistrationView(registration)));
122
+ if (!this.#targetRegistrations.has(target)) this.#targetRegistrations.set(target, /* @__PURE__ */ new Set());
123
+ this.#targetRegistrations.get(target).add(id);
124
+ this.#ensureListenersForTarget(target);
125
+ const manager = this;
126
+ return {
127
+ get id() {
128
+ return id;
129
+ },
130
+ get isActive() {
131
+ return manager.#registrations.has(id);
132
+ },
133
+ get callback() {
134
+ return manager.#registrations.get(id)?.callback ?? callback;
135
+ },
136
+ set callback(newCallback) {
137
+ const reg = manager.#registrations.get(id);
138
+ if (reg) reg.callback = newCallback;
139
+ },
140
+ setOptions: (newOptions) => {
141
+ const reg = manager.#registrations.get(id);
142
+ if (reg) {
143
+ reg.options = {
144
+ ...reg.options,
145
+ ...newOptions
146
+ };
147
+ manager.registrations.setState((prev) => new Map(prev).set(id, toRegistrationView(reg)));
148
+ }
149
+ },
150
+ unregister: () => {
151
+ manager.#unregister(id);
152
+ }
153
+ };
154
+ }
155
+ /**
156
+ * Unregisters a sequence by its registration ID.
157
+ */
158
+ #unregister(id) {
159
+ const registration = this.#registrations.get(id);
160
+ if (!registration) return;
161
+ const target = registration.target;
162
+ this.#registrations.delete(id);
163
+ this.registrations.setState((prev) => {
164
+ const next = new Map(prev);
165
+ next.delete(id);
166
+ return next;
167
+ });
168
+ const targetRegs = this.#targetRegistrations.get(target);
169
+ if (targetRegs) {
170
+ targetRegs.delete(id);
171
+ if (targetRegs.size === 0) this.#removeListenersForTarget(target);
172
+ }
173
+ }
174
+ /**
175
+ * Ensures event listeners are attached for a specific target.
176
+ */
177
+ #ensureListenersForTarget(target) {
178
+ if (typeof document === "undefined") return;
179
+ if (this.#targetListeners.has(target)) return;
180
+ const keydownHandler = this.#createTargetKeyDownHandler(target);
181
+ const keyupHandler = this.#createTargetKeyUpHandler(target);
182
+ target.addEventListener("keydown", keydownHandler);
183
+ target.addEventListener("keyup", keyupHandler);
184
+ this.#targetListeners.set(target, {
185
+ keydown: keydownHandler,
186
+ keyup: keyupHandler
187
+ });
188
+ }
189
+ /**
190
+ * Removes event listeners for a specific target.
191
+ */
192
+ #removeListenersForTarget(target) {
193
+ if (typeof document === "undefined") return;
194
+ const listeners = this.#targetListeners.get(target);
195
+ if (!listeners) return;
196
+ target.removeEventListener("keydown", listeners.keydown);
197
+ target.removeEventListener("keyup", listeners.keyup);
198
+ this.#targetListeners.delete(target);
199
+ this.#targetRegistrations.delete(target);
200
+ }
201
+ /**
202
+ * Creates a keydown handler for a specific target.
203
+ */
204
+ #createTargetKeyDownHandler(target) {
205
+ return (event) => {
206
+ this.#processTargetEvent(event, target, "keydown");
207
+ };
208
+ }
209
+ /**
210
+ * Creates a keyup handler for a specific target.
211
+ */
212
+ #createTargetKeyUpHandler(target) {
213
+ return (event) => {
214
+ this.#processTargetEvent(event, target, "keyup");
215
+ };
216
+ }
217
+ /**
218
+ * Processes keyboard events for a specific target and event type.
219
+ */
220
+ #processTargetEvent(event, target, eventType) {
221
+ const targetRegs = this.#targetRegistrations.get(target);
222
+ if (!targetRegs) return;
223
+ const now = Date.now();
224
+ for (const id of targetRegs) {
225
+ const registration = this.#registrations.get(id);
226
+ if (!registration) continue;
227
+ if (!require_manager_utils.isEventForTarget(event, target)) continue;
228
+ if (!registration.options.enabled) continue;
229
+ if (registration.options.ignoreInputs !== false) {
230
+ if (require_manager_utils.isInputElement(event.target)) {
231
+ if (event.target !== registration.target) continue;
232
+ }
233
+ }
234
+ if (registration.options.eventType !== eventType) continue;
235
+ const timeout = registration.options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT;
236
+ if (registration.currentIndex > 0 && now - registration.lastKeyTime > timeout) registration.currentIndex = 0;
237
+ const expectedHotkey = registration.parsedSequence[registration.currentIndex];
238
+ if (!expectedHotkey) continue;
239
+ if (require_match.matchesKeyboardEvent(event, expectedHotkey, registration.options.platform)) {
240
+ registration.lastKeyTime = now;
241
+ registration.currentIndex++;
242
+ if (registration.currentIndex >= registration.parsedSequence.length) {
243
+ if (registration.options.preventDefault) event.preventDefault();
244
+ if (registration.options.stopPropagation) event.stopPropagation();
245
+ const context = {
246
+ hotkey: registration.sequence.join(" "),
247
+ parsedHotkey: registration.parsedSequence[registration.parsedSequence.length - 1]
248
+ };
249
+ registration.callback(event, context);
250
+ registration.currentIndex = 0;
251
+ }
252
+ } else if (registration.currentIndex > 0) {
253
+ const firstHotkey = registration.parsedSequence[0];
254
+ if (require_match.matchesKeyboardEvent(event, firstHotkey, registration.options.platform)) {
255
+ registration.currentIndex = 1;
256
+ registration.lastKeyTime = now;
257
+ } else registration.currentIndex = 0;
258
+ }
259
+ }
260
+ }
261
+ /**
262
+ * Finds an existing registration with the same sequence and target.
263
+ */
264
+ #findConflictingSequence(sequence, target) {
265
+ const key = sequenceKey(sequence);
266
+ for (const registration of this.#registrations.values()) if (sequenceKey(registration.sequence) === key && registration.target === target) return registration;
267
+ return null;
268
+ }
269
+ /**
270
+ * Resets all sequence progress.
271
+ */
272
+ resetAll() {
273
+ for (const registration of this.#registrations.values()) {
274
+ registration.currentIndex = 0;
275
+ registration.lastKeyTime = 0;
276
+ }
277
+ }
278
+ /**
279
+ * Triggers a sequence's callback programmatically from devtools.
280
+ * Creates a synthetic KeyboardEvent from the last key in the sequence.
281
+ *
282
+ * @param id - The registration ID to trigger
283
+ * @returns True if the registration was found and triggered
284
+ */
285
+ triggerSequence(id) {
286
+ const registration = this.#registrations.get(id);
287
+ if (!registration) return false;
288
+ const lastParsed = registration.parsedSequence[registration.parsedSequence.length - 1];
289
+ if (!lastParsed) return false;
290
+ const syntheticEvent = new KeyboardEvent(registration.options.eventType ?? "keydown", {
291
+ key: lastParsed.key,
292
+ ctrlKey: lastParsed.ctrl,
293
+ shiftKey: lastParsed.shift,
294
+ altKey: lastParsed.alt,
295
+ metaKey: lastParsed.meta,
296
+ bubbles: true,
297
+ cancelable: true
298
+ });
299
+ registration.triggerCount++;
300
+ this.registrations.setState((prev) => new Map(prev).set(id, toRegistrationView(registration)));
301
+ const context = {
302
+ hotkey: registration.sequence.join(" "),
303
+ parsedHotkey: lastParsed
304
+ };
305
+ registration.callback(syntheticEvent, context);
306
+ return true;
307
+ }
308
+ /**
309
+ * Gets the number of registered sequences.
310
+ */
311
+ getRegistrationCount() {
312
+ return this.#registrations.size;
313
+ }
314
+ /**
315
+ * Destroys the manager and removes all listeners.
316
+ */
317
+ destroy() {
318
+ for (const target of this.#targetListeners.keys()) this.#removeListenersForTarget(target);
319
+ this.#registrations.clear();
320
+ this.registrations.setState(() => /* @__PURE__ */ new Map());
321
+ }
322
+ };
323
+ /**
324
+ * Gets the singleton SequenceManager instance.
325
+ * Convenience function for accessing the manager.
326
+ */
327
+ function getSequenceManager() {
328
+ return SequenceManager.getInstance();
329
+ }
330
+ /**
331
+ * Creates a simple sequence matcher for one-off use.
332
+ *
333
+ * This is a low-level helper that does not support ignoreInputs, target,
334
+ * or other HotkeyOptions. Callers must handle input filtering themselves
335
+ * if attaching to document.
336
+ *
337
+ * @param sequence - The sequence of hotkeys to match
338
+ * @param options - Options including timeout
339
+ * @returns An object with match() and reset() methods
340
+ *
341
+ * @example
342
+ * ```ts
343
+ * const matcher = createSequenceMatcher(['G', 'G'], { timeout: 500 })
344
+ *
345
+ * document.addEventListener('keydown', (event) => {
346
+ * if (matcher.match(event)) {
347
+ * console.log('Sequence matched!')
348
+ * }
349
+ * })
350
+ * ```
351
+ */
352
+ function createSequenceMatcher(sequence, options = {}) {
353
+ const platform = options.platform ?? require_constants.detectPlatform();
354
+ const timeout = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT;
355
+ const parsedSequence = sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform));
356
+ let currentIndex = 0;
357
+ let lastKeyTime = 0;
358
+ return {
359
+ match(event) {
360
+ const now = Date.now();
361
+ if (currentIndex > 0 && now - lastKeyTime > timeout) currentIndex = 0;
362
+ const expected = parsedSequence[currentIndex];
363
+ if (!expected) return false;
364
+ if (require_match.matchesKeyboardEvent(event, expected, platform)) {
365
+ lastKeyTime = now;
366
+ currentIndex++;
367
+ if (currentIndex >= parsedSequence.length) {
368
+ currentIndex = 0;
369
+ return true;
370
+ }
371
+ } else if (currentIndex > 0) if (require_match.matchesKeyboardEvent(event, parsedSequence[0], platform)) {
372
+ currentIndex = 1;
373
+ lastKeyTime = now;
374
+ } else currentIndex = 0;
375
+ return false;
376
+ },
377
+ reset() {
378
+ currentIndex = 0;
379
+ lastKeyTime = 0;
380
+ },
381
+ getProgress() {
382
+ return currentIndex;
383
+ }
384
+ };
385
+ }
386
+
387
+ //#endregion
388
+ exports.SequenceManager = SequenceManager;
389
+ exports.createSequenceMatcher = createSequenceMatcher;
390
+ exports.getSequenceManager = getSequenceManager;
391
+ //# sourceMappingURL=sequence-manager.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sequence-manager.cjs","names":["#instance","Store","#platform","detectPlatform","parseHotkey","#findConflictingSequence","formatHotkeySequence","#unregister","getDefaultIgnoreInputs","defaultHotkeyOptions","#registrations","#targetRegistrations","#ensureListenersForTarget","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","#processTargetEvent","isEventForTarget","isInputElement","matchesKeyboardEvent"],"sources":["../src/sequence-manager.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { formatHotkeySequence } from './format'\nimport { detectPlatform } from './constants'\nimport { parseHotkey } from './parse'\nimport { matchesKeyboardEvent } from './match'\nimport {\n defaultHotkeyOptions,\n getDefaultIgnoreInputs,\n handleConflict,\n isEventForTarget,\n isInputElement,\n} from './manager.utils'\nimport type { HotkeyOptions } from './hotkey-manager'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n} from './hotkey'\n\ntype Target = HTMLElement | Document | Window\n\n/**\n * Options for hotkey sequence matching.\n * Extends HotkeyOptions but excludes requireReset (not applicable to sequences).\n */\nexport interface SequenceOptions extends Omit<HotkeyOptions, 'requireReset'> {\n /** Timeout between keys in milliseconds. Default: 1000 */\n timeout?: number\n}\n\n/**\n * A sequence of hotkeys for Vim-style shortcuts.\n *\n * @example\n * ```ts\n * const gotoTop: HotkeySequence = ['G', 'G'] // gg\n * const deleteLine: HotkeySequence = ['D', 'D'] // dd\n * const deleteWord: HotkeySequence = ['D', 'I', 'W'] // diw\n * ```\n */\nexport type HotkeySequence = Array<Hotkey>\n\n/**\n * Default timeout between keys in a sequence (in milliseconds).\n */\nconst DEFAULT_SEQUENCE_TIMEOUT = 1000\n\nlet sequenceIdCounter = 0\n\n/**\n * Generates a unique ID for sequence registrations.\n */\nfunction generateSequenceId(): string {\n return `sequence_${++sequenceIdCounter}`\n}\n\n/**\n * Returns a canonical string for sequence conflict comparison.\n */\nfunction sequenceKey(sequence: HotkeySequence): string {\n return sequence.join('|')\n}\n\n/**\n * View of a sequence registration for devtools display.\n * Excludes internal matching state (currentIndex, lastKeyTime).\n */\nexport interface SequenceRegistrationView {\n id: string\n sequence: HotkeySequence\n options: SequenceOptions\n target: Target\n triggerCount: number\n}\n\n/**\n * Internal representation of a sequence registration.\n */\ninterface SequenceRegistration {\n id: string\n sequence: HotkeySequence\n parsedSequence: Array<ParsedHotkey>\n callback: HotkeyCallback\n options: SequenceOptions\n target: Target\n currentIndex: number\n lastKeyTime: number\n triggerCount: number\n}\n\n/**\n * A handle returned from SequenceManager.register() that allows updating\n * the callback and options without re-registering the sequence.\n *\n * @example\n * ```ts\n * const handle = manager.register(['G', 'G'], callback, options)\n *\n * handle.callback = newCallback\n * handle.setOptions({ timeout: 500 })\n * handle.unregister()\n * ```\n */\nexport interface SequenceRegistrationHandle {\n readonly id: string\n readonly isActive: boolean\n callback: HotkeyCallback\n setOptions: (options: Partial<SequenceOptions>) => void\n unregister: () => void\n}\n\n/**\n * Manages keyboard sequence matching for Vim-style shortcuts.\n *\n * This class allows registering multi-key sequences like 'g g' or 'd d'\n * that trigger callbacks when the full sequence is pressed within\n * a configurable timeout.\n *\n * @example\n * ```ts\n * const matcher = SequenceManager.getInstance()\n *\n * // Register 'g g' to go to top\n * const unregister = matcher.register(['G', 'G'], (event, context) => {\n * scrollToTop()\n * }, { timeout: 500 })\n *\n * // Later, to unregister:\n * unregister()\n * ```\n */\n/**\n * Builds a devtools view from an internal registration.\n */\nfunction toRegistrationView(\n reg: SequenceRegistration,\n): SequenceRegistrationView {\n return {\n id: reg.id,\n sequence: reg.sequence,\n options: reg.options,\n target: reg.target,\n triggerCount: reg.triggerCount,\n }\n}\n\nexport class SequenceManager {\n static #instance: SequenceManager | null = null\n\n /**\n * The TanStack Store containing sequence registration views for devtools.\n * Subscribe to this to observe registration changes.\n */\n readonly registrations: Store<Map<string, SequenceRegistrationView>> =\n new Store(new Map())\n\n #registrations: Map<string, SequenceRegistration> = new Map()\n #targetListeners: Map<\n Target,\n {\n keydown: (event: KeyboardEvent) => void\n keyup: (event: KeyboardEvent) => void\n }\n > = new Map()\n #targetRegistrations: Map<Target, Set<string>> = new Map()\n #platform: 'mac' | 'windows' | 'linux'\n\n private constructor() {\n this.#platform = detectPlatform()\n }\n\n /**\n * Gets the singleton instance of SequenceManager.\n */\n static getInstance(): SequenceManager {\n if (!SequenceManager.#instance) {\n SequenceManager.#instance = new SequenceManager()\n }\n return SequenceManager.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (SequenceManager.#instance) {\n SequenceManager.#instance.destroy()\n SequenceManager.#instance = null\n }\n }\n\n /**\n * Registers a hotkey sequence handler.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior\n * @returns A handle to update or unregister the sequence\n */\n register(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: SequenceOptions = {},\n ): SequenceRegistrationHandle {\n if (sequence.length === 0) {\n throw new Error('Sequence must contain at least one hotkey')\n }\n\n const id = generateSequenceId()\n const platform = options.platform ?? this.#platform\n const parsedSequence = sequence.map((hotkey) =>\n parseHotkey(hotkey, platform),\n )\n\n // Resolve target: default to document if not provided or null\n const target: Target =\n options.target ??\n (typeof document !== 'undefined' ? document : ({} as Document))\n\n // Resolve conflict behavior\n const conflictBehavior = options.conflictBehavior ?? 'warn'\n\n // Check for existing registrations with the same sequence and target\n const conflictingRegistration = this.#findConflictingSequence(\n sequence,\n target,\n )\n\n if (conflictingRegistration) {\n handleConflict(\n conflictingRegistration.id,\n formatHotkeySequence(sequence),\n conflictBehavior,\n (regId) => this.#unregister(regId),\n )\n }\n\n const firstStep = parsedSequence[0]!\n const resolvedIgnoreInputs =\n options.ignoreInputs ?? getDefaultIgnoreInputs(firstStep)\n\n const baseOptions = {\n ...defaultHotkeyOptions,\n timeout: DEFAULT_SEQUENCE_TIMEOUT,\n ...options,\n platform,\n ignoreInputs: resolvedIgnoreInputs,\n }\n\n const registration: SequenceRegistration = {\n id,\n sequence,\n parsedSequence,\n callback,\n options: baseOptions,\n target,\n currentIndex: 0,\n lastKeyTime: 0,\n triggerCount: 0,\n }\n\n this.#registrations.set(id, registration)\n this.registrations.setState((prev) =>\n new Map(prev).set(id, toRegistrationView(registration)),\n )\n\n // Track registration for this target\n if (!this.#targetRegistrations.has(target)) {\n this.#targetRegistrations.set(target, new Set())\n }\n this.#targetRegistrations.get(target)!.add(id)\n\n // Ensure listeners are attached for this target\n this.#ensureListenersForTarget(target)\n\n const manager = this\n const handle: SequenceRegistrationHandle = {\n get id() {\n return id\n },\n get isActive() {\n return manager.#registrations.has(id)\n },\n get callback() {\n const reg = manager.#registrations.get(id)\n return reg?.callback ?? callback\n },\n set callback(newCallback: HotkeyCallback) {\n const reg = manager.#registrations.get(id)\n if (reg) {\n reg.callback = newCallback\n }\n },\n setOptions: (newOptions: Partial<SequenceOptions>) => {\n const reg = manager.#registrations.get(id)\n if (reg) {\n reg.options = { ...reg.options, ...newOptions }\n manager.registrations.setState((prev) =>\n new Map(prev).set(id, toRegistrationView(reg)),\n )\n }\n },\n unregister: () => {\n manager.#unregister(id)\n },\n }\n\n return handle\n }\n\n /**\n * Unregisters a sequence by its registration ID.\n */\n #unregister(id: string): void {\n const registration = this.#registrations.get(id)\n if (!registration) {\n return\n }\n\n const target = registration.target\n\n this.#registrations.delete(id)\n this.registrations.setState((prev) => {\n const next = new Map(prev)\n next.delete(id)\n return next\n })\n\n // Remove from target registrations tracking\n const targetRegs = this.#targetRegistrations.get(target)\n if (targetRegs) {\n targetRegs.delete(id)\n if (targetRegs.size === 0) {\n this.#removeListenersForTarget(target)\n }\n }\n }\n\n /**\n * Ensures event listeners are attached for a specific target.\n */\n #ensureListenersForTarget(target: Target): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n if (this.#targetListeners.has(target)) {\n return\n }\n\n const keydownHandler = this.#createTargetKeyDownHandler(target)\n const keyupHandler = this.#createTargetKeyUpHandler(target)\n\n target.addEventListener('keydown', keydownHandler as EventListener)\n target.addEventListener('keyup', keyupHandler as EventListener)\n\n this.#targetListeners.set(target, {\n keydown: keydownHandler,\n keyup: keyupHandler,\n })\n }\n\n /**\n * Removes event listeners for a specific target.\n */\n #removeListenersForTarget(target: Target): void {\n if (typeof document === 'undefined') {\n return\n }\n\n const listeners = this.#targetListeners.get(target)\n if (!listeners) {\n return\n }\n\n target.removeEventListener('keydown', listeners.keydown as EventListener)\n target.removeEventListener('keyup', listeners.keyup as EventListener)\n\n this.#targetListeners.delete(target)\n this.#targetRegistrations.delete(target)\n }\n\n /**\n * Creates a keydown handler for a specific target.\n */\n #createTargetKeyDownHandler(target: Target): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keydown')\n }\n }\n\n /**\n * Creates a keyup handler for a specific target.\n */\n #createTargetKeyUpHandler(target: Target): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keyup')\n }\n }\n\n /**\n * Processes keyboard events for a specific target and event type.\n */\n #processTargetEvent(\n event: KeyboardEvent,\n target: Target,\n eventType: 'keydown' | 'keyup',\n ): void {\n const targetRegs = this.#targetRegistrations.get(target)\n if (!targetRegs) {\n return\n }\n\n const now = Date.now()\n\n for (const id of targetRegs) {\n const registration = this.#registrations.get(id)\n if (!registration) {\n continue\n }\n\n if (!isEventForTarget(event, target)) {\n continue\n }\n\n if (!registration.options.enabled) {\n continue\n }\n\n // Check if we should ignore input elements (defaults to true)\n if (registration.options.ignoreInputs !== false) {\n if (isInputElement(event.target)) {\n // Don't ignore if the sequence is explicitly scoped to this input element\n if (event.target !== registration.target) {\n continue\n }\n }\n }\n\n // Only process registrations that listen for this event type\n if (registration.options.eventType !== eventType) {\n continue\n }\n\n const timeout = registration.options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT\n\n // Check if sequence has timed out\n if (\n registration.currentIndex > 0 &&\n now - registration.lastKeyTime > timeout\n ) {\n registration.currentIndex = 0\n }\n\n const expectedHotkey =\n registration.parsedSequence[registration.currentIndex]\n if (!expectedHotkey) {\n continue\n }\n\n if (\n matchesKeyboardEvent(\n event,\n expectedHotkey,\n registration.options.platform,\n )\n ) {\n registration.lastKeyTime = now\n registration.currentIndex++\n\n if (registration.currentIndex >= registration.parsedSequence.length) {\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n const context: HotkeyCallbackContext = {\n hotkey: registration.sequence.join(' ') as Hotkey,\n parsedHotkey:\n registration.parsedSequence[\n registration.parsedSequence.length - 1\n ]!,\n }\n\n registration.callback(event, context)\n\n registration.currentIndex = 0\n }\n } else if (registration.currentIndex > 0) {\n const firstHotkey = registration.parsedSequence[0]!\n if (\n matchesKeyboardEvent(\n event,\n firstHotkey,\n registration.options.platform,\n )\n ) {\n registration.currentIndex = 1\n registration.lastKeyTime = now\n } else {\n registration.currentIndex = 0\n }\n }\n }\n }\n\n /**\n * Finds an existing registration with the same sequence and target.\n */\n #findConflictingSequence(\n sequence: HotkeySequence,\n target: Target,\n ): SequenceRegistration | null {\n const key = sequenceKey(sequence)\n for (const registration of this.#registrations.values()) {\n if (\n sequenceKey(registration.sequence) === key &&\n registration.target === target\n ) {\n return registration\n }\n }\n return null\n }\n\n /**\n * Resets all sequence progress.\n */\n resetAll(): void {\n for (const registration of this.#registrations.values()) {\n registration.currentIndex = 0\n registration.lastKeyTime = 0\n }\n }\n\n /**\n * Triggers a sequence's callback programmatically from devtools.\n * Creates a synthetic KeyboardEvent from the last key in the sequence.\n *\n * @param id - The registration ID to trigger\n * @returns True if the registration was found and triggered\n */\n triggerSequence(id: string): boolean {\n const registration = this.#registrations.get(id)\n if (!registration) {\n return false\n }\n\n const lastParsed =\n registration.parsedSequence[registration.parsedSequence.length - 1]\n if (!lastParsed) {\n return false\n }\n\n const syntheticEvent = new KeyboardEvent(\n registration.options.eventType ?? 'keydown',\n {\n key: lastParsed.key,\n ctrlKey: lastParsed.ctrl,\n shiftKey: lastParsed.shift,\n altKey: lastParsed.alt,\n metaKey: lastParsed.meta,\n bubbles: true,\n cancelable: true,\n },\n )\n\n registration.triggerCount++\n\n this.registrations.setState((prev) =>\n new Map(prev).set(id, toRegistrationView(registration)),\n )\n\n const context: HotkeyCallbackContext = {\n hotkey: registration.sequence.join(' ') as Hotkey,\n parsedHotkey: lastParsed,\n }\n\n registration.callback(syntheticEvent, context)\n\n return true\n }\n\n /**\n * Gets the number of registered sequences.\n */\n getRegistrationCount(): number {\n return this.#registrations.size\n }\n\n /**\n * Destroys the manager and removes all listeners.\n */\n destroy(): void {\n for (const target of this.#targetListeners.keys()) {\n this.#removeListenersForTarget(target)\n }\n this.#registrations.clear()\n this.registrations.setState(() => new Map())\n }\n}\n\n/**\n * Gets the singleton SequenceManager instance.\n * Convenience function for accessing the manager.\n */\nexport function getSequenceManager(): SequenceManager {\n return SequenceManager.getInstance()\n}\n\n/**\n * Creates a simple sequence matcher for one-off use.\n *\n * This is a low-level helper that does not support ignoreInputs, target,\n * or other HotkeyOptions. Callers must handle input filtering themselves\n * if attaching to document.\n *\n * @param sequence - The sequence of hotkeys to match\n * @param options - Options including timeout\n * @returns An object with match() and reset() methods\n *\n * @example\n * ```ts\n * const matcher = createSequenceMatcher(['G', 'G'], { timeout: 500 })\n *\n * document.addEventListener('keydown', (event) => {\n * if (matcher.match(event)) {\n * console.log('Sequence matched!')\n * }\n * })\n * ```\n */\nexport function createSequenceMatcher(\n sequence: HotkeySequence,\n options: { timeout?: number; platform?: 'mac' | 'windows' | 'linux' } = {},\n): {\n match: (event: KeyboardEvent) => boolean\n reset: () => void\n getProgress: () => number\n} {\n const platform = options.platform ?? detectPlatform()\n const timeout = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT\n const parsedSequence = sequence.map((hotkey) => parseHotkey(hotkey, platform))\n\n let currentIndex = 0\n let lastKeyTime = 0\n\n return {\n match(event: KeyboardEvent): boolean {\n const now = Date.now()\n\n if (currentIndex > 0 && now - lastKeyTime > timeout) {\n currentIndex = 0\n }\n\n const expected = parsedSequence[currentIndex]\n if (!expected) {\n return false\n }\n\n if (matchesKeyboardEvent(event, expected, platform)) {\n lastKeyTime = now\n currentIndex++\n\n if (currentIndex >= parsedSequence.length) {\n currentIndex = 0\n return true\n }\n } else if (currentIndex > 0) {\n if (matchesKeyboardEvent(event, parsedSequence[0]!, platform)) {\n currentIndex = 1\n lastKeyTime = now\n } else {\n currentIndex = 0\n }\n }\n\n return false\n },\n\n reset(): void {\n currentIndex = 0\n lastKeyTime = 0\n },\n\n getProgress(): number {\n return currentIndex\n },\n }\n}\n"],"mappings":";;;;;;;;;;;AA8CA,MAAM,2BAA2B;AAEjC,IAAI,oBAAoB;;;;AAKxB,SAAS,qBAA6B;AACpC,QAAO,YAAY,EAAE;;;;;AAMvB,SAAS,YAAY,UAAkC;AACrD,QAAO,SAAS,KAAK,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;AA0E3B,SAAS,mBACP,KAC0B;AAC1B,QAAO;EACL,IAAI,IAAI;EACR,UAAU,IAAI;EACd,SAAS,IAAI;EACb,QAAQ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