@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/src/format.ts DELETED
@@ -1,142 +0,0 @@
1
- import {
2
- KEY_DISPLAY_SYMBOLS,
3
- LINUX_MODIFIER_LABELS,
4
- MAC_MODIFIER_LABELS,
5
- MAC_MODIFIER_SYMBOLS,
6
- MODIFIER_ALIASES,
7
- MODIFIER_ORDER,
8
- PUNCTUATION_KEY_DISPLAY_LABELS,
9
- WINDOWS_MODIFIER_LABELS,
10
- detectPlatform,
11
- } from './constants'
12
- import {
13
- isModifierKey,
14
- normalizeHotkeyFromParsed,
15
- normalizeRegisterableHotkey,
16
- } from './parse'
17
- import type {
18
- CanonicalModifier,
19
- FormatDisplayOptions,
20
- Hotkey,
21
- ParsedHotkey,
22
- RegisterableHotkey,
23
- } from './hotkey'
24
-
25
- /**
26
- * Converts a hotkey sequence array to a display string.
27
- *
28
- * @param sequence - Array of hotkey strings that form the sequence
29
- * @returns A space-separated string (e.g. ['G','G'] → 'G G')
30
- *
31
- * @example
32
- * ```ts
33
- * formatHotkeySequence(['G', 'G']) // 'G G'
34
- * formatHotkeySequence(['D', 'I', 'W']) // 'D I W'
35
- * ```
36
- */
37
- export function formatHotkeySequence(sequence: Array<Hotkey>): string {
38
- return sequence.join(' ')
39
- }
40
-
41
- /**
42
- * Converts a ParsedHotkey back to a hotkey string.
43
- *
44
- * @param parsed - The parsed hotkey object
45
- * @returns A hotkey string in canonical form
46
- *
47
- * @example
48
- * ```ts
49
- * formatHotkey({ key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] })
50
- * // Returns: 'Control+Shift+S'
51
- * ```
52
- */
53
- export function formatHotkey(parsed: ParsedHotkey): string {
54
- const parts: Array<string> = []
55
-
56
- // Add modifiers in canonical order
57
- for (const modifier of MODIFIER_ORDER) {
58
- if (parsed.modifiers.includes(modifier)) {
59
- parts.push(modifier)
60
- }
61
- }
62
-
63
- // Add the key
64
- parts.push(parsed.key)
65
-
66
- return parts.join('+')
67
- }
68
-
69
- /**
70
- * Formats a hotkey for display in a user interface.
71
- *
72
- * On macOS, uses symbols (⌘⇧S) in the same modifier order as {@link normalizeHotkeyFromParsed}.
73
- * On Windows/Linux, uses text (Ctrl+Shift+S) with `+` separators.
74
- * The separator can be customized with `separatorToken`.
75
- *
76
- * @param hotkey - The hotkey string or ParsedHotkey to format
77
- * @param options - Formatting options
78
- * @returns A formatted string suitable for display
79
- *
80
- * @example
81
- * ```ts
82
- * formatForDisplay('Mod+Shift+S', { platform: 'mac' })
83
- * // Returns: '⌘ ⇧ S' (symbols separated by spaces on macOS)
84
- *
85
- * formatForDisplay('Mod+Shift+S', { platform: 'windows' })
86
- * // Returns: 'Ctrl+Shift+S'
87
- *
88
- * formatForDisplay('Escape')
89
- * // Returns: 'Esc' (on all platforms)
90
- * ```
91
- */
92
- export function formatForDisplay(
93
- hotkey: RegisterableHotkey | (string & {}),
94
- options: FormatDisplayOptions = {},
95
- ): string {
96
- const platform = options.platform ?? detectPlatform()
97
- const useSymbols = options.useSymbols ?? true
98
- const separatorToken =
99
- options.separatorToken ?? (platform === 'mac' && useSymbols ? ' ' : '+')
100
- const normalizedHotkey = normalizeRegisterableHotkey(
101
- hotkey as RegisterableHotkey,
102
- platform,
103
- )
104
- return normalizedHotkey
105
- .split('+')
106
- .map((segment) => {
107
- if (isModifierKey(segment)) {
108
- const modifierToken = (MODIFIER_ALIASES[segment] ??
109
- MODIFIER_ALIASES[segment.toLowerCase()]) as CanonicalModifier | 'Mod'
110
- return platform === 'mac'
111
- ? useSymbols
112
- ? MAC_MODIFIER_SYMBOLS[modifierToken]
113
- : MAC_MODIFIER_LABELS[modifierToken]
114
- : platform === 'windows'
115
- ? WINDOWS_MODIFIER_LABELS[modifierToken]
116
- : LINUX_MODIFIER_LABELS[modifierToken]
117
- } else {
118
- const keyDisplaySymbol =
119
- useSymbols &&
120
- KEY_DISPLAY_SYMBOLS[segment as keyof typeof KEY_DISPLAY_SYMBOLS]
121
- if (keyDisplaySymbol) return keyDisplaySymbol
122
-
123
- const punctuationKeyDisplayLabel =
124
- !useSymbols &&
125
- PUNCTUATION_KEY_DISPLAY_LABELS[
126
- segment as keyof typeof PUNCTUATION_KEY_DISPLAY_LABELS
127
- ]
128
- return punctuationKeyDisplayLabel || segment
129
- }
130
- })
131
- .join(separatorToken)
132
- }
133
-
134
- /**
135
- * @deprecated Use {@link formatForDisplay} instead with `useSymbols: false` option.
136
- */
137
- export function formatWithLabels(
138
- hotkey: RegisterableHotkey,
139
- options: Omit<FormatDisplayOptions, 'useSymbols'> = {},
140
- ): string {
141
- return formatForDisplay(hotkey, { ...options, useSymbols: false })
142
- }