@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.
- package/LICENSE +21 -0
- package/README.md +121 -45
- package/dist/constants.cjs +444 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +226 -0
- package/dist/constants.d.ts +226 -0
- package/dist/constants.js +428 -0
- package/dist/constants.js.map +1 -0
- package/dist/format.cjs +178 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +110 -0
- package/dist/format.d.ts +110 -0
- package/dist/format.js +175 -0
- package/dist/format.js.map +1 -0
- package/dist/hotkey-manager.cjs +420 -0
- package/dist/hotkey-manager.cjs.map +1 -0
- package/dist/hotkey-manager.d.cts +207 -0
- package/dist/hotkey-manager.d.ts +207 -0
- package/dist/hotkey-manager.js +419 -0
- package/dist/hotkey-manager.js.map +1 -0
- package/dist/hotkey.d.cts +278 -0
- package/dist/hotkey.d.ts +278 -0
- package/dist/index.cjs +54 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/key-state-tracker.cjs +197 -0
- package/dist/key-state-tracker.cjs.map +1 -0
- package/dist/key-state-tracker.d.cts +107 -0
- package/dist/key-state-tracker.d.ts +107 -0
- package/dist/key-state-tracker.js +196 -0
- package/dist/key-state-tracker.js.map +1 -0
- package/dist/match.cjs +143 -0
- package/dist/match.cjs.map +1 -0
- package/dist/match.d.cts +79 -0
- package/dist/match.d.ts +79 -0
- package/dist/match.js +141 -0
- package/dist/match.js.map +1 -0
- package/dist/parse.cjs +266 -0
- package/dist/parse.cjs.map +1 -0
- package/dist/parse.d.cts +169 -0
- package/dist/parse.d.ts +169 -0
- package/dist/parse.js +258 -0
- package/dist/parse.js.map +1 -0
- package/dist/recorder.cjs +177 -0
- package/dist/recorder.cjs.map +1 -0
- package/dist/recorder.d.cts +108 -0
- package/dist/recorder.d.ts +108 -0
- package/dist/recorder.js +177 -0
- package/dist/recorder.js.map +1 -0
- package/dist/sequence.cjs +242 -0
- package/dist/sequence.cjs.map +1 -0
- package/dist/sequence.d.cts +109 -0
- package/dist/sequence.d.ts +109 -0
- package/dist/sequence.js +240 -0
- package/dist/sequence.js.map +1 -0
- package/dist/validate.cjs +116 -0
- package/dist/validate.cjs.map +1 -0
- package/dist/validate.d.cts +56 -0
- package/dist/validate.d.ts +56 -0
- package/dist/validate.js +114 -0
- package/dist/validate.js.map +1 -0
- package/package.json +55 -7
- package/src/constants.ts +514 -0
- package/src/format.ts +261 -0
- package/src/hotkey-manager.ts +822 -0
- package/src/hotkey.ts +411 -0
- package/src/index.ts +10 -0
- package/src/key-state-tracker.ts +249 -0
- package/src/match.ts +222 -0
- package/src/parse.ts +368 -0
- package/src/recorder.ts +266 -0
- package/src/sequence.ts +391 -0
- package/src/validate.ts +171 -0
package/src/validate.ts
ADDED
|
@@ -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
|
+
}
|