@tanstack/hotkeys 0.0.1 → 0.0.2
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 +189 -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 +401 -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 +400 -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 +798 -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/dist/validate.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ALL_KEYS, MODIFIER_ALIASES } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
//#region src/validate.ts
|
|
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
|
+
function validateHotkey(hotkey) {
|
|
25
|
+
const warnings = [];
|
|
26
|
+
const errors = [];
|
|
27
|
+
if (!hotkey || hotkey.trim() === "") return {
|
|
28
|
+
valid: false,
|
|
29
|
+
warnings: [],
|
|
30
|
+
errors: ["Hotkey cannot be empty"]
|
|
31
|
+
};
|
|
32
|
+
const parts = hotkey.split("+").map((p) => p.trim());
|
|
33
|
+
if (parts.length === 0 || parts.some((p) => p === "")) return {
|
|
34
|
+
valid: false,
|
|
35
|
+
warnings: [],
|
|
36
|
+
errors: ["Invalid hotkey format: empty parts detected"]
|
|
37
|
+
};
|
|
38
|
+
const modifierParts = parts.slice(0, -1);
|
|
39
|
+
const keyPart = parts[parts.length - 1];
|
|
40
|
+
for (const modifier of modifierParts) if (!(MODIFIER_ALIASES[modifier] ?? MODIFIER_ALIASES[modifier.toLowerCase()])) errors.push(`Unknown modifier: '${modifier}'`);
|
|
41
|
+
if (!isKnownKey(normalizeKeyForValidation(keyPart)) && !isKnownKey(keyPart)) warnings.push(`Unknown key: '${keyPart}'. This may still work but won't have type-safe autocomplete.`);
|
|
42
|
+
return {
|
|
43
|
+
valid: errors.length === 0,
|
|
44
|
+
warnings,
|
|
45
|
+
errors
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Normalizes a key for validation checking.
|
|
50
|
+
*/
|
|
51
|
+
function normalizeKeyForValidation(key) {
|
|
52
|
+
if (key.length === 1 && /^[a-zA-Z]$/.test(key)) return key.toUpperCase();
|
|
53
|
+
if (/^f([1-9]|1[0-2])$/i.test(key)) return key.toUpperCase();
|
|
54
|
+
return key;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Checks if a key is in the known keys set.
|
|
58
|
+
*/
|
|
59
|
+
function isKnownKey(key) {
|
|
60
|
+
if (ALL_KEYS.has(key)) return true;
|
|
61
|
+
if (key.length === 1 && ALL_KEYS.has(key.toUpperCase())) return true;
|
|
62
|
+
return key in {
|
|
63
|
+
Esc: true,
|
|
64
|
+
Return: true,
|
|
65
|
+
Space: true,
|
|
66
|
+
" ": true,
|
|
67
|
+
Del: true,
|
|
68
|
+
Up: true,
|
|
69
|
+
Down: true,
|
|
70
|
+
Left: true,
|
|
71
|
+
Right: true
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Validates a hotkey and throws an error if invalid.
|
|
76
|
+
* Useful for development-time validation.
|
|
77
|
+
*
|
|
78
|
+
* @param hotkey - The hotkey string to validate
|
|
79
|
+
* @throws Error if the hotkey is invalid
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* assertValidHotkey('Mod+S') // OK
|
|
84
|
+
* assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function assertValidHotkey(hotkey) {
|
|
88
|
+
const result = validateHotkey(hotkey);
|
|
89
|
+
if (!result.valid) throw new Error(`Invalid hotkey '${hotkey}': ${result.errors.join(", ")}`);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Validates a hotkey and logs warnings to the console.
|
|
93
|
+
* Useful for development-time feedback.
|
|
94
|
+
*
|
|
95
|
+
* @param hotkey - The hotkey string to validate
|
|
96
|
+
* @returns True if the hotkey is valid (may still have warnings)
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* checkHotkey('Alt+C')
|
|
101
|
+
* // Console: Warning: Alt+C may not work reliably on macOS...
|
|
102
|
+
* // Returns: true
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
function checkHotkey(hotkey) {
|
|
106
|
+
const result = validateHotkey(hotkey);
|
|
107
|
+
if (result.errors.length > 0) console.error(`Hotkey '${hotkey}' errors:`, result.errors.join("; "));
|
|
108
|
+
if (result.warnings.length > 0) console.warn(`Hotkey '${hotkey}' warnings:`, result.warnings.join("; "));
|
|
109
|
+
return result.valid;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
export { assertValidHotkey, checkHotkey, validateHotkey };
|
|
114
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","names":[],"sources":["../src/validate.ts"],"sourcesContent":["import { ALL_KEYS, MODIFIER_ALIASES } from './constants'\nimport type { Hotkey, ValidationResult } from './hotkey'\n\n/**\n * Validates a hotkey string and returns any warnings or errors.\n *\n * Checks for:\n * - Valid syntax (modifier+...+key format)\n * - Known modifiers\n * - Known keys\n *\n * @param hotkey - The hotkey string to validate\n * @returns A ValidationResult with validity status, warnings, and errors\n *\n * @example\n * ```ts\n * validateHotkey('Mod+S')\n * // { valid: true, warnings: [], errors: [] }\n *\n * validateHotkey('')\n * // { valid: false, warnings: [], errors: ['Hotkey cannot be empty'] }\n * ```\n */\nexport function validateHotkey(\n hotkey: Hotkey | (string & {}),\n): ValidationResult {\n const warnings: Array<string> = []\n const errors: Array<string> = []\n\n // Check for empty string\n if (!hotkey || hotkey.trim() === '') {\n return {\n valid: false,\n warnings: [],\n errors: ['Hotkey cannot be empty'],\n }\n }\n\n const parts = hotkey.split('+').map((p) => p.trim())\n\n // Must have at least one part (the key)\n if (parts.length === 0 || parts.some((p) => p === '')) {\n return {\n valid: false,\n warnings: [],\n errors: ['Invalid hotkey format: empty parts detected'],\n }\n }\n\n // Validate modifiers (all parts except the last)\n const modifierParts = parts.slice(0, -1)\n const keyPart = parts[parts.length - 1]!\n\n // Check for unknown modifiers\n for (const modifier of modifierParts) {\n const normalized =\n MODIFIER_ALIASES[modifier] ?? MODIFIER_ALIASES[modifier.toLowerCase()]\n if (!normalized) {\n errors.push(`Unknown modifier: '${modifier}'`)\n }\n }\n\n // Check if key is known\n const normalizedKey = normalizeKeyForValidation(keyPart)\n if (!isKnownKey(normalizedKey) && !isKnownKey(keyPart)) {\n warnings.push(\n `Unknown key: '${keyPart}'. This may still work but won't have type-safe autocomplete.`,\n )\n }\n\n return {\n valid: errors.length === 0,\n warnings,\n errors,\n }\n}\n\n/**\n * Normalizes a key for validation checking.\n */\nfunction normalizeKeyForValidation(key: string): string {\n // Single letter to uppercase\n if (key.length === 1 && /^[a-zA-Z]$/.test(key)) {\n return key.toUpperCase()\n }\n\n // Function keys to uppercase\n if (/^f([1-9]|1[0-2])$/i.test(key)) {\n return key.toUpperCase()\n }\n\n return key\n}\n\n/**\n * Checks if a key is in the known keys set.\n */\nfunction isKnownKey(key: string): boolean {\n // Check direct match\n if (ALL_KEYS.has(key as any)) {\n return true\n }\n\n // Check uppercase version for letters\n if (key.length === 1 && ALL_KEYS.has(key.toUpperCase() as any)) {\n return true\n }\n\n // Check common aliases\n const aliases: Record<string, boolean> = {\n Esc: true,\n Return: true,\n Space: true,\n ' ': true,\n Del: true,\n Up: true,\n Down: true,\n Left: true,\n Right: true,\n }\n\n return key in aliases\n}\n\n/**\n * Validates a hotkey and throws an error if invalid.\n * Useful for development-time validation.\n *\n * @param hotkey - The hotkey string to validate\n * @throws Error if the hotkey is invalid\n *\n * @example\n * ```ts\n * assertValidHotkey('Mod+S') // OK\n * assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty\n * ```\n */\nexport function assertValidHotkey(hotkey: Hotkey | (string & {})): void {\n const result = validateHotkey(hotkey)\n if (!result.valid) {\n throw new Error(`Invalid hotkey '${hotkey}': ${result.errors.join(', ')}`)\n }\n}\n\n/**\n * Validates a hotkey and logs warnings to the console.\n * Useful for development-time feedback.\n *\n * @param hotkey - The hotkey string to validate\n * @returns True if the hotkey is valid (may still have warnings)\n *\n * @example\n * ```ts\n * checkHotkey('Alt+C')\n * // Console: Warning: Alt+C may not work reliably on macOS...\n * // Returns: true\n * ```\n */\nexport function checkHotkey(hotkey: Hotkey | (string & {})): boolean {\n const result = validateHotkey(hotkey)\n\n if (result.errors.length > 0) {\n console.error(`Hotkey '${hotkey}' errors:`, result.errors.join('; '))\n }\n\n if (result.warnings.length > 0) {\n console.warn(`Hotkey '${hotkey}' warnings:`, result.warnings.join('; '))\n }\n\n return result.valid\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,eACd,QACkB;CAClB,MAAM,WAA0B,EAAE;CAClC,MAAM,SAAwB,EAAE;AAGhC,KAAI,CAAC,UAAU,OAAO,MAAM,KAAK,GAC/B,QAAO;EACL,OAAO;EACP,UAAU,EAAE;EACZ,QAAQ,CAAC,yBAAyB;EACnC;CAGH,MAAM,QAAQ,OAAO,MAAM,IAAI,CAAC,KAAK,MAAM,EAAE,MAAM,CAAC;AAGpD,KAAI,MAAM,WAAW,KAAK,MAAM,MAAM,MAAM,MAAM,GAAG,CACnD,QAAO;EACL,OAAO;EACP,UAAU,EAAE;EACZ,QAAQ,CAAC,8CAA8C;EACxD;CAIH,MAAM,gBAAgB,MAAM,MAAM,GAAG,GAAG;CACxC,MAAM,UAAU,MAAM,MAAM,SAAS;AAGrC,MAAK,MAAM,YAAY,cAGrB,KAAI,EADF,iBAAiB,aAAa,iBAAiB,SAAS,aAAa,GAErE,QAAO,KAAK,sBAAsB,SAAS,GAAG;AAMlD,KAAI,CAAC,WADiB,0BAA0B,QAAQ,CAC1B,IAAI,CAAC,WAAW,QAAQ,CACpD,UAAS,KACP,iBAAiB,QAAQ,+DAC1B;AAGH,QAAO;EACL,OAAO,OAAO,WAAW;EACzB;EACA;EACD;;;;;AAMH,SAAS,0BAA0B,KAAqB;AAEtD,KAAI,IAAI,WAAW,KAAK,aAAa,KAAK,IAAI,CAC5C,QAAO,IAAI,aAAa;AAI1B,KAAI,qBAAqB,KAAK,IAAI,CAChC,QAAO,IAAI,aAAa;AAG1B,QAAO;;;;;AAMT,SAAS,WAAW,KAAsB;AAExC,KAAI,SAAS,IAAI,IAAW,CAC1B,QAAO;AAIT,KAAI,IAAI,WAAW,KAAK,SAAS,IAAI,IAAI,aAAa,CAAQ,CAC5D,QAAO;AAgBT,QAAO,OAZkC;EACvC,KAAK;EACL,QAAQ;EACR,OAAO;EACP,KAAK;EACL,KAAK;EACL,IAAI;EACJ,MAAM;EACN,MAAM;EACN,OAAO;EACR;;;;;;;;;;;;;;;AAkBH,SAAgB,kBAAkB,QAAsC;CACtE,MAAM,SAAS,eAAe,OAAO;AACrC,KAAI,CAAC,OAAO,MACV,OAAM,IAAI,MAAM,mBAAmB,OAAO,KAAK,OAAO,OAAO,KAAK,KAAK,GAAG;;;;;;;;;;;;;;;;AAkB9E,SAAgB,YAAY,QAAyC;CACnE,MAAM,SAAS,eAAe,OAAO;AAErC,KAAI,OAAO,OAAO,SAAS,EACzB,SAAQ,MAAM,WAAW,OAAO,YAAY,OAAO,OAAO,KAAK,KAAK,CAAC;AAGvE,KAAI,OAAO,SAAS,SAAS,EAC3B,SAAQ,KAAK,WAAW,OAAO,cAAc,OAAO,SAAS,KAAK,KAAK,CAAC;AAG1E,QAAO,OAAO"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/hotkeys",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Type-safe, framework-agnostic keyboard hotkey management for the browser",
|
|
5
|
+
"author": "Tanner Linsley",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/TanStack/hotkeys.git",
|
|
10
|
+
"directory": "packages/hotkeys"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://tanstack.com/hotkeys",
|
|
13
|
+
"funding": {
|
|
14
|
+
"type": "github",
|
|
15
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
16
|
+
},
|
|
5
17
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
"tanstack",
|
|
19
|
+
"keys",
|
|
20
|
+
"hotkeys",
|
|
21
|
+
"keyboard",
|
|
22
|
+
"shortcuts",
|
|
23
|
+
"keybindings",
|
|
24
|
+
"typescript"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./dist/index.cjs",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.cts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist/",
|
|
43
|
+
"src"
|
|
44
|
+
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@tanstack/store": "^0.8.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"clean": "premove ./build ./dist",
|
|
50
|
+
"lint": "eslint ./src",
|
|
51
|
+
"lint:fix": "eslint ./src --fix",
|
|
52
|
+
"test:eslint": "eslint ./src",
|
|
53
|
+
"test:lib": "vitest",
|
|
54
|
+
"test:lib:dev": "pnpm test:lib --watch",
|
|
55
|
+
"test:types": "tsc",
|
|
56
|
+
"build": "tsdown"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CanonicalModifier,
|
|
3
|
+
EditingKey,
|
|
4
|
+
FunctionKey,
|
|
5
|
+
LetterKey,
|
|
6
|
+
NavigationKey,
|
|
7
|
+
NumberKey,
|
|
8
|
+
PunctuationKey,
|
|
9
|
+
} from './hotkey'
|
|
10
|
+
|
|
11
|
+
// =============================================================================
|
|
12
|
+
// Platform Detection
|
|
13
|
+
// =============================================================================
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Detects the current platform based on browser navigator properties.
|
|
17
|
+
*
|
|
18
|
+
* Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
|
|
19
|
+
* Control elsewhere) and for platform-specific hotkey formatting.
|
|
20
|
+
*
|
|
21
|
+
* @returns The detected platform: 'mac', 'windows', or 'linux'
|
|
22
|
+
* @remarks Defaults to 'linux' in SSR environments where navigator is undefined
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
|
|
27
|
+
* const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function detectPlatform(): 'mac' | 'windows' | 'linux' {
|
|
31
|
+
if (typeof navigator === 'undefined') {
|
|
32
|
+
return 'linux' // Default for SSR
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const platform = navigator.platform.toLowerCase()
|
|
36
|
+
const userAgent = navigator.userAgent.toLowerCase()
|
|
37
|
+
|
|
38
|
+
if (platform.includes('mac') || userAgent.includes('mac')) {
|
|
39
|
+
return 'mac'
|
|
40
|
+
}
|
|
41
|
+
if (platform.includes('win') || userAgent.includes('win')) {
|
|
42
|
+
return 'windows'
|
|
43
|
+
}
|
|
44
|
+
return 'linux'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// =============================================================================
|
|
48
|
+
// Modifier Aliases
|
|
49
|
+
// =============================================================================
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Canonical order for modifiers in normalized hotkey strings.
|
|
53
|
+
*
|
|
54
|
+
* Defines the standard order in which modifiers should appear when formatting
|
|
55
|
+
* hotkeys. This ensures consistent, predictable output across the library.
|
|
56
|
+
*
|
|
57
|
+
* Order: Control → Alt → Shift → Meta
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // Input: 'Shift+Control+Meta+S'
|
|
62
|
+
* // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export const MODIFIER_ORDER: Array<CanonicalModifier> = [
|
|
66
|
+
'Control',
|
|
67
|
+
'Alt',
|
|
68
|
+
'Shift',
|
|
69
|
+
'Meta',
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Set of canonical modifier key names for fast lookup.
|
|
74
|
+
*
|
|
75
|
+
* Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when
|
|
76
|
+
* a modifier is released so we can clear non-modifier keys whose keyup events
|
|
77
|
+
* may have been swallowed by the OS (e.g. macOS Cmd+key combos).
|
|
78
|
+
*/
|
|
79
|
+
export const MODIFIER_KEYS = new Set<string>(MODIFIER_ORDER)
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.
|
|
83
|
+
*
|
|
84
|
+
* This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')
|
|
85
|
+
* which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the
|
|
86
|
+
* platform-adaptive 'Mod' token.
|
|
87
|
+
*
|
|
88
|
+
* The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`
|
|
89
|
+
* based on the detected platform (Command on Mac, Control elsewhere).
|
|
90
|
+
*
|
|
91
|
+
* @remarks Case-insensitive lookups are supported via lowercase variants
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* MODIFIER_ALIASES['Ctrl'] // 'Control'
|
|
96
|
+
* MODIFIER_ALIASES['Cmd'] // 'Meta'
|
|
97
|
+
* MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export const MODIFIER_ALIASES: Record<string, CanonicalModifier | 'Mod'> = {
|
|
101
|
+
// Control variants
|
|
102
|
+
Control: 'Control',
|
|
103
|
+
Ctrl: 'Control',
|
|
104
|
+
control: 'Control',
|
|
105
|
+
ctrl: 'Control',
|
|
106
|
+
|
|
107
|
+
// Shift variants
|
|
108
|
+
Shift: 'Shift',
|
|
109
|
+
shift: 'Shift',
|
|
110
|
+
|
|
111
|
+
// Alt variants
|
|
112
|
+
Alt: 'Alt',
|
|
113
|
+
Option: 'Alt',
|
|
114
|
+
alt: 'Alt',
|
|
115
|
+
option: 'Alt',
|
|
116
|
+
|
|
117
|
+
// Meta/Command variants
|
|
118
|
+
Command: 'Meta',
|
|
119
|
+
Cmd: 'Meta',
|
|
120
|
+
Meta: 'Meta',
|
|
121
|
+
command: 'Meta',
|
|
122
|
+
cmd: 'Meta',
|
|
123
|
+
meta: 'Meta',
|
|
124
|
+
|
|
125
|
+
// Platform-adaptive (resolved at runtime)
|
|
126
|
+
CommandOrControl: 'Mod',
|
|
127
|
+
Mod: 'Mod',
|
|
128
|
+
commandorcontrol: 'Mod',
|
|
129
|
+
mod: 'Mod',
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
|
|
134
|
+
*
|
|
135
|
+
* The 'Mod' token represents the "primary modifier" on each platform:
|
|
136
|
+
* - macOS: 'Meta' (Command key ⌘)
|
|
137
|
+
* - Windows/Linux: 'Control' (Ctrl key)
|
|
138
|
+
*
|
|
139
|
+
* This enables cross-platform hotkey definitions like 'Mod+S' that automatically
|
|
140
|
+
* map to Command+S on Mac and Ctrl+S on Windows/Linux.
|
|
141
|
+
*
|
|
142
|
+
* @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
|
|
143
|
+
* @param platform - The target platform. Defaults to auto-detection.
|
|
144
|
+
* @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* resolveModifier('Mod', 'mac') // 'Meta'
|
|
149
|
+
* resolveModifier('Mod', 'windows') // 'Control'
|
|
150
|
+
* resolveModifier('Control', 'mac') // 'Control' (unchanged)
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export function resolveModifier(
|
|
154
|
+
modifier: CanonicalModifier | 'Mod',
|
|
155
|
+
platform: 'mac' | 'windows' | 'linux' = detectPlatform(),
|
|
156
|
+
): CanonicalModifier {
|
|
157
|
+
if (modifier === 'Mod') {
|
|
158
|
+
return platform === 'mac' ? 'Meta' : 'Control'
|
|
159
|
+
}
|
|
160
|
+
return modifier
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// =============================================================================
|
|
164
|
+
// Valid Keys
|
|
165
|
+
// =============================================================================
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Set of all valid letter keys (A-Z).
|
|
169
|
+
*
|
|
170
|
+
* Used for validation and type checking. Letter keys are matched case-insensitively
|
|
171
|
+
* in hotkey matching, but normalized to uppercase in canonical form.
|
|
172
|
+
*/
|
|
173
|
+
export const LETTER_KEYS = new Set<LetterKey>([
|
|
174
|
+
'A',
|
|
175
|
+
'B',
|
|
176
|
+
'C',
|
|
177
|
+
'D',
|
|
178
|
+
'E',
|
|
179
|
+
'F',
|
|
180
|
+
'G',
|
|
181
|
+
'H',
|
|
182
|
+
'I',
|
|
183
|
+
'J',
|
|
184
|
+
'K',
|
|
185
|
+
'L',
|
|
186
|
+
'M',
|
|
187
|
+
'N',
|
|
188
|
+
'O',
|
|
189
|
+
'P',
|
|
190
|
+
'Q',
|
|
191
|
+
'R',
|
|
192
|
+
'S',
|
|
193
|
+
'T',
|
|
194
|
+
'U',
|
|
195
|
+
'V',
|
|
196
|
+
'W',
|
|
197
|
+
'X',
|
|
198
|
+
'Y',
|
|
199
|
+
'Z',
|
|
200
|
+
])
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Set of all valid number keys (0-9).
|
|
204
|
+
*
|
|
205
|
+
* Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),
|
|
206
|
+
* so they're excluded from Shift-based hotkey combinations to avoid
|
|
207
|
+
* layout-dependent behavior.
|
|
208
|
+
*/
|
|
209
|
+
export const NUMBER_KEYS = new Set<NumberKey>([
|
|
210
|
+
'0',
|
|
211
|
+
'1',
|
|
212
|
+
'2',
|
|
213
|
+
'3',
|
|
214
|
+
'4',
|
|
215
|
+
'5',
|
|
216
|
+
'6',
|
|
217
|
+
'7',
|
|
218
|
+
'8',
|
|
219
|
+
'9',
|
|
220
|
+
])
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Set of all valid function keys (F1-F12).
|
|
224
|
+
*
|
|
225
|
+
* Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,
|
|
226
|
+
* Alt+F4 to close windows) and application-specific commands.
|
|
227
|
+
*/
|
|
228
|
+
export const FUNCTION_KEYS = new Set<FunctionKey>([
|
|
229
|
+
'F1',
|
|
230
|
+
'F2',
|
|
231
|
+
'F3',
|
|
232
|
+
'F4',
|
|
233
|
+
'F5',
|
|
234
|
+
'F6',
|
|
235
|
+
'F7',
|
|
236
|
+
'F8',
|
|
237
|
+
'F9',
|
|
238
|
+
'F10',
|
|
239
|
+
'F11',
|
|
240
|
+
'F12',
|
|
241
|
+
])
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Set of all valid navigation keys for cursor movement and document navigation.
|
|
245
|
+
*
|
|
246
|
+
* Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).
|
|
247
|
+
* These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or
|
|
248
|
+
* navigation shortcuts (Alt+ArrowLeft for back).
|
|
249
|
+
*/
|
|
250
|
+
export const NAVIGATION_KEYS = new Set<NavigationKey>([
|
|
251
|
+
'ArrowUp',
|
|
252
|
+
'ArrowDown',
|
|
253
|
+
'ArrowLeft',
|
|
254
|
+
'ArrowRight',
|
|
255
|
+
'Home',
|
|
256
|
+
'End',
|
|
257
|
+
'PageUp',
|
|
258
|
+
'PageDown',
|
|
259
|
+
])
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Set of all valid editing and special keys.
|
|
263
|
+
*
|
|
264
|
+
* Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and
|
|
265
|
+
* special actions (Escape, Space). These keys are frequently combined with modifiers
|
|
266
|
+
* for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).
|
|
267
|
+
*/
|
|
268
|
+
export const EDITING_KEYS = new Set<EditingKey>([
|
|
269
|
+
'Enter',
|
|
270
|
+
'Escape',
|
|
271
|
+
'Space',
|
|
272
|
+
'Tab',
|
|
273
|
+
'Backspace',
|
|
274
|
+
'Delete',
|
|
275
|
+
])
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Set of all valid punctuation keys commonly used in keyboard shortcuts.
|
|
279
|
+
*
|
|
280
|
+
* These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,
|
|
281
|
+
* typically US keyboard layout). Common shortcuts include:
|
|
282
|
+
* - `Mod+/` - Toggle comment
|
|
283
|
+
* - `Mod+[` / `Mod+]` - Indent/outdent
|
|
284
|
+
* - `Mod+=` / `Mod+-` - Zoom in/out
|
|
285
|
+
*
|
|
286
|
+
* Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),
|
|
287
|
+
* so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.
|
|
288
|
+
*/
|
|
289
|
+
export const PUNCTUATION_KEYS = new Set<PunctuationKey>([
|
|
290
|
+
'/',
|
|
291
|
+
'[',
|
|
292
|
+
']',
|
|
293
|
+
'\\',
|
|
294
|
+
'=',
|
|
295
|
+
'-',
|
|
296
|
+
',',
|
|
297
|
+
'.',
|
|
298
|
+
'`',
|
|
299
|
+
])
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Set of all valid non-modifier keys.
|
|
303
|
+
*
|
|
304
|
+
* This is the union of all key type sets (letters, numbers, function keys, navigation,
|
|
305
|
+
* editing, and punctuation). Used primarily for validation to check if a key string
|
|
306
|
+
* is recognized and will have type-safe autocomplete support.
|
|
307
|
+
*
|
|
308
|
+
* @see {@link LETTER_KEYS}
|
|
309
|
+
* @see {@link NUMBER_KEYS}
|
|
310
|
+
* @see {@link FUNCTION_KEYS}
|
|
311
|
+
* @see {@link NAVIGATION_KEYS}
|
|
312
|
+
* @see {@link EDITING_KEYS}
|
|
313
|
+
* @see {@link PUNCTUATION_KEYS}
|
|
314
|
+
*/
|
|
315
|
+
export const ALL_KEYS = new Set([
|
|
316
|
+
...LETTER_KEYS,
|
|
317
|
+
...NUMBER_KEYS,
|
|
318
|
+
...FUNCTION_KEYS,
|
|
319
|
+
...NAVIGATION_KEYS,
|
|
320
|
+
...EDITING_KEYS,
|
|
321
|
+
...PUNCTUATION_KEYS,
|
|
322
|
+
])
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Maps key name aliases to their canonical form.
|
|
326
|
+
*
|
|
327
|
+
* Handles common variations and alternative names for keys to provide a more
|
|
328
|
+
* forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and
|
|
329
|
+
* they'll all normalize to 'Escape'.
|
|
330
|
+
*
|
|
331
|
+
* This map is used internally by `normalizeKeyName()` to convert user input
|
|
332
|
+
* into the canonical key names used throughout the library.
|
|
333
|
+
*
|
|
334
|
+
* @remarks Case-insensitive lookups are supported via lowercase variants
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* KEY_ALIASES['Esc'] // 'Escape'
|
|
339
|
+
* KEY_ALIASES['Del'] // 'Delete'
|
|
340
|
+
* KEY_ALIASES['Up'] // 'ArrowUp'
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
const KEY_ALIASES: Record<string, string> = {
|
|
344
|
+
// Escape variants
|
|
345
|
+
Esc: 'Escape',
|
|
346
|
+
esc: 'Escape',
|
|
347
|
+
escape: 'Escape',
|
|
348
|
+
|
|
349
|
+
// Enter variants
|
|
350
|
+
Return: 'Enter',
|
|
351
|
+
return: 'Enter',
|
|
352
|
+
enter: 'Enter',
|
|
353
|
+
|
|
354
|
+
// Space variants
|
|
355
|
+
' ': 'Space',
|
|
356
|
+
space: 'Space',
|
|
357
|
+
Spacebar: 'Space',
|
|
358
|
+
spacebar: 'Space',
|
|
359
|
+
|
|
360
|
+
// Tab variants
|
|
361
|
+
tab: 'Tab',
|
|
362
|
+
|
|
363
|
+
// Backspace variants
|
|
364
|
+
backspace: 'Backspace',
|
|
365
|
+
|
|
366
|
+
// Delete variants
|
|
367
|
+
Del: 'Delete',
|
|
368
|
+
del: 'Delete',
|
|
369
|
+
delete: 'Delete',
|
|
370
|
+
|
|
371
|
+
// Arrow key variants
|
|
372
|
+
Up: 'ArrowUp',
|
|
373
|
+
up: 'ArrowUp',
|
|
374
|
+
arrowup: 'ArrowUp',
|
|
375
|
+
Down: 'ArrowDown',
|
|
376
|
+
down: 'ArrowDown',
|
|
377
|
+
arrowdown: 'ArrowDown',
|
|
378
|
+
Left: 'ArrowLeft',
|
|
379
|
+
left: 'ArrowLeft',
|
|
380
|
+
arrowleft: 'ArrowLeft',
|
|
381
|
+
Right: 'ArrowRight',
|
|
382
|
+
right: 'ArrowRight',
|
|
383
|
+
arrowright: 'ArrowRight',
|
|
384
|
+
|
|
385
|
+
// Navigation variants
|
|
386
|
+
home: 'Home',
|
|
387
|
+
end: 'End',
|
|
388
|
+
pageup: 'PageUp',
|
|
389
|
+
pagedown: 'PageDown',
|
|
390
|
+
PgUp: 'PageUp',
|
|
391
|
+
PgDn: 'PageDown',
|
|
392
|
+
pgup: 'PageUp',
|
|
393
|
+
pgdn: 'PageDown',
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Normalizes a key name to its canonical form.
|
|
398
|
+
*
|
|
399
|
+
* Converts various key name formats (aliases, case variations) into the standard
|
|
400
|
+
* canonical names used throughout the library. This enables a more forgiving API
|
|
401
|
+
* where users can write keys in different ways and still get correct behavior.
|
|
402
|
+
*
|
|
403
|
+
* Normalization rules:
|
|
404
|
+
* 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')
|
|
405
|
+
* 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')
|
|
406
|
+
* 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')
|
|
407
|
+
* 4. Other keys → returned as-is (already canonical or unknown)
|
|
408
|
+
*
|
|
409
|
+
* @param key - The key name to normalize (can be an alias, lowercase, etc.)
|
|
410
|
+
* @returns The canonical key name
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* normalizeKeyName('esc') // 'Escape'
|
|
415
|
+
* normalizeKeyName('a') // 'A'
|
|
416
|
+
* normalizeKeyName('f1') // 'F1'
|
|
417
|
+
* normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)
|
|
418
|
+
* ```
|
|
419
|
+
*/
|
|
420
|
+
export function normalizeKeyName(key: string): string {
|
|
421
|
+
// Check aliases first
|
|
422
|
+
if (key in KEY_ALIASES) {
|
|
423
|
+
return KEY_ALIASES[key]!
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Check if it's a single letter (normalize to uppercase)
|
|
427
|
+
if (key.length === 1 && /^[a-zA-Z]$/.test(key)) {
|
|
428
|
+
return key.toUpperCase()
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Check if it's a function key (normalize case)
|
|
432
|
+
const upperKey = key.toUpperCase()
|
|
433
|
+
if (/^F([1-9]|1[0-2])$/.test(upperKey)) {
|
|
434
|
+
return upperKey
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return key
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// =============================================================================
|
|
441
|
+
// Display Symbols
|
|
442
|
+
// =============================================================================
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Modifier key symbols for macOS display.
|
|
446
|
+
*
|
|
447
|
+
* Used by formatting functions to display hotkeys with macOS-style symbols
|
|
448
|
+
* (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides
|
|
449
|
+
* a native macOS look and feel in hotkey displays.
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* ```ts
|
|
453
|
+
* MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'
|
|
454
|
+
* MAC_MODIFIER_SYMBOLS['Control'] // '⌃'
|
|
455
|
+
* MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'
|
|
456
|
+
* MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'
|
|
457
|
+
* ```
|
|
458
|
+
*/
|
|
459
|
+
export const MAC_MODIFIER_SYMBOLS: Record<CanonicalModifier, string> = {
|
|
460
|
+
Control: '⌃',
|
|
461
|
+
Alt: '⌥',
|
|
462
|
+
Shift: '⇧',
|
|
463
|
+
Meta: '⌘',
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Modifier key labels for Windows/Linux display.
|
|
468
|
+
*
|
|
469
|
+
* Used by formatting functions to display hotkeys with standard text labels
|
|
470
|
+
* (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.
|
|
471
|
+
* This provides a familiar Windows/Linux look and feel in hotkey displays.
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'
|
|
476
|
+
* STANDARD_MODIFIER_LABELS['Meta'] // 'Win'
|
|
477
|
+
* STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'
|
|
478
|
+
* STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
export const STANDARD_MODIFIER_LABELS: Record<CanonicalModifier, string> = {
|
|
482
|
+
Control: 'Ctrl',
|
|
483
|
+
Alt: 'Alt',
|
|
484
|
+
Shift: 'Shift',
|
|
485
|
+
Meta: 'Win',
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Special key symbols for display formatting.
|
|
490
|
+
*
|
|
491
|
+
* Maps certain keys to their visual symbols for better readability in hotkey displays.
|
|
492
|
+
* Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter
|
|
493
|
+
* instead of text labels.
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```ts
|
|
497
|
+
* KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'
|
|
498
|
+
* KEY_DISPLAY_SYMBOLS['Enter'] // '↵'
|
|
499
|
+
* KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'
|
|
500
|
+
* KEY_DISPLAY_SYMBOLS['Space'] // '␣'
|
|
501
|
+
* ```
|
|
502
|
+
*/
|
|
503
|
+
export const KEY_DISPLAY_SYMBOLS: Record<string, string> = {
|
|
504
|
+
ArrowUp: '↑',
|
|
505
|
+
ArrowDown: '↓',
|
|
506
|
+
ArrowLeft: '←',
|
|
507
|
+
ArrowRight: '→',
|
|
508
|
+
Enter: '↵',
|
|
509
|
+
Escape: 'Esc',
|
|
510
|
+
Backspace: '⌫',
|
|
511
|
+
Delete: '⌦',
|
|
512
|
+
Tab: '⇥',
|
|
513
|
+
Space: '␣',
|
|
514
|
+
}
|