@tanstack/hotkeys 0.8.0 → 0.10.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.
Files changed (109) hide show
  1. package/README.md +15 -1
  2. package/dist/{manager.utils.js → _event-target.js} +2 -41
  3. package/dist/_keyboard-event.js +75 -0
  4. package/dist/_match.js +48 -0
  5. package/dist/_named-keys.d.ts +6 -0
  6. package/dist/_named-keys.js +65 -0
  7. package/dist/_recorder-chord.js +42 -0
  8. package/dist/_recording-guard.js +55 -0
  9. package/dist/_registration.js +49 -0
  10. package/dist/conflicts.d.ts +36 -0
  11. package/dist/conflicts.js +54 -0
  12. package/dist/constants.d.ts +16 -144
  13. package/dist/constants.js +55 -172
  14. package/dist/display-labels.d.ts +90 -0
  15. package/dist/display-labels.js +112 -0
  16. package/dist/format.d.ts +48 -12
  17. package/dist/format.js +35 -39
  18. package/dist/hint.d.ts +20 -0
  19. package/dist/hint.js +23 -0
  20. package/dist/hotkey-manager.d.ts +14 -12
  21. package/dist/hotkey-manager.js +70 -21
  22. package/dist/hotkey-recorder.d.ts +8 -8
  23. package/dist/hotkey-recorder.js +74 -15
  24. package/dist/hotkey-sequence-recorder.d.ts +13 -9
  25. package/dist/hotkey-sequence-recorder.js +90 -28
  26. package/dist/{hotkey.d.ts → hotkey.types.d.ts} +58 -126
  27. package/dist/index.d.ts +14 -9
  28. package/dist/index.js +10 -6
  29. package/dist/key-state-tracker.d.ts +4 -7
  30. package/dist/key-state-tracker.js +30 -27
  31. package/dist/key.types.d.ts +79 -0
  32. package/dist/match.d.ts +24 -11
  33. package/dist/match.js +19 -30
  34. package/dist/parse.d.ts +18 -15
  35. package/dist/parse.js +39 -22
  36. package/dist/platform.d.ts +41 -0
  37. package/dist/platform.js +52 -0
  38. package/dist/recorder-options.d.ts +31 -0
  39. package/dist/sequence-manager.d.ts +27 -15
  40. package/dist/sequence-manager.js +61 -30
  41. package/dist/validate.d.ts +16 -8
  42. package/dist/validate.js +8 -13
  43. package/package.json +8 -13
  44. package/dist/constants.cjs +0 -535
  45. package/dist/constants.cjs.map +0 -1
  46. package/dist/constants.d.cts +0 -290
  47. package/dist/constants.js.map +0 -1
  48. package/dist/format.cjs +0 -91
  49. package/dist/format.cjs.map +0 -1
  50. package/dist/format.d.cts +0 -60
  51. package/dist/format.js.map +0 -1
  52. package/dist/hotkey-manager.cjs +0 -383
  53. package/dist/hotkey-manager.cjs.map +0 -1
  54. package/dist/hotkey-manager.d.cts +0 -230
  55. package/dist/hotkey-manager.js.map +0 -1
  56. package/dist/hotkey-recorder.cjs +0 -179
  57. package/dist/hotkey-recorder.cjs.map +0 -1
  58. package/dist/hotkey-recorder.d.cts +0 -116
  59. package/dist/hotkey-recorder.js.map +0 -1
  60. package/dist/hotkey-sequence-recorder.cjs +0 -192
  61. package/dist/hotkey-sequence-recorder.cjs.map +0 -1
  62. package/dist/hotkey-sequence-recorder.d.cts +0 -79
  63. package/dist/hotkey-sequence-recorder.js.map +0 -1
  64. package/dist/hotkey.d.cts +0 -301
  65. package/dist/index.cjs +0 -63
  66. package/dist/index.d.cts +0 -13
  67. package/dist/key-state-tracker.cjs +0 -197
  68. package/dist/key-state-tracker.cjs.map +0 -1
  69. package/dist/key-state-tracker.d.cts +0 -107
  70. package/dist/key-state-tracker.js.map +0 -1
  71. package/dist/manager.utils.cjs +0 -115
  72. package/dist/manager.utils.cjs.map +0 -1
  73. package/dist/manager.utils.d.cts +0 -13
  74. package/dist/manager.utils.d.ts +0 -13
  75. package/dist/manager.utils.js.map +0 -1
  76. package/dist/match.cjs +0 -153
  77. package/dist/match.cjs.map +0 -1
  78. package/dist/match.d.cts +0 -85
  79. package/dist/match.js.map +0 -1
  80. package/dist/parse.cjs +0 -228
  81. package/dist/parse.cjs.map +0 -1
  82. package/dist/parse.d.cts +0 -128
  83. package/dist/parse.js.map +0 -1
  84. package/dist/recorder-chord.cjs +0 -18
  85. package/dist/recorder-chord.cjs.map +0 -1
  86. package/dist/recorder-chord.js +0 -18
  87. package/dist/recorder-chord.js.map +0 -1
  88. package/dist/sequence-manager.cjs +0 -415
  89. package/dist/sequence-manager.cjs.map +0 -1
  90. package/dist/sequence-manager.d.cts +0 -157
  91. package/dist/sequence-manager.js.map +0 -1
  92. package/dist/validate.cjs +0 -116
  93. package/dist/validate.cjs.map +0 -1
  94. package/dist/validate.d.cts +0 -56
  95. package/dist/validate.js.map +0 -1
  96. package/src/constants.ts +0 -603
  97. package/src/format.ts +0 -142
  98. package/src/hotkey-manager.ts +0 -759
  99. package/src/hotkey-recorder.ts +0 -272
  100. package/src/hotkey-sequence-recorder.ts +0 -328
  101. package/src/hotkey.ts +0 -436
  102. package/src/index.ts +0 -11
  103. package/src/key-state-tracker.ts +0 -249
  104. package/src/manager.utils.ts +0 -231
  105. package/src/match.ts +0 -259
  106. package/src/parse.ts +0 -328
  107. package/src/recorder-chord.ts +0 -25
  108. package/src/sequence-manager.ts +0 -734
  109. package/src/validate.ts +0 -171
package/dist/parse.d.cts DELETED
@@ -1,128 +0,0 @@
1
- import { Hotkey, Key, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.cjs";
2
- import { MODIFIER_ALIASES } from "./constants.cjs";
3
-
4
- //#region src/parse.d.ts
5
- /**
6
- * Parses a hotkey string into its component parts.
7
- *
8
- * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
9
- * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
10
- * @returns A ParsedHotkey object with the key and modifier flags
11
- *
12
- * @example
13
- * ```ts
14
- * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
15
- * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
16
- * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
17
- * ```
18
- */
19
- declare function parseHotkey(hotkey: Hotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
20
- /**
21
- * Converts a RawHotkey object to a ParsedHotkey.
22
- * Optional modifier booleans default to false; modifiers array is derived from them.
23
- * When `mod` is true, it is resolved to Control or Meta based on platform.
24
- *
25
- * @param raw - The raw hotkey object
26
- * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
27
- * @returns A ParsedHotkey suitable for matching and formatting
28
- *
29
- * @example
30
- * ```ts
31
- * rawHotkeyToParsedHotkey({ key: 'Escape' })
32
- * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
33
- *
34
- * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
35
- * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
36
- *
37
- * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
38
- * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
39
- * ```
40
- */
41
- declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'windows' | 'linux'): ParsedHotkey;
42
- /**
43
- * Normalizes a hotkey string to its canonical form.
44
- *
45
- * - When `Mod` is allowed for the platform (Command on Mac without Control;
46
- * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
47
- * then the key (e.g. `Mod+Shift+E`, `Mod+S`).
48
- * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
49
- * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
50
- *
51
- * @param hotkey - The hotkey string to normalize
52
- * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
53
- * @returns The normalized hotkey string
54
- *
55
- * @example
56
- * ```ts
57
- * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
58
- * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
59
- * normalizeHotkey('esc') // 'Escape'
60
- * ```
61
- */
62
- declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
63
- /**
64
- * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
65
- */
66
- declare function normalizeHotkeyFromParsed(parsed: ParsedHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
67
- /**
68
- * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
69
- * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
70
- */
71
- declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
72
- /**
73
- * Checks if a string is a recognized modifier token (including aliases).
74
- *
75
- * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
76
- * spellings like `OS` / `Win` match the same alias table.
77
- *
78
- * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
79
- * @returns True if the string is a recognized modifier
80
- */
81
- declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MODIFIER_ALIASES;
82
- /**
83
- * Parses a KeyboardEvent into a ParsedHotkey object.
84
- *
85
- * This function extracts the key and modifier state from a keyboard event
86
- * and converts it into the same format used by `parseHotkey()`.
87
- *
88
- * @param event - The KeyboardEvent to parse
89
- * @param platform - The target platform for resolving modifiers (defaults to auto-detection)
90
- * @returns A ParsedHotkey object representing the keyboard event
91
- *
92
- * @example
93
- * ```ts
94
- * document.addEventListener('keydown', (event) => {
95
- * const parsed = parseKeyboardEvent(event)
96
- * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }
97
- * })
98
- * ```
99
- */
100
- declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
101
- /**
102
- * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
103
- *
104
- * @param event - The keyboard event (typically `keydown`)
105
- * @param platform - Target platform for `Mod` eligibility
106
- */
107
- declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
108
- /**
109
- * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
110
- *
111
- * This is useful for validating that a recorded hotkey is complete and not
112
- * just a combination of modifiers without an action key.
113
- *
114
- * @param hotkey - The hotkey string or ParsedHotkey to check
115
- * @param platform - The target platform for parsing (defaults to auto-detection)
116
- * @returns True if the hotkey contains at least one non-modifier key
117
- *
118
- * @example
119
- * ```ts
120
- * hasNonModifierKey('Control+Shift+S') // true
121
- * hasNonModifierKey('Control+Shift') // false (no action key)
122
- * hasNonModifierKey(parseHotkey('Mod+A')) // true
123
- * ```
124
- */
125
- declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
126
- //#endregion
127
- export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
128
- //# sourceMappingURL=parse.d.cts.map
package/dist/parse.js.map DELETED
@@ -1 +0,0 @@
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":"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"}
@@ -1,18 +0,0 @@
1
- const require_constants = require('./constants.cjs');
2
- const require_parse = require('./parse.cjs');
3
-
4
- //#region src/recorder-chord.ts
5
- /**
6
- * Parses a keydown event into a single normalized hotkey chord, or null if
7
- * the event should be ignored (modifier-only or invalid).
8
- */
9
- function hotkeyChordFromKeydown(event, platform) {
10
- if (require_parse.isModifierKey(require_constants.normalizeKeyName(event.key))) return null;
11
- const finalHotkey = require_parse.normalizeHotkeyFromEvent(event, platform);
12
- if (!require_parse.hasNonModifierKey(finalHotkey, platform)) return null;
13
- return finalHotkey;
14
- }
15
-
16
- //#endregion
17
- exports.hotkeyChordFromKeydown = hotkeyChordFromKeydown;
18
- //# sourceMappingURL=recorder-chord.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"recorder-chord.cjs","names":["isModifierKey","normalizeKeyName","normalizeHotkeyFromEvent","hasNonModifierKey"],"sources":["../src/recorder-chord.ts"],"sourcesContent":["import { normalizeKeyName } from './constants'\nimport {\n hasNonModifierKey,\n isModifierKey,\n normalizeHotkeyFromEvent,\n} from './parse'\nimport type { Hotkey } from './hotkey'\n\n/**\n * Parses a keydown event into a single normalized hotkey chord, or null if\n * the event should be ignored (modifier-only or invalid).\n */\nexport function hotkeyChordFromKeydown(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey | null {\n if (isModifierKey(normalizeKeyName(event.key))) {\n return null\n }\n const finalHotkey = normalizeHotkeyFromEvent(event, platform)\n if (!hasNonModifierKey(finalHotkey, platform)) {\n return null\n }\n return finalHotkey\n}\n"],"mappings":";;;;;;;;AAYA,SAAgB,uBACd,OACA,UACe;AACf,KAAIA,4BAAcC,mCAAiB,MAAM,IAAI,CAAC,CAC5C,QAAO;CAET,MAAM,cAAcC,uCAAyB,OAAO,SAAS;AAC7D,KAAI,CAACC,gCAAkB,aAAa,SAAS,CAC3C,QAAO;AAET,QAAO"}
@@ -1,18 +0,0 @@
1
- import { normalizeKeyName } from "./constants.js";
2
- import { hasNonModifierKey, isModifierKey, normalizeHotkeyFromEvent } from "./parse.js";
3
-
4
- //#region src/recorder-chord.ts
5
- /**
6
- * Parses a keydown event into a single normalized hotkey chord, or null if
7
- * the event should be ignored (modifier-only or invalid).
8
- */
9
- function hotkeyChordFromKeydown(event, platform) {
10
- if (isModifierKey(normalizeKeyName(event.key))) return null;
11
- const finalHotkey = normalizeHotkeyFromEvent(event, platform);
12
- if (!hasNonModifierKey(finalHotkey, platform)) return null;
13
- return finalHotkey;
14
- }
15
-
16
- //#endregion
17
- export { hotkeyChordFromKeydown };
18
- //# sourceMappingURL=recorder-chord.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"recorder-chord.js","names":[],"sources":["../src/recorder-chord.ts"],"sourcesContent":["import { normalizeKeyName } from './constants'\nimport {\n hasNonModifierKey,\n isModifierKey,\n normalizeHotkeyFromEvent,\n} from './parse'\nimport type { Hotkey } from './hotkey'\n\n/**\n * Parses a keydown event into a single normalized hotkey chord, or null if\n * the event should be ignored (modifier-only or invalid).\n */\nexport function hotkeyChordFromKeydown(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey | null {\n if (isModifierKey(normalizeKeyName(event.key))) {\n return null\n }\n const finalHotkey = normalizeHotkeyFromEvent(event, platform)\n if (!hasNonModifierKey(finalHotkey, platform)) {\n return null\n }\n return finalHotkey\n}\n"],"mappings":";;;;;;;;AAYA,SAAgB,uBACd,OACA,UACe;AACf,KAAI,cAAc,iBAAiB,MAAM,IAAI,CAAC,CAC5C,QAAO;CAET,MAAM,cAAc,yBAAyB,OAAO,SAAS;AAC7D,KAAI,CAAC,kBAAkB,aAAa,SAAS,CAC3C,QAAO;AAET,QAAO"}
@@ -1,415 +0,0 @@
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
- * Exported for consumers that display sequence state (e.g. devtools).
12
- */
13
- const DEFAULT_SEQUENCE_TIMEOUT = 1e3;
14
- let sequenceIdCounter = 0;
15
- /**
16
- * Generates a unique ID for sequence registrations.
17
- */
18
- function generateSequenceId() {
19
- return `sequence_${++sequenceIdCounter}`;
20
- }
21
- /**
22
- * Returns a canonical string for sequence conflict comparison.
23
- */
24
- function sequenceKey(sequence) {
25
- return sequence.join("|");
26
- }
27
- /**
28
- * Manages keyboard sequence matching for Vim-style shortcuts.
29
- *
30
- * This class allows registering multi-key sequences like 'g g' or 'd d'
31
- * that trigger callbacks when the full sequence is pressed within
32
- * a configurable timeout.
33
- *
34
- * Modifier-only `keydown` / `keyup` events (Shift, Control, Alt, or Meta
35
- * alone) are not processed: they neither match a step nor reset progress.
36
- * That supports chained modifier chords (e.g. `Shift+R` then `Shift+T`) when
37
- * the user presses a modifier alone between steps.
38
- *
39
- * @example
40
- * ```ts
41
- * const matcher = SequenceManager.getInstance()
42
- *
43
- * // Register 'g g' to go to top
44
- * const unregister = matcher.register(['G', 'G'], (event, context) => {
45
- * scrollToTop()
46
- * }, { timeout: 500 })
47
- *
48
- * // Later, to unregister:
49
- * unregister()
50
- * ```
51
- */
52
- /**
53
- * Builds a devtools view from an internal registration.
54
- */
55
- function toRegistrationView(reg) {
56
- const len = reg.parsedSequence.length;
57
- const inProgress = reg.currentIndex > 0 && reg.currentIndex < len;
58
- return {
59
- id: reg.id,
60
- sequence: reg.sequence,
61
- options: reg.options,
62
- target: reg.target,
63
- triggerCount: reg.triggerCount,
64
- hasFired: reg.hasFired,
65
- matchedStepCount: inProgress ? reg.currentIndex : 0,
66
- partialMatchLastKeyTime: inProgress ? reg.lastKeyTime : 0
67
- };
68
- }
69
- var SequenceManager = class SequenceManager {
70
- static #instance = null;
71
- #registrations = /* @__PURE__ */ new Map();
72
- #targetListeners = /* @__PURE__ */ new Map();
73
- #targetRegistrations = /* @__PURE__ */ new Map();
74
- #platform;
75
- constructor() {
76
- this.registrations = new _tanstack_store.Store(/* @__PURE__ */ new Map());
77
- this.#platform = require_constants.detectPlatform();
78
- }
79
- /**
80
- * Gets the singleton instance of SequenceManager.
81
- */
82
- static getInstance() {
83
- if (!SequenceManager.#instance) SequenceManager.#instance = new SequenceManager();
84
- return SequenceManager.#instance;
85
- }
86
- /**
87
- * Resets the singleton instance. Useful for testing.
88
- */
89
- static resetInstance() {
90
- if (SequenceManager.#instance) {
91
- SequenceManager.#instance.destroy();
92
- SequenceManager.#instance = null;
93
- }
94
- }
95
- #syncRegistrationView(registration) {
96
- this.registrations.setState((prev) => new Map(prev).set(registration.id, toRegistrationView(registration)));
97
- }
98
- /**
99
- * Registers a hotkey sequence handler.
100
- *
101
- * @param sequence - Array of hotkey strings that form the sequence
102
- * @param callback - Function to call when the sequence is completed
103
- * @param options - Options for the sequence behavior
104
- * @returns A handle to update or unregister the sequence
105
- */
106
- register(sequence, callback, options = {}) {
107
- if (sequence.length === 0) throw new Error("Sequence must contain at least one hotkey");
108
- const id = generateSequenceId();
109
- const platform = options.platform ?? this.#platform;
110
- const parsedSequence = sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform));
111
- const target = options.target ?? (typeof document !== "undefined" ? document : {});
112
- const conflictBehavior = options.conflictBehavior ?? "warn";
113
- const conflictingRegistration = this.#findConflictingSequence(sequence, target);
114
- if (conflictingRegistration) require_manager_utils.handleConflict(conflictingRegistration.id, require_format.formatHotkeySequence(sequence), conflictBehavior, (regId) => this.#unregister(regId));
115
- const firstStep = parsedSequence[0];
116
- const resolvedIgnoreInputs = options.ignoreInputs ?? require_manager_utils.getDefaultIgnoreInputs(firstStep);
117
- const registration = {
118
- id,
119
- sequence,
120
- parsedSequence,
121
- callback,
122
- options: {
123
- ...require_manager_utils.defaultHotkeyOptions,
124
- timeout: DEFAULT_SEQUENCE_TIMEOUT,
125
- ...options,
126
- platform,
127
- ignoreInputs: resolvedIgnoreInputs
128
- },
129
- target,
130
- currentIndex: 0,
131
- lastKeyTime: 0,
132
- triggerCount: 0,
133
- hasFired: false
134
- };
135
- this.#registrations.set(id, registration);
136
- this.registrations.setState((prev) => new Map(prev).set(id, toRegistrationView(registration)));
137
- if (!this.#targetRegistrations.has(target)) this.#targetRegistrations.set(target, /* @__PURE__ */ new Set());
138
- this.#targetRegistrations.get(target).add(id);
139
- this.#ensureListenersForTarget(target);
140
- const manager = this;
141
- return {
142
- get id() {
143
- return id;
144
- },
145
- get isActive() {
146
- return manager.#registrations.has(id);
147
- },
148
- get callback() {
149
- return manager.#registrations.get(id)?.callback ?? callback;
150
- },
151
- set callback(newCallback) {
152
- const reg = manager.#registrations.get(id);
153
- if (reg) reg.callback = newCallback;
154
- },
155
- setOptions: (newOptions) => {
156
- const reg = manager.#registrations.get(id);
157
- if (reg) {
158
- reg.options = {
159
- ...reg.options,
160
- ...newOptions
161
- };
162
- manager.registrations.setState((prev) => new Map(prev).set(id, toRegistrationView(reg)));
163
- }
164
- },
165
- unregister: () => {
166
- manager.#unregister(id);
167
- }
168
- };
169
- }
170
- /**
171
- * Unregisters a sequence by its registration ID.
172
- */
173
- #unregister(id) {
174
- const registration = this.#registrations.get(id);
175
- if (!registration) return;
176
- const target = registration.target;
177
- this.#registrations.delete(id);
178
- this.registrations.setState((prev) => {
179
- const next = new Map(prev);
180
- next.delete(id);
181
- return next;
182
- });
183
- const targetRegs = this.#targetRegistrations.get(target);
184
- if (targetRegs) {
185
- targetRegs.delete(id);
186
- if (targetRegs.size === 0) this.#removeListenersForTarget(target);
187
- }
188
- }
189
- /**
190
- * Ensures event listeners are attached for a specific target.
191
- */
192
- #ensureListenersForTarget(target) {
193
- if (typeof document === "undefined") return;
194
- if (this.#targetListeners.has(target)) return;
195
- const keydownHandler = this.#createTargetKeyDownHandler(target);
196
- const keyupHandler = this.#createTargetKeyUpHandler(target);
197
- target.addEventListener("keydown", keydownHandler);
198
- target.addEventListener("keyup", keyupHandler);
199
- this.#targetListeners.set(target, {
200
- keydown: keydownHandler,
201
- keyup: keyupHandler
202
- });
203
- }
204
- /**
205
- * Removes event listeners for a specific target.
206
- */
207
- #removeListenersForTarget(target) {
208
- if (typeof document === "undefined") return;
209
- const listeners = this.#targetListeners.get(target);
210
- if (!listeners) return;
211
- target.removeEventListener("keydown", listeners.keydown);
212
- target.removeEventListener("keyup", listeners.keyup);
213
- this.#targetListeners.delete(target);
214
- this.#targetRegistrations.delete(target);
215
- }
216
- /**
217
- * Creates a keydown handler for a specific target.
218
- */
219
- #createTargetKeyDownHandler(target) {
220
- return (event) => {
221
- this.#processTargetEvent(event, target, "keydown");
222
- };
223
- }
224
- /**
225
- * Creates a keyup handler for a specific target.
226
- */
227
- #createTargetKeyUpHandler(target) {
228
- return (event) => {
229
- this.#processTargetEvent(event, target, "keyup");
230
- };
231
- }
232
- /**
233
- * Processes keyboard events for a specific target and event type.
234
- */
235
- #processTargetEvent(event, target, eventType) {
236
- if (require_parse.isModifierKey(require_constants.normalizeKeyName(event.key))) return;
237
- const targetRegs = this.#targetRegistrations.get(target);
238
- if (!targetRegs) return;
239
- const now = Date.now();
240
- registrationIds: for (const id of targetRegs) {
241
- const registration = this.#registrations.get(id);
242
- if (!registration) continue;
243
- if (!require_manager_utils.isEventForTarget(event, target)) continue;
244
- if (!registration.options.enabled) continue;
245
- if (registration.options.ignoreInputs !== false) {
246
- if (require_manager_utils.shouldIgnoreInputEvent(event, target, registration.target)) continue registrationIds;
247
- }
248
- if (registration.options.eventType !== eventType) continue;
249
- const timeout = registration.options.timeout ?? 1e3;
250
- if (registration.currentIndex > 0 && now - registration.lastKeyTime > timeout) {
251
- registration.currentIndex = 0;
252
- this.#syncRegistrationView(registration);
253
- }
254
- const expectedHotkey = registration.parsedSequence[registration.currentIndex];
255
- if (!expectedHotkey) continue;
256
- if (require_match.matchesKeyboardEvent(event, expectedHotkey, registration.options.platform)) {
257
- registration.lastKeyTime = now;
258
- registration.currentIndex++;
259
- if (registration.currentIndex >= registration.parsedSequence.length) {
260
- if (registration.options.preventDefault) event.preventDefault();
261
- if (registration.options.stopPropagation) event.stopPropagation();
262
- const context = {
263
- hotkey: registration.sequence.join(" "),
264
- parsedHotkey: registration.parsedSequence[registration.parsedSequence.length - 1]
265
- };
266
- registration.triggerCount++;
267
- registration.hasFired = true;
268
- registration.currentIndex = 0;
269
- this.#syncRegistrationView(registration);
270
- registration.callback(event, context);
271
- } else this.#syncRegistrationView(registration);
272
- } else if (registration.currentIndex > 0) {
273
- const firstHotkey = registration.parsedSequence[0];
274
- if (require_match.matchesKeyboardEvent(event, firstHotkey, registration.options.platform)) {
275
- registration.currentIndex = 1;
276
- registration.lastKeyTime = now;
277
- } else registration.currentIndex = 0;
278
- this.#syncRegistrationView(registration);
279
- }
280
- }
281
- }
282
- /**
283
- * Finds an existing registration with the same sequence and target.
284
- */
285
- #findConflictingSequence(sequence, target) {
286
- const key = sequenceKey(sequence);
287
- for (const registration of this.#registrations.values()) if (sequenceKey(registration.sequence) === key && registration.target === target) return registration;
288
- return null;
289
- }
290
- /**
291
- * Resets all sequence progress.
292
- */
293
- resetAll() {
294
- for (const registration of this.#registrations.values()) {
295
- registration.currentIndex = 0;
296
- registration.lastKeyTime = 0;
297
- this.#syncRegistrationView(registration);
298
- }
299
- }
300
- /**
301
- * Triggers a sequence's callback programmatically from devtools.
302
- * Creates a synthetic KeyboardEvent from the last key in the sequence.
303
- *
304
- * @param id - The registration ID to trigger
305
- * @returns True if the registration was found and triggered
306
- */
307
- triggerSequence(id) {
308
- const registration = this.#registrations.get(id);
309
- if (!registration) return false;
310
- const lastParsed = registration.parsedSequence[registration.parsedSequence.length - 1];
311
- if (!lastParsed) return false;
312
- const syntheticEvent = new KeyboardEvent(registration.options.eventType ?? "keydown", {
313
- key: lastParsed.key,
314
- ctrlKey: lastParsed.ctrl,
315
- shiftKey: lastParsed.shift,
316
- altKey: lastParsed.alt,
317
- metaKey: lastParsed.meta,
318
- bubbles: true,
319
- cancelable: true
320
- });
321
- registration.triggerCount++;
322
- registration.hasFired = true;
323
- this.#syncRegistrationView(registration);
324
- const context = {
325
- hotkey: registration.sequence.join(" "),
326
- parsedHotkey: lastParsed
327
- };
328
- registration.callback(syntheticEvent, context);
329
- return true;
330
- }
331
- /**
332
- * Gets the number of registered sequences.
333
- */
334
- getRegistrationCount() {
335
- return this.#registrations.size;
336
- }
337
- /**
338
- * Destroys the manager and removes all listeners.
339
- */
340
- destroy() {
341
- for (const target of this.#targetListeners.keys()) this.#removeListenersForTarget(target);
342
- this.#registrations.clear();
343
- this.registrations.setState(() => /* @__PURE__ */ new Map());
344
- }
345
- };
346
- /**
347
- * Gets the singleton SequenceManager instance.
348
- * Convenience function for accessing the manager.
349
- */
350
- function getSequenceManager() {
351
- return SequenceManager.getInstance();
352
- }
353
- /**
354
- * Creates a simple sequence matcher for one-off use.
355
- *
356
- * This is a low-level helper that does not support ignoreInputs, target,
357
- * or other HotkeyOptions. Callers must handle input filtering themselves
358
- * if attaching to document.
359
- *
360
- * @param sequence - The sequence of hotkeys to match
361
- * @param options - Options including timeout
362
- * @returns An object with match() and reset() methods
363
- *
364
- * @example
365
- * ```ts
366
- * const matcher = createSequenceMatcher(['G', 'G'], { timeout: 500 })
367
- *
368
- * document.addEventListener('keydown', (event) => {
369
- * if (matcher.match(event)) {
370
- * console.log('Sequence matched!')
371
- * }
372
- * })
373
- * ```
374
- */
375
- function createSequenceMatcher(sequence, options = {}) {
376
- const platform = options.platform ?? require_constants.detectPlatform();
377
- const timeout = options.timeout ?? 1e3;
378
- const parsedSequence = sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform));
379
- let currentIndex = 0;
380
- let lastKeyTime = 0;
381
- return {
382
- match(event) {
383
- const now = Date.now();
384
- if (currentIndex > 0 && now - lastKeyTime > timeout) currentIndex = 0;
385
- const expected = parsedSequence[currentIndex];
386
- if (!expected) return false;
387
- if (require_match.matchesKeyboardEvent(event, expected, platform)) {
388
- lastKeyTime = now;
389
- currentIndex++;
390
- if (currentIndex >= parsedSequence.length) {
391
- currentIndex = 0;
392
- return true;
393
- }
394
- } else if (currentIndex > 0) if (require_match.matchesKeyboardEvent(event, parsedSequence[0], platform)) {
395
- currentIndex = 1;
396
- lastKeyTime = now;
397
- } else currentIndex = 0;
398
- return false;
399
- },
400
- reset() {
401
- currentIndex = 0;
402
- lastKeyTime = 0;
403
- },
404
- getProgress() {
405
- return currentIndex;
406
- }
407
- };
408
- }
409
-
410
- //#endregion
411
- exports.DEFAULT_SEQUENCE_TIMEOUT = DEFAULT_SEQUENCE_TIMEOUT;
412
- exports.SequenceManager = SequenceManager;
413
- exports.createSequenceMatcher = createSequenceMatcher;
414
- exports.getSequenceManager = getSequenceManager;
415
- //# sourceMappingURL=sequence-manager.cjs.map