@tanstack/hotkeys 0.8.0 → 0.9.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/README.md +15 -1
- package/dist/{manager.utils.js → _event-target.cjs} +5 -41
- package/dist/_event-target.cjs.map +1 -0
- package/dist/{manager.utils.cjs → _event-target.js} +3 -47
- package/dist/_event-target.js.map +1 -0
- package/dist/_keyboard-event.cjs +83 -0
- package/dist/_keyboard-event.cjs.map +1 -0
- package/dist/_keyboard-event.js +76 -0
- package/dist/_keyboard-event.js.map +1 -0
- package/dist/_match.cjs +49 -0
- package/dist/_match.cjs.map +1 -0
- package/dist/_match.js +49 -0
- package/dist/_match.js.map +1 -0
- package/dist/_named-keys.cjs +68 -0
- package/dist/_named-keys.cjs.map +1 -0
- package/dist/_named-keys.d.cts +8 -0
- package/dist/_named-keys.d.ts +8 -0
- package/dist/_named-keys.js +66 -0
- package/dist/_named-keys.js.map +1 -0
- package/dist/_recorder-chord.cjs +44 -0
- package/dist/_recorder-chord.cjs.map +1 -0
- package/dist/_recorder-chord.js +43 -0
- package/dist/_recorder-chord.js.map +1 -0
- package/dist/_recording-guard.cjs +59 -0
- package/dist/_recording-guard.cjs.map +1 -0
- package/dist/_recording-guard.js +56 -0
- package/dist/_recording-guard.js.map +1 -0
- package/dist/_registration.cjs +54 -0
- package/dist/_registration.cjs.map +1 -0
- package/dist/_registration.js +50 -0
- package/dist/_registration.js.map +1 -0
- package/dist/conflicts.cjs +55 -0
- package/dist/conflicts.cjs.map +1 -0
- package/dist/conflicts.d.cts +39 -0
- package/dist/conflicts.d.ts +39 -0
- package/dist/conflicts.js +55 -0
- package/dist/conflicts.js.map +1 -0
- package/dist/constants.cjs +46 -171
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +4 -129
- package/dist/constants.d.ts +4 -129
- package/dist/constants.js +48 -164
- package/dist/constants.js.map +1 -1
- package/dist/display-labels.cjs +119 -0
- package/dist/display-labels.cjs.map +1 -0
- package/dist/display-labels.d.cts +93 -0
- package/dist/display-labels.d.ts +93 -0
- package/dist/display-labels.js +113 -0
- package/dist/display-labels.js.map +1 -0
- package/dist/format.cjs +30 -36
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.cts +46 -7
- package/dist/format.d.ts +46 -7
- package/dist/format.js +31 -37
- package/dist/format.js.map +1 -1
- package/dist/hint.cjs +24 -0
- package/dist/hint.cjs.map +1 -0
- package/dist/hint.d.cts +23 -0
- package/dist/hint.d.ts +23 -0
- package/dist/hint.js +24 -0
- package/dist/hint.js.map +1 -0
- package/dist/hotkey-manager.cjs +53 -22
- package/dist/hotkey-manager.cjs.map +1 -1
- package/dist/hotkey-manager.d.cts +6 -2
- package/dist/hotkey-manager.d.ts +6 -2
- package/dist/hotkey-manager.js +49 -18
- package/dist/hotkey-manager.js.map +1 -1
- package/dist/hotkey-recorder.cjs +66 -10
- package/dist/hotkey-recorder.cjs.map +1 -1
- package/dist/hotkey-recorder.d.cts +5 -2
- package/dist/hotkey-recorder.d.ts +5 -2
- package/dist/hotkey-recorder.js +65 -9
- package/dist/hotkey-recorder.js.map +1 -1
- package/dist/hotkey-sequence-recorder.cjs +85 -22
- package/dist/hotkey-sequence-recorder.cjs.map +1 -1
- package/dist/hotkey-sequence-recorder.d.cts +9 -2
- package/dist/hotkey-sequence-recorder.d.ts +9 -2
- package/dist/hotkey-sequence-recorder.js +84 -21
- package/dist/hotkey-sequence-recorder.js.map +1 -1
- package/dist/{hotkey.d.ts → hotkey.types.d.cts} +55 -120
- package/dist/{hotkey.d.cts → hotkey.types.d.ts} +55 -120
- package/dist/index.cjs +18 -10
- package/dist/index.d.cts +14 -9
- package/dist/index.d.ts +14 -9
- package/dist/index.js +10 -6
- package/dist/key-state-tracker.cjs +24 -24
- package/dist/key-state-tracker.cjs.map +1 -1
- package/dist/key-state-tracker.js +24 -24
- package/dist/key-state-tracker.js.map +1 -1
- package/dist/key.types.d.cts +82 -0
- package/dist/key.types.d.ts +82 -0
- package/dist/match.cjs +21 -30
- package/dist/match.cjs.map +1 -1
- package/dist/match.d.cts +19 -3
- package/dist/match.d.ts +19 -3
- package/dist/match.js +19 -29
- package/dist/match.js.map +1 -1
- package/dist/parse.cjs +43 -26
- package/dist/parse.cjs.map +1 -1
- package/dist/parse.d.cts +10 -4
- package/dist/parse.d.ts +10 -4
- package/dist/parse.js +35 -19
- package/dist/parse.js.map +1 -1
- package/dist/platform.cjs +55 -0
- package/dist/platform.cjs.map +1 -0
- package/dist/platform.d.cts +44 -0
- package/dist/platform.d.ts +44 -0
- package/dist/platform.js +53 -0
- package/dist/platform.js.map +1 -0
- package/dist/recorder-options.d.cts +34 -0
- package/dist/recorder-options.d.ts +34 -0
- package/dist/sequence-manager.cjs +54 -28
- package/dist/sequence-manager.cjs.map +1 -1
- package/dist/sequence-manager.d.cts +18 -3
- package/dist/sequence-manager.d.ts +18 -3
- package/dist/sequence-manager.js +49 -23
- package/dist/sequence-manager.js.map +1 -1
- package/dist/validate.cjs +6 -10
- package/dist/validate.cjs.map +1 -1
- package/dist/validate.d.cts +13 -2
- package/dist/validate.d.ts +13 -2
- package/dist/validate.js +7 -11
- package/dist/validate.js.map +1 -1
- package/package.json +1 -1
- package/src/{manager.utils.ts → _event-target.ts} +0 -74
- package/src/_keyboard-event.ts +101 -0
- package/src/_match.ts +96 -0
- package/src/_named-keys.ts +62 -0
- package/src/_recorder-chord.ts +76 -0
- package/src/_recording-guard.ts +63 -0
- package/src/_registration.ts +79 -0
- package/src/conflicts.ts +125 -0
- package/src/constants.ts +48 -196
- package/src/display-labels.ts +123 -0
- package/src/format.ts +111 -52
- package/src/hint.ts +42 -0
- package/src/hotkey-manager.ts +95 -40
- package/src/hotkey-recorder.ts +91 -15
- package/src/hotkey-sequence-recorder.ts +110 -28
- package/src/{hotkey.ts → hotkey.types.ts} +57 -224
- package/src/index.ts +11 -5
- package/src/key-state-tracker.ts +36 -24
- package/src/key.types.ts +268 -0
- package/src/match.ts +41 -90
- package/src/parse.ts +67 -26
- package/src/platform.ts +66 -0
- package/src/recorder-options.ts +34 -0
- package/src/sequence-manager.ts +77 -42
- package/src/validate.ts +29 -21
- package/dist/manager.utils.cjs.map +0 -1
- package/dist/manager.utils.d.cts +0 -13
- package/dist/manager.utils.d.ts +0 -13
- package/dist/manager.utils.js.map +0 -1
- package/dist/recorder-chord.cjs +0 -18
- package/dist/recorder-chord.cjs.map +0 -1
- package/dist/recorder-chord.js +0 -18
- package/dist/recorder-chord.js.map +0 -1
- package/src/recorder-chord.ts +0 -25
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { detectPlatform } from "./platform.js";
|
|
2
|
+
import { keysEqual } from "./_keyboard-event.js";
|
|
3
|
+
import { parseRegisterableHotkey } from "./parse.js";
|
|
4
|
+
import { matchesKeyboardEvent } from "./match.js";
|
|
5
|
+
import { getHotkeyManager, toHotkeyRegistrationView } from "./hotkey-manager.js";
|
|
6
|
+
import { getSequenceManager } from "./sequence-manager.js";
|
|
7
|
+
|
|
8
|
+
//#region src/conflicts.ts
|
|
9
|
+
/** Whether two targets share a document and one contains the other (or is its window). */
|
|
10
|
+
function targetsOverlap(a, b) {
|
|
11
|
+
if (a === b) return true;
|
|
12
|
+
const documentOf = (target) => {
|
|
13
|
+
if ("document" in target) return target.document;
|
|
14
|
+
if (target.nodeType === 9) return target;
|
|
15
|
+
return target.ownerDocument;
|
|
16
|
+
};
|
|
17
|
+
if (documentOf(a) !== documentOf(b)) return false;
|
|
18
|
+
if (!("contains" in a) || !("contains" in b)) return true;
|
|
19
|
+
return a.contains(b) || b.contains(a);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Find equivalent bindings and sequence-prefix conflicts in the live registry.
|
|
23
|
+
* With source events, also checks observed logical/physical overlap. Does not
|
|
24
|
+
* discover unmounted handlers, external listeners, or other keyboard layouts.
|
|
25
|
+
*/
|
|
26
|
+
function findHotkeyConflicts(candidate, options = {}) {
|
|
27
|
+
const steps = Array.isArray(candidate) ? candidate : [candidate];
|
|
28
|
+
if (steps.length === 0) return [];
|
|
29
|
+
const platform = options.platform ?? detectPlatform();
|
|
30
|
+
const parsed = steps.map((step) => parseRegisterableHotkey(step, platform));
|
|
31
|
+
const target = options.target ?? (typeof document === "undefined" ? void 0 : document);
|
|
32
|
+
const conflicts = [];
|
|
33
|
+
const matches = (registration, bindings) => {
|
|
34
|
+
if (!options.includeDisabled && registration.options.enabled === false || (registration.options.eventType ?? "keydown") !== (options.eventType ?? "keydown") || options.excludeIds?.includes(registration.id) || options.exclude?.(registration) || options.scope !== "all" && target && !targetsOverlap(target, registration.target)) return false;
|
|
35
|
+
return parsed.slice(0, Math.min(parsed.length, bindings.length)).every((a, index) => {
|
|
36
|
+
const b = parseRegisterableHotkey(bindings[index], registration.options.platform);
|
|
37
|
+
const same = a.ctrl === b.ctrl && a.alt === b.alt && a.shift === b.shift && a.meta === b.meta && (a.code !== void 0 || b.code !== void 0 ? a.code === b.code : keysEqual(a.key, b.key));
|
|
38
|
+
const event = options.events?.[index];
|
|
39
|
+
return same || event !== void 0 && matchesKeyboardEvent(event, a, platform) && matchesKeyboardEvent(event, b, registration.options.platform);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
for (const registration of getHotkeyManager().registrations.state.values()) if (matches(registration, [registration.hotkey])) conflicts.push({
|
|
43
|
+
type: "hotkey",
|
|
44
|
+
registration: toHotkeyRegistrationView(registration)
|
|
45
|
+
});
|
|
46
|
+
for (const registration of getSequenceManager().registrations.state.values()) if (matches(registration, registration.sequence)) conflicts.push({
|
|
47
|
+
type: "sequence",
|
|
48
|
+
registration
|
|
49
|
+
});
|
|
50
|
+
return conflicts;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { findHotkeyConflicts };
|
|
55
|
+
//# sourceMappingURL=conflicts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conflicts.js","names":[],"sources":["../src/conflicts.ts"],"sourcesContent":["import { detectPlatform } from './platform'\nimport { parseRegisterableHotkey } from './parse'\nimport { keysEqual } from './_keyboard-event'\nimport { matchesKeyboardEvent } from './match'\nimport { getHotkeyManager, toHotkeyRegistrationView } from './hotkey-manager'\nimport { getSequenceManager } from './sequence-manager'\nimport type { HotkeyRegistrationView } from './hotkey-manager'\nimport type { SequenceRegistrationView } from './sequence-manager'\nimport type { RegisterableHotkey } from './hotkey.types'\n\nexport type HotkeyConflict =\n | { type: 'hotkey'; registration: HotkeyRegistrationView }\n | { type: 'sequence'; registration: SequenceRegistrationView }\n\nexport interface HotkeyConflictOptions {\n /** Intended registration target. Defaults to document; disjoint targets are excluded. */\n target?: HTMLElement | Document | Window\n /** Check all targets instead of overlapping targets. Default: overlapping. */\n scope?: 'overlapping' | 'all'\n /** Intended event type. Default: keydown. */\n eventType?: 'keydown' | 'keyup'\n /** Include disabled registrations. Default: false. */\n includeDisabled?: boolean\n /** IDs of registrations being edited. */\n excludeIds?: ReadonlyArray<string>\n /** Additional app-specific exclusions, such as all instances of one action. */\n exclude?: (\n registration: HotkeyRegistrationView | SequenceRegistrationView,\n ) => boolean\n /** Platform used to resolve Mod in the candidate. Registrations retain their own platform. */\n platform?: 'mac' | 'windows' | 'linux'\n /** Source events allow detecting physical/logical overlap on the recorded layout. */\n events?: ReadonlyArray<KeyboardEvent>\n}\n\n/** Whether two targets share a document and one contains the other (or is its window). */\nfunction targetsOverlap(\n a: HTMLElement | Document | Window,\n b: HTMLElement | Document | Window,\n): boolean {\n if (a === b) return true\n // Resolve the owning document without relying on cross-frame instanceof checks.\n const documentOf = (target: typeof a): Document | null => {\n if ('document' in target) return target.document\n if (target.nodeType === 9) return target as Document\n return target.ownerDocument\n }\n if (documentOf(a) !== documentOf(b)) return false\n if (!('contains' in a) || !('contains' in b)) return true\n return a.contains(b as Node) || b.contains(a as Node)\n}\n\n/**\n * Find equivalent bindings and sequence-prefix conflicts in the live registry.\n * With source events, also checks observed logical/physical overlap. Does not\n * discover unmounted handlers, external listeners, or other keyboard layouts.\n */\nexport function findHotkeyConflicts(\n candidate: RegisterableHotkey | ReadonlyArray<RegisterableHotkey>,\n options: HotkeyConflictOptions = {},\n): Array<HotkeyConflict> {\n const steps: ReadonlyArray<RegisterableHotkey> = Array.isArray(candidate)\n ? candidate\n : [candidate as RegisterableHotkey]\n if (steps.length === 0) return []\n const platform = options.platform ?? detectPlatform()\n const parsed = steps.map((step) => parseRegisterableHotkey(step, platform))\n const target =\n options.target ?? (typeof document === 'undefined' ? undefined : document)\n const conflicts: Array<HotkeyConflict> = []\n // Apply scope policy before comparing equivalent bindings or observed event overlap.\n const matches = (\n registration: HotkeyRegistrationView | SequenceRegistrationView,\n bindings: ReadonlyArray<RegisterableHotkey>,\n ) => {\n if (\n (!options.includeDisabled && registration.options.enabled === false) ||\n (registration.options.eventType ?? 'keydown') !==\n (options.eventType ?? 'keydown') ||\n options.excludeIds?.includes(registration.id) ||\n options.exclude?.(registration) ||\n (options.scope !== 'all' &&\n target &&\n !targetsOverlap(target, registration.target))\n )\n return false\n // Compare only the common prefix: a shorter sequence can collide with a longer one.\n return parsed\n .slice(0, Math.min(parsed.length, bindings.length))\n .every((a, index) => {\n const b = parseRegisterableHotkey(\n bindings[index]!,\n registration.options.platform,\n )\n const same =\n a.ctrl === b.ctrl &&\n a.alt === b.alt &&\n a.shift === b.shift &&\n a.meta === b.meta &&\n (a.code !== undefined || b.code !== undefined\n ? a.code === b.code\n : keysEqual(a.key, b.key))\n // A source event can establish code/key overlap without guessing its layout.\n const event = options.events?.[index]\n return (\n same ||\n (event !== undefined &&\n matchesKeyboardEvent(event, a, platform) &&\n matchesKeyboardEvent(event, b, registration.options.platform))\n )\n })\n }\n for (const registration of getHotkeyManager().registrations.state.values()) {\n if (matches(registration, [registration.hotkey]))\n conflicts.push({\n type: 'hotkey',\n registration: toHotkeyRegistrationView(registration),\n })\n }\n for (const registration of getSequenceManager().registrations.state.values()) {\n if (matches(registration, registration.sequence))\n conflicts.push({ type: 'sequence', registration })\n }\n return conflicts\n}\n"],"mappings":";;;;;;;;;AAoCA,SAAS,eACP,GACA,GACS;AACT,KAAI,MAAM,EAAG,QAAO;CAEpB,MAAM,cAAc,WAAsC;AACxD,MAAI,cAAc,OAAQ,QAAO,OAAO;AACxC,MAAI,OAAO,aAAa,EAAG,QAAO;AAClC,SAAO,OAAO;;AAEhB,KAAI,WAAW,EAAE,KAAK,WAAW,EAAE,CAAE,QAAO;AAC5C,KAAI,EAAE,cAAc,MAAM,EAAE,cAAc,GAAI,QAAO;AACrD,QAAO,EAAE,SAAS,EAAU,IAAI,EAAE,SAAS,EAAU;;;;;;;AAQvD,SAAgB,oBACd,WACA,UAAiC,EAAE,EACZ;CACvB,MAAM,QAA2C,MAAM,QAAQ,UAAU,GACrE,YACA,CAAC,UAAgC;AACrC,KAAI,MAAM,WAAW,EAAG,QAAO,EAAE;CACjC,MAAM,WAAW,QAAQ,YAAY,gBAAgB;CACrD,MAAM,SAAS,MAAM,KAAK,SAAS,wBAAwB,MAAM,SAAS,CAAC;CAC3E,MAAM,SACJ,QAAQ,WAAW,OAAO,aAAa,cAAc,SAAY;CACnE,MAAM,YAAmC,EAAE;CAE3C,MAAM,WACJ,cACA,aACG;AACH,MACG,CAAC,QAAQ,mBAAmB,aAAa,QAAQ,YAAY,UAC7D,aAAa,QAAQ,aAAa,gBAChC,QAAQ,aAAa,cACxB,QAAQ,YAAY,SAAS,aAAa,GAAG,IAC7C,QAAQ,UAAU,aAAa,IAC9B,QAAQ,UAAU,SACjB,UACA,CAAC,eAAe,QAAQ,aAAa,OAAO,CAE9C,QAAO;AAET,SAAO,OACJ,MAAM,GAAG,KAAK,IAAI,OAAO,QAAQ,SAAS,OAAO,CAAC,CAClD,OAAO,GAAG,UAAU;GACnB,MAAM,IAAI,wBACR,SAAS,QACT,aAAa,QAAQ,SACtB;GACD,MAAM,OACJ,EAAE,SAAS,EAAE,QACb,EAAE,QAAQ,EAAE,OACZ,EAAE,UAAU,EAAE,SACd,EAAE,SAAS,EAAE,SACZ,EAAE,SAAS,UAAa,EAAE,SAAS,SAChC,EAAE,SAAS,EAAE,OACb,UAAU,EAAE,KAAK,EAAE,IAAI;GAE7B,MAAM,QAAQ,QAAQ,SAAS;AAC/B,UACE,QACC,UAAU,UACT,qBAAqB,OAAO,GAAG,SAAS,IACxC,qBAAqB,OAAO,GAAG,aAAa,QAAQ,SAAS;IAEjE;;AAEN,MAAK,MAAM,gBAAgB,kBAAkB,CAAC,cAAc,MAAM,QAAQ,CACxE,KAAI,QAAQ,cAAc,CAAC,aAAa,OAAO,CAAC,CAC9C,WAAU,KAAK;EACb,MAAM;EACN,cAAc,yBAAyB,aAAa;EACrD,CAAC;AAEN,MAAK,MAAM,gBAAgB,oBAAoB,CAAC,cAAc,MAAM,QAAQ,CAC1E,KAAI,QAAQ,cAAc,aAAa,SAAS,CAC9C,WAAU,KAAK;EAAE,MAAM;EAAY;EAAc,CAAC;AAEtD,QAAO"}
|
package/dist/constants.cjs
CHANGED
|
@@ -1,29 +1,7 @@
|
|
|
1
|
+
const require__named_keys = require('./_named-keys.cjs');
|
|
1
2
|
|
|
2
3
|
//#region src/constants.ts
|
|
3
4
|
/**
|
|
4
|
-
* Detects the current platform based on browser navigator properties.
|
|
5
|
-
*
|
|
6
|
-
* Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
|
|
7
|
-
* Control elsewhere) and for platform-specific hotkey formatting.
|
|
8
|
-
*
|
|
9
|
-
* @returns The detected platform: 'mac', 'windows', or 'linux'
|
|
10
|
-
* @remarks Defaults to 'linux' in SSR environments where navigator is undefined
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
|
|
15
|
-
* const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
function detectPlatform() {
|
|
19
|
-
if (typeof navigator === "undefined") return "linux";
|
|
20
|
-
const platform = navigator.platform?.toLowerCase() ?? "";
|
|
21
|
-
const userAgent = navigator.userAgent?.toLowerCase() ?? "";
|
|
22
|
-
if (platform.includes("mac") || userAgent.includes("mac")) return "mac";
|
|
23
|
-
if (platform.includes("win") || userAgent.includes("win")) return "windows";
|
|
24
|
-
return "linux";
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
5
|
* Canonical order for modifiers in normalized hotkey strings.
|
|
28
6
|
*
|
|
29
7
|
* Defines the standard order in which modifiers should appear when formatting
|
|
@@ -97,31 +75,6 @@ const MODIFIER_ALIASES = {
|
|
|
97
75
|
mod: "Mod"
|
|
98
76
|
};
|
|
99
77
|
/**
|
|
100
|
-
* Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
|
|
101
|
-
*
|
|
102
|
-
* The 'Mod' token represents the "primary modifier" on each platform:
|
|
103
|
-
* - macOS: 'Meta' (Command key ⌘)
|
|
104
|
-
* - Windows/Linux: 'Control' (Ctrl key)
|
|
105
|
-
*
|
|
106
|
-
* This enables cross-platform hotkey definitions like 'Mod+S' that automatically
|
|
107
|
-
* map to Command+S on Mac and Ctrl+S on Windows/Linux.
|
|
108
|
-
*
|
|
109
|
-
* @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
|
|
110
|
-
* @param platform - The target platform. Defaults to auto-detection.
|
|
111
|
-
* @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```ts
|
|
115
|
-
* resolveModifier('Mod', 'mac') // 'Meta'
|
|
116
|
-
* resolveModifier('Mod', 'windows') // 'Control'
|
|
117
|
-
* resolveModifier('Control', 'mac') // 'Control' (unchanged)
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
function resolveModifier(modifier, platform = detectPlatform()) {
|
|
121
|
-
if (modifier === "Mod") return platform === "mac" ? "Meta" : "Control";
|
|
122
|
-
return modifier;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
78
|
* Set of all valid letter keys (A-Z).
|
|
126
79
|
*
|
|
127
80
|
* Used for validation and type checking. Letter keys are matched case-insensitively
|
|
@@ -175,7 +128,7 @@ const NUMBER_KEYS = new Set([
|
|
|
175
128
|
"9"
|
|
176
129
|
]);
|
|
177
130
|
/**
|
|
178
|
-
* Set of all valid function keys (F1-
|
|
131
|
+
* Set of all valid function keys (F1-F24).
|
|
179
132
|
*
|
|
180
133
|
* Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,
|
|
181
134
|
* Alt+F4 to close windows) and application-specific commands.
|
|
@@ -192,7 +145,19 @@ const FUNCTION_KEYS = new Set([
|
|
|
192
145
|
"F9",
|
|
193
146
|
"F10",
|
|
194
147
|
"F11",
|
|
195
|
-
"F12"
|
|
148
|
+
"F12",
|
|
149
|
+
"F13",
|
|
150
|
+
"F14",
|
|
151
|
+
"F15",
|
|
152
|
+
"F16",
|
|
153
|
+
"F17",
|
|
154
|
+
"F18",
|
|
155
|
+
"F19",
|
|
156
|
+
"F20",
|
|
157
|
+
"F21",
|
|
158
|
+
"F22",
|
|
159
|
+
"F23",
|
|
160
|
+
"F24"
|
|
196
161
|
]);
|
|
197
162
|
/**
|
|
198
163
|
* Set of all valid navigation keys for cursor movement and document navigation.
|
|
@@ -248,7 +213,29 @@ const PUNCTUATION_KEYS = new Set([
|
|
|
248
213
|
",",
|
|
249
214
|
".",
|
|
250
215
|
";",
|
|
251
|
-
"`"
|
|
216
|
+
"`",
|
|
217
|
+
"'",
|
|
218
|
+
"+",
|
|
219
|
+
"?",
|
|
220
|
+
"!",
|
|
221
|
+
"@",
|
|
222
|
+
"#",
|
|
223
|
+
"$",
|
|
224
|
+
"%",
|
|
225
|
+
"^",
|
|
226
|
+
"&",
|
|
227
|
+
"*",
|
|
228
|
+
"(",
|
|
229
|
+
")",
|
|
230
|
+
"_",
|
|
231
|
+
"{",
|
|
232
|
+
"}",
|
|
233
|
+
"|",
|
|
234
|
+
":",
|
|
235
|
+
"\"",
|
|
236
|
+
"<",
|
|
237
|
+
">",
|
|
238
|
+
"~"
|
|
252
239
|
]);
|
|
253
240
|
/**
|
|
254
241
|
* Maps `KeyboardEvent.code` values for punctuation keys to their canonical characters.
|
|
@@ -268,6 +255,7 @@ const PUNCTUATION_CODE_MAP = {
|
|
|
268
255
|
Equal: "=",
|
|
269
256
|
Minus: "-",
|
|
270
257
|
Period: ".",
|
|
258
|
+
Quote: "'",
|
|
271
259
|
Semicolon: ";",
|
|
272
260
|
Slash: "/"
|
|
273
261
|
};
|
|
@@ -291,8 +279,11 @@ const ALL_KEYS = new Set([
|
|
|
291
279
|
...FUNCTION_KEYS,
|
|
292
280
|
...NAVIGATION_KEYS,
|
|
293
281
|
...EDITING_KEYS,
|
|
294
|
-
...PUNCTUATION_KEYS
|
|
282
|
+
...PUNCTUATION_KEYS,
|
|
283
|
+
...require__named_keys.SHARED_NAMED_KEYS,
|
|
284
|
+
...require__named_keys.LOGICAL_ONLY_NAMED_KEYS
|
|
295
285
|
]);
|
|
286
|
+
const CANONICAL_KEY_NAMES = new Map(Array.from(ALL_KEYS, (key) => [key.toLowerCase(), key]));
|
|
296
287
|
/**
|
|
297
288
|
* Maps key name aliases to their canonical form.
|
|
298
289
|
*
|
|
@@ -313,6 +304,8 @@ const ALL_KEYS = new Set([
|
|
|
313
304
|
* ```
|
|
314
305
|
*/
|
|
315
306
|
const KEY_ALIASES = {
|
|
307
|
+
Plus: "+",
|
|
308
|
+
plus: "+",
|
|
316
309
|
Esc: "Escape",
|
|
317
310
|
esc: "Escape",
|
|
318
311
|
escape: "Escape",
|
|
@@ -397,128 +390,14 @@ function normalizeKeyName(key) {
|
|
|
397
390
|
const upper = key.toUpperCase();
|
|
398
391
|
return upper.length === 1 ? upper : key;
|
|
399
392
|
}
|
|
400
|
-
|
|
401
|
-
if (/^F([1-9]|1[0-2])$/.test(upperKey)) return upperKey;
|
|
402
|
-
return key;
|
|
393
|
+
return CANONICAL_KEY_NAMES.get(key.toLowerCase()) ?? key;
|
|
403
394
|
}
|
|
404
|
-
/**
|
|
405
|
-
* Modifier key symbols for macOS display.
|
|
406
|
-
*
|
|
407
|
-
* Used by formatting functions to display hotkeys with macOS-style symbols
|
|
408
|
-
* (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides
|
|
409
|
-
* a native macOS look and feel in hotkey displays.
|
|
410
|
-
*
|
|
411
|
-
* @example
|
|
412
|
-
* ```ts
|
|
413
|
-
* MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'
|
|
414
|
-
* MAC_MODIFIER_SYMBOLS['Control'] // '⌃'
|
|
415
|
-
* MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'
|
|
416
|
-
* MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'
|
|
417
|
-
* ```
|
|
418
|
-
*/
|
|
419
|
-
const MAC_MODIFIER_SYMBOLS = {
|
|
420
|
-
Control: "⌃",
|
|
421
|
-
Alt: "⌥",
|
|
422
|
-
Shift: "⇧",
|
|
423
|
-
Meta: "⌘",
|
|
424
|
-
Mod: "⌘"
|
|
425
|
-
};
|
|
426
|
-
/**
|
|
427
|
-
* Modifier key labels for macOS display.
|
|
428
|
-
*
|
|
429
|
-
* Used by formatting functions to display hotkeys with macOS-style text labels
|
|
430
|
-
* (e.g., 'Control' for Control, 'Option' for Alt, 'Cmd' for Meta) instead of symbols.
|
|
431
|
-
* This provides a familiar macOS look and feel in hotkey displays.
|
|
432
|
-
*
|
|
433
|
-
* @example
|
|
434
|
-
* ```ts
|
|
435
|
-
* MAC_MODIFIER_LABELS['Control'] // 'control'
|
|
436
|
-
* MAC_MODIFIER_LABELS['Alt'] // 'option'
|
|
437
|
-
* MAC_MODIFIER_LABELS['Shift'] // 'shift'
|
|
438
|
-
* MAC_MODIFIER_LABELS['Meta'] // 'cmd'
|
|
439
|
-
* ```
|
|
440
|
-
*/
|
|
441
|
-
const MAC_MODIFIER_LABELS = {
|
|
442
|
-
Control: "Control",
|
|
443
|
-
Alt: "Option",
|
|
444
|
-
Shift: "Shift",
|
|
445
|
-
Meta: "Cmd",
|
|
446
|
-
Mod: "Cmd"
|
|
447
|
-
};
|
|
448
|
-
/**
|
|
449
|
-
* Modifier key labels for Windows/Linux display.
|
|
450
|
-
*
|
|
451
|
-
* Used by formatting functions to display hotkeys with standard text labels
|
|
452
|
-
* (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.
|
|
453
|
-
* This provides a familiar Windows/Linux look and feel in hotkey displays.
|
|
454
|
-
*
|
|
455
|
-
* @example
|
|
456
|
-
* ```ts
|
|
457
|
-
* STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'
|
|
458
|
-
* STANDARD_MODIFIER_LABELS['Meta'] // 'Win'
|
|
459
|
-
* STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'
|
|
460
|
-
* STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'
|
|
461
|
-
* ```
|
|
462
|
-
*/
|
|
463
|
-
const WINDOWS_MODIFIER_LABELS = {
|
|
464
|
-
Control: "Ctrl",
|
|
465
|
-
Alt: "Alt",
|
|
466
|
-
Shift: "Shift",
|
|
467
|
-
Meta: "Win",
|
|
468
|
-
Mod: "Ctrl"
|
|
469
|
-
};
|
|
470
|
-
const LINUX_MODIFIER_LABELS = {
|
|
471
|
-
...WINDOWS_MODIFIER_LABELS,
|
|
472
|
-
Meta: "Super"
|
|
473
|
-
};
|
|
474
|
-
const PUNCTUATION_KEY_DISPLAY_LABELS = {
|
|
475
|
-
"`": "Backquote",
|
|
476
|
-
"\\": "Backslash",
|
|
477
|
-
"[": "Left Bracket",
|
|
478
|
-
"]": "Right Bracket",
|
|
479
|
-
",": "Comma",
|
|
480
|
-
"=": "Equal",
|
|
481
|
-
"-": "Minus",
|
|
482
|
-
".": "Period",
|
|
483
|
-
";": "Semicolon"
|
|
484
|
-
};
|
|
485
|
-
/**
|
|
486
|
-
* Special key symbols for display formatting.
|
|
487
|
-
*
|
|
488
|
-
* Maps certain keys to their visual symbols for better readability in hotkey displays.
|
|
489
|
-
* Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter
|
|
490
|
-
* instead of text labels.
|
|
491
|
-
*
|
|
492
|
-
* @example
|
|
493
|
-
* ```ts
|
|
494
|
-
* KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'
|
|
495
|
-
* KEY_DISPLAY_SYMBOLS['Enter'] // '↵'
|
|
496
|
-
* KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'
|
|
497
|
-
* KEY_DISPLAY_SYMBOLS['Space'] // '␣'
|
|
498
|
-
* ```
|
|
499
|
-
*/
|
|
500
|
-
const KEY_DISPLAY_SYMBOLS = {
|
|
501
|
-
ArrowUp: "↑",
|
|
502
|
-
ArrowDown: "↓",
|
|
503
|
-
ArrowLeft: "←",
|
|
504
|
-
ArrowRight: "→",
|
|
505
|
-
Enter: "↵",
|
|
506
|
-
Escape: "Esc",
|
|
507
|
-
Backspace: "⌫",
|
|
508
|
-
Delete: "⌦",
|
|
509
|
-
Tab: "⇥",
|
|
510
|
-
Space: "␣"
|
|
511
|
-
};
|
|
512
395
|
|
|
513
396
|
//#endregion
|
|
514
397
|
exports.ALL_KEYS = ALL_KEYS;
|
|
515
398
|
exports.EDITING_KEYS = EDITING_KEYS;
|
|
516
399
|
exports.FUNCTION_KEYS = FUNCTION_KEYS;
|
|
517
|
-
exports.KEY_DISPLAY_SYMBOLS = KEY_DISPLAY_SYMBOLS;
|
|
518
400
|
exports.LETTER_KEYS = LETTER_KEYS;
|
|
519
|
-
exports.LINUX_MODIFIER_LABELS = LINUX_MODIFIER_LABELS;
|
|
520
|
-
exports.MAC_MODIFIER_LABELS = MAC_MODIFIER_LABELS;
|
|
521
|
-
exports.MAC_MODIFIER_SYMBOLS = MAC_MODIFIER_SYMBOLS;
|
|
522
401
|
exports.MODIFIER_ALIASES = MODIFIER_ALIASES;
|
|
523
402
|
exports.MODIFIER_KEYS = MODIFIER_KEYS;
|
|
524
403
|
exports.MODIFIER_ORDER = MODIFIER_ORDER;
|
|
@@ -526,10 +405,6 @@ exports.NAVIGATION_KEYS = NAVIGATION_KEYS;
|
|
|
526
405
|
exports.NUMBER_KEYS = NUMBER_KEYS;
|
|
527
406
|
exports.PUNCTUATION_CODE_MAP = PUNCTUATION_CODE_MAP;
|
|
528
407
|
exports.PUNCTUATION_KEYS = PUNCTUATION_KEYS;
|
|
529
|
-
exports.PUNCTUATION_KEY_DISPLAY_LABELS = PUNCTUATION_KEY_DISPLAY_LABELS;
|
|
530
|
-
exports.WINDOWS_MODIFIER_LABELS = WINDOWS_MODIFIER_LABELS;
|
|
531
|
-
exports.detectPlatform = detectPlatform;
|
|
532
408
|
exports.isSingleLetterKey = isSingleLetterKey;
|
|
533
409
|
exports.normalizeKeyName = normalizeKeyName;
|
|
534
|
-
exports.resolveModifier = resolveModifier;
|
|
535
410
|
//# sourceMappingURL=constants.cjs.map
|
package/dist/constants.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.cjs","names":[],"sources":["../src/constants.ts"],"sourcesContent":["import type {\n CanonicalModifier,\n EditingKey,\n FunctionKey,\n LetterKey,\n NavigationKey,\n NumberKey,\n PunctuationKey,\n} from './hotkey'\n\n/**\n * Detects the current platform based on browser navigator properties.\n *\n * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,\n * Control elsewhere) and for platform-specific hotkey formatting.\n *\n * @returns The detected platform: 'mac', 'windows', or 'linux'\n * @remarks Defaults to 'linux' in SSR environments where navigator is undefined\n *\n * @example\n * ```ts\n * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'\n * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere\n * ```\n */\nexport function detectPlatform(): 'mac' | 'windows' | 'linux' {\n if (typeof navigator === 'undefined') {\n return 'linux' // Default for SSR\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const platform = navigator.platform?.toLowerCase() ?? ''\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const userAgent = navigator.userAgent?.toLowerCase() ?? ''\n\n if (platform.includes('mac') || userAgent.includes('mac')) {\n return 'mac'\n }\n if (platform.includes('win') || userAgent.includes('win')) {\n return 'windows'\n }\n return 'linux'\n}\n\n/**\n * Canonical order for modifiers in normalized hotkey strings.\n *\n * Defines the standard order in which modifiers should appear when formatting\n * hotkeys. This ensures consistent, predictable output across the library.\n *\n * Order: Control → Alt → Shift → Meta\n *\n * @example\n * ```ts\n * // Input: 'Shift+Control+Meta+S'\n * // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)\n * ```\n */\nexport const MODIFIER_ORDER: Array<CanonicalModifier> = [\n 'Control',\n 'Alt',\n 'Shift',\n 'Meta',\n]\n\n/**\n * Set of canonical modifier key names for fast lookup.\n *\n * Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when\n * a modifier is released so we can clear non-modifier keys whose keyup events\n * may have been swallowed by the OS (e.g. macOS Cmd+key combos).\n */\nexport const MODIFIER_KEYS = new Set<string>(MODIFIER_ORDER)\n\n/**\n * Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.\n *\n * This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')\n * which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the\n * platform-adaptive 'Mod' token.\n *\n * The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`\n * based on the detected platform (Command on Mac, Control elsewhere).\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * MODIFIER_ALIASES['Ctrl'] // 'Control'\n * MODIFIER_ALIASES['Cmd'] // 'Meta'\n * MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)\n * ```\n */\nexport const MODIFIER_ALIASES: Record<string, CanonicalModifier | 'Mod'> = {\n // Control variants\n Control: 'Control',\n Ctrl: 'Control',\n control: 'Control',\n ctrl: 'Control',\n\n // Shift variants\n Shift: 'Shift',\n shift: 'Shift',\n\n // Alt variants\n Alt: 'Alt',\n Option: 'Alt',\n alt: 'Alt',\n option: 'Alt',\n\n // Meta/Command variants\n Command: 'Meta',\n Cmd: 'Meta',\n Meta: 'Meta',\n command: 'Meta',\n cmd: 'Meta',\n meta: 'Meta',\n\n // DOM `KeyboardEvent.key` spellings for the meta / Windows key\n OS: 'Meta',\n os: 'Meta',\n Win: 'Meta',\n win: 'Meta',\n\n // Platform-adaptive (resolved at runtime)\n CommandOrControl: 'Mod',\n Mod: 'Mod',\n commandorcontrol: 'Mod',\n mod: 'Mod',\n}\n\n/**\n * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.\n *\n * The 'Mod' token represents the \"primary modifier\" on each platform:\n * - macOS: 'Meta' (Command key ⌘)\n * - Windows/Linux: 'Control' (Ctrl key)\n *\n * This enables cross-platform hotkey definitions like 'Mod+S' that automatically\n * map to Command+S on Mac and Ctrl+S on Windows/Linux.\n *\n * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.\n * @param platform - The target platform. Defaults to auto-detection.\n * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')\n *\n * @example\n * ```ts\n * resolveModifier('Mod', 'mac') // 'Meta'\n * resolveModifier('Mod', 'windows') // 'Control'\n * resolveModifier('Control', 'mac') // 'Control' (unchanged)\n * ```\n */\nexport function resolveModifier(\n modifier: CanonicalModifier | 'Mod',\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): CanonicalModifier {\n if (modifier === 'Mod') {\n return platform === 'mac' ? 'Meta' : 'Control'\n }\n return modifier\n}\n\n/**\n * Set of all valid letter keys (A-Z).\n *\n * Used for validation and type checking. Letter keys are matched case-insensitively\n * in hotkey matching, but normalized to uppercase in canonical form.\n */\nexport const LETTER_KEYS = new Set<LetterKey>([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n 'H',\n 'I',\n 'J',\n 'K',\n 'L',\n 'M',\n 'N',\n 'O',\n 'P',\n 'Q',\n 'R',\n 'S',\n 'T',\n 'U',\n 'V',\n 'W',\n 'X',\n 'Y',\n 'Z',\n])\n\n/**\n * Set of all valid number keys (0-9).\n *\n * Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid\n * layout-dependent behavior.\n */\nexport const NUMBER_KEYS = new Set<NumberKey>([\n '0',\n '1',\n '2',\n '3',\n '4',\n '5',\n '6',\n '7',\n '8',\n '9',\n])\n\n/**\n * Set of all valid function keys (F1-F12).\n *\n * Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,\n * Alt+F4 to close windows) and application-specific commands.\n */\nexport const FUNCTION_KEYS = new Set<FunctionKey>([\n 'F1',\n 'F2',\n 'F3',\n 'F4',\n 'F5',\n 'F6',\n 'F7',\n 'F8',\n 'F9',\n 'F10',\n 'F11',\n 'F12',\n])\n\n/**\n * Set of all valid navigation keys for cursor movement and document navigation.\n *\n * Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).\n * These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or\n * navigation shortcuts (Alt+ArrowLeft for back).\n */\nexport const NAVIGATION_KEYS = new Set<NavigationKey>([\n 'ArrowUp',\n 'ArrowDown',\n 'ArrowLeft',\n 'ArrowRight',\n 'Home',\n 'End',\n 'PageUp',\n 'PageDown',\n])\n\n/**\n * Set of all valid editing and special keys.\n *\n * Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and\n * special actions (Escape, Space). These keys are frequently combined with modifiers\n * for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).\n */\nexport const EDITING_KEYS = new Set<EditingKey>([\n 'Enter',\n 'Escape',\n 'Space',\n 'Tab',\n 'Backspace',\n 'Delete',\n])\n\n/**\n * Set of all valid punctuation keys commonly used in keyboard shortcuts.\n *\n * These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,\n * typically US keyboard layout). Common shortcuts include:\n * - `Mod+/` - Toggle comment\n * - `Mod+[` / `Mod+]` - Indent/outdent\n * - `Mod+=` / `Mod+-` - Zoom in/out\n *\n * Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.\n */\nexport const PUNCTUATION_KEYS = new Set<PunctuationKey>([\n '/',\n '[',\n ']',\n '\\\\',\n '=',\n '-',\n ',',\n '.',\n ';',\n '`',\n])\n\n/**\n * Maps `KeyboardEvent.code` values for punctuation keys to their canonical characters.\n *\n * On macOS, holding the Option (Alt) key transforms punctuation keys into special characters\n * (e.g., Option+Minus → en-dash '–'), causing `event.key` to differ from the expected character.\n * However, `event.code` still reports the physical key (e.g., 'Minus'). This map enables\n * falling back to `event.code` for punctuation keys, similar to the existing `Key*`/`Digit*`\n * fallbacks for letters and digits.\n */\nexport const PUNCTUATION_CODE_MAP: Record<string, string> = {\n Backquote: '`',\n Backslash: '\\\\',\n BracketLeft: '[',\n BracketRight: ']',\n Comma: ',',\n Equal: '=',\n Minus: '-',\n Period: '.',\n Semicolon: ';',\n Slash: '/',\n}\n\n/**\n * Set of all valid non-modifier keys.\n *\n * This is the union of all key type sets (letters, numbers, function keys, navigation,\n * editing, and punctuation). Used primarily for validation to check if a key string\n * is recognized and will have type-safe autocomplete support.\n *\n * @see {@link LETTER_KEYS}\n * @see {@link NUMBER_KEYS}\n * @see {@link FUNCTION_KEYS}\n * @see {@link NAVIGATION_KEYS}\n * @see {@link EDITING_KEYS}\n * @see {@link PUNCTUATION_KEYS}\n */\nexport const ALL_KEYS = new Set([\n ...LETTER_KEYS,\n ...NUMBER_KEYS,\n ...FUNCTION_KEYS,\n ...NAVIGATION_KEYS,\n ...EDITING_KEYS,\n ...PUNCTUATION_KEYS,\n])\n\n/**\n * Maps key name aliases to their canonical form.\n *\n * Handles common variations and alternative names for keys to provide a more\n * forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and\n * they'll all normalize to 'Escape'.\n *\n * This map is used internally by `normalizeKeyName()` to convert user input\n * into the canonical key names used throughout the library.\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * KEY_ALIASES['Esc'] // 'Escape'\n * KEY_ALIASES['Del'] // 'Delete'\n * KEY_ALIASES['Up'] // 'ArrowUp'\n * ```\n */\nconst KEY_ALIASES: Record<string, string> = {\n // Escape variants\n Esc: 'Escape',\n esc: 'Escape',\n escape: 'Escape',\n\n // Enter variants\n Return: 'Enter',\n return: 'Enter',\n enter: 'Enter',\n\n // Space variants\n ' ': 'Space',\n space: 'Space',\n Spacebar: 'Space',\n spacebar: 'Space',\n\n // Tab variants\n tab: 'Tab',\n\n // Backspace variants\n backspace: 'Backspace',\n\n // Delete variants\n Del: 'Delete',\n del: 'Delete',\n delete: 'Delete',\n\n // Arrow key variants\n Up: 'ArrowUp',\n up: 'ArrowUp',\n arrowup: 'ArrowUp',\n Down: 'ArrowDown',\n down: 'ArrowDown',\n arrowdown: 'ArrowDown',\n Left: 'ArrowLeft',\n left: 'ArrowLeft',\n arrowleft: 'ArrowLeft',\n Right: 'ArrowRight',\n right: 'ArrowRight',\n arrowright: 'ArrowRight',\n\n // Navigation variants\n home: 'Home',\n end: 'End',\n pageup: 'PageUp',\n pagedown: 'PageDown',\n PgUp: 'PageUp',\n PgDn: 'PageDown',\n pgup: 'PageUp',\n pgdn: 'PageDown',\n}\n\n/**\n * Normalizes a key name to its canonical form.\n *\n * Converts various key name formats (aliases, case variations) into the standard\n * canonical names used throughout the library. This enables a more forgiving API\n * where users can write keys in different ways and still get correct behavior.\n *\n * Normalization rules:\n * 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')\n * 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')\n * 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')\n * 4. Other keys → returned as-is (already canonical or unknown)\n *\n * @param key - The key name to normalize (can be an alias, lowercase, etc.)\n * @returns The canonical key name\n *\n * @example\n * ```ts\n * normalizeKeyName('esc') // 'Escape'\n * normalizeKeyName('a') // 'A'\n * normalizeKeyName('f1') // 'F1'\n * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)\n * ```\n */\nexport function isSingleLetterKey(key: string): boolean {\n return /^\\p{Letter}$/u.test(key)\n}\n\n/**\n * Normalizes a key name to its canonical form.\n *\n * @param key - The key name to normalize (can be an alias, lowercase, etc.)\n * @returns The canonical key name\n *\n * @example\n * ```ts\n * normalizeKeyName('esc') // 'Escape'\n * normalizeKeyName('a') // 'A'\n * normalizeKeyName('f1') // 'F1'\n * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)\n * ```\n */\nexport function normalizeKeyName(key: string): string {\n // key can be undefined in rare cases\n // (browser extensions synthesizing key events, accessibility tools, certain OS/browser combinations).\n if (!key) return ''\n // Check aliases first\n if (key in KEY_ALIASES) {\n return KEY_ALIASES[key]!\n }\n\n if (isSingleLetterKey(key)) {\n const upper = key.toUpperCase()\n // Some Unicode letters (e.g., 'ß') uppercase to multi-character sequences\n // ('SS'). Avoid changing the string length so the rest of the matching\n // logic can rely on a stable single-character key value.\n return upper.length === 1 ? upper : key\n }\n\n // Check if it's a function key (normalize case)\n const upperKey = key.toUpperCase()\n if (/^F([1-9]|1[0-2])$/.test(upperKey)) {\n return upperKey\n }\n\n return key\n}\n\n// =============================================================================\n// Display Symbols\n// =============================================================================\n\n/**\n * Modifier key symbols for macOS display.\n *\n * Used by formatting functions to display hotkeys with macOS-style symbols\n * (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides\n * a native macOS look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'\n * MAC_MODIFIER_SYMBOLS['Control'] // '⌃'\n * MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'\n * MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'\n * ```\n */\nexport const MAC_MODIFIER_SYMBOLS: Record<CanonicalModifier | 'Mod', string> = {\n Control: '⌃',\n Alt: '⌥',\n Shift: '⇧',\n Meta: '⌘',\n Mod: '⌘',\n}\n\n/**\n * Modifier key labels for macOS display.\n *\n * Used by formatting functions to display hotkeys with macOS-style text labels\n * (e.g., 'Control' for Control, 'Option' for Alt, 'Cmd' for Meta) instead of symbols.\n * This provides a familiar macOS look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * MAC_MODIFIER_LABELS['Control'] // 'control'\n * MAC_MODIFIER_LABELS['Alt'] // 'option'\n * MAC_MODIFIER_LABELS['Shift'] // 'shift'\n * MAC_MODIFIER_LABELS['Meta'] // 'cmd'\n * ```\n */\nexport const MAC_MODIFIER_LABELS: Record<CanonicalModifier | 'Mod', string> = {\n Control: 'Control',\n Alt: 'Option',\n Shift: 'Shift',\n Meta: 'Cmd',\n Mod: 'Cmd',\n}\n\n/**\n * Modifier key labels for Windows/Linux display.\n *\n * Used by formatting functions to display hotkeys with standard text labels\n * (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.\n * This provides a familiar Windows/Linux look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'\n * STANDARD_MODIFIER_LABELS['Meta'] // 'Win'\n * STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'\n * STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'\n * ```\n */\nexport const WINDOWS_MODIFIER_LABELS: Record<\n CanonicalModifier | 'Mod',\n string\n> = {\n Control: 'Ctrl',\n Alt: 'Alt',\n Shift: 'Shift',\n Meta: 'Win',\n Mod: 'Ctrl',\n}\n\nexport const LINUX_MODIFIER_LABELS: Record<CanonicalModifier | 'Mod', string> =\n {\n ...WINDOWS_MODIFIER_LABELS,\n Meta: 'Super',\n }\n\nexport const PUNCTUATION_KEY_DISPLAY_LABELS = {\n '`': 'Backquote',\n '\\\\': 'Backslash',\n '[': 'Left Bracket',\n ']': 'Right Bracket',\n ',': 'Comma',\n '=': 'Equal',\n '-': 'Minus',\n '.': 'Period',\n ';': 'Semicolon',\n} as const satisfies Record<string, string>\n\n/**\n * Special key symbols for display formatting.\n *\n * Maps certain keys to their visual symbols for better readability in hotkey displays.\n * Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter\n * instead of text labels.\n *\n * @example\n * ```ts\n * KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'\n * KEY_DISPLAY_SYMBOLS['Enter'] // '↵'\n * KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'\n * KEY_DISPLAY_SYMBOLS['Space'] // '␣'\n * ```\n */\nexport const KEY_DISPLAY_SYMBOLS = {\n ArrowUp: '↑',\n ArrowDown: '↓',\n ArrowLeft: '←',\n ArrowRight: '→',\n Enter: '↵',\n Escape: 'Esc',\n Backspace: '⌫',\n Delete: '⌦',\n Tab: '⇥',\n Space: '␣',\n} as const satisfies Record<string, string>\n"],"mappings":";;;;;;;;;;;;;;;;;AAyBA,SAAgB,iBAA8C;AAC5D,KAAI,OAAO,cAAc,YACvB,QAAO;CAIT,MAAM,WAAW,UAAU,UAAU,aAAa,IAAI;CAEtD,MAAM,YAAY,UAAU,WAAW,aAAa,IAAI;AAExD,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,KAAI,SAAS,SAAS,MAAM,IAAI,UAAU,SAAS,MAAM,CACvD,QAAO;AAET,QAAO;;;;;;;;;;;;;;;;AAiBT,MAAa,iBAA2C;CACtD;CACA;CACA;CACA;CACD;;;;;;;;AASD,MAAa,gBAAgB,IAAI,IAAY,eAAe;;;;;;;;;;;;;;;;;;;;AAqB5D,MAAa,mBAA8D;CAEzE,SAAS;CACT,MAAM;CACN,SAAS;CACT,MAAM;CAGN,OAAO;CACP,OAAO;CAGP,KAAK;CACL,QAAQ;CACR,KAAK;CACL,QAAQ;CAGR,SAAS;CACT,KAAK;CACL,MAAM;CACN,SAAS;CACT,KAAK;CACL,MAAM;CAGN,IAAI;CACJ,IAAI;CACJ,KAAK;CACL,KAAK;CAGL,kBAAkB;CAClB,KAAK;CACL,kBAAkB;CAClB,KAAK;CACN;;;;;;;;;;;;;;;;;;;;;;AAuBD,SAAgB,gBACd,UACA,WAAwC,gBAAgB,EACrC;AACnB,KAAI,aAAa,MACf,QAAO,aAAa,QAAQ,SAAS;AAEvC,QAAO;;;;;;;;AAST,MAAa,cAAc,IAAI,IAAe;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;AASF,MAAa,cAAc,IAAI,IAAe;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;AAQF,MAAa,gBAAgB,IAAI,IAAiB;CAChD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;AASF,MAAa,kBAAkB,IAAI,IAAmB;CACpD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;AASF,MAAa,eAAe,IAAI,IAAgB;CAC9C;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;;;;;;AAcF,MAAa,mBAAmB,IAAI,IAAoB;CACtD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;;;AAWF,MAAa,uBAA+C;CAC1D,WAAW;CACX,WAAW;CACX,aAAa;CACb,cAAc;CACd,OAAO;CACP,OAAO;CACP,OAAO;CACP,QAAQ;CACR,WAAW;CACX,OAAO;CACR;;;;;;;;;;;;;;;AAgBD,MAAa,WAAW,IAAI,IAAI;CAC9B,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACJ,CAAC;;;;;;;;;;;;;;;;;;;;AAqBF,MAAM,cAAsC;CAE1C,KAAK;CACL,KAAK;CACL,QAAQ;CAGR,QAAQ;CACR,QAAQ;CACR,OAAO;CAGP,KAAK;CACL,OAAO;CACP,UAAU;CACV,UAAU;CAGV,KAAK;CAGL,WAAW;CAGX,KAAK;CACL,KAAK;CACL,QAAQ;CAGR,IAAI;CACJ,IAAI;CACJ,SAAS;CACT,MAAM;CACN,MAAM;CACN,WAAW;CACX,MAAM;CACN,MAAM;CACN,WAAW;CACX,OAAO;CACP,OAAO;CACP,YAAY;CAGZ,MAAM;CACN,KAAK;CACL,QAAQ;CACR,UAAU;CACV,MAAM;CACN,MAAM;CACN,MAAM;CACN,MAAM;CACP;;;;;;;;;;;;;;;;;;;;;;;;;AA0BD,SAAgB,kBAAkB,KAAsB;AACtD,QAAO,gBAAgB,KAAK,IAAI;;;;;;;;;;;;;;;;AAiBlC,SAAgB,iBAAiB,KAAqB;AAGpD,KAAI,CAAC,IAAK,QAAO;AAEjB,KAAI,OAAO,YACT,QAAO,YAAY;AAGrB,KAAI,kBAAkB,IAAI,EAAE;EAC1B,MAAM,QAAQ,IAAI,aAAa;AAI/B,SAAO,MAAM,WAAW,IAAI,QAAQ;;CAItC,MAAM,WAAW,IAAI,aAAa;AAClC,KAAI,oBAAoB,KAAK,SAAS,CACpC,QAAO;AAGT,QAAO;;;;;;;;;;;;;;;;;AAsBT,MAAa,uBAAkE;CAC7E,SAAS;CACT,KAAK;CACL,OAAO;CACP,MAAM;CACN,KAAK;CACN;;;;;;;;;;;;;;;;AAiBD,MAAa,sBAAiE;CAC5E,SAAS;CACT,KAAK;CACL,OAAO;CACP,MAAM;CACN,KAAK;CACN;;;;;;;;;;;;;;;;AAiBD,MAAa,0BAGT;CACF,SAAS;CACT,KAAK;CACL,OAAO;CACP,MAAM;CACN,KAAK;CACN;AAED,MAAa,wBACX;CACE,GAAG;CACH,MAAM;CACP;AAEH,MAAa,iCAAiC;CAC5C,KAAK;CACL,MAAM;CACN,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACN;;;;;;;;;;;;;;;;AAiBD,MAAa,sBAAsB;CACjC,SAAS;CACT,WAAW;CACX,WAAW;CACX,YAAY;CACZ,OAAO;CACP,QAAQ;CACR,WAAW;CACX,QAAQ;CACR,KAAK;CACL,OAAO;CACR"}
|
|
1
|
+
{"version":3,"file":"constants.cjs","names":["SHARED_NAMED_KEYS","LOGICAL_ONLY_NAMED_KEYS"],"sources":["../src/constants.ts"],"sourcesContent":["import { LOGICAL_ONLY_NAMED_KEYS, SHARED_NAMED_KEYS } from './_named-keys'\nimport type {\n CanonicalModifier,\n EditingKey,\n FunctionKey,\n LetterKey,\n NavigationKey,\n NumberKey,\n PunctuationKey,\n} from './key.types'\n\n/**\n * Canonical order for modifiers in normalized hotkey strings.\n *\n * Defines the standard order in which modifiers should appear when formatting\n * hotkeys. This ensures consistent, predictable output across the library.\n *\n * Order: Control → Alt → Shift → Meta\n *\n * @example\n * ```ts\n * // Input: 'Shift+Control+Meta+S'\n * // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)\n * ```\n */\nexport const MODIFIER_ORDER: Array<CanonicalModifier> = [\n 'Control',\n 'Alt',\n 'Shift',\n 'Meta',\n]\n\n/**\n * Set of canonical modifier key names for fast lookup.\n *\n * Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when\n * a modifier is released so we can clear non-modifier keys whose keyup events\n * may have been swallowed by the OS (e.g. macOS Cmd+key combos).\n */\nexport const MODIFIER_KEYS = new Set<string>(MODIFIER_ORDER)\n\n/**\n * Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.\n *\n * This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')\n * which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the\n * platform-adaptive 'Mod' token.\n *\n * The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`\n * based on the detected platform (Command on Mac, Control elsewhere).\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * MODIFIER_ALIASES['Ctrl'] // 'Control'\n * MODIFIER_ALIASES['Cmd'] // 'Meta'\n * MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)\n * ```\n */\nexport const MODIFIER_ALIASES: Record<string, CanonicalModifier | 'Mod'> = {\n // Control variants\n Control: 'Control',\n Ctrl: 'Control',\n control: 'Control',\n ctrl: 'Control',\n\n // Shift variants\n Shift: 'Shift',\n shift: 'Shift',\n\n // Alt variants\n Alt: 'Alt',\n Option: 'Alt',\n alt: 'Alt',\n option: 'Alt',\n\n // Meta/Command variants\n Command: 'Meta',\n Cmd: 'Meta',\n Meta: 'Meta',\n command: 'Meta',\n cmd: 'Meta',\n meta: 'Meta',\n\n // DOM `KeyboardEvent.key` spellings for the meta / Windows key\n OS: 'Meta',\n os: 'Meta',\n Win: 'Meta',\n win: 'Meta',\n\n // Platform-adaptive (resolved at runtime)\n CommandOrControl: 'Mod',\n Mod: 'Mod',\n commandorcontrol: 'Mod',\n mod: 'Mod',\n}\n\n/**\n * Set of all valid letter keys (A-Z).\n *\n * Used for validation and type checking. Letter keys are matched case-insensitively\n * in hotkey matching, but normalized to uppercase in canonical form.\n */\nexport const LETTER_KEYS = new Set<LetterKey>([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n 'H',\n 'I',\n 'J',\n 'K',\n 'L',\n 'M',\n 'N',\n 'O',\n 'P',\n 'Q',\n 'R',\n 'S',\n 'T',\n 'U',\n 'V',\n 'W',\n 'X',\n 'Y',\n 'Z',\n])\n\n/**\n * Set of all valid number keys (0-9).\n *\n * Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid\n * layout-dependent behavior.\n */\nexport const NUMBER_KEYS = new Set<NumberKey>([\n '0',\n '1',\n '2',\n '3',\n '4',\n '5',\n '6',\n '7',\n '8',\n '9',\n])\n\n/**\n * Set of all valid function keys (F1-F24).\n *\n * Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,\n * Alt+F4 to close windows) and application-specific commands.\n */\nexport const FUNCTION_KEYS = new Set<FunctionKey>([\n 'F1',\n 'F2',\n 'F3',\n 'F4',\n 'F5',\n 'F6',\n 'F7',\n 'F8',\n 'F9',\n 'F10',\n 'F11',\n 'F12',\n 'F13',\n 'F14',\n 'F15',\n 'F16',\n 'F17',\n 'F18',\n 'F19',\n 'F20',\n 'F21',\n 'F22',\n 'F23',\n 'F24',\n])\n\n/**\n * Set of all valid navigation keys for cursor movement and document navigation.\n *\n * Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).\n * These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or\n * navigation shortcuts (Alt+ArrowLeft for back).\n */\nexport const NAVIGATION_KEYS = new Set<NavigationKey>([\n 'ArrowUp',\n 'ArrowDown',\n 'ArrowLeft',\n 'ArrowRight',\n 'Home',\n 'End',\n 'PageUp',\n 'PageDown',\n])\n\n/**\n * Set of all valid editing and special keys.\n *\n * Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and\n * special actions (Escape, Space). These keys are frequently combined with modifiers\n * for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).\n */\nexport const EDITING_KEYS = new Set<EditingKey>([\n 'Enter',\n 'Escape',\n 'Space',\n 'Tab',\n 'Backspace',\n 'Delete',\n])\n\n/**\n * Set of all valid punctuation keys commonly used in keyboard shortcuts.\n *\n * These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,\n * typically US keyboard layout). Common shortcuts include:\n * - `Mod+/` - Toggle comment\n * - `Mod+[` / `Mod+]` - Indent/outdent\n * - `Mod+=` / `Mod+-` - Zoom in/out\n *\n * Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.\n */\nexport const PUNCTUATION_KEYS = new Set<PunctuationKey>([\n '/',\n '[',\n ']',\n '\\\\',\n '=',\n '-',\n ',',\n '.',\n ';',\n '`',\n \"'\",\n '+',\n '?',\n '!',\n '@',\n '#',\n '$',\n '%',\n '^',\n '&',\n '*',\n '(',\n ')',\n '_',\n '{',\n '}',\n '|',\n ':',\n '\"',\n '<',\n '>',\n '~',\n])\n\n/**\n * Maps `KeyboardEvent.code` values for punctuation keys to their canonical characters.\n *\n * On macOS, holding the Option (Alt) key transforms punctuation keys into special characters\n * (e.g., Option+Minus → en-dash '–'), causing `event.key` to differ from the expected character.\n * However, `event.code` still reports the physical key (e.g., 'Minus'). This map enables\n * falling back to `event.code` for punctuation keys, similar to the existing `Key*`/`Digit*`\n * fallbacks for letters and digits.\n */\nexport const PUNCTUATION_CODE_MAP: Record<string, string> = {\n Backquote: '`',\n Backslash: '\\\\',\n BracketLeft: '[',\n BracketRight: ']',\n Comma: ',',\n Equal: '=',\n Minus: '-',\n Period: '.',\n Quote: \"'\",\n Semicolon: ';',\n Slash: '/',\n}\n\n/**\n * Set of all valid non-modifier keys.\n *\n * This is the union of all key type sets (letters, numbers, function keys, navigation,\n * editing, and punctuation). Used primarily for validation to check if a key string\n * is recognized and will have type-safe autocomplete support.\n *\n * @see {@link LETTER_KEYS}\n * @see {@link NUMBER_KEYS}\n * @see {@link FUNCTION_KEYS}\n * @see {@link NAVIGATION_KEYS}\n * @see {@link EDITING_KEYS}\n * @see {@link PUNCTUATION_KEYS}\n */\nexport const ALL_KEYS = new Set([\n ...LETTER_KEYS,\n ...NUMBER_KEYS,\n ...FUNCTION_KEYS,\n ...NAVIGATION_KEYS,\n ...EDITING_KEYS,\n ...PUNCTUATION_KEYS,\n ...SHARED_NAMED_KEYS,\n ...LOGICAL_ONLY_NAMED_KEYS,\n])\n\n// Share canonical spellings with validation, including newly supported named keys.\nconst CANONICAL_KEY_NAMES = new Map<string, string>(\n Array.from(ALL_KEYS, (key) => [key.toLowerCase(), key]),\n)\n\n/**\n * Maps key name aliases to their canonical form.\n *\n * Handles common variations and alternative names for keys to provide a more\n * forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and\n * they'll all normalize to 'Escape'.\n *\n * This map is used internally by `normalizeKeyName()` to convert user input\n * into the canonical key names used throughout the library.\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * KEY_ALIASES['Esc'] // 'Escape'\n * KEY_ALIASES['Del'] // 'Delete'\n * KEY_ALIASES['Up'] // 'ArrowUp'\n * ```\n */\nconst KEY_ALIASES: Record<string, string> = {\n Plus: '+',\n plus: '+',\n // Escape variants\n Esc: 'Escape',\n esc: 'Escape',\n escape: 'Escape',\n\n // Enter variants\n Return: 'Enter',\n return: 'Enter',\n enter: 'Enter',\n\n // Space variants\n ' ': 'Space',\n space: 'Space',\n Spacebar: 'Space',\n spacebar: 'Space',\n\n // Tab variants\n tab: 'Tab',\n\n // Backspace variants\n backspace: 'Backspace',\n\n // Delete variants\n Del: 'Delete',\n del: 'Delete',\n delete: 'Delete',\n\n // Arrow key variants\n Up: 'ArrowUp',\n up: 'ArrowUp',\n arrowup: 'ArrowUp',\n Down: 'ArrowDown',\n down: 'ArrowDown',\n arrowdown: 'ArrowDown',\n Left: 'ArrowLeft',\n left: 'ArrowLeft',\n arrowleft: 'ArrowLeft',\n Right: 'ArrowRight',\n right: 'ArrowRight',\n arrowright: 'ArrowRight',\n\n // Navigation variants\n home: 'Home',\n end: 'End',\n pageup: 'PageUp',\n pagedown: 'PageDown',\n PgUp: 'PageUp',\n PgDn: 'PageDown',\n pgup: 'PageUp',\n pgdn: 'PageDown',\n}\n\n/**\n * Normalizes a key name to its canonical form.\n *\n * Converts various key name formats (aliases, case variations) into the standard\n * canonical names used throughout the library. This enables a more forgiving API\n * where users can write keys in different ways and still get correct behavior.\n *\n * Normalization rules:\n * 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')\n * 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')\n * 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')\n * 4. Other keys → returned as-is (already canonical or unknown)\n *\n * @param key - The key name to normalize (can be an alias, lowercase, etc.)\n * @returns The canonical key name\n *\n * @example\n * ```ts\n * normalizeKeyName('esc') // 'Escape'\n * normalizeKeyName('a') // 'A'\n * normalizeKeyName('f1') // 'F1'\n * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)\n * ```\n */\nexport function isSingleLetterKey(key: string): boolean {\n return /^\\p{Letter}$/u.test(key)\n}\n\n/**\n * Normalizes a key name to its canonical form.\n *\n * @param key - The key name to normalize (can be an alias, lowercase, etc.)\n * @returns The canonical key name\n *\n * @example\n * ```ts\n * normalizeKeyName('esc') // 'Escape'\n * normalizeKeyName('a') // 'A'\n * normalizeKeyName('f1') // 'F1'\n * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)\n * ```\n */\nexport function normalizeKeyName(key: string): string {\n // key can be undefined in rare cases\n // (browser extensions synthesizing key events, accessibility tools, certain OS/browser combinations).\n if (!key) return ''\n // Check aliases first\n if (key in KEY_ALIASES) {\n return KEY_ALIASES[key]!\n }\n\n if (isSingleLetterKey(key)) {\n const upper = key.toUpperCase()\n // Some Unicode letters (e.g., 'ß') uppercase to multi-character sequences\n // ('SS'). Avoid changing the string length so the rest of the matching\n // logic can rely on a stable single-character key value.\n return upper.length === 1 ? upper : key\n }\n\n return CANONICAL_KEY_NAMES.get(key.toLowerCase()) ?? key\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAyBA,MAAa,iBAA2C;CACtD;CACA;CACA;CACA;CACD;;;;;;;;AASD,MAAa,gBAAgB,IAAI,IAAY,eAAe;;;;;;;;;;;;;;;;;;;;AAqB5D,MAAa,mBAA8D;CAEzE,SAAS;CACT,MAAM;CACN,SAAS;CACT,MAAM;CAGN,OAAO;CACP,OAAO;CAGP,KAAK;CACL,QAAQ;CACR,KAAK;CACL,QAAQ;CAGR,SAAS;CACT,KAAK;CACL,MAAM;CACN,SAAS;CACT,KAAK;CACL,MAAM;CAGN,IAAI;CACJ,IAAI;CACJ,KAAK;CACL,KAAK;CAGL,kBAAkB;CAClB,KAAK;CACL,kBAAkB;CAClB,KAAK;CACN;;;;;;;AAQD,MAAa,cAAc,IAAI,IAAe;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;AASF,MAAa,cAAc,IAAI,IAAe;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;AAQF,MAAa,gBAAgB,IAAI,IAAiB;CAChD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;AASF,MAAa,kBAAkB,IAAI,IAAmB;CACpD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;AASF,MAAa,eAAe,IAAI,IAAgB;CAC9C;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;;;;;;AAcF,MAAa,mBAAmB,IAAI,IAAoB;CACtD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;;;;;;;AAWF,MAAa,uBAA+C;CAC1D,WAAW;CACX,WAAW;CACX,aAAa;CACb,cAAc;CACd,OAAO;CACP,OAAO;CACP,OAAO;CACP,QAAQ;CACR,OAAO;CACP,WAAW;CACX,OAAO;CACR;;;;;;;;;;;;;;;AAgBD,MAAa,WAAW,IAAI,IAAI;CAC9B,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAGA;CACH,GAAGC;CACJ,CAAC;AAGF,MAAM,sBAAsB,IAAI,IAC9B,MAAM,KAAK,WAAW,QAAQ,CAAC,IAAI,aAAa,EAAE,IAAI,CAAC,CACxD;;;;;;;;;;;;;;;;;;;;AAqBD,MAAM,cAAsC;CAC1C,MAAM;CACN,MAAM;CAEN,KAAK;CACL,KAAK;CACL,QAAQ;CAGR,QAAQ;CACR,QAAQ;CACR,OAAO;CAGP,KAAK;CACL,OAAO;CACP,UAAU;CACV,UAAU;CAGV,KAAK;CAGL,WAAW;CAGX,KAAK;CACL,KAAK;CACL,QAAQ;CAGR,IAAI;CACJ,IAAI;CACJ,SAAS;CACT,MAAM;CACN,MAAM;CACN,WAAW;CACX,MAAM;CACN,MAAM;CACN,WAAW;CACX,OAAO;CACP,OAAO;CACP,YAAY;CAGZ,MAAM;CACN,KAAK;CACL,QAAQ;CACR,UAAU;CACV,MAAM;CACN,MAAM;CACN,MAAM;CACN,MAAM;CACP;;;;;;;;;;;;;;;;;;;;;;;;;AA0BD,SAAgB,kBAAkB,KAAsB;AACtD,QAAO,gBAAgB,KAAK,IAAI;;;;;;;;;;;;;;;;AAiBlC,SAAgB,iBAAiB,KAAqB;AAGpD,KAAI,CAAC,IAAK,QAAO;AAEjB,KAAI,OAAO,YACT,QAAO,YAAY;AAGrB,KAAI,kBAAkB,IAAI,EAAE;EAC1B,MAAM,QAAQ,IAAI,aAAa;AAI/B,SAAO,MAAM,WAAW,IAAI,QAAQ;;AAGtC,QAAO,oBAAoB,IAAI,IAAI,aAAa,CAAC,IAAI"}
|
package/dist/constants.d.cts
CHANGED
|
@@ -1,22 +1,6 @@
|
|
|
1
|
-
import { CanonicalModifier, EditingKey, FunctionKey, LetterKey, NavigationKey, NumberKey, PunctuationKey } from "./
|
|
1
|
+
import { CanonicalModifier, EditingKey, FunctionKey, LetterKey, NavigationKey, NumberKey, PunctuationKey } from "./key.types.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/constants.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Detects the current platform based on browser navigator properties.
|
|
6
|
-
*
|
|
7
|
-
* Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
|
|
8
|
-
* Control elsewhere) and for platform-specific hotkey formatting.
|
|
9
|
-
*
|
|
10
|
-
* @returns The detected platform: 'mac', 'windows', or 'linux'
|
|
11
|
-
* @remarks Defaults to 'linux' in SSR environments where navigator is undefined
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
|
|
16
|
-
* const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
declare function detectPlatform(): 'mac' | 'windows' | 'linux';
|
|
20
4
|
/**
|
|
21
5
|
* Canonical order for modifiers in normalized hotkey strings.
|
|
22
6
|
*
|
|
@@ -60,28 +44,6 @@ declare const MODIFIER_KEYS: Set<string>;
|
|
|
60
44
|
* ```
|
|
61
45
|
*/
|
|
62
46
|
declare const MODIFIER_ALIASES: Record<string, CanonicalModifier | 'Mod'>;
|
|
63
|
-
/**
|
|
64
|
-
* Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
|
|
65
|
-
*
|
|
66
|
-
* The 'Mod' token represents the "primary modifier" on each platform:
|
|
67
|
-
* - macOS: 'Meta' (Command key ⌘)
|
|
68
|
-
* - Windows/Linux: 'Control' (Ctrl key)
|
|
69
|
-
*
|
|
70
|
-
* This enables cross-platform hotkey definitions like 'Mod+S' that automatically
|
|
71
|
-
* map to Command+S on Mac and Ctrl+S on Windows/Linux.
|
|
72
|
-
*
|
|
73
|
-
* @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
|
|
74
|
-
* @param platform - The target platform. Defaults to auto-detection.
|
|
75
|
-
* @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```ts
|
|
79
|
-
* resolveModifier('Mod', 'mac') // 'Meta'
|
|
80
|
-
* resolveModifier('Mod', 'windows') // 'Control'
|
|
81
|
-
* resolveModifier('Control', 'mac') // 'Control' (unchanged)
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
declare function resolveModifier(modifier: CanonicalModifier | 'Mod', platform?: 'mac' | 'windows' | 'linux'): CanonicalModifier;
|
|
85
47
|
/**
|
|
86
48
|
* Set of all valid letter keys (A-Z).
|
|
87
49
|
*
|
|
@@ -98,7 +60,7 @@ declare const LETTER_KEYS: Set<LetterKey>;
|
|
|
98
60
|
*/
|
|
99
61
|
declare const NUMBER_KEYS: Set<NumberKey>;
|
|
100
62
|
/**
|
|
101
|
-
* Set of all valid function keys (F1-
|
|
63
|
+
* Set of all valid function keys (F1-F24).
|
|
102
64
|
*
|
|
103
65
|
* Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,
|
|
104
66
|
* Alt+F4 to close windows) and application-specific commands.
|
|
@@ -157,7 +119,7 @@ declare const PUNCTUATION_CODE_MAP: Record<string, string>;
|
|
|
157
119
|
* @see {@link EDITING_KEYS}
|
|
158
120
|
* @see {@link PUNCTUATION_KEYS}
|
|
159
121
|
*/
|
|
160
|
-
declare const ALL_KEYS: Set<LetterKey | NumberKey | FunctionKey | NavigationKey | EditingKey | PunctuationKey>;
|
|
122
|
+
declare const ALL_KEYS: Set<LetterKey | NumberKey | FunctionKey | NavigationKey | EditingKey | PunctuationKey | "CapsLock" | "ContextMenu" | "Convert" | "KanaMode" | "NonConvert" | "Help" | "Insert" | "NumLock" | "Fn" | "FnLock" | "PrintScreen" | "ScrollLock" | "Pause" | "BrowserBack" | "BrowserFavorites" | "BrowserForward" | "BrowserHome" | "BrowserRefresh" | "BrowserSearch" | "BrowserStop" | "Eject" | "LaunchMail" | "MediaPlayPause" | "MediaStop" | "MediaTrackNext" | "MediaTrackPrevious" | "Power" | "AudioVolumeDown" | "AudioVolumeMute" | "AudioVolumeUp" | "WakeUp" | "Hyper" | "Super" | "Again" | "Copy" | "Cut" | "Find" | "Open" | "Paste" | "Props" | "Select" | "Undo" | "Hiragana" | "Katakana" | "Clear" | "Cancel" | "Standby" | "LaunchApplication1" | "LaunchApplication2" | "LaunchMediaPlayer" | "HangulMode" | "HanjaMode" | "Zenkaku" | "Hankaku" | "ZenkakuHankaku">;
|
|
161
123
|
/**
|
|
162
124
|
* Normalizes a key name to its canonical form.
|
|
163
125
|
*
|
|
@@ -198,93 +160,6 @@ declare function isSingleLetterKey(key: string): boolean;
|
|
|
198
160
|
* ```
|
|
199
161
|
*/
|
|
200
162
|
declare function normalizeKeyName(key: string): string;
|
|
201
|
-
/**
|
|
202
|
-
* Modifier key symbols for macOS display.
|
|
203
|
-
*
|
|
204
|
-
* Used by formatting functions to display hotkeys with macOS-style symbols
|
|
205
|
-
* (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides
|
|
206
|
-
* a native macOS look and feel in hotkey displays.
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```ts
|
|
210
|
-
* MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'
|
|
211
|
-
* MAC_MODIFIER_SYMBOLS['Control'] // '⌃'
|
|
212
|
-
* MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'
|
|
213
|
-
* MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
declare const MAC_MODIFIER_SYMBOLS: Record<CanonicalModifier | 'Mod', string>;
|
|
217
|
-
/**
|
|
218
|
-
* Modifier key labels for macOS display.
|
|
219
|
-
*
|
|
220
|
-
* Used by formatting functions to display hotkeys with macOS-style text labels
|
|
221
|
-
* (e.g., 'Control' for Control, 'Option' for Alt, 'Cmd' for Meta) instead of symbols.
|
|
222
|
-
* This provides a familiar macOS look and feel in hotkey displays.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```ts
|
|
226
|
-
* MAC_MODIFIER_LABELS['Control'] // 'control'
|
|
227
|
-
* MAC_MODIFIER_LABELS['Alt'] // 'option'
|
|
228
|
-
* MAC_MODIFIER_LABELS['Shift'] // 'shift'
|
|
229
|
-
* MAC_MODIFIER_LABELS['Meta'] // 'cmd'
|
|
230
|
-
* ```
|
|
231
|
-
*/
|
|
232
|
-
declare const MAC_MODIFIER_LABELS: Record<CanonicalModifier | 'Mod', string>;
|
|
233
|
-
/**
|
|
234
|
-
* Modifier key labels for Windows/Linux display.
|
|
235
|
-
*
|
|
236
|
-
* Used by formatting functions to display hotkeys with standard text labels
|
|
237
|
-
* (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.
|
|
238
|
-
* This provides a familiar Windows/Linux look and feel in hotkey displays.
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```ts
|
|
242
|
-
* STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'
|
|
243
|
-
* STANDARD_MODIFIER_LABELS['Meta'] // 'Win'
|
|
244
|
-
* STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'
|
|
245
|
-
* STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'
|
|
246
|
-
* ```
|
|
247
|
-
*/
|
|
248
|
-
declare const WINDOWS_MODIFIER_LABELS: Record<CanonicalModifier | 'Mod', string>;
|
|
249
|
-
declare const LINUX_MODIFIER_LABELS: Record<CanonicalModifier | 'Mod', string>;
|
|
250
|
-
declare const PUNCTUATION_KEY_DISPLAY_LABELS: {
|
|
251
|
-
readonly '`': "Backquote";
|
|
252
|
-
readonly '\\': "Backslash";
|
|
253
|
-
readonly '[': "Left Bracket";
|
|
254
|
-
readonly ']': "Right Bracket";
|
|
255
|
-
readonly ',': "Comma";
|
|
256
|
-
readonly '=': "Equal";
|
|
257
|
-
readonly '-': "Minus";
|
|
258
|
-
readonly '.': "Period";
|
|
259
|
-
readonly ';': "Semicolon";
|
|
260
|
-
};
|
|
261
|
-
/**
|
|
262
|
-
* Special key symbols for display formatting.
|
|
263
|
-
*
|
|
264
|
-
* Maps certain keys to their visual symbols for better readability in hotkey displays.
|
|
265
|
-
* Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter
|
|
266
|
-
* instead of text labels.
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```ts
|
|
270
|
-
* KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'
|
|
271
|
-
* KEY_DISPLAY_SYMBOLS['Enter'] // '↵'
|
|
272
|
-
* KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'
|
|
273
|
-
* KEY_DISPLAY_SYMBOLS['Space'] // '␣'
|
|
274
|
-
* ```
|
|
275
|
-
*/
|
|
276
|
-
declare const KEY_DISPLAY_SYMBOLS: {
|
|
277
|
-
readonly ArrowUp: "↑";
|
|
278
|
-
readonly ArrowDown: "↓";
|
|
279
|
-
readonly ArrowLeft: "←";
|
|
280
|
-
readonly ArrowRight: "→";
|
|
281
|
-
readonly Enter: "↵";
|
|
282
|
-
readonly Escape: "Esc";
|
|
283
|
-
readonly Backspace: "⌫";
|
|
284
|
-
readonly Delete: "⌦";
|
|
285
|
-
readonly Tab: "⇥";
|
|
286
|
-
readonly Space: "␣";
|
|
287
|
-
};
|
|
288
163
|
//#endregion
|
|
289
|
-
export { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS,
|
|
164
|
+
export { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, LETTER_KEYS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_CODE_MAP, PUNCTUATION_KEYS, isSingleLetterKey, normalizeKeyName };
|
|
290
165
|
//# sourceMappingURL=constants.d.cts.map
|