@tanstack/hotkeys 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/dist/{manager.utils.js → _event-target.js} +2 -41
- package/dist/_keyboard-event.js +75 -0
- package/dist/_match.js +48 -0
- package/dist/_named-keys.d.ts +6 -0
- package/dist/_named-keys.js +65 -0
- package/dist/_recorder-chord.js +42 -0
- package/dist/_recording-guard.js +55 -0
- package/dist/_registration.js +49 -0
- package/dist/conflicts.d.ts +36 -0
- package/dist/conflicts.js +54 -0
- package/dist/constants.d.ts +16 -144
- package/dist/constants.js +55 -172
- package/dist/display-labels.d.ts +90 -0
- package/dist/display-labels.js +112 -0
- package/dist/format.d.ts +48 -12
- package/dist/format.js +35 -39
- package/dist/hint.d.ts +20 -0
- package/dist/hint.js +23 -0
- package/dist/hotkey-manager.d.ts +14 -12
- package/dist/hotkey-manager.js +70 -21
- package/dist/hotkey-recorder.d.ts +8 -8
- package/dist/hotkey-recorder.js +74 -15
- package/dist/hotkey-sequence-recorder.d.ts +13 -9
- package/dist/hotkey-sequence-recorder.js +90 -28
- package/dist/{hotkey.d.ts → hotkey.types.d.ts} +58 -126
- package/dist/index.d.ts +14 -9
- package/dist/index.js +10 -6
- package/dist/key-state-tracker.d.ts +4 -7
- package/dist/key-state-tracker.js +30 -27
- package/dist/key.types.d.ts +79 -0
- package/dist/match.d.ts +24 -11
- package/dist/match.js +19 -30
- package/dist/parse.d.ts +18 -15
- package/dist/parse.js +39 -22
- package/dist/platform.d.ts +41 -0
- package/dist/platform.js +52 -0
- package/dist/recorder-options.d.ts +31 -0
- package/dist/sequence-manager.d.ts +27 -15
- package/dist/sequence-manager.js +61 -30
- package/dist/validate.d.ts +16 -8
- package/dist/validate.js +8 -13
- package/package.json +8 -13
- package/dist/constants.cjs +0 -535
- package/dist/constants.cjs.map +0 -1
- package/dist/constants.d.cts +0 -290
- package/dist/constants.js.map +0 -1
- package/dist/format.cjs +0 -91
- package/dist/format.cjs.map +0 -1
- package/dist/format.d.cts +0 -60
- package/dist/format.js.map +0 -1
- package/dist/hotkey-manager.cjs +0 -383
- package/dist/hotkey-manager.cjs.map +0 -1
- package/dist/hotkey-manager.d.cts +0 -230
- package/dist/hotkey-manager.js.map +0 -1
- package/dist/hotkey-recorder.cjs +0 -179
- package/dist/hotkey-recorder.cjs.map +0 -1
- package/dist/hotkey-recorder.d.cts +0 -116
- package/dist/hotkey-recorder.js.map +0 -1
- package/dist/hotkey-sequence-recorder.cjs +0 -192
- package/dist/hotkey-sequence-recorder.cjs.map +0 -1
- package/dist/hotkey-sequence-recorder.d.cts +0 -79
- package/dist/hotkey-sequence-recorder.js.map +0 -1
- package/dist/hotkey.d.cts +0 -301
- package/dist/index.cjs +0 -63
- package/dist/index.d.cts +0 -13
- package/dist/key-state-tracker.cjs +0 -197
- package/dist/key-state-tracker.cjs.map +0 -1
- package/dist/key-state-tracker.d.cts +0 -107
- package/dist/key-state-tracker.js.map +0 -1
- package/dist/manager.utils.cjs +0 -115
- 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/match.cjs +0 -153
- package/dist/match.cjs.map +0 -1
- package/dist/match.d.cts +0 -85
- package/dist/match.js.map +0 -1
- package/dist/parse.cjs +0 -228
- package/dist/parse.cjs.map +0 -1
- package/dist/parse.d.cts +0 -128
- package/dist/parse.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/dist/sequence-manager.cjs +0 -415
- package/dist/sequence-manager.cjs.map +0 -1
- package/dist/sequence-manager.d.cts +0 -157
- package/dist/sequence-manager.js.map +0 -1
- package/dist/validate.cjs +0 -116
- package/dist/validate.cjs.map +0 -1
- package/dist/validate.d.cts +0 -56
- package/dist/validate.js.map +0 -1
- package/src/constants.ts +0 -603
- package/src/format.ts +0 -142
- package/src/hotkey-manager.ts +0 -759
- package/src/hotkey-recorder.ts +0 -272
- package/src/hotkey-sequence-recorder.ts +0 -328
- package/src/hotkey.ts +0 -436
- package/src/index.ts +0 -11
- package/src/key-state-tracker.ts +0 -249
- package/src/manager.utils.ts +0 -231
- package/src/match.ts +0 -259
- package/src/parse.ts +0 -328
- package/src/recorder-chord.ts +0 -25
- package/src/sequence-manager.ts +0 -734
- package/src/validate.ts +0 -171
package/dist/match.cjs
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
const require_constants = require('./constants.cjs');
|
|
2
|
-
const require_parse = require('./parse.cjs');
|
|
3
|
-
|
|
4
|
-
//#region src/match.ts
|
|
5
|
-
/**
|
|
6
|
-
* Checks if a KeyboardEvent matches a hotkey.
|
|
7
|
-
*
|
|
8
|
-
* Uses the `key` property from KeyboardEvent for matching, with a fallback to `code`
|
|
9
|
-
* for letter keys, digit keys (0-9), and punctuation keys when `key` produces special
|
|
10
|
-
* characters (e.g., macOS Option+letter, Shift+number, or Option+punctuation).
|
|
11
|
-
* Letter keys are matched case-insensitively.
|
|
12
|
-
*
|
|
13
|
-
* Also handles "dead key" events where `event.key` is `'Dead'` instead of the expected
|
|
14
|
-
* character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,
|
|
15
|
-
* Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.
|
|
16
|
-
* In these cases, `event.code` is used to determine the physical key.
|
|
17
|
-
*
|
|
18
|
-
* @param event - The KeyboardEvent to check
|
|
19
|
-
* @param hotkey - The hotkey string or ParsedHotkey to match against
|
|
20
|
-
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
21
|
-
* @returns True if the event matches the hotkey
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* document.addEventListener('keydown', (event) => {
|
|
26
|
-
* if (matchesKeyboardEvent(event, 'Mod+S')) {
|
|
27
|
-
* event.preventDefault()
|
|
28
|
-
* handleSave()
|
|
29
|
-
* }
|
|
30
|
-
* })
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
function matchesKeyboardEvent(event, hotkey, platform = require_constants.detectPlatform()) {
|
|
34
|
-
const parsed = typeof hotkey === "string" ? require_parse.parseHotkey(hotkey, platform) : hotkey;
|
|
35
|
-
if (event.ctrlKey !== parsed.ctrl) return false;
|
|
36
|
-
if (event.shiftKey !== parsed.shift) return false;
|
|
37
|
-
if (event.altKey !== parsed.alt) return false;
|
|
38
|
-
if (event.metaKey !== parsed.meta) return false;
|
|
39
|
-
const eventKey = require_constants.normalizeKeyName(event.key);
|
|
40
|
-
const hotkeyKey = parsed.key;
|
|
41
|
-
if (eventKey !== "Dead" && eventKey.length === 1 && hotkeyKey.length === 1) {
|
|
42
|
-
if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) return true;
|
|
43
|
-
if (require_constants.isSingleLetterKey(eventKey) && (/^[A-Za-z]$/.test(eventKey) || !event.altKey)) return false;
|
|
44
|
-
}
|
|
45
|
-
if (event.code && (eventKey === "Dead" || eventKey.length === 1 && hotkeyKey.length === 1)) {
|
|
46
|
-
if (event.code.startsWith("Key")) {
|
|
47
|
-
const codeLetter = event.code.slice(3);
|
|
48
|
-
if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) return codeLetter.toUpperCase() === hotkeyKey.toUpperCase();
|
|
49
|
-
}
|
|
50
|
-
if (event.code.startsWith("Digit")) {
|
|
51
|
-
const codeDigit = event.code.slice(5);
|
|
52
|
-
if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) return codeDigit === hotkeyKey;
|
|
53
|
-
}
|
|
54
|
-
if (event.code in require_constants.PUNCTUATION_CODE_MAP) return require_constants.PUNCTUATION_CODE_MAP[event.code] === hotkeyKey;
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
return eventKey === hotkeyKey;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Creates a keyboard event handler that calls the callback when the hotkey matches.
|
|
61
|
-
*
|
|
62
|
-
* @param hotkey - The hotkey string or ParsedHotkey to match
|
|
63
|
-
* @param callback - The function to call when the hotkey matches
|
|
64
|
-
* @param options - Options for matching and handling
|
|
65
|
-
* @returns A function that can be used as an event handler
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {
|
|
70
|
-
* console.log(`${hotkey} was pressed`)
|
|
71
|
-
* handleSave()
|
|
72
|
-
* })
|
|
73
|
-
*
|
|
74
|
-
* document.addEventListener('keydown', handler)
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
function createHotkeyHandler(hotkey, callback, options = {}) {
|
|
78
|
-
const { preventDefault = true, stopPropagation = true, platform } = options;
|
|
79
|
-
const resolvedPlatform = platform ?? require_constants.detectPlatform();
|
|
80
|
-
const hotkeyString = typeof hotkey === "string" ? hotkey : formatParsedHotkey(hotkey);
|
|
81
|
-
const parsed = typeof hotkey === "string" ? require_parse.parseHotkey(hotkey, resolvedPlatform) : hotkey;
|
|
82
|
-
const context = {
|
|
83
|
-
hotkey: hotkeyString,
|
|
84
|
-
parsedHotkey: parsed
|
|
85
|
-
};
|
|
86
|
-
return (event) => {
|
|
87
|
-
if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {
|
|
88
|
-
if (preventDefault) event.preventDefault();
|
|
89
|
-
if (stopPropagation) event.stopPropagation();
|
|
90
|
-
callback(event, context);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Creates a handler that matches multiple hotkeys.
|
|
96
|
-
*
|
|
97
|
-
* @param handlers - A map of hotkey strings to their handlers
|
|
98
|
-
* @param options - Options for matching and handling
|
|
99
|
-
* @returns A function that can be used as an event handler
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```ts
|
|
103
|
-
* const handler = createMultiHotkeyHandler({
|
|
104
|
-
* 'Mod+S': (event, { hotkey }) => handleSave(),
|
|
105
|
-
* 'Mod+Z': (event, { hotkey }) => handleUndo(),
|
|
106
|
-
* 'Mod+Shift+Z': (event, { hotkey }) => handleRedo(),
|
|
107
|
-
* })
|
|
108
|
-
*
|
|
109
|
-
* document.addEventListener('keydown', handler)
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
function createMultiHotkeyHandler(handlers, options = {}) {
|
|
113
|
-
const { preventDefault = true, stopPropagation = true, platform } = options;
|
|
114
|
-
const resolvedPlatform = platform ?? require_constants.detectPlatform();
|
|
115
|
-
const parsedHandlers = Object.entries(handlers).filter((entry) => Boolean(entry[1])).map(([hotkey, handler]) => {
|
|
116
|
-
const parsed = require_parse.parseHotkey(hotkey, resolvedPlatform);
|
|
117
|
-
return {
|
|
118
|
-
parsed,
|
|
119
|
-
handler,
|
|
120
|
-
context: {
|
|
121
|
-
hotkey,
|
|
122
|
-
parsedHotkey: parsed
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
return (event) => {
|
|
127
|
-
for (const { parsed, handler, context } of parsedHandlers) if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {
|
|
128
|
-
if (preventDefault) event.preventDefault();
|
|
129
|
-
if (stopPropagation) event.stopPropagation();
|
|
130
|
-
handler(event, context);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Formats a ParsedHotkey back to a hotkey string.
|
|
137
|
-
* Used internally to provide the hotkey string in callback context.
|
|
138
|
-
*/
|
|
139
|
-
function formatParsedHotkey(parsed) {
|
|
140
|
-
const parts = [];
|
|
141
|
-
if (parsed.ctrl) parts.push("Control");
|
|
142
|
-
if (parsed.alt) parts.push("Alt");
|
|
143
|
-
if (parsed.shift) parts.push("Shift");
|
|
144
|
-
if (parsed.meta) parts.push("Meta");
|
|
145
|
-
parts.push(parsed.key);
|
|
146
|
-
return parts.join("+");
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
//#endregion
|
|
150
|
-
exports.createHotkeyHandler = createHotkeyHandler;
|
|
151
|
-
exports.createMultiHotkeyHandler = createMultiHotkeyHandler;
|
|
152
|
-
exports.matchesKeyboardEvent = matchesKeyboardEvent;
|
|
153
|
-
//# sourceMappingURL=match.cjs.map
|
package/dist/match.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"match.cjs","names":["detectPlatform","parseHotkey","normalizeKeyName","isSingleLetterKey","PUNCTUATION_CODE_MAP"],"sources":["../src/match.ts"],"sourcesContent":["import {\n PUNCTUATION_CODE_MAP,\n detectPlatform,\n isSingleLetterKey,\n normalizeKeyName,\n} from './constants'\nimport { parseHotkey } from './parse'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n} from './hotkey'\n\n/**\n * Checks if a KeyboardEvent matches a hotkey.\n *\n * Uses the `key` property from KeyboardEvent for matching, with a fallback to `code`\n * for letter keys, digit keys (0-9), and punctuation keys when `key` produces special\n * characters (e.g., macOS Option+letter, Shift+number, or Option+punctuation).\n * Letter keys are matched case-insensitively.\n *\n * Also handles \"dead key\" events where `event.key` is `'Dead'` instead of the expected\n * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,\n * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.\n * In these cases, `event.code` is used to determine the physical key.\n *\n * @param event - The KeyboardEvent to check\n * @param hotkey - The hotkey string or ParsedHotkey to match against\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns True if the event matches the hotkey\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * if (matchesKeyboardEvent(event, 'Mod+S')) {\n * event.preventDefault()\n * handleSave()\n * }\n * })\n * ```\n */\nexport function matchesKeyboardEvent(\n event: KeyboardEvent,\n hotkey: Hotkey | ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check modifiers\n if (event.ctrlKey !== parsed.ctrl) {\n return false\n }\n if (event.shiftKey !== parsed.shift) {\n return false\n }\n if (event.altKey !== parsed.alt) {\n return false\n }\n if (event.metaKey !== parsed.meta) {\n return false\n }\n\n // Check key (case-insensitive for letters)\n const eventKey = normalizeKeyName(event.key)\n const hotkeyKey = parsed.key\n\n // For single-character keys (not dead keys), try direct event.key match first\n if (eventKey !== 'Dead' && eventKey.length === 1 && hotkeyKey.length === 1) {\n if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) {\n return true\n }\n\n // If event.key is a letter, we usually trust the keyboard layout.\n // For ASCII letters: always trust layout (Dvorak, Colemak, AZERTY support).\n // For non-ASCII letters without Alt: trust layout (e.g., Cyrillic keyboards).\n //\n // For non-ASCII letters WITH Alt held down: fall through to the event.code\n // fallback. macOS Option+letter combinations produce non-ASCII letters as\n // event.key (e.g., Option+A → 'å', Option+' → 'æ', Option+P → 'π'), but\n // event.code still reflects the physical key. We want Alt+A to match even\n // when Option+A fires event.key='å'.\n if (\n isSingleLetterKey(eventKey) &&\n (/^[A-Za-z]$/.test(eventKey) || !event.altKey)\n ) {\n return false\n }\n }\n\n // Fallback to event.code for dead keys or single-char mismatches where\n // event.key is a non-letter special character.\n // Dead keys: Option+letter on macOS, international layouts produce event.key === 'Dead'\n // Single-char mismatches: Cmd+Option+T gives '†' instead of 'T', Shift+4 gives '$'\n if (\n event.code &&\n (eventKey === 'Dead' || (eventKey.length === 1 && hotkeyKey.length === 1))\n ) {\n // fallback for letter keys (common with mac option + letter)\n if (event.code.startsWith('Key')) {\n const codeLetter = event.code.slice(3)\n if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) {\n return codeLetter.toUpperCase() === hotkeyKey.toUpperCase()\n }\n }\n\n // fallback for number keys (common with mac option + num)\n if (event.code.startsWith('Digit')) {\n const codeDigit = event.code.slice(5)\n if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) {\n return codeDigit === hotkeyKey\n }\n }\n // Fallback for punctuation keys (e.g., Minus, Slash, BracketLeft).\n // On macOS, Option+punctuation produces composed characters (e.g., Option+- → '–'),\n // but event.code still reports the physical key.\n if (event.code in PUNCTUATION_CODE_MAP) {\n return PUNCTUATION_CODE_MAP[event.code] === hotkeyKey\n }\n\n return false\n }\n\n // For special keys, compare exactly (after normalization)\n return eventKey === hotkeyKey\n}\n\n/**\n * Options for creating a hotkey handler.\n */\nexport interface CreateHotkeyHandlerOptions {\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n}\n\n/**\n * Creates a keyboard event handler that calls the callback when the hotkey matches.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to match\n * @param callback - The function to call when the hotkey matches\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {\n * console.log(`${hotkey} was pressed`)\n * handleSave()\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createHotkeyHandler(\n hotkey: Hotkey | ParsedHotkey,\n callback: HotkeyCallback,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n const hotkeyString: Hotkey =\n typeof hotkey === 'string' ? hotkey : formatParsedHotkey(hotkey)\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, resolvedPlatform) : hotkey\n\n const context: HotkeyCallbackContext = {\n hotkey: hotkeyString,\n parsedHotkey: parsed,\n }\n\n return (event: KeyboardEvent) => {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n callback(event, context)\n }\n }\n}\n\ntype MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback }\n\n/**\n * Creates a handler that matches multiple hotkeys.\n *\n * @param handlers - A map of hotkey strings to their handlers\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createMultiHotkeyHandler({\n * 'Mod+S': (event, { hotkey }) => handleSave(),\n * 'Mod+Z': (event, { hotkey }) => handleUndo(),\n * 'Mod+Shift+Z': (event, { hotkey }) => handleRedo(),\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createMultiHotkeyHandler(\n handlers: MultiHotkeyHandler,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n // Pre-parse all hotkeys for efficiency\n const parsedHandlers = Object.entries(handlers)\n .filter((entry): entry is [string, HotkeyCallback] => Boolean(entry[1]))\n .map(([hotkey, handler]) => {\n const parsed = parseHotkey(hotkey as Hotkey, resolvedPlatform)\n const context: HotkeyCallbackContext = {\n hotkey: hotkey as Hotkey,\n parsedHotkey: parsed,\n }\n return { parsed, handler, context }\n })\n\n return (event: KeyboardEvent) => {\n for (const { parsed, handler, context } of parsedHandlers) {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n handler(event, context)\n return // Only handle the first match\n }\n }\n }\n}\n\n/**\n * Formats a ParsedHotkey back to a hotkey string.\n * Used internally to provide the hotkey string in callback context.\n */\nfunction formatParsedHotkey(parsed: ParsedHotkey): Hotkey {\n const parts: Array<string> = []\n\n if (parsed.ctrl) parts.push('Control')\n if (parsed.alt) parts.push('Alt')\n if (parsed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;AACrC,KAAI,OAAO,KAAM,OAAM,KAAK,OAAO;AACnC,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI"}
|
package/dist/match.d.cts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { Hotkey, HotkeyCallback, ParsedHotkey } from "./hotkey.cjs";
|
|
2
|
-
|
|
3
|
-
//#region src/match.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Checks if a KeyboardEvent matches a hotkey.
|
|
6
|
-
*
|
|
7
|
-
* Uses the `key` property from KeyboardEvent for matching, with a fallback to `code`
|
|
8
|
-
* for letter keys, digit keys (0-9), and punctuation keys when `key` produces special
|
|
9
|
-
* characters (e.g., macOS Option+letter, Shift+number, or Option+punctuation).
|
|
10
|
-
* Letter keys are matched case-insensitively.
|
|
11
|
-
*
|
|
12
|
-
* Also handles "dead key" events where `event.key` is `'Dead'` instead of the expected
|
|
13
|
-
* character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,
|
|
14
|
-
* Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.
|
|
15
|
-
* In these cases, `event.code` is used to determine the physical key.
|
|
16
|
-
*
|
|
17
|
-
* @param event - The KeyboardEvent to check
|
|
18
|
-
* @param hotkey - The hotkey string or ParsedHotkey to match against
|
|
19
|
-
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
20
|
-
* @returns True if the event matches the hotkey
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* document.addEventListener('keydown', (event) => {
|
|
25
|
-
* if (matchesKeyboardEvent(event, 'Mod+S')) {
|
|
26
|
-
* event.preventDefault()
|
|
27
|
-
* handleSave()
|
|
28
|
-
* }
|
|
29
|
-
* })
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
declare function matchesKeyboardEvent(event: KeyboardEvent, hotkey: Hotkey | ParsedHotkey, platform?: 'mac' | 'windows' | 'linux'): boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Options for creating a hotkey handler.
|
|
35
|
-
*/
|
|
36
|
-
interface CreateHotkeyHandlerOptions {
|
|
37
|
-
/** Prevent the default browser action when the hotkey matches. Defaults to true */
|
|
38
|
-
preventDefault?: boolean;
|
|
39
|
-
/** Stop event propagation when the hotkey matches. Defaults to true */
|
|
40
|
-
stopPropagation?: boolean;
|
|
41
|
-
/** The target platform for resolving 'Mod' */
|
|
42
|
-
platform?: 'mac' | 'windows' | 'linux';
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Creates a keyboard event handler that calls the callback when the hotkey matches.
|
|
46
|
-
*
|
|
47
|
-
* @param hotkey - The hotkey string or ParsedHotkey to match
|
|
48
|
-
* @param callback - The function to call when the hotkey matches
|
|
49
|
-
* @param options - Options for matching and handling
|
|
50
|
-
* @returns A function that can be used as an event handler
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```ts
|
|
54
|
-
* const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {
|
|
55
|
-
* console.log(`${hotkey} was pressed`)
|
|
56
|
-
* handleSave()
|
|
57
|
-
* })
|
|
58
|
-
*
|
|
59
|
-
* document.addEventListener('keydown', handler)
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
declare function createHotkeyHandler(hotkey: Hotkey | ParsedHotkey, callback: HotkeyCallback, options?: CreateHotkeyHandlerOptions): (event: KeyboardEvent) => void;
|
|
63
|
-
type MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback };
|
|
64
|
-
/**
|
|
65
|
-
* Creates a handler that matches multiple hotkeys.
|
|
66
|
-
*
|
|
67
|
-
* @param handlers - A map of hotkey strings to their handlers
|
|
68
|
-
* @param options - Options for matching and handling
|
|
69
|
-
* @returns A function that can be used as an event handler
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* const handler = createMultiHotkeyHandler({
|
|
74
|
-
* 'Mod+S': (event, { hotkey }) => handleSave(),
|
|
75
|
-
* 'Mod+Z': (event, { hotkey }) => handleUndo(),
|
|
76
|
-
* 'Mod+Shift+Z': (event, { hotkey }) => handleRedo(),
|
|
77
|
-
* })
|
|
78
|
-
*
|
|
79
|
-
* document.addEventListener('keydown', handler)
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
declare function createMultiHotkeyHandler(handlers: MultiHotkeyHandler, options?: CreateHotkeyHandlerOptions): (event: KeyboardEvent) => void;
|
|
83
|
-
//#endregion
|
|
84
|
-
export { CreateHotkeyHandlerOptions, createHotkeyHandler, createMultiHotkeyHandler, matchesKeyboardEvent };
|
|
85
|
-
//# sourceMappingURL=match.d.cts.map
|
package/dist/match.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"match.js","names":[],"sources":["../src/match.ts"],"sourcesContent":["import {\n PUNCTUATION_CODE_MAP,\n detectPlatform,\n isSingleLetterKey,\n normalizeKeyName,\n} from './constants'\nimport { parseHotkey } from './parse'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n} from './hotkey'\n\n/**\n * Checks if a KeyboardEvent matches a hotkey.\n *\n * Uses the `key` property from KeyboardEvent for matching, with a fallback to `code`\n * for letter keys, digit keys (0-9), and punctuation keys when `key` produces special\n * characters (e.g., macOS Option+letter, Shift+number, or Option+punctuation).\n * Letter keys are matched case-insensitively.\n *\n * Also handles \"dead key\" events where `event.key` is `'Dead'` instead of the expected\n * character. This commonly occurs on macOS with Option+letter combinations (e.g., Option+E,\n * Option+I, Option+U, Option+N) and on Windows/Linux with international keyboard layouts.\n * In these cases, `event.code` is used to determine the physical key.\n *\n * @param event - The KeyboardEvent to check\n * @param hotkey - The hotkey string or ParsedHotkey to match against\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns True if the event matches the hotkey\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * if (matchesKeyboardEvent(event, 'Mod+S')) {\n * event.preventDefault()\n * handleSave()\n * }\n * })\n * ```\n */\nexport function matchesKeyboardEvent(\n event: KeyboardEvent,\n hotkey: Hotkey | ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check modifiers\n if (event.ctrlKey !== parsed.ctrl) {\n return false\n }\n if (event.shiftKey !== parsed.shift) {\n return false\n }\n if (event.altKey !== parsed.alt) {\n return false\n }\n if (event.metaKey !== parsed.meta) {\n return false\n }\n\n // Check key (case-insensitive for letters)\n const eventKey = normalizeKeyName(event.key)\n const hotkeyKey = parsed.key\n\n // For single-character keys (not dead keys), try direct event.key match first\n if (eventKey !== 'Dead' && eventKey.length === 1 && hotkeyKey.length === 1) {\n if (eventKey.toUpperCase() === hotkeyKey.toUpperCase()) {\n return true\n }\n\n // If event.key is a letter, we usually trust the keyboard layout.\n // For ASCII letters: always trust layout (Dvorak, Colemak, AZERTY support).\n // For non-ASCII letters without Alt: trust layout (e.g., Cyrillic keyboards).\n //\n // For non-ASCII letters WITH Alt held down: fall through to the event.code\n // fallback. macOS Option+letter combinations produce non-ASCII letters as\n // event.key (e.g., Option+A → 'å', Option+' → 'æ', Option+P → 'π'), but\n // event.code still reflects the physical key. We want Alt+A to match even\n // when Option+A fires event.key='å'.\n if (\n isSingleLetterKey(eventKey) &&\n (/^[A-Za-z]$/.test(eventKey) || !event.altKey)\n ) {\n return false\n }\n }\n\n // Fallback to event.code for dead keys or single-char mismatches where\n // event.key is a non-letter special character.\n // Dead keys: Option+letter on macOS, international layouts produce event.key === 'Dead'\n // Single-char mismatches: Cmd+Option+T gives '†' instead of 'T', Shift+4 gives '$'\n if (\n event.code &&\n (eventKey === 'Dead' || (eventKey.length === 1 && hotkeyKey.length === 1))\n ) {\n // fallback for letter keys (common with mac option + letter)\n if (event.code.startsWith('Key')) {\n const codeLetter = event.code.slice(3)\n if (codeLetter.length === 1 && /^[A-Za-z]$/.test(codeLetter)) {\n return codeLetter.toUpperCase() === hotkeyKey.toUpperCase()\n }\n }\n\n // fallback for number keys (common with mac option + num)\n if (event.code.startsWith('Digit')) {\n const codeDigit = event.code.slice(5)\n if (codeDigit.length === 1 && /^[0-9]$/.test(codeDigit)) {\n return codeDigit === hotkeyKey\n }\n }\n // Fallback for punctuation keys (e.g., Minus, Slash, BracketLeft).\n // On macOS, Option+punctuation produces composed characters (e.g., Option+- → '–'),\n // but event.code still reports the physical key.\n if (event.code in PUNCTUATION_CODE_MAP) {\n return PUNCTUATION_CODE_MAP[event.code] === hotkeyKey\n }\n\n return false\n }\n\n // For special keys, compare exactly (after normalization)\n return eventKey === hotkeyKey\n}\n\n/**\n * Options for creating a hotkey handler.\n */\nexport interface CreateHotkeyHandlerOptions {\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n}\n\n/**\n * Creates a keyboard event handler that calls the callback when the hotkey matches.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to match\n * @param callback - The function to call when the hotkey matches\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createHotkeyHandler('Mod+S', (event, { hotkey, parsedHotkey }) => {\n * console.log(`${hotkey} was pressed`)\n * handleSave()\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createHotkeyHandler(\n hotkey: Hotkey | ParsedHotkey,\n callback: HotkeyCallback,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n const hotkeyString: Hotkey =\n typeof hotkey === 'string' ? hotkey : formatParsedHotkey(hotkey)\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, resolvedPlatform) : hotkey\n\n const context: HotkeyCallbackContext = {\n hotkey: hotkeyString,\n parsedHotkey: parsed,\n }\n\n return (event: KeyboardEvent) => {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n callback(event, context)\n }\n }\n}\n\ntype MultiHotkeyHandler = { [K in Hotkey]?: HotkeyCallback }\n\n/**\n * Creates a handler that matches multiple hotkeys.\n *\n * @param handlers - A map of hotkey strings to their handlers\n * @param options - Options for matching and handling\n * @returns A function that can be used as an event handler\n *\n * @example\n * ```ts\n * const handler = createMultiHotkeyHandler({\n * 'Mod+S': (event, { hotkey }) => handleSave(),\n * 'Mod+Z': (event, { hotkey }) => handleUndo(),\n * 'Mod+Shift+Z': (event, { hotkey }) => handleRedo(),\n * })\n *\n * document.addEventListener('keydown', handler)\n * ```\n */\nexport function createMultiHotkeyHandler(\n handlers: MultiHotkeyHandler,\n options: CreateHotkeyHandlerOptions = {},\n): (event: KeyboardEvent) => void {\n const { preventDefault = true, stopPropagation = true, platform } = options\n const resolvedPlatform = platform ?? detectPlatform()\n\n // Pre-parse all hotkeys for efficiency\n const parsedHandlers = Object.entries(handlers)\n .filter((entry): entry is [string, HotkeyCallback] => Boolean(entry[1]))\n .map(([hotkey, handler]) => {\n const parsed = parseHotkey(hotkey as Hotkey, resolvedPlatform)\n const context: HotkeyCallbackContext = {\n hotkey: hotkey as Hotkey,\n parsedHotkey: parsed,\n }\n return { parsed, handler, context }\n })\n\n return (event: KeyboardEvent) => {\n for (const { parsed, handler, context } of parsedHandlers) {\n if (matchesKeyboardEvent(event, parsed, resolvedPlatform)) {\n if (preventDefault) {\n event.preventDefault()\n }\n if (stopPropagation) {\n event.stopPropagation()\n }\n handler(event, context)\n return // Only handle the first match\n }\n }\n }\n}\n\n/**\n * Formats a ParsedHotkey back to a hotkey string.\n * Used internally to provide the hotkey string in callback context.\n */\nfunction formatParsedHotkey(parsed: ParsedHotkey): Hotkey {\n const parts: Array<string> = []\n\n if (parsed.ctrl) parts.push('Control')\n if (parsed.alt) parts.push('Alt')\n if (parsed.shift) parts.push('Shift')\n if (parsed.meta) parts.push('Meta')\n parts.push(parsed.key)\n\n return parts.join('+') as Hotkey\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,qBACd,OACA,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAG/D,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;AAET,KAAI,MAAM,aAAa,OAAO,MAC5B,QAAO;AAET,KAAI,MAAM,WAAW,OAAO,IAC1B,QAAO;AAET,KAAI,MAAM,YAAY,OAAO,KAC3B,QAAO;CAIT,MAAM,WAAW,iBAAiB,MAAM,IAAI;CAC5C,MAAM,YAAY,OAAO;AAGzB,KAAI,aAAa,UAAU,SAAS,WAAW,KAAK,UAAU,WAAW,GAAG;AAC1E,MAAI,SAAS,aAAa,KAAK,UAAU,aAAa,CACpD,QAAO;AAYT,MACE,kBAAkB,SAAS,KAC1B,aAAa,KAAK,SAAS,IAAI,CAAC,MAAM,QAEvC,QAAO;;AAQX,KACE,MAAM,SACL,aAAa,UAAW,SAAS,WAAW,KAAK,UAAU,WAAW,IACvE;AAEA,MAAI,MAAM,KAAK,WAAW,MAAM,EAAE;GAChC,MAAM,aAAa,MAAM,KAAK,MAAM,EAAE;AACtC,OAAI,WAAW,WAAW,KAAK,aAAa,KAAK,WAAW,CAC1D,QAAO,WAAW,aAAa,KAAK,UAAU,aAAa;;AAK/D,MAAI,MAAM,KAAK,WAAW,QAAQ,EAAE;GAClC,MAAM,YAAY,MAAM,KAAK,MAAM,EAAE;AACrC,OAAI,UAAU,WAAW,KAAK,UAAU,KAAK,UAAU,CACrD,QAAO,cAAc;;AAMzB,MAAI,MAAM,QAAQ,qBAChB,QAAO,qBAAqB,MAAM,UAAU;A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,KAAI,OAAO,IAAK,OAAM,KAAK,MAAM;AACjC,KAAI,OAAO,MAAO,OAAM,KAAK,QAAQ;AACrC,KAAI,OAAO,KAAM,OAAM,KAAK,OAAO;AACnC,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI"}
|
package/dist/parse.cjs
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
const require_constants = require('./constants.cjs');
|
|
2
|
-
|
|
3
|
-
//#region src/parse.ts
|
|
4
|
-
/**
|
|
5
|
-
* Parses a hotkey string into its component parts.
|
|
6
|
-
*
|
|
7
|
-
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
|
-
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
-
* @returns A ParsedHotkey object with the key and modifier flags
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
14
|
-
* parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
|
|
15
|
-
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
function parseHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
19
|
-
const parts = hotkey.split("+");
|
|
20
|
-
const modifiers = /* @__PURE__ */ new Set();
|
|
21
|
-
let key = "";
|
|
22
|
-
for (let i = 0; i < parts.length; i++) {
|
|
23
|
-
const part = parts[i].trim();
|
|
24
|
-
if (i === parts.length - 1) key = require_constants.normalizeKeyName(part);
|
|
25
|
-
else {
|
|
26
|
-
const alias = require_constants.MODIFIER_ALIASES[part] ?? require_constants.MODIFIER_ALIASES[part.toLowerCase()];
|
|
27
|
-
if (alias) {
|
|
28
|
-
const resolved = require_constants.resolveModifier(alias, platform);
|
|
29
|
-
modifiers.add(resolved);
|
|
30
|
-
} else if (parts.length === 1) key = require_constants.normalizeKeyName(part);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (!key && parts.length > 0) key = require_constants.normalizeKeyName(parts[parts.length - 1].trim());
|
|
34
|
-
return {
|
|
35
|
-
key,
|
|
36
|
-
ctrl: modifiers.has("Control"),
|
|
37
|
-
shift: modifiers.has("Shift"),
|
|
38
|
-
alt: modifiers.has("Alt"),
|
|
39
|
-
meta: modifiers.has("Meta"),
|
|
40
|
-
modifiers: require_constants.MODIFIER_ORDER.filter((m) => modifiers.has(m))
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Converts a RawHotkey object to a ParsedHotkey.
|
|
45
|
-
* Optional modifier booleans default to false; modifiers array is derived from them.
|
|
46
|
-
* When `mod` is true, it is resolved to Control or Meta based on platform.
|
|
47
|
-
*
|
|
48
|
-
* @param raw - The raw hotkey object
|
|
49
|
-
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
50
|
-
* @returns A ParsedHotkey suitable for matching and formatting
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```ts
|
|
54
|
-
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
55
|
-
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
56
|
-
*
|
|
57
|
-
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
58
|
-
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
59
|
-
*
|
|
60
|
-
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
61
|
-
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
function rawHotkeyToParsedHotkey(raw, platform = require_constants.detectPlatform()) {
|
|
65
|
-
let ctrl = raw.ctrl ?? false;
|
|
66
|
-
const shift = raw.shift ?? false;
|
|
67
|
-
const alt = raw.alt ?? false;
|
|
68
|
-
let meta = raw.meta ?? false;
|
|
69
|
-
if (raw.mod) if (require_constants.resolveModifier("Mod", platform) === "Control") ctrl = true;
|
|
70
|
-
else meta = true;
|
|
71
|
-
const modifiers = require_constants.MODIFIER_ORDER.filter((m) => {
|
|
72
|
-
switch (m) {
|
|
73
|
-
case "Control": return ctrl;
|
|
74
|
-
case "Shift": return shift;
|
|
75
|
-
case "Alt": return alt;
|
|
76
|
-
case "Meta": return meta;
|
|
77
|
-
default: return false;
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
return {
|
|
81
|
-
key: raw.key,
|
|
82
|
-
ctrl,
|
|
83
|
-
shift,
|
|
84
|
-
alt,
|
|
85
|
-
meta,
|
|
86
|
-
modifiers
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Serializes a parsed hotkey to the canonical string form used for registration
|
|
91
|
-
* and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;
|
|
92
|
-
* otherwise full `Control+Alt+Shift+Meta` order.
|
|
93
|
-
*/
|
|
94
|
-
function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
95
|
-
const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
|
|
96
|
-
const parts = [];
|
|
97
|
-
if (canUseMod) {
|
|
98
|
-
parts.push("Mod");
|
|
99
|
-
if (parsed.alt) parts.push("Alt");
|
|
100
|
-
if (parsed.shift) parts.push("Shift");
|
|
101
|
-
} else for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
102
|
-
parts.push(require_constants.normalizeKeyName(parsed.key));
|
|
103
|
-
return parts.join("+");
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Normalizes a hotkey string to its canonical form.
|
|
107
|
-
*
|
|
108
|
-
* - When `Mod` is allowed for the platform (Command on Mac without Control;
|
|
109
|
-
* Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
|
|
110
|
-
* then the key (e.g. `Mod+Shift+E`, `Mod+S`).
|
|
111
|
-
* - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
|
|
112
|
-
* - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
|
|
113
|
-
*
|
|
114
|
-
* @param hotkey - The hotkey string to normalize
|
|
115
|
-
* @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
|
|
116
|
-
* @returns The normalized hotkey string
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```ts
|
|
120
|
-
* normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
|
|
121
|
-
* normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
|
|
122
|
-
* normalizeHotkey('esc') // 'Escape'
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
function normalizeHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
126
|
-
return normalizedHotkeyStringFromParsed(parseHotkey(hotkey, platform), platform);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
|
|
130
|
-
*/
|
|
131
|
-
function normalizeHotkeyFromParsed(parsed, platform = require_constants.detectPlatform()) {
|
|
132
|
-
return normalizedHotkeyStringFromParsed(parsed, platform);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
|
|
136
|
-
* Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
|
|
137
|
-
*/
|
|
138
|
-
function normalizeRegisterableHotkey(hotkey, platform = require_constants.detectPlatform()) {
|
|
139
|
-
return typeof hotkey === "string" ? normalizeHotkey(hotkey, platform) : normalizeHotkeyFromParsed(rawHotkeyToParsedHotkey(hotkey, platform), platform);
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Checks if a string is a recognized modifier token (including aliases).
|
|
143
|
-
*
|
|
144
|
-
* For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
|
|
145
|
-
* spellings like `OS` / `Win` match the same alias table.
|
|
146
|
-
*
|
|
147
|
-
* @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
|
|
148
|
-
* @returns True if the string is a recognized modifier
|
|
149
|
-
*/
|
|
150
|
-
function isModifierKey(key) {
|
|
151
|
-
return key in require_constants.MODIFIER_ALIASES || key.toLowerCase() in require_constants.MODIFIER_ALIASES;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Parses a KeyboardEvent into a ParsedHotkey object.
|
|
155
|
-
*
|
|
156
|
-
* This function extracts the key and modifier state from a keyboard event
|
|
157
|
-
* and converts it into the same format used by `parseHotkey()`.
|
|
158
|
-
*
|
|
159
|
-
* @param event - The KeyboardEvent to parse
|
|
160
|
-
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
161
|
-
* @returns A ParsedHotkey object representing the keyboard event
|
|
162
|
-
*
|
|
163
|
-
* @example
|
|
164
|
-
* ```ts
|
|
165
|
-
* document.addEventListener('keydown', (event) => {
|
|
166
|
-
* const parsed = parseKeyboardEvent(event)
|
|
167
|
-
* console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }
|
|
168
|
-
* })
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
function parseKeyboardEvent(event) {
|
|
172
|
-
const normalizedKey = require_constants.normalizeKeyName(event.key);
|
|
173
|
-
const modifiers = [];
|
|
174
|
-
if (event.ctrlKey) modifiers.push("Control");
|
|
175
|
-
if (event.altKey) modifiers.push("Alt");
|
|
176
|
-
if (event.shiftKey) modifiers.push("Shift");
|
|
177
|
-
if (event.metaKey) modifiers.push("Meta");
|
|
178
|
-
return {
|
|
179
|
-
key: normalizedKey,
|
|
180
|
-
ctrl: event.ctrlKey,
|
|
181
|
-
shift: event.shiftKey,
|
|
182
|
-
alt: event.altKey,
|
|
183
|
-
meta: event.metaKey,
|
|
184
|
-
modifiers
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
|
|
189
|
-
*
|
|
190
|
-
* @param event - The keyboard event (typically `keydown`)
|
|
191
|
-
* @param platform - Target platform for `Mod` eligibility
|
|
192
|
-
*/
|
|
193
|
-
function normalizeHotkeyFromEvent(event, platform = require_constants.detectPlatform()) {
|
|
194
|
-
return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
198
|
-
*
|
|
199
|
-
* This is useful for validating that a recorded hotkey is complete and not
|
|
200
|
-
* just a combination of modifiers without an action key.
|
|
201
|
-
*
|
|
202
|
-
* @param hotkey - The hotkey string or ParsedHotkey to check
|
|
203
|
-
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
204
|
-
* @returns True if the hotkey contains at least one non-modifier key
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* ```ts
|
|
208
|
-
* hasNonModifierKey('Control+Shift+S') // true
|
|
209
|
-
* hasNonModifierKey('Control+Shift') // false (no action key)
|
|
210
|
-
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
function hasNonModifierKey(hotkey, platform = require_constants.detectPlatform()) {
|
|
214
|
-
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
|
|
215
|
-
return !isModifierKey(parsed.key) && parsed.key.length > 0;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
//#endregion
|
|
219
|
-
exports.hasNonModifierKey = hasNonModifierKey;
|
|
220
|
-
exports.isModifierKey = isModifierKey;
|
|
221
|
-
exports.normalizeHotkey = normalizeHotkey;
|
|
222
|
-
exports.normalizeHotkeyFromEvent = normalizeHotkeyFromEvent;
|
|
223
|
-
exports.normalizeHotkeyFromParsed = normalizeHotkeyFromParsed;
|
|
224
|
-
exports.normalizeRegisterableHotkey = normalizeRegisterableHotkey;
|
|
225
|
-
exports.parseHotkey = parseHotkey;
|
|
226
|
-
exports.parseKeyboardEvent = parseKeyboardEvent;
|
|
227
|
-
exports.rawHotkeyToParsedHotkey = rawHotkeyToParsedHotkey;
|
|
228
|
-
//# sourceMappingURL=parse.cjs.map
|
package/dist/parse.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parse.cjs","names":["detectPlatform","normalizeKeyName","MODIFIER_ALIASES","resolveModifier","MODIFIER_ORDER"],"sources":["../src/parse.ts"],"sourcesContent":["import {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n detectPlatform,\n normalizeKeyName,\n resolveModifier,\n} from './constants'\nimport type {\n CanonicalModifier,\n Hotkey,\n Key,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey object with the key and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = hotkey.split('+')\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n return {\n key,\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n key: raw.key,\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(normalizeKeyName(parsed.key))\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey {\n const normalizedKey = normalizeKeyName(event.key)\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (event.ctrlKey) modifiers.push('Control')\n if (event.altKey) modifiers.push('Alt')\n if (event.shiftKey) modifiers.push('Shift')\n if (event.metaKey) modifiers.push('Meta')\n\n return {\n key: normalizedKey,\n ctrl: event.ctrlKey,\n shift: event.shiftKey,\n alt: event.altKey,\n meta: event.metaKey,\n modifiers,\n }\n}\n\n/**\n * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform)\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifierKey(parsed.key)\n\n // A val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