@tanstack/hotkeys 0.0.1 → 0.1.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 (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +121 -45
  3. package/dist/constants.cjs +444 -0
  4. package/dist/constants.cjs.map +1 -0
  5. package/dist/constants.d.cts +226 -0
  6. package/dist/constants.d.ts +226 -0
  7. package/dist/constants.js +428 -0
  8. package/dist/constants.js.map +1 -0
  9. package/dist/format.cjs +178 -0
  10. package/dist/format.cjs.map +1 -0
  11. package/dist/format.d.cts +110 -0
  12. package/dist/format.d.ts +110 -0
  13. package/dist/format.js +175 -0
  14. package/dist/format.js.map +1 -0
  15. package/dist/hotkey-manager.cjs +420 -0
  16. package/dist/hotkey-manager.cjs.map +1 -0
  17. package/dist/hotkey-manager.d.cts +207 -0
  18. package/dist/hotkey-manager.d.ts +207 -0
  19. package/dist/hotkey-manager.js +419 -0
  20. package/dist/hotkey-manager.js.map +1 -0
  21. package/dist/hotkey.d.cts +278 -0
  22. package/dist/hotkey.d.ts +278 -0
  23. package/dist/index.cjs +54 -0
  24. package/dist/index.d.cts +11 -0
  25. package/dist/index.d.ts +11 -0
  26. package/dist/index.js +11 -0
  27. package/dist/key-state-tracker.cjs +197 -0
  28. package/dist/key-state-tracker.cjs.map +1 -0
  29. package/dist/key-state-tracker.d.cts +107 -0
  30. package/dist/key-state-tracker.d.ts +107 -0
  31. package/dist/key-state-tracker.js +196 -0
  32. package/dist/key-state-tracker.js.map +1 -0
  33. package/dist/match.cjs +143 -0
  34. package/dist/match.cjs.map +1 -0
  35. package/dist/match.d.cts +79 -0
  36. package/dist/match.d.ts +79 -0
  37. package/dist/match.js +141 -0
  38. package/dist/match.js.map +1 -0
  39. package/dist/parse.cjs +266 -0
  40. package/dist/parse.cjs.map +1 -0
  41. package/dist/parse.d.cts +169 -0
  42. package/dist/parse.d.ts +169 -0
  43. package/dist/parse.js +258 -0
  44. package/dist/parse.js.map +1 -0
  45. package/dist/recorder.cjs +177 -0
  46. package/dist/recorder.cjs.map +1 -0
  47. package/dist/recorder.d.cts +108 -0
  48. package/dist/recorder.d.ts +108 -0
  49. package/dist/recorder.js +177 -0
  50. package/dist/recorder.js.map +1 -0
  51. package/dist/sequence.cjs +242 -0
  52. package/dist/sequence.cjs.map +1 -0
  53. package/dist/sequence.d.cts +109 -0
  54. package/dist/sequence.d.ts +109 -0
  55. package/dist/sequence.js +240 -0
  56. package/dist/sequence.js.map +1 -0
  57. package/dist/validate.cjs +116 -0
  58. package/dist/validate.cjs.map +1 -0
  59. package/dist/validate.d.cts +56 -0
  60. package/dist/validate.d.ts +56 -0
  61. package/dist/validate.js +114 -0
  62. package/dist/validate.js.map +1 -0
  63. package/package.json +55 -7
  64. package/src/constants.ts +514 -0
  65. package/src/format.ts +261 -0
  66. package/src/hotkey-manager.ts +822 -0
  67. package/src/hotkey.ts +411 -0
  68. package/src/index.ts +10 -0
  69. package/src/key-state-tracker.ts +249 -0
  70. package/src/match.ts +222 -0
  71. package/src/parse.ts +368 -0
  72. package/src/recorder.ts +266 -0
  73. package/src/sequence.ts +391 -0
  74. package/src/validate.ts +171 -0
@@ -0,0 +1,171 @@
1
+ import { ALL_KEYS, MODIFIER_ALIASES } from './constants'
2
+ import type { Hotkey, ValidationResult } from './hotkey'
3
+
4
+ /**
5
+ * Validates a hotkey string and returns any warnings or errors.
6
+ *
7
+ * Checks for:
8
+ * - Valid syntax (modifier+...+key format)
9
+ * - Known modifiers
10
+ * - Known keys
11
+ *
12
+ * @param hotkey - The hotkey string to validate
13
+ * @returns A ValidationResult with validity status, warnings, and errors
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * validateHotkey('Mod+S')
18
+ * // { valid: true, warnings: [], errors: [] }
19
+ *
20
+ * validateHotkey('')
21
+ * // { valid: false, warnings: [], errors: ['Hotkey cannot be empty'] }
22
+ * ```
23
+ */
24
+ export function validateHotkey(
25
+ hotkey: Hotkey | (string & {}),
26
+ ): ValidationResult {
27
+ const warnings: Array<string> = []
28
+ const errors: Array<string> = []
29
+
30
+ // Check for empty string
31
+ if (!hotkey || hotkey.trim() === '') {
32
+ return {
33
+ valid: false,
34
+ warnings: [],
35
+ errors: ['Hotkey cannot be empty'],
36
+ }
37
+ }
38
+
39
+ const parts = hotkey.split('+').map((p) => p.trim())
40
+
41
+ // Must have at least one part (the key)
42
+ if (parts.length === 0 || parts.some((p) => p === '')) {
43
+ return {
44
+ valid: false,
45
+ warnings: [],
46
+ errors: ['Invalid hotkey format: empty parts detected'],
47
+ }
48
+ }
49
+
50
+ // Validate modifiers (all parts except the last)
51
+ const modifierParts = parts.slice(0, -1)
52
+ const keyPart = parts[parts.length - 1]!
53
+
54
+ // Check for unknown modifiers
55
+ for (const modifier of modifierParts) {
56
+ const normalized =
57
+ MODIFIER_ALIASES[modifier] ?? MODIFIER_ALIASES[modifier.toLowerCase()]
58
+ if (!normalized) {
59
+ errors.push(`Unknown modifier: '${modifier}'`)
60
+ }
61
+ }
62
+
63
+ // Check if key is known
64
+ const normalizedKey = normalizeKeyForValidation(keyPart)
65
+ if (!isKnownKey(normalizedKey) && !isKnownKey(keyPart)) {
66
+ warnings.push(
67
+ `Unknown key: '${keyPart}'. This may still work but won't have type-safe autocomplete.`,
68
+ )
69
+ }
70
+
71
+ return {
72
+ valid: errors.length === 0,
73
+ warnings,
74
+ errors,
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Normalizes a key for validation checking.
80
+ */
81
+ function normalizeKeyForValidation(key: string): string {
82
+ // Single letter to uppercase
83
+ if (key.length === 1 && /^[a-zA-Z]$/.test(key)) {
84
+ return key.toUpperCase()
85
+ }
86
+
87
+ // Function keys to uppercase
88
+ if (/^f([1-9]|1[0-2])$/i.test(key)) {
89
+ return key.toUpperCase()
90
+ }
91
+
92
+ return key
93
+ }
94
+
95
+ /**
96
+ * Checks if a key is in the known keys set.
97
+ */
98
+ function isKnownKey(key: string): boolean {
99
+ // Check direct match
100
+ if (ALL_KEYS.has(key as any)) {
101
+ return true
102
+ }
103
+
104
+ // Check uppercase version for letters
105
+ if (key.length === 1 && ALL_KEYS.has(key.toUpperCase() as any)) {
106
+ return true
107
+ }
108
+
109
+ // Check common aliases
110
+ const aliases: Record<string, boolean> = {
111
+ Esc: true,
112
+ Return: true,
113
+ Space: true,
114
+ ' ': true,
115
+ Del: true,
116
+ Up: true,
117
+ Down: true,
118
+ Left: true,
119
+ Right: true,
120
+ }
121
+
122
+ return key in aliases
123
+ }
124
+
125
+ /**
126
+ * Validates a hotkey and throws an error if invalid.
127
+ * Useful for development-time validation.
128
+ *
129
+ * @param hotkey - The hotkey string to validate
130
+ * @throws Error if the hotkey is invalid
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * assertValidHotkey('Mod+S') // OK
135
+ * assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty
136
+ * ```
137
+ */
138
+ export function assertValidHotkey(hotkey: Hotkey | (string & {})): void {
139
+ const result = validateHotkey(hotkey)
140
+ if (!result.valid) {
141
+ throw new Error(`Invalid hotkey '${hotkey}': ${result.errors.join(', ')}`)
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Validates a hotkey and logs warnings to the console.
147
+ * Useful for development-time feedback.
148
+ *
149
+ * @param hotkey - The hotkey string to validate
150
+ * @returns True if the hotkey is valid (may still have warnings)
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * checkHotkey('Alt+C')
155
+ * // Console: Warning: Alt+C may not work reliably on macOS...
156
+ * // Returns: true
157
+ * ```
158
+ */
159
+ export function checkHotkey(hotkey: Hotkey | (string & {})): boolean {
160
+ const result = validateHotkey(hotkey)
161
+
162
+ if (result.errors.length > 0) {
163
+ console.error(`Hotkey '${hotkey}' errors:`, result.errors.join('; '))
164
+ }
165
+
166
+ if (result.warnings.length > 0) {
167
+ console.warn(`Hotkey '${hotkey}' warnings:`, result.warnings.join('; '))
168
+ }
169
+
170
+ return result.valid
171
+ }