@tanstack/hotkeys 0.7.1 → 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 +67 -11
- package/dist/hotkey-recorder.cjs.map +1 -1
- package/dist/hotkey-recorder.d.cts +6 -3
- package/dist/hotkey-recorder.d.ts +6 -3
- package/dist/hotkey-recorder.js +66 -10
- 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 +2 -2
- 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 +92 -16
- 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
package/dist/format.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { MODIFIER_ALIASES, MODIFIER_ORDER, PUNCTUATION_CODE_MAP, normalizeKeyName } from "./constants.js";
|
|
2
|
+
import { KEY_DISPLAY_SYMBOLS, LINUX_MODIFIER_LABELS, MAC_MODIFIER_LABELS, MAC_MODIFIER_SYMBOLS, PUNCTUATION_KEY_DISPLAY_LABELS, WINDOWS_MODIFIER_LABELS } from "./display-labels.js";
|
|
3
|
+
import { detectPlatform } from "./platform.js";
|
|
4
|
+
import { assertLogicalKey, splitHotkeyParts } from "./_keyboard-event.js";
|
|
5
|
+
import { isModifierKey, normalizeHotkeyFromParsed, normalizeRegisterableHotkey } from "./parse.js";
|
|
3
6
|
|
|
4
7
|
//#region src/format.ts
|
|
5
8
|
/**
|
|
6
|
-
*
|
|
9
|
+
* Joins stored sequence steps with spaces, preserving physical code tokens.
|
|
10
|
+
* For user-facing labels, call formatForDisplay on each step instead.
|
|
7
11
|
*
|
|
8
12
|
* @param sequence - Array of hotkey strings that form the sequence
|
|
9
13
|
* @returns A space-separated string (e.g. ['G','G'] → 'G G')
|
|
@@ -30,48 +34,38 @@ function formatHotkeySequence(sequence) {
|
|
|
30
34
|
* ```
|
|
31
35
|
*/
|
|
32
36
|
function formatHotkey(parsed) {
|
|
37
|
+
if (parsed.code === void 0) assertLogicalKey(parsed.key);
|
|
33
38
|
const parts = [];
|
|
34
39
|
for (const modifier of MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
35
|
-
parts.push(parsed.key);
|
|
40
|
+
parts.push(parsed.code !== void 0 ? `[${parsed.code}]` : parsed.key);
|
|
36
41
|
return parts.join("+");
|
|
37
42
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Formats a hotkey for display in a user interface.
|
|
40
|
-
*
|
|
41
|
-
* On macOS, uses symbols (⌘⇧S) in the same modifier order as {@link normalizeHotkeyFromParsed}.
|
|
42
|
-
* On Windows/Linux, uses text (Ctrl+Shift+S) with `+` separators.
|
|
43
|
-
* The separator can be customized with `separatorToken`.
|
|
44
|
-
*
|
|
45
|
-
* @param hotkey - The hotkey string or ParsedHotkey to format
|
|
46
|
-
* @param options - Formatting options
|
|
47
|
-
* @returns A formatted string suitable for display
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* formatForDisplay('Mod+Shift+S', { platform: 'mac' })
|
|
52
|
-
* // Returns: '⌘ ⇧ S' (symbols separated by spaces on macOS)
|
|
53
|
-
*
|
|
54
|
-
* formatForDisplay('Mod+Shift+S', { platform: 'windows' })
|
|
55
|
-
* // Returns: 'Ctrl+Shift+S'
|
|
56
|
-
*
|
|
57
|
-
* formatForDisplay('Escape')
|
|
58
|
-
* // Returns: 'Esc' (on all platforms)
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
43
|
function formatForDisplay(hotkey, options = {}) {
|
|
62
44
|
const platform = options.platform ?? detectPlatform();
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
45
|
+
const symbols = options.useSymbols ?? true;
|
|
46
|
+
const modifierSymbols = typeof symbols === "boolean" ? symbols : symbols.modifiers ?? true;
|
|
47
|
+
const keySymbols = typeof symbols === "boolean" ? symbols : symbols.keys ?? true;
|
|
48
|
+
const separator = options.separatorToken ?? (platform === "mac" && modifierSymbols ? " " : "+");
|
|
49
|
+
const parts = splitHotkeyParts(typeof hotkey === "object" && "modifiers" in hotkey ? normalizeHotkeyFromParsed(hotkey, platform) : normalizeRegisterableHotkey(hotkey, platform)).map((segment) => {
|
|
66
50
|
if (isModifierKey(segment)) {
|
|
67
|
-
const
|
|
68
|
-
return platform === "mac" ?
|
|
69
|
-
} else {
|
|
70
|
-
const keyDisplaySymbol = useSymbols && KEY_DISPLAY_SYMBOLS[segment];
|
|
71
|
-
if (keyDisplaySymbol) return keyDisplaySymbol;
|
|
72
|
-
return !useSymbols && PUNCTUATION_KEY_DISPLAY_LABELS[segment] || segment;
|
|
51
|
+
const modifier = MODIFIER_ALIASES[segment] ?? MODIFIER_ALIASES[segment.toLowerCase()];
|
|
52
|
+
return platform === "mac" ? modifierSymbols ? MAC_MODIFIER_SYMBOLS[modifier] : MAC_MODIFIER_LABELS[modifier] : platform === "windows" ? WINDOWS_MODIFIER_LABELS[modifier] : LINUX_MODIFIER_LABELS[modifier];
|
|
73
53
|
}
|
|
74
|
-
|
|
54
|
+
const code = /^\[([A-Za-z][A-Za-z0-9]*)\]$/.exec(segment)?.[1];
|
|
55
|
+
const label = options.keyLabels?.[code ?? segment];
|
|
56
|
+
if (label !== void 0) return label;
|
|
57
|
+
const numpad = code?.startsWith("Numpad") ?? false;
|
|
58
|
+
let displayKey = code ?? segment;
|
|
59
|
+
const mappedKey = code ? options.layoutMap?.get(code) : void 0;
|
|
60
|
+
if (mappedKey) displayKey = normalizeKeyName(mappedKey);
|
|
61
|
+
else if (code) if (/^Key[A-Z]$/.test(code)) displayKey = code.slice(3);
|
|
62
|
+
else if (/^Digit[0-9]$/.test(code)) displayKey = code.slice(5);
|
|
63
|
+
else if (numpad) displayKey = code.slice(6);
|
|
64
|
+
else displayKey = PUNCTUATION_CODE_MAP[code] ?? code;
|
|
65
|
+
const display = (keySymbols ? KEY_DISPLAY_SYMBOLS : PUNCTUATION_KEY_DISPLAY_LABELS)[displayKey] ?? displayKey;
|
|
66
|
+
return numpad ? `Numpad ${display.replace(/([a-z])([A-Z])/g, "$1 $2")}` : display;
|
|
67
|
+
});
|
|
68
|
+
return options.parts ? parts : parts.join(separator);
|
|
75
69
|
}
|
|
76
70
|
/**
|
|
77
71
|
* @deprecated Use {@link formatForDisplay} instead with `useSymbols: false` option.
|
package/dist/format.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","names":[],"sources":["../src/format.ts"],"sourcesContent":["import {\n KEY_DISPLAY_SYMBOLS,\n LINUX_MODIFIER_LABELS,\n MAC_MODIFIER_LABELS,\n MAC_MODIFIER_SYMBOLS,\n
|
|
1
|
+
{"version":3,"file":"format.js","names":[],"sources":["../src/format.ts"],"sourcesContent":["import {\n KEY_DISPLAY_SYMBOLS,\n LINUX_MODIFIER_LABELS,\n MAC_MODIFIER_LABELS,\n MAC_MODIFIER_SYMBOLS,\n PUNCTUATION_KEY_DISPLAY_LABELS,\n WINDOWS_MODIFIER_LABELS,\n} from './display-labels'\nimport {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n PUNCTUATION_CODE_MAP,\n normalizeKeyName,\n} from './constants'\nimport { detectPlatform } from './platform'\nimport { assertLogicalKey, splitHotkeyParts } from './_keyboard-event'\nimport {\n isModifierKey,\n normalizeHotkeyFromParsed,\n normalizeRegisterableHotkey,\n} from './parse'\nimport type { CanonicalModifier } from './key.types'\nimport type { Hotkey, ParsedHotkey, RegisterableHotkey } from './hotkey.types'\n\n/**\n * Joins stored sequence steps with spaces, preserving physical code tokens.\n * For user-facing labels, call formatForDisplay on each step instead.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @returns A space-separated string (e.g. ['G','G'] → 'G G')\n *\n * @example\n * ```ts\n * formatHotkeySequence(['G', 'G']) // 'G G'\n * formatHotkeySequence(['D', 'I', 'W']) // 'D I W'\n * ```\n */\nexport function formatHotkeySequence(sequence: Array<Hotkey>): string {\n return sequence.join(' ')\n}\n\n/**\n * Converts a ParsedHotkey back to a hotkey string.\n *\n * @param parsed - The parsed hotkey object\n * @returns A hotkey string in canonical form\n *\n * @example\n * ```ts\n * formatHotkey({ key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] })\n * // Returns: 'Control+Shift+S'\n * ```\n */\nexport function formatHotkey(parsed: ParsedHotkey): string {\n if (parsed.code === undefined) assertLogicalKey(parsed.key)\n const parts: Array<string> = []\n\n // Add modifiers in canonical order\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n\n // Add the key\n parts.push(parsed.code !== undefined ? `[${parsed.code}]` : parsed.key)\n\n return parts.join('+')\n}\n\nexport type DisplayHotkey = RegisterableHotkey | ParsedHotkey | (string & {})\n\n/**\n * Formats a hotkey for display in a user interface.\n *\n * On macOS, uses symbols (⌘⇧S) in the same modifier order as {@link normalizeHotkeyFromParsed}.\n * On Windows/Linux, uses text (Ctrl+Shift+S) with `+` separators.\n * The separator can be customized with `separatorToken`.\n * Physical codes use conventional display labels: `[KeyS]` becomes `S` and\n * `[Digit2]` becomes `2`. These labels do not infer the active keyboard layout\n * or change matching. Supply a resolved `layoutMap` for layout-aware labels;\n * `keyLabels` overrides both layout entries and fallback labels.\n *\n * @param hotkey - A logical or physical string, raw binding, or ParsedHotkey\n * @param options - Formatting options\n * @returns Display text, or individual key labels when parts is true\n *\n * @example\n * ```ts\n * formatForDisplay('Mod+Shift+S', { platform: 'mac' })\n * // Returns: '⌘ ⇧ S' (symbols separated by spaces on macOS)\n *\n * formatForDisplay('Mod+Shift+S', { platform: 'windows' })\n * // Returns: 'Ctrl+Shift+S'\n *\n * formatForDisplay('Escape')\n * // Returns: 'Esc' (on all platforms)\n * ```\n */\nexport function formatForDisplay(\n hotkey: DisplayHotkey,\n options: FormatDisplayOptions & { parts: true },\n): Array<string>\nexport function formatForDisplay(\n hotkey: DisplayHotkey,\n options?: FormatDisplayOptions & { parts?: false },\n): string\nexport function formatForDisplay(\n hotkey: DisplayHotkey,\n options: FormatDisplayOptions,\n): string | Array<string>\nexport function formatForDisplay(\n hotkey: DisplayHotkey,\n options: FormatDisplayOptions = {},\n): string | Array<string> {\n const platform = options.platform ?? detectPlatform()\n const symbols = options.useSymbols ?? true\n const modifierSymbols =\n typeof symbols === 'boolean' ? symbols : (symbols.modifiers ?? true)\n const keySymbols =\n typeof symbols === 'boolean' ? symbols : (symbols.keys ?? true)\n const separator =\n options.separatorToken ??\n (platform === 'mac' && modifierSymbols ? ' ' : '+')\n const normalized =\n typeof hotkey === 'object' && 'modifiers' in hotkey\n ? normalizeHotkeyFromParsed(hotkey, platform)\n : normalizeRegisterableHotkey(hotkey as RegisterableHotkey, platform)\n const parts = splitHotkeyParts(normalized).map((segment) => {\n if (isModifierKey(segment)) {\n const modifier = (MODIFIER_ALIASES[segment] ??\n MODIFIER_ALIASES[segment.toLowerCase()]) as CanonicalModifier | 'Mod'\n return platform === 'mac'\n ? modifierSymbols\n ? MAC_MODIFIER_SYMBOLS[modifier]\n : MAC_MODIFIER_LABELS[modifier]\n : platform === 'windows'\n ? WINDOWS_MODIFIER_LABELS[modifier]\n : LINUX_MODIFIER_LABELS[modifier]\n }\n const code = /^\\[([A-Za-z][A-Za-z0-9]*)\\]$/.exec(segment)?.[1]\n const label = options.keyLabels?.[code ?? segment]\n if (label !== undefined) return label\n // Shorten conventional position labels for display only; stored identity is unchanged.\n const numpad = code?.startsWith('Numpad') ?? false\n let displayKey = code ?? segment\n const mappedKey = code ? options.layoutMap?.get(code) : undefined\n if (mappedKey) {\n // Layout entries are logical labels; normalize their case and whitespace names.\n displayKey = normalizeKeyName(mappedKey)\n } else if (code) {\n if (/^Key[A-Z]$/.test(code)) displayKey = code.slice(3)\n else if (/^Digit[0-9]$/.test(code)) displayKey = code.slice(5)\n else if (numpad) displayKey = code.slice(6)\n else displayKey = PUNCTUATION_CODE_MAP[code] ?? code\n }\n const labels = keySymbols\n ? KEY_DISPLAY_SYMBOLS\n : PUNCTUATION_KEY_DISPLAY_LABELS\n const display =\n (labels as Partial<Record<string, string>>)[displayKey] ?? displayKey\n // Retain numpad identity while making longer names such as MemoryAdd readable.\n return numpad\n ? `Numpad ${display.replace(/([a-z])([A-Z])/g, '$1 $2')}`\n : display\n })\n return options.parts ? parts : parts.join(separator)\n}\n\n/**\n * @deprecated Use {@link formatForDisplay} instead with `useSymbols: false` option.\n */\nexport function formatWithLabels(\n hotkey: RegisterableHotkey,\n options: Omit<FormatDisplayOptions, 'useSymbols' | 'parts'> = {},\n): string {\n return formatForDisplay(hotkey, { ...options, useSymbols: false })\n}\n\n/**\n * Options for formatting hotkeys for display.\n */\nexport interface FormatDisplayOptions {\n /** The target platform. Defaults to auto-detection. */\n platform?: 'mac' | 'windows' | 'linux'\n /** Whether to use symbols for the display. Defaults to true. */\n useSymbols?: boolean | { modifiers?: boolean; keys?: boolean }\n /** Return individual key labels instead of a joined string. Default: false. */\n parts?: boolean\n /**\n * An already-resolved code-to-key map, such as KeyboardLayoutMap or Map.\n * Used only for physical bindings. Mapped keys receive normal display formatting;\n * missing or empty entries use conventional fallback labels. The caller owns\n * loading and refreshing the map; this formatter never calls browser APIs.\n */\n layoutMap?: { get: (code: string) => string | undefined }\n /** Final display overrides for logical keys or physical codes; take precedence over layoutMap. Never changes matching. */\n keyLabels?: Readonly<Record<string, string>>\n /** Override the separator between display tokens. Defaults to platform-specific formatting when null/undefined. */\n separatorToken?: string | null\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,qBAAqB,UAAiC;AACpE,QAAO,SAAS,KAAK,IAAI;;;;;;;;;;;;;;AAe3B,SAAgB,aAAa,QAA8B;AACzD,KAAI,OAAO,SAAS,OAAW,kBAAiB,OAAO,IAAI;CAC3D,MAAM,QAAuB,EAAE;AAG/B,MAAK,MAAM,YAAY,eACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAK,SAAS;AAKxB,OAAM,KAAK,OAAO,SAAS,SAAY,IAAI,OAAO,KAAK,KA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ACF,QAAO,QAAQ,QAAQ,QAAQ,MAAM,KAAK,UAAU;;;;;AAMtD,SAAgB,iBACd,QACA,UAA8D,EAAE,EACxD;AACR,QAAO,iBAAiB,QAAQ;EAAE,GAAG;EAAS,YAAY;EAAO,CAAC"}
|
package/dist/hint.cjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_parse = require('./parse.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/hint.ts
|
|
5
|
+
/**
|
|
6
|
+
* Returns whether held modifiers should reveal a shortcut hint.
|
|
7
|
+
* By default, any nonempty subset of the binding's modifiers qualifies; an
|
|
8
|
+
* unrelated modifier hides the hint. `exact` requires every binding modifier.
|
|
9
|
+
* Nonmodifier keys are ignored. This does not check registration or focus.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true
|
|
13
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false
|
|
14
|
+
*/
|
|
15
|
+
function matchesHeldModifiers(hotkey, heldKeys, options = {}) {
|
|
16
|
+
if (heldKeys.includes("AltGraph")) return false;
|
|
17
|
+
const held = [...new Set(heldKeys.map(require_constants.normalizeKeyName).filter((key) => require_constants.MODIFIER_KEYS.has(key)))];
|
|
18
|
+
const { modifiers } = require_parse.parseRegisterableHotkey(hotkey, options.platform);
|
|
19
|
+
return held.length > 0 && held.every((key) => modifiers.includes(key)) && (!options.exact || held.length === modifiers.length);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.matchesHeldModifiers = matchesHeldModifiers;
|
|
24
|
+
//# sourceMappingURL=hint.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hint.cjs","names":["normalizeKeyName","MODIFIER_KEYS","parseRegisterableHotkey"],"sources":["../src/hint.ts"],"sourcesContent":["import { MODIFIER_KEYS, normalizeKeyName } from './constants'\nimport { parseRegisterableHotkey } from './parse'\nimport type { CanonicalModifier } from './key.types'\nimport type { RegisterableHotkey } from './hotkey.types'\n\nexport interface HeldModifierOptions {\n /** Resolve Mod for this platform. Defaults to platform detection. */\n platform?: 'mac' | 'windows' | 'linux'\n /** Require every binding modifier to be held. Default: false (nonempty subset). */\n exact?: boolean\n}\n\n/**\n * Returns whether held modifiers should reveal a shortcut hint.\n * By default, any nonempty subset of the binding's modifiers qualifies; an\n * unrelated modifier hides the hint. `exact` requires every binding modifier.\n * Nonmodifier keys are ignored. This does not check registration or focus.\n *\n * @example\n * matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true\n * matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false\n */\nexport function matchesHeldModifiers(\n hotkey: RegisterableHotkey,\n heldKeys: ReadonlyArray<string>,\n options: HeldModifierOptions = {},\n): boolean {\n // AltGraph (AltGr) types alternate characters such as @ or € on many layouts.\n // It can synthesize Ctrl+Alt; character entry should not reveal shortcut hints.\n if (heldKeys.includes('AltGraph')) return false\n const held = [\n ...new Set(\n heldKeys.map(normalizeKeyName).filter((key) => MODIFIER_KEYS.has(key)),\n ),\n ]\n const { modifiers } = parseRegisterableHotkey(hotkey, options.platform)\n return (\n held.length > 0 &&\n held.every((key) => modifiers.includes(key as CanonicalModifier)) &&\n (!options.exact || held.length === modifiers.length)\n )\n}\n"],"mappings":";;;;;;;;;;;;;;AAsBA,SAAgB,qBACd,QACA,UACA,UAA+B,EAAE,EACxB;AAGT,KAAI,SAAS,SAAS,WAAW,CAAE,QAAO;CAC1C,MAAM,OAAO,CACX,GAAG,IAAI,IACL,SAAS,IAAIA,mCAAiB,CAAC,QAAQ,QAAQC,gCAAc,IAAI,IAAI,CAAC,CACvE,CACF;CACD,MAAM,EAAE,cAAcC,sCAAwB,QAAQ,QAAQ,SAAS;AACvE,QACE,KAAK,SAAS,KACd,KAAK,OAAO,QAAQ,UAAU,SAAS,IAAyB,CAAC,KAChE,CAAC,QAAQ,SAAS,KAAK,WAAW,UAAU"}
|
package/dist/hint.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RegisterableHotkey } from "./hotkey.types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hint.d.ts
|
|
4
|
+
interface HeldModifierOptions {
|
|
5
|
+
/** Resolve Mod for this platform. Defaults to platform detection. */
|
|
6
|
+
platform?: 'mac' | 'windows' | 'linux';
|
|
7
|
+
/** Require every binding modifier to be held. Default: false (nonempty subset). */
|
|
8
|
+
exact?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Returns whether held modifiers should reveal a shortcut hint.
|
|
12
|
+
* By default, any nonempty subset of the binding's modifiers qualifies; an
|
|
13
|
+
* unrelated modifier hides the hint. `exact` requires every binding modifier.
|
|
14
|
+
* Nonmodifier keys are ignored. This does not check registration or focus.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true
|
|
18
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false
|
|
19
|
+
*/
|
|
20
|
+
declare function matchesHeldModifiers(hotkey: RegisterableHotkey, heldKeys: ReadonlyArray<string>, options?: HeldModifierOptions): boolean;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { HeldModifierOptions, matchesHeldModifiers };
|
|
23
|
+
//# sourceMappingURL=hint.d.cts.map
|
package/dist/hint.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RegisterableHotkey } from "./hotkey.types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hint.d.ts
|
|
4
|
+
interface HeldModifierOptions {
|
|
5
|
+
/** Resolve Mod for this platform. Defaults to platform detection. */
|
|
6
|
+
platform?: 'mac' | 'windows' | 'linux';
|
|
7
|
+
/** Require every binding modifier to be held. Default: false (nonempty subset). */
|
|
8
|
+
exact?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Returns whether held modifiers should reveal a shortcut hint.
|
|
12
|
+
* By default, any nonempty subset of the binding's modifiers qualifies; an
|
|
13
|
+
* unrelated modifier hides the hint. `exact` requires every binding modifier.
|
|
14
|
+
* Nonmodifier keys are ignored. This does not check registration or focus.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true
|
|
18
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false
|
|
19
|
+
*/
|
|
20
|
+
declare function matchesHeldModifiers(hotkey: RegisterableHotkey, heldKeys: ReadonlyArray<string>, options?: HeldModifierOptions): boolean;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { HeldModifierOptions, matchesHeldModifiers };
|
|
23
|
+
//# sourceMappingURL=hint.d.ts.map
|
package/dist/hint.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MODIFIER_KEYS, normalizeKeyName } from "./constants.js";
|
|
2
|
+
import { parseRegisterableHotkey } from "./parse.js";
|
|
3
|
+
|
|
4
|
+
//#region src/hint.ts
|
|
5
|
+
/**
|
|
6
|
+
* Returns whether held modifiers should reveal a shortcut hint.
|
|
7
|
+
* By default, any nonempty subset of the binding's modifiers qualifies; an
|
|
8
|
+
* unrelated modifier hides the hint. `exact` requires every binding modifier.
|
|
9
|
+
* Nonmodifier keys are ignored. This does not check registration or focus.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true
|
|
13
|
+
* matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false
|
|
14
|
+
*/
|
|
15
|
+
function matchesHeldModifiers(hotkey, heldKeys, options = {}) {
|
|
16
|
+
if (heldKeys.includes("AltGraph")) return false;
|
|
17
|
+
const held = [...new Set(heldKeys.map(normalizeKeyName).filter((key) => MODIFIER_KEYS.has(key)))];
|
|
18
|
+
const { modifiers } = parseRegisterableHotkey(hotkey, options.platform);
|
|
19
|
+
return held.length > 0 && held.every((key) => modifiers.includes(key)) && (!options.exact || held.length === modifiers.length);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { matchesHeldModifiers };
|
|
24
|
+
//# sourceMappingURL=hint.js.map
|
package/dist/hint.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hint.js","names":[],"sources":["../src/hint.ts"],"sourcesContent":["import { MODIFIER_KEYS, normalizeKeyName } from './constants'\nimport { parseRegisterableHotkey } from './parse'\nimport type { CanonicalModifier } from './key.types'\nimport type { RegisterableHotkey } from './hotkey.types'\n\nexport interface HeldModifierOptions {\n /** Resolve Mod for this platform. Defaults to platform detection. */\n platform?: 'mac' | 'windows' | 'linux'\n /** Require every binding modifier to be held. Default: false (nonempty subset). */\n exact?: boolean\n}\n\n/**\n * Returns whether held modifiers should reveal a shortcut hint.\n * By default, any nonempty subset of the binding's modifiers qualifies; an\n * unrelated modifier hides the hint. `exact` requires every binding modifier.\n * Nonmodifier keys are ignored. This does not check registration or focus.\n *\n * @example\n * matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt']) // true\n * matchesHeldModifiers('Alt+Shift+[KeyK]', ['Alt', 'Control']) // false\n */\nexport function matchesHeldModifiers(\n hotkey: RegisterableHotkey,\n heldKeys: ReadonlyArray<string>,\n options: HeldModifierOptions = {},\n): boolean {\n // AltGraph (AltGr) types alternate characters such as @ or € on many layouts.\n // It can synthesize Ctrl+Alt; character entry should not reveal shortcut hints.\n if (heldKeys.includes('AltGraph')) return false\n const held = [\n ...new Set(\n heldKeys.map(normalizeKeyName).filter((key) => MODIFIER_KEYS.has(key)),\n ),\n ]\n const { modifiers } = parseRegisterableHotkey(hotkey, options.platform)\n return (\n held.length > 0 &&\n held.every((key) => modifiers.includes(key as CanonicalModifier)) &&\n (!options.exact || held.length === modifiers.length)\n )\n}\n"],"mappings":";;;;;;;;;;;;;;AAsBA,SAAgB,qBACd,QACA,UACA,UAA+B,EAAE,EACxB;AAGT,KAAI,SAAS,SAAS,WAAW,CAAE,QAAO;CAC1C,MAAM,OAAO,CACX,GAAG,IAAI,IACL,SAAS,IAAI,iBAAiB,CAAC,QAAQ,QAAQ,cAAc,IAAI,IAAI,CAAC,CACvE,CACF;CACD,MAAM,EAAE,cAAc,wBAAwB,QAAQ,QAAQ,SAAS;AACvE,QACE,KAAK,SAAS,KACd,KAAK,OAAO,QAAQ,UAAU,SAAS,IAAyB,CAAC,KAChE,CAAC,QAAQ,SAAS,KAAK,WAAW,UAAU"}
|
package/dist/hotkey-manager.cjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_platform = require('./platform.cjs');
|
|
2
3
|
const require_parse = require('./parse.cjs');
|
|
3
4
|
const require_format = require('./format.cjs');
|
|
5
|
+
const require__match = require('./_match.cjs');
|
|
4
6
|
const require_match = require('./match.cjs');
|
|
5
|
-
const
|
|
7
|
+
const require__recording_guard = require('./_recording-guard.cjs');
|
|
8
|
+
const require__registration = require('./_registration.cjs');
|
|
9
|
+
const require__event_target = require('./_event-target.cjs');
|
|
6
10
|
let _tanstack_store = require("@tanstack/store");
|
|
7
11
|
|
|
8
12
|
//#region src/hotkey-manager.ts
|
|
@@ -54,7 +58,7 @@ var HotkeyManager = class HotkeyManager {
|
|
|
54
58
|
#targetRegistrations = /* @__PURE__ */ new Map();
|
|
55
59
|
constructor() {
|
|
56
60
|
this.registrations = new _tanstack_store.Store(/* @__PURE__ */ new Map());
|
|
57
|
-
this.#platform =
|
|
61
|
+
this.#platform = require_platform.detectPlatform();
|
|
58
62
|
}
|
|
59
63
|
/**
|
|
60
64
|
* Gets the singleton instance of HotkeyManager.
|
|
@@ -122,15 +126,15 @@ var HotkeyManager = class HotkeyManager {
|
|
|
122
126
|
const target = options.target ?? document;
|
|
123
127
|
const conflictBehavior = options.conflictBehavior ?? "warn";
|
|
124
128
|
const conflictingRegistration = this.#findConflictingRegistration(hotkeyStr, target);
|
|
125
|
-
if (conflictingRegistration)
|
|
126
|
-
const resolvedIgnoreInputs = options.ignoreInputs ??
|
|
129
|
+
if (conflictingRegistration) require__registration.handleConflict(conflictingRegistration.id, hotkeyStr, conflictBehavior, (id) => this.#unregister(id));
|
|
130
|
+
const resolvedIgnoreInputs = options.ignoreInputs ?? require__registration.getDefaultIgnoreInputs(parsedHotkey);
|
|
127
131
|
const registration = {
|
|
128
132
|
id,
|
|
129
133
|
hotkey: hotkeyStr,
|
|
130
134
|
parsedHotkey,
|
|
131
135
|
callback,
|
|
132
136
|
options: {
|
|
133
|
-
...
|
|
137
|
+
...require__registration.defaultHotkeyOptions,
|
|
134
138
|
requireReset: false,
|
|
135
139
|
...options,
|
|
136
140
|
platform,
|
|
@@ -162,7 +166,10 @@ var HotkeyManager = class HotkeyManager {
|
|
|
162
166
|
setOptions: (newOptions) => {
|
|
163
167
|
manager.registrations.setState((prev) => {
|
|
164
168
|
const reg = prev.get(id);
|
|
165
|
-
if (reg
|
|
169
|
+
if (reg && !require__registration.optionsEqual(reg.options, {
|
|
170
|
+
...reg.options,
|
|
171
|
+
...newOptions
|
|
172
|
+
})) {
|
|
166
173
|
const next = new Map(prev);
|
|
167
174
|
next.set(id, {
|
|
168
175
|
...reg,
|
|
@@ -230,33 +237,52 @@ var HotkeyManager = class HotkeyManager {
|
|
|
230
237
|
* Processes keyboard events for a specific target and event type.
|
|
231
238
|
*/
|
|
232
239
|
#processTargetEvent(event, target, eventType) {
|
|
240
|
+
if (eventType === "keyup") for (const id of this.#targetRegistrations.get(target) ?? []) {
|
|
241
|
+
const registration = this.registrations.state.get(id);
|
|
242
|
+
if (registration?.hasFired && this.#shouldResetRegistration(registration, event)) {
|
|
243
|
+
registration.hasFired = false;
|
|
244
|
+
registration.activeMatch = void 0;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (require__recording_guard.isRecordingEvent(event)) return;
|
|
233
248
|
const targetRegs = this.#targetRegistrations.get(target);
|
|
234
249
|
if (!targetRegs) return;
|
|
250
|
+
const matches = /* @__PURE__ */ new Map();
|
|
251
|
+
let bestScore = 0;
|
|
252
|
+
for (const id of targetRegs) {
|
|
253
|
+
const registration = this.registrations.state.get(id);
|
|
254
|
+
if (!registration || !registration.options.enabled || registration.options.eventType !== eventType || !require__event_target.isEventForTarget(event, target) || registration.options.ignoreInputs !== false && require__event_target.shouldIgnoreInputEvent(event, target, registration.target)) continue;
|
|
255
|
+
const result = require__match.matchKeyboardEvent(event, registration.parsedHotkey, registration.options.platform);
|
|
256
|
+
if (result.matched) {
|
|
257
|
+
matches.set(id, result);
|
|
258
|
+
bestScore = Math.max(bestScore, result.score);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
235
261
|
for (const id of targetRegs) {
|
|
236
262
|
const registration = this.registrations.state.get(id);
|
|
237
263
|
if (!registration) continue;
|
|
238
|
-
if (!
|
|
264
|
+
if (!require__event_target.isEventForTarget(event, target)) continue;
|
|
239
265
|
if (!registration.options.enabled) continue;
|
|
240
266
|
if (registration.options.ignoreInputs !== false) {
|
|
241
|
-
if (
|
|
267
|
+
if (require__event_target.shouldIgnoreInputEvent(event, target, registration.target)) continue;
|
|
242
268
|
}
|
|
243
269
|
if (eventType === "keydown") {
|
|
244
270
|
if (registration.options.eventType !== "keydown") continue;
|
|
245
|
-
|
|
271
|
+
const match = matches.get(id);
|
|
272
|
+
if (match?.matched && match.score === bestScore) {
|
|
246
273
|
if (registration.options.preventDefault) event.preventDefault();
|
|
247
274
|
if (registration.options.stopPropagation) event.stopPropagation();
|
|
248
275
|
if (!registration.options.requireReset || !registration.hasFired) {
|
|
249
276
|
this.#executeHotkeyCallback(registration, event);
|
|
250
|
-
if (registration.options.requireReset)
|
|
277
|
+
if (registration.options.requireReset) {
|
|
278
|
+
registration.hasFired = true;
|
|
279
|
+
registration.activeMatch = match.identity;
|
|
280
|
+
}
|
|
251
281
|
}
|
|
252
282
|
}
|
|
253
|
-
} else {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
if (registration.options.requireReset && registration.hasFired) {
|
|
258
|
-
if (this.#shouldResetRegistration(registration, event)) registration.hasFired = false;
|
|
259
|
-
}
|
|
283
|
+
} else if (registration.options.eventType === "keyup") {
|
|
284
|
+
const match = matches.get(id);
|
|
285
|
+
if (match?.matched && match.score === bestScore) this.#executeHotkeyCallback(registration, event);
|
|
260
286
|
}
|
|
261
287
|
}
|
|
262
288
|
}
|
|
@@ -294,7 +320,7 @@ var HotkeyManager = class HotkeyManager {
|
|
|
294
320
|
* Finds an existing registration with the same hotkey and target.
|
|
295
321
|
*/
|
|
296
322
|
#findConflictingRegistration(hotkey, target) {
|
|
297
|
-
for (const registration of this.registrations.state.values()) if (registration.hotkey
|
|
323
|
+
for (const registration of this.registrations.state.values()) if (require_match.areHotkeysEqual(registration.hotkey, hotkey, registration.options.platform) && registration.target === target) return registration;
|
|
298
324
|
return null;
|
|
299
325
|
}
|
|
300
326
|
/**
|
|
@@ -303,8 +329,13 @@ var HotkeyManager = class HotkeyManager {
|
|
|
303
329
|
#shouldResetRegistration(registration, event) {
|
|
304
330
|
const parsed = registration.parsedHotkey;
|
|
305
331
|
const releasedKey = require_constants.normalizeKeyName(event.key);
|
|
306
|
-
|
|
307
|
-
if (
|
|
332
|
+
if (registration.activeMatch?.code && event.code === registration.activeMatch.code) return true;
|
|
333
|
+
if (parsed.code !== void 0) {
|
|
334
|
+
if (event.code === parsed.code) return true;
|
|
335
|
+
} else {
|
|
336
|
+
const parsedKeyNormalized = parsed.key.length === 1 ? parsed.key.toUpperCase() : parsed.key;
|
|
337
|
+
if ((releasedKey.length === 1 ? releasedKey.toUpperCase() : releasedKey) === parsedKeyNormalized) return true;
|
|
338
|
+
}
|
|
308
339
|
if (parsed.ctrl && releasedKey === "Control") return true;
|
|
309
340
|
if (parsed.shift && releasedKey === "Shift") return true;
|
|
310
341
|
if (parsed.alt && releasedKey === "Alt") return true;
|
|
@@ -323,7 +354,7 @@ var HotkeyManager = class HotkeyManager {
|
|
|
323
354
|
if (!registration) return false;
|
|
324
355
|
const parsed = registration.parsedHotkey;
|
|
325
356
|
const syntheticEvent = new KeyboardEvent(registration.options.eventType ?? "keydown", {
|
|
326
|
-
key: parsed.key,
|
|
357
|
+
...parsed.code !== void 0 ? { code: parsed.code } : { key: parsed.key },
|
|
327
358
|
ctrlKey: parsed.ctrl,
|
|
328
359
|
shiftKey: parsed.shift,
|
|
329
360
|
altKey: parsed.alt,
|
|
@@ -353,7 +384,7 @@ var HotkeyManager = class HotkeyManager {
|
|
|
353
384
|
* @returns True if a matching registration exists
|
|
354
385
|
*/
|
|
355
386
|
isRegistered(hotkey, target) {
|
|
356
|
-
for (const registration of this.registrations.state.values()) if (registration.hotkey
|
|
387
|
+
for (const registration of this.registrations.state.values()) if (require_match.areHotkeysEqual(registration.hotkey, hotkey, registration.options.platform)) {
|
|
357
388
|
if (target === void 0 || registration.target === target) return true;
|
|
358
389
|
}
|
|
359
390
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hotkey-manager.cjs","names":["#instance","Store","#platform","detectPlatform","parseHotkey","rawHotkeyToParsedHotkey","formatHotkey","#findConflictingRegistration","#unregister","getDefaultIgnoreInputs","defaultHotkeyOptions","#targetRegistrations","#ensureListenersForTarget","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","isEventForTarget","shouldIgnoreInputEvent","matchesKeyboardEvent","#executeHotkeyCallback","#shouldResetRegistration","#processTargetEvent","normalizeKeyName"],"sources":["../src/hotkey-manager.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { detectPlatform, normalizeKeyName } from './constants'\nimport { formatHotkey } from './format'\nimport { parseHotkey, rawHotkeyToParsedHotkey } from './parse'\nimport { matchesKeyboardEvent } from './match'\nimport {\n defaultHotkeyOptions,\n getDefaultIgnoreInputs,\n handleConflict,\n isEventForTarget,\n shouldIgnoreInputEvent,\n} from './manager.utils'\nimport type { ConflictBehavior } from './manager.utils'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeyMeta,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\nexport type { ConflictBehavior }\n\n/**\n * Options for registering a hotkey.\n */\nexport interface HotkeyOptions {\n /** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */\n conflictBehavior?: ConflictBehavior\n /**\n * Soft-disable: when `false`, the callback does not run but the registration\n * stays in `HotkeyManager` (and in devtools). Toggling this should update the\n * existing handle via `setOptions` rather than unregistering. Defaults to `true`.\n */\n enabled?: boolean\n /** The event type to listen for. Defaults to 'keydown' */\n eventType?: 'keydown' | 'keyup'\n /** Whether to ignore hotkeys when keyboard events originate from input-like elements (text inputs, textarea, select, contenteditable — button-type inputs like type=button/submit/reset are not ignored). Defaults based on hotkey: true for single keys and Shift/Alt combos; false for Ctrl/Meta shortcuts and Escape */\n ignoreInputs?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** If true, only trigger once until all keys are released. Default: false */\n requireReset?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The DOM element to attach the event listener to. Defaults to document. */\n target?: HTMLElement | Document | Window | null\n /** Optional metadata (name, description, custom fields via declaration merging) */\n meta?: HotkeyMeta\n}\n\n/**\n * A registered hotkey handler in the HotkeyManager.\n */\nexport interface HotkeyRegistration {\n /** The callback to invoke */\n callback: HotkeyCallback\n /** Whether this registration has fired and needs reset (for requireReset) */\n hasFired: boolean\n /** The original hotkey string */\n hotkey: Hotkey\n /** Unique identifier for this registration */\n id: string\n /** Options for this registration */\n options: HotkeyOptions\n /** The parsed hotkey */\n parsedHotkey: ParsedHotkey\n /** The resolved target element for this registration */\n target: HTMLElement | Document | Window\n /** How many times this registration's callback has been triggered */\n triggerCount: number\n}\n\n/**\n * Public view of a hotkey registration for display and introspection.\n * Omits the callback function which is an internal implementation detail.\n */\nexport interface HotkeyRegistrationView {\n /** The original hotkey string */\n hotkey: Hotkey\n /** Unique identifier for this registration */\n id: string\n /** Options for this registration */\n options: HotkeyOptions\n /** The parsed hotkey */\n parsedHotkey: ParsedHotkey\n /** The resolved target element for this registration */\n target: HTMLElement | Document | Window\n /** How many times this registration's callback has been triggered */\n triggerCount: number\n /** Whether this registration has fired and needs reset (for requireReset) */\n hasFired: boolean\n}\n\n/**\n * Creates a public view from an internal registration,\n * stripping the callback function.\n */\nexport function toHotkeyRegistrationView(\n reg: HotkeyRegistration,\n): HotkeyRegistrationView {\n return {\n hotkey: reg.hotkey,\n id: reg.id,\n options: reg.options,\n parsedHotkey: reg.parsedHotkey,\n target: reg.target,\n triggerCount: reg.triggerCount,\n hasFired: reg.hasFired,\n }\n}\n\n/**\n * A handle returned from HotkeyManager.register() that allows updating\n * the callback and options without re-registering the hotkey.\n *\n * @example\n * ```ts\n * const handle = manager.register('Mod+S', callback, options)\n *\n * // Update callback without re-registering (avoids stale closures)\n * handle.callback = newCallback\n *\n * // Update options without re-registering\n * handle.setOptions({ enabled: false })\n *\n * // Check if still active\n * if (handle.isActive) {\n * // ...\n * }\n *\n * // Unregister when done\n * handle.unregister()\n * ```\n */\nexport interface HotkeyRegistrationHandle {\n /**\n * The callback function. Can be set directly to update without re-registering.\n * This avoids stale closures when the callback references React state.\n */\n callback: HotkeyCallback\n /** Unique identifier for this registration */\n readonly id: string\n /** Check if this registration is still active (not unregistered) */\n readonly isActive: boolean\n /**\n * Update options (merged with existing options).\n * Useful for updating `enabled`, `preventDefault`, etc. without re-registering.\n */\n setOptions: (options: Partial<HotkeyOptions>) => void\n /** Unregister this hotkey */\n unregister: () => void\n}\n\nlet registrationIdCounter = 0\n\n/**\n * Generates a unique ID for hotkey registrations.\n */\nfunction generateId(): string {\n return `hotkey_${++registrationIdCounter}`\n}\n\n/**\n * Singleton manager for hotkey registrations.\n *\n * This class provides a centralized way to register and manage keyboard hotkeys.\n * It uses a single event listener for efficiency, regardless of how many hotkeys\n * are registered.\n *\n * @example\n * ```ts\n * const manager = HotkeyManager.getInstance()\n *\n * const unregister = manager.register('Mod+S', (event, context) => {\n * console.log('Save triggered!')\n * })\n *\n * // Later, to unregister:\n * unregister()\n * ```\n */\nexport class HotkeyManager {\n static #instance: HotkeyManager | null = null\n\n /**\n * The TanStack Store containing all hotkey registrations.\n * Use this to subscribe to registration changes or access current registrations.\n *\n * @example\n * ```ts\n * const manager = HotkeyManager.getInstance()\n *\n * // Subscribe to registration changes\n * const unsubscribe = manager.registrations.subscribe(() => {\n * console.log('Registrations changed:', manager.registrations.state.size)\n * })\n *\n * // Access current registrations\n * for (const [id, reg] of manager.registrations.state) {\n * console.log(reg.hotkey, reg.options.enabled)\n * }\n * ```\n */\n readonly registrations: Store<Map<string, HotkeyRegistration>> = new Store(\n new Map(),\n )\n #platform: 'mac' | 'windows' | 'linux'\n #targetListeners: Map<\n HTMLElement | Document | Window,\n {\n keydown: (event: KeyboardEvent) => void\n keyup: (event: KeyboardEvent) => void\n }\n > = new Map()\n #targetRegistrations: Map<HTMLElement | Document | Window, Set<string>> =\n new Map()\n\n private constructor() {\n this.#platform = detectPlatform()\n }\n\n /**\n * Gets the singleton instance of HotkeyManager.\n */\n static getInstance(): HotkeyManager {\n if (!HotkeyManager.#instance) {\n HotkeyManager.#instance = new HotkeyManager()\n }\n return HotkeyManager.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (HotkeyManager.#instance) {\n HotkeyManager.#instance.destroy()\n HotkeyManager.#instance = null\n }\n }\n\n /**\n * Registers a hotkey handler and returns a handle for updating the registration.\n *\n * The returned handle allows updating the callback and options without\n * re-registering, which is useful for avoiding stale closures in React.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior\n * @returns A handle for managing the registration\n *\n * @example\n * ```ts\n * const handle = manager.register('Mod+S', callback)\n *\n * // Update callback without re-registering (avoids stale closures)\n * handle.callback = newCallback\n *\n * // Update options\n * handle.setOptions({ enabled: false })\n *\n * // Unregister when done\n * handle.unregister()\n * ```\n */\n register(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: HotkeyOptions = {},\n ): HotkeyRegistrationHandle {\n // In SSR environments, return a no-op handle since there's no DOM to attach listeners to\n if (typeof document === 'undefined' && !options.target) {\n let currentCallback = callback\n return {\n id: generateId(),\n unregister: () => {},\n get callback() {\n return currentCallback\n },\n set callback(newCallback: HotkeyCallback) {\n currentCallback = newCallback\n },\n setOptions: () => {},\n get isActive() {\n return false\n },\n }\n }\n\n const id = generateId()\n const platform = options.platform ?? this.#platform\n const parsedHotkey =\n typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)\n : rawHotkeyToParsedHotkey(hotkey, platform)\n const hotkeyStr = (\n typeof hotkey === 'string' ? hotkey : formatHotkey(parsedHotkey)\n ) as Hotkey\n\n // Resolve target: default to document if not provided\n // Note: SSR case without explicit target is handled above\n const target = options.target ?? document\n\n // Resolve conflict behavior\n const conflictBehavior = options.conflictBehavior ?? 'warn'\n\n // Check for existing registrations with the same hotkey and target\n const conflictingRegistration = this.#findConflictingRegistration(\n hotkeyStr,\n target,\n )\n\n if (conflictingRegistration) {\n handleConflict(\n conflictingRegistration.id,\n hotkeyStr,\n conflictBehavior,\n (id) => this.#unregister(id),\n )\n }\n\n const resolvedIgnoreInputs =\n options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey)\n\n const baseOptions = {\n ...defaultHotkeyOptions,\n requireReset: false,\n ...options,\n platform,\n }\n\n const registration: HotkeyRegistration = {\n id,\n hotkey: hotkeyStr,\n parsedHotkey,\n callback,\n options: { ...baseOptions, ignoreInputs: resolvedIgnoreInputs },\n hasFired: false,\n triggerCount: 0,\n target,\n }\n\n this.registrations.setState((prev) => new Map(prev).set(id, registration))\n\n // Track registration for this target\n if (!this.#targetRegistrations.has(target)) {\n this.#targetRegistrations.set(target, new Set())\n }\n this.#targetRegistrations.get(target)!.add(id)\n\n // Ensure listeners are attached for this target\n this.#ensureListenersForTarget(target)\n\n // Create and return the handle\n const manager = this\n const handle: HotkeyRegistrationHandle = {\n get id() {\n return id\n },\n unregister: () => {\n manager.#unregister(id)\n },\n get callback() {\n const reg = manager.registrations.state.get(id)\n return reg?.callback ?? callback\n },\n set callback(newCallback: HotkeyCallback) {\n const reg = manager.registrations.state.get(id)\n if (reg) {\n reg.callback = newCallback\n }\n },\n setOptions: (newOptions: Partial<HotkeyOptions>) => {\n manager.registrations.setState((prev) => {\n const reg = prev.get(id)\n if (reg) {\n const next = new Map(prev)\n next.set(id, { ...reg, options: { ...reg.options, ...newOptions } })\n return next\n }\n return prev\n })\n },\n get isActive() {\n return manager.registrations.state.has(id)\n },\n }\n\n return handle\n }\n\n /**\n * Unregisters a hotkey by its registration ID.\n */\n #unregister(id: string): void {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n return\n }\n\n const target = registration.target\n\n // Remove registration\n this.registrations.setState((prev) => {\n const next = new Map(prev)\n next.delete(id)\n return next\n })\n\n // Remove from target registrations tracking\n const targetRegs = this.#targetRegistrations.get(target)\n if (targetRegs) {\n targetRegs.delete(id)\n // If no more registrations for this target, remove listeners\n if (targetRegs.size === 0) {\n this.#removeListenersForTarget(target)\n }\n }\n }\n\n /**\n * Ensures event listeners are attached for a specific target.\n */\n #ensureListenersForTarget(target: HTMLElement | Document | Window): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n // Skip if listeners already exist for this target\n if (this.#targetListeners.has(target)) {\n return\n }\n\n const keydownHandler = this.#createTargetKeyDownHandler(target)\n const keyupHandler = this.#createTargetKeyUpHandler(target)\n\n target.addEventListener('keydown', keydownHandler as EventListener)\n target.addEventListener('keyup', keyupHandler as EventListener)\n\n this.#targetListeners.set(target, {\n keydown: keydownHandler,\n keyup: keyupHandler,\n })\n }\n\n /**\n * Removes event listeners for a specific target.\n */\n #removeListenersForTarget(target: HTMLElement | Document | Window): void {\n if (typeof document === 'undefined') {\n return\n }\n\n const listeners = this.#targetListeners.get(target)\n if (!listeners) {\n return\n }\n\n target.removeEventListener('keydown', listeners.keydown as EventListener)\n target.removeEventListener('keyup', listeners.keyup as EventListener)\n\n this.#targetListeners.delete(target)\n this.#targetRegistrations.delete(target)\n }\n\n /**\n * Processes keyboard events for a specific target and event type.\n */\n #processTargetEvent(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n eventType: 'keydown' | 'keyup',\n ): void {\n const targetRegs = this.#targetRegistrations.get(target)\n if (!targetRegs) {\n return\n }\n\n for (const id of targetRegs) {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n continue\n }\n\n // Check if event originated from or bubbled to this target\n if (!isEventForTarget(event, target)) {\n continue\n }\n\n if (!registration.options.enabled) {\n continue\n }\n\n // Check if we should ignore input elements (defaults to true)\n if (registration.options.ignoreInputs !== false) {\n if (shouldIgnoreInputEvent(event, target, registration.target)) {\n continue\n }\n }\n\n // Handle keydown events\n if (eventType === 'keydown') {\n if (registration.options.eventType !== 'keydown') {\n continue\n }\n\n // Check if the hotkey matches first\n const matches = matchesKeyboardEvent(\n event,\n registration.parsedHotkey,\n registration.options.platform,\n )\n\n if (matches) {\n // Always apply preventDefault/stopPropagation if the hotkey matches,\n // even when requireReset is active and has already fired\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n // Only execute callback if requireReset is not active or hasn't fired yet\n if (!registration.options.requireReset || !registration.hasFired) {\n this.#executeHotkeyCallback(registration, event)\n\n // Mark as fired if requireReset is enabled\n if (registration.options.requireReset) {\n registration.hasFired = true\n }\n }\n }\n }\n // Handle keyup events\n else {\n if (registration.options.eventType === 'keyup') {\n if (\n matchesKeyboardEvent(\n event,\n registration.parsedHotkey,\n registration.options.platform,\n )\n ) {\n this.#executeHotkeyCallback(registration, event)\n }\n }\n\n // Reset hasFired when any key in the hotkey is released\n if (registration.options.requireReset && registration.hasFired) {\n if (this.#shouldResetRegistration(registration, event)) {\n registration.hasFired = false\n }\n }\n }\n }\n }\n\n /**\n * Executes a hotkey callback with proper event handling.\n */\n #executeHotkeyCallback(\n registration: HotkeyRegistration,\n event: KeyboardEvent,\n ): void {\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n registration.triggerCount++\n\n // Notify the store so subscribers (e.g. devtools) see the updated count.\n // We create a new Map but keep the same registration reference to preserve\n // identity for mutation-based fields like hasFired.\n this.registrations.setState((prev) => new Map(prev))\n\n const context: HotkeyCallbackContext = {\n hotkey: registration.hotkey,\n parsedHotkey: registration.parsedHotkey,\n }\n\n registration.callback(event, context)\n }\n\n /**\n * Creates a keydown handler for a specific target.\n */\n #createTargetKeyDownHandler(\n target: HTMLElement | Document | Window,\n ): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keydown')\n }\n }\n\n /**\n * Creates a keyup handler for a specific target.\n */\n #createTargetKeyUpHandler(\n target: HTMLElement | Document | Window,\n ): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keyup')\n }\n }\n\n /**\n * Finds an existing registration with the same hotkey and target.\n */\n #findConflictingRegistration(\n hotkey: Hotkey,\n target: HTMLElement | Document | Window,\n ): HotkeyRegistration | null {\n for (const registration of this.registrations.state.values()) {\n if (registration.hotkey === hotkey && registration.target === target) {\n return registration\n }\n }\n return null\n }\n\n /**\n * Determines if a registration should be reset based on the keyup event.\n */\n #shouldResetRegistration(\n registration: HotkeyRegistration,\n event: KeyboardEvent,\n ): boolean {\n const parsed = registration.parsedHotkey\n const releasedKey = normalizeKeyName(event.key)\n\n // Reset if the main key is released\n // Compare case-insensitively for single-letter keys\n const parsedKeyNormalized =\n parsed.key.length === 1 ? parsed.key.toUpperCase() : parsed.key\n const releasedKeyNormalized =\n releasedKey.length === 1 ? releasedKey.toUpperCase() : releasedKey\n\n if (releasedKeyNormalized === parsedKeyNormalized) {\n return true\n }\n\n // Reset if any required modifier is released\n // Use normalized key names and check against canonical modifier names\n if (parsed.ctrl && releasedKey === 'Control') {\n return true\n }\n if (parsed.shift && releasedKey === 'Shift') {\n return true\n }\n if (parsed.alt && releasedKey === 'Alt') {\n return true\n }\n if (parsed.meta && releasedKey === 'Meta') {\n return true\n }\n\n return false\n }\n\n /**\n * Triggers a registration's callback programmatically from devtools.\n * Creates a synthetic KeyboardEvent and invokes the callback.\n *\n * @param id - The registration ID to trigger\n * @returns True if the registration was found and triggered\n */\n triggerRegistration(id: string): boolean {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n return false\n }\n\n const parsed = registration.parsedHotkey\n const syntheticEvent = new KeyboardEvent(\n registration.options.eventType ?? 'keydown',\n {\n key: parsed.key,\n ctrlKey: parsed.ctrl,\n shiftKey: parsed.shift,\n altKey: parsed.alt,\n metaKey: parsed.meta,\n bubbles: true,\n cancelable: true,\n },\n )\n\n registration.triggerCount++\n\n // Notify the store so subscribers (e.g. devtools) see the updated count\n this.registrations.setState((prev) => new Map(prev))\n\n registration.callback(syntheticEvent, {\n hotkey: registration.hotkey,\n parsedHotkey: registration.parsedHotkey,\n })\n\n return true\n }\n\n /**\n * Gets the number of registered hotkeys.\n */\n getRegistrationCount(): number {\n return this.registrations.state.size\n }\n\n /**\n * Checks if a specific hotkey is registered.\n *\n * @param hotkey - The hotkey string to check\n * @param target - Optional target element to match (if provided, both hotkey and target must match)\n * @returns True if a matching registration exists\n */\n isRegistered(\n hotkey: Hotkey,\n target?: HTMLElement | Document | Window,\n ): boolean {\n for (const registration of this.registrations.state.values()) {\n if (registration.hotkey === hotkey) {\n // If target is specified, both must match\n if (target === undefined || registration.target === target) {\n return true\n }\n }\n }\n return false\n }\n\n /**\n * Destroys the manager and removes all listeners.\n */\n destroy(): void {\n // Remove all target listeners\n for (const target of this.#targetListeners.keys()) {\n this.#removeListenersForTarget(target)\n }\n\n this.registrations.setState(() => new Map())\n this.#targetListeners.clear()\n this.#targetRegistrations.clear()\n }\n}\n\n/**\n * Gets the singleton HotkeyManager instance.\n * Convenience function for accessing the manager.\n */\nexport function getHotkeyManager(): HotkeyManager {\n return HotkeyManager.getInstance()\n}\n"],"mappings":";;;;;;;;;;;;AAqGA,SAAgB,yBACd,KACwB;AACxB,QAAO;EACL,QAAQ,IAAI;EACZ,IAAI,IAAI;EACR,SAAS,IAAI;EACb,cAAc,IAAI;EAClB,QAAQ,IAAI;EACZ,cAAc,IAAI;EAClB,UAAU,IAAI;EACf;;AA6CH,IAAI,wBAAwB;;;;AAK5B,SAAS,aAAqB;AAC5B,QAAO,UAAU,EAAE;;;;;;;;;;;;;;;;;;;;;AAsBrB,IAAa,gBAAb,MAAa,cAAc;CACzB,QAAOA,WAAkC;CAwBzC;CACA,mCAMI,IAAI,KAAK;CACb,uCACE,IAAI,KAAK;CAEX,AAAQ,cAAc;uBAd2C,IAAIC,sCACnE,IAAI,KAAK,CACV;AAaC,QAAKC,WAAYC,kCAAgB;;;;;CAMnC,OAAO,cAA6B;AAClC,MAAI,CAAC,eAAcH,SACjB,gBAAcA,WAAY,IAAI,eAAe;AAE/C,SAAO,eAAcA;;;;;CAMvB,OAAO,gBAAsB;AAC3B,MAAI,eAAcA,UAAW;AAC3B,kBAAcA,SAAU,SAAS;AACjC,kBAAcA,WAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6B9B,SACE,QACA,UACA,UAAyB,EAAE,EACD;AAE1B,MAAI,OAAO,aAAa,eAAe,CAAC,QAAQ,QAAQ;GACtD,IAAI,kBAAkB;AACtB,UAAO;IACL,IAAI,YAAY;IAChB,kBAAkB;IAClB,IAAI,WAAW;AACb,YAAO;;IAET,IAAI,SAAS,aAA6B;AACxC,uBAAkB;;IAEpB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AAc,aAAa;GAC5B,CAAC;AAEF,SAAO;;;;;CAMT,uBAA+B;AAC7B,SAAO,KAAK,cAAc,MAAM;;;;;;;;;CAUlC,aACE,QACA,QACS;AACT,OAAK,MAAM,gBAAgB,KAAK,cAAc,MAAM,QAAQ,CAC1D,KAAI,aAAa,WAAW,QAE1B;OAAI,WAAW,UAAa,aAAa,WAAW,OAClD,QAAO;;AAIb,SAAO;;;;;CAMT,UAAgB;AAEd,OAAK,MAAM,UAAU,MAAKT,gBAAiB,MAAM,CAC/C,OAAKD,yBAA0B,OAAO;AAGxC,OAAK,cAAc,+BAAe,IAAI,KAAK,CAAC;AAC5C,QAAKC,gBAAiB,OAAO;AAC7B,QAAKH,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
|
1
|
+
{"version":3,"file":"hotkey-manager.cjs","names":["#instance","Store","#platform","detectPlatform","parseHotkey","rawHotkeyToParsedHotkey","formatHotkey","#findConflictingRegistration","#unregister","getDefaultIgnoreInputs","defaultHotkeyOptions","#targetRegistrations","#ensureListenersForTarget","optionsEqual","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","#shouldResetRegistration","isRecordingEvent","isEventForTarget","shouldIgnoreInputEvent","matchKeyboardEvent","#executeHotkeyCallback","#processTargetEvent","areHotkeysEqual","normalizeKeyName"],"sources":["../src/hotkey-manager.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { isRecordingEvent } from './_recording-guard'\nimport { areHotkeysEqual } from './match'\nimport { detectPlatform } from './platform'\nimport { normalizeKeyName } from './constants'\nimport { formatHotkey } from './format'\nimport { parseHotkey, rawHotkeyToParsedHotkey } from './parse'\nimport { matchKeyboardEvent } from './_match'\nimport {\n defaultHotkeyOptions,\n getDefaultIgnoreInputs,\n handleConflict,\n optionsEqual,\n} from './_registration'\nimport { isEventForTarget, shouldIgnoreInputEvent } from './_event-target'\nimport type {\n ConflictBehavior,\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeyMeta,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey.types'\n\nexport type { ConflictBehavior }\n\n/**\n * Options for registering a hotkey.\n */\nexport interface HotkeyOptions {\n /** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */\n conflictBehavior?: ConflictBehavior\n /**\n * Soft-disable: when `false`, the callback does not run but the registration\n * stays in `HotkeyManager` (and in devtools). Toggling this should update the\n * existing handle via `setOptions` rather than unregistering. Defaults to `true`.\n */\n enabled?: boolean\n /** The event type to listen for. Defaults to 'keydown' */\n eventType?: 'keydown' | 'keyup'\n /** Whether to ignore hotkeys when keyboard events originate from input-like elements (text inputs, textarea, select, contenteditable — button-type inputs like type=button/submit/reset are not ignored). Defaults based on hotkey: true for single keys and Shift/Alt combos; false for Ctrl/Meta shortcuts and Escape */\n ignoreInputs?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** If true, only trigger once until all keys are released. Default: false */\n requireReset?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The DOM element to attach the event listener to. Defaults to document. */\n target?: HTMLElement | Document | Window | null\n /** Optional metadata (name, description, custom fields via declaration merging) */\n meta?: HotkeyMeta\n}\n\n/**\n * A registered hotkey handler in the HotkeyManager.\n */\nexport interface HotkeyRegistration {\n /** The callback to invoke */\n callback: HotkeyCallback\n /** Whether this registration has fired and needs reset (for requireReset) */\n hasFired: boolean\n /** The concrete key/code that activated a requireReset registration. */\n activeMatch?: { key: string; code: string }\n /** The original hotkey string */\n hotkey: Hotkey\n /** Unique identifier for this registration */\n id: string\n /** Options for this registration */\n options: HotkeyOptions\n /** The parsed hotkey */\n parsedHotkey: ParsedHotkey\n /** The resolved target element for this registration */\n target: HTMLElement | Document | Window\n /** How many times this registration's callback has been triggered */\n triggerCount: number\n}\n\n/**\n * Public view of a hotkey registration for display and introspection.\n * Omits the callback function which is an internal implementation detail.\n */\nexport interface HotkeyRegistrationView {\n /** The original hotkey string */\n hotkey: Hotkey\n /** Unique identifier for this registration */\n id: string\n /** Options for this registration */\n options: HotkeyOptions\n /** The parsed hotkey */\n parsedHotkey: ParsedHotkey\n /** The resolved target element for this registration */\n target: HTMLElement | Document | Window\n /** How many times this registration's callback has been triggered */\n triggerCount: number\n /** Whether this registration has fired and needs reset (for requireReset) */\n hasFired: boolean\n}\n\n/**\n * Creates a public view from an internal registration,\n * stripping the callback function.\n */\nexport function toHotkeyRegistrationView(\n reg: HotkeyRegistration,\n): HotkeyRegistrationView {\n return {\n hotkey: reg.hotkey,\n id: reg.id,\n options: reg.options,\n parsedHotkey: reg.parsedHotkey,\n target: reg.target,\n triggerCount: reg.triggerCount,\n hasFired: reg.hasFired,\n }\n}\n\n/**\n * A handle returned from HotkeyManager.register() that allows updating\n * the callback and options without re-registering the hotkey.\n *\n * @example\n * ```ts\n * const handle = manager.register('Mod+S', callback, options)\n *\n * // Update callback without re-registering (avoids stale closures)\n * handle.callback = newCallback\n *\n * // Update options without re-registering\n * handle.setOptions({ enabled: false })\n *\n * // Check if still active\n * if (handle.isActive) {\n * // ...\n * }\n *\n * // Unregister when done\n * handle.unregister()\n * ```\n */\nexport interface HotkeyRegistrationHandle {\n /**\n * The callback function. Can be set directly to update without re-registering.\n * This avoids stale closures when the callback references React state.\n */\n callback: HotkeyCallback\n /** Unique identifier for this registration */\n readonly id: string\n /** Check if this registration is still active (not unregistered) */\n readonly isActive: boolean\n /**\n * Update options (merged with existing options).\n * Useful for updating `enabled`, `preventDefault`, etc. without re-registering.\n */\n setOptions: (options: Partial<HotkeyOptions>) => void\n /** Unregister this hotkey */\n unregister: () => void\n}\n\nlet registrationIdCounter = 0\n\n/**\n * Generates a unique ID for hotkey registrations.\n */\nfunction generateId(): string {\n return `hotkey_${++registrationIdCounter}`\n}\n\n/**\n * Singleton manager for hotkey registrations.\n *\n * This class provides a centralized way to register and manage keyboard hotkeys.\n * It uses a single event listener for efficiency, regardless of how many hotkeys\n * are registered.\n *\n * @example\n * ```ts\n * const manager = HotkeyManager.getInstance()\n *\n * const unregister = manager.register('Mod+S', (event, context) => {\n * console.log('Save triggered!')\n * })\n *\n * // Later, to unregister:\n * unregister()\n * ```\n */\nexport class HotkeyManager {\n static #instance: HotkeyManager | null = null\n\n /**\n * The TanStack Store containing all hotkey registrations.\n * Use this to subscribe to registration changes or access current registrations.\n *\n * @example\n * ```ts\n * const manager = HotkeyManager.getInstance()\n *\n * // Subscribe to registration changes\n * const unsubscribe = manager.registrations.subscribe(() => {\n * console.log('Registrations changed:', manager.registrations.state.size)\n * })\n *\n * // Access current registrations\n * for (const [id, reg] of manager.registrations.state) {\n * console.log(reg.hotkey, reg.options.enabled)\n * }\n * ```\n */\n readonly registrations: Store<Map<string, HotkeyRegistration>> = new Store(\n new Map(),\n )\n #platform: 'mac' | 'windows' | 'linux'\n #targetListeners: Map<\n HTMLElement | Document | Window,\n {\n keydown: (event: KeyboardEvent) => void\n keyup: (event: KeyboardEvent) => void\n }\n > = new Map()\n #targetRegistrations: Map<HTMLElement | Document | Window, Set<string>> =\n new Map()\n\n private constructor() {\n this.#platform = detectPlatform()\n }\n\n /**\n * Gets the singleton instance of HotkeyManager.\n */\n static getInstance(): HotkeyManager {\n if (!HotkeyManager.#instance) {\n HotkeyManager.#instance = new HotkeyManager()\n }\n return HotkeyManager.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (HotkeyManager.#instance) {\n HotkeyManager.#instance.destroy()\n HotkeyManager.#instance = null\n }\n }\n\n /**\n * Registers a hotkey handler and returns a handle for updating the registration.\n *\n * The returned handle allows updating the callback and options without\n * re-registering, which is useful for avoiding stale closures in React.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior\n * @returns A handle for managing the registration\n *\n * @example\n * ```ts\n * const handle = manager.register('Mod+S', callback)\n *\n * // Update callback without re-registering (avoids stale closures)\n * handle.callback = newCallback\n *\n * // Update options\n * handle.setOptions({ enabled: false })\n *\n * // Unregister when done\n * handle.unregister()\n * ```\n */\n register(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: HotkeyOptions = {},\n ): HotkeyRegistrationHandle {\n // In SSR environments, return a no-op handle since there's no DOM to attach listeners to\n if (typeof document === 'undefined' && !options.target) {\n let currentCallback = callback\n return {\n id: generateId(),\n unregister: () => {},\n get callback() {\n return currentCallback\n },\n set callback(newCallback: HotkeyCallback) {\n currentCallback = newCallback\n },\n setOptions: () => {},\n get isActive() {\n return false\n },\n }\n }\n\n const id = generateId()\n const platform = options.platform ?? this.#platform\n const parsedHotkey =\n typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)\n : rawHotkeyToParsedHotkey(hotkey, platform)\n const hotkeyStr = (\n typeof hotkey === 'string' ? hotkey : formatHotkey(parsedHotkey)\n ) as Hotkey\n\n // Resolve target: default to document if not provided\n // Note: SSR case without explicit target is handled above\n const target = options.target ?? document\n\n // Resolve conflict behavior\n const conflictBehavior = options.conflictBehavior ?? 'warn'\n\n // Check for existing registrations with the same hotkey and target\n const conflictingRegistration = this.#findConflictingRegistration(\n hotkeyStr,\n target,\n )\n\n if (conflictingRegistration) {\n handleConflict(\n conflictingRegistration.id,\n hotkeyStr,\n conflictBehavior,\n (id) => this.#unregister(id),\n )\n }\n\n const resolvedIgnoreInputs =\n options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey)\n\n const baseOptions = {\n ...defaultHotkeyOptions,\n requireReset: false,\n ...options,\n platform,\n }\n\n const registration: HotkeyRegistration = {\n id,\n hotkey: hotkeyStr,\n parsedHotkey,\n callback,\n options: { ...baseOptions, ignoreInputs: resolvedIgnoreInputs },\n hasFired: false,\n triggerCount: 0,\n target,\n }\n\n this.registrations.setState((prev) => new Map(prev).set(id, registration))\n\n // Track registration for this target\n if (!this.#targetRegistrations.has(target)) {\n this.#targetRegistrations.set(target, new Set())\n }\n this.#targetRegistrations.get(target)!.add(id)\n\n // Ensure listeners are attached for this target\n this.#ensureListenersForTarget(target)\n\n // Create and return the handle\n const manager = this\n const handle: HotkeyRegistrationHandle = {\n get id() {\n return id\n },\n unregister: () => {\n manager.#unregister(id)\n },\n get callback() {\n const reg = manager.registrations.state.get(id)\n return reg?.callback ?? callback\n },\n set callback(newCallback: HotkeyCallback) {\n const reg = manager.registrations.state.get(id)\n if (reg) {\n reg.callback = newCallback\n }\n },\n setOptions: (newOptions: Partial<HotkeyOptions>) => {\n manager.registrations.setState((prev) => {\n const reg = prev.get(id)\n if (\n reg &&\n !optionsEqual(reg.options, { ...reg.options, ...newOptions })\n ) {\n const next = new Map(prev)\n next.set(id, { ...reg, options: { ...reg.options, ...newOptions } })\n return next\n }\n return prev\n })\n },\n get isActive() {\n return manager.registrations.state.has(id)\n },\n }\n\n return handle\n }\n\n /**\n * Unregisters a hotkey by its registration ID.\n */\n #unregister(id: string): void {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n return\n }\n\n const target = registration.target\n\n // Remove registration\n this.registrations.setState((prev) => {\n const next = new Map(prev)\n next.delete(id)\n return next\n })\n\n // Remove from target registrations tracking\n const targetRegs = this.#targetRegistrations.get(target)\n if (targetRegs) {\n targetRegs.delete(id)\n // If no more registrations for this target, remove listeners\n if (targetRegs.size === 0) {\n this.#removeListenersForTarget(target)\n }\n }\n }\n\n /**\n * Ensures event listeners are attached for a specific target.\n */\n #ensureListenersForTarget(target: HTMLElement | Document | Window): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n // Skip if listeners already exist for this target\n if (this.#targetListeners.has(target)) {\n return\n }\n\n const keydownHandler = this.#createTargetKeyDownHandler(target)\n const keyupHandler = this.#createTargetKeyUpHandler(target)\n\n target.addEventListener('keydown', keydownHandler as EventListener)\n target.addEventListener('keyup', keyupHandler as EventListener)\n\n this.#targetListeners.set(target, {\n keydown: keydownHandler,\n keyup: keyupHandler,\n })\n }\n\n /**\n * Removes event listeners for a specific target.\n */\n #removeListenersForTarget(target: HTMLElement | Document | Window): void {\n if (typeof document === 'undefined') {\n return\n }\n\n const listeners = this.#targetListeners.get(target)\n if (!listeners) {\n return\n }\n\n target.removeEventListener('keydown', listeners.keydown as EventListener)\n target.removeEventListener('keyup', listeners.keyup as EventListener)\n\n this.#targetListeners.delete(target)\n this.#targetRegistrations.delete(target)\n }\n\n /**\n * Processes keyboard events for a specific target and event type.\n */\n #processTargetEvent(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n eventType: 'keydown' | 'keyup',\n ): void {\n // Releases clear latches even when focus or enabled state now prevents callbacks.\n if (eventType === 'keyup') {\n for (const id of this.#targetRegistrations.get(target) ?? []) {\n const registration = this.registrations.state.get(id)\n if (\n registration?.hasFired &&\n this.#shouldResetRegistration(registration, event)\n ) {\n registration.hasFired = false\n registration.activeMatch = undefined\n }\n }\n }\n if (isRecordingEvent(event)) return\n const targetRegs = this.#targetRegistrations.get(target)\n if (!targetRegs) {\n return\n }\n\n const matches = new Map<string, ReturnType<typeof matchKeyboardEvent>>()\n let bestScore = 0\n\n // Exact logical/explicit-code matches outrank compatibility fallbacks.\n for (const id of targetRegs) {\n const registration = this.registrations.state.get(id)\n if (\n !registration ||\n !registration.options.enabled ||\n registration.options.eventType !== eventType ||\n !isEventForTarget(event, target) ||\n (registration.options.ignoreInputs !== false &&\n shouldIgnoreInputEvent(event, target, registration.target))\n ) {\n continue\n }\n const result = matchKeyboardEvent(\n event,\n registration.parsedHotkey,\n registration.options.platform,\n )\n if (result.matched) {\n matches.set(id, result)\n bestScore = Math.max(bestScore, result.score)\n }\n }\n\n for (const id of targetRegs) {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n continue\n }\n\n // Check if event originated from or bubbled to this target\n if (!isEventForTarget(event, target)) {\n continue\n }\n\n if (!registration.options.enabled) {\n continue\n }\n\n // Check if we should ignore input elements (defaults to true)\n if (registration.options.ignoreInputs !== false) {\n if (shouldIgnoreInputEvent(event, target, registration.target)) {\n continue\n }\n }\n\n // Handle keydown events\n if (eventType === 'keydown') {\n if (registration.options.eventType !== 'keydown') {\n continue\n }\n\n const match = matches.get(id)\n\n if (match?.matched && match.score === bestScore) {\n // Always apply preventDefault/stopPropagation if the hotkey matches,\n // even when requireReset is active and has already fired\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n // Only execute callback if requireReset is not active or hasn't fired yet\n if (!registration.options.requireReset || !registration.hasFired) {\n this.#executeHotkeyCallback(registration, event)\n\n // Mark as fired if requireReset is enabled\n if (registration.options.requireReset) {\n registration.hasFired = true\n registration.activeMatch = match.identity\n }\n }\n }\n }\n // Handle keyup events\n else {\n if (registration.options.eventType === 'keyup') {\n const match = matches.get(id)\n if (match?.matched && match.score === bestScore) {\n this.#executeHotkeyCallback(registration, event)\n }\n }\n }\n }\n }\n\n /**\n * Executes a hotkey callback with proper event handling.\n */\n #executeHotkeyCallback(\n registration: HotkeyRegistration,\n event: KeyboardEvent,\n ): void {\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n registration.triggerCount++\n\n // Notify the store so subscribers (e.g. devtools) see the updated count.\n // We create a new Map but keep the same registration reference to preserve\n // identity for mutation-based fields like hasFired.\n this.registrations.setState((prev) => new Map(prev))\n\n const context: HotkeyCallbackContext = {\n hotkey: registration.hotkey,\n parsedHotkey: registration.parsedHotkey,\n }\n\n registration.callback(event, context)\n }\n\n /**\n * Creates a keydown handler for a specific target.\n */\n #createTargetKeyDownHandler(\n target: HTMLElement | Document | Window,\n ): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keydown')\n }\n }\n\n /**\n * Creates a keyup handler for a specific target.\n */\n #createTargetKeyUpHandler(\n target: HTMLElement | Document | Window,\n ): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keyup')\n }\n }\n\n /**\n * Finds an existing registration with the same hotkey and target.\n */\n #findConflictingRegistration(\n hotkey: Hotkey,\n target: HTMLElement | Document | Window,\n ): HotkeyRegistration | null {\n for (const registration of this.registrations.state.values()) {\n if (\n areHotkeysEqual(\n registration.hotkey,\n hotkey,\n registration.options.platform,\n ) &&\n registration.target === target\n ) {\n return registration\n }\n }\n return null\n }\n\n /**\n * Determines if a registration should be reset based on the keyup event.\n */\n #shouldResetRegistration(\n registration: HotkeyRegistration,\n event: KeyboardEvent,\n ): boolean {\n const parsed = registration.parsedHotkey\n const releasedKey = normalizeKeyName(event.key)\n\n if (\n registration.activeMatch?.code &&\n event.code === registration.activeMatch.code\n ) {\n return true\n }\n\n // Compare the release against the identity this binding actually stores.\n if (parsed.code !== undefined) {\n if (event.code === parsed.code) return true\n } else {\n const parsedKeyNormalized =\n parsed.key.length === 1 ? parsed.key.toUpperCase() : parsed.key\n const releasedKeyNormalized =\n releasedKey.length === 1 ? releasedKey.toUpperCase() : releasedKey\n if (releasedKeyNormalized === parsedKeyNormalized) return true\n }\n\n // Reset if any required modifier is released\n // Use normalized key names and check against canonical modifier names\n if (parsed.ctrl && releasedKey === 'Control') {\n return true\n }\n if (parsed.shift && releasedKey === 'Shift') {\n return true\n }\n if (parsed.alt && releasedKey === 'Alt') {\n return true\n }\n if (parsed.meta && releasedKey === 'Meta') {\n return true\n }\n\n return false\n }\n\n /**\n * Triggers a registration's callback programmatically from devtools.\n * Creates a synthetic KeyboardEvent and invokes the callback.\n *\n * @param id - The registration ID to trigger\n * @returns True if the registration was found and triggered\n */\n triggerRegistration(id: string): boolean {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n return false\n }\n\n const parsed = registration.parsedHotkey\n const syntheticEvent = new KeyboardEvent(\n registration.options.eventType ?? 'keydown',\n {\n ...(parsed.code !== undefined\n ? { code: parsed.code }\n : { key: parsed.key }),\n ctrlKey: parsed.ctrl,\n shiftKey: parsed.shift,\n altKey: parsed.alt,\n metaKey: parsed.meta,\n bubbles: true,\n cancelable: true,\n },\n )\n\n registration.triggerCount++\n\n // Notify the store so subscribers (e.g. devtools) see the updated count\n this.registrations.setState((prev) => new Map(prev))\n\n registration.callback(syntheticEvent, {\n hotkey: registration.hotkey,\n parsedHotkey: registration.parsedHotkey,\n })\n\n return true\n }\n\n /**\n * Gets the number of registered hotkeys.\n */\n getRegistrationCount(): number {\n return this.registrations.state.size\n }\n\n /**\n * Checks if a specific hotkey is registered.\n *\n * @param hotkey - The hotkey string to check\n * @param target - Optional target element to match (if provided, both hotkey and target must match)\n * @returns True if a matching registration exists\n */\n isRegistered(\n hotkey: Hotkey,\n target?: HTMLElement | Document | Window,\n ): boolean {\n for (const registration of this.registrations.state.values()) {\n if (\n areHotkeysEqual(\n registration.hotkey,\n hotkey,\n registration.options.platform,\n )\n ) {\n // If target is specified, both must match\n if (target === undefined || registration.target === target) {\n return true\n }\n }\n }\n return false\n }\n\n /**\n * Destroys the manager and removes all listeners.\n */\n destroy(): void {\n // Remove all target listeners\n for (const target of this.#targetListeners.keys()) {\n this.#removeListenersForTarget(target)\n }\n\n this.registrations.setState(() => new Map())\n this.#targetListeners.clear()\n this.#targetRegistrations.clear()\n }\n}\n\n/**\n * Gets the singleton HotkeyManager instance.\n * Convenience function for accessing the manager.\n */\nexport function getHotkeyManager(): HotkeyManager {\n return HotkeyManager.getInstance()\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA0GA,SAAgB,yBACd,KACwB;AACxB,QAAO;EACL,QAAQ,IAAI;EACZ,IAAI,IAAI;EACR,SAAS,IAAI;EACb,cAAc,IAAI;EAClB,QAAQ,IAAI;EACZ,cAAc,IAAI;EAClB,UAAU,IAAI;EACf;;AA6CH,IAAI,wBAAwB;;;;AAK5B,SAAS,aAAqB;AAC5B,QAAO,UAAU,EAAE;;;;;;;;;;;;;;;;;;;;;AAsBrB,IAAa,gBAAb,MAAa,c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AAG;AACrD,MAAI,CAAC,aACH,QAAO;EAGT,MAAM,SAAS,aAAa;EAC5B,MAAM,iBAAiB,IAAI,cACzB,aAAa,QAAQ,aAAa,WAClC;GACE,GAAI,OAAO,SAAS,SAChB,EAAE,MAAM,OAAO,MAAM,GACrB,EAAE,KAAK,OAAO,KAAK;GACvB,SAAS,OAAO;GAChB,UAAU,OAAO;GACjB,QAAQ,OAAO;GACf,SAAS,OAAO;GAChB,SAAS;GACT,YAAY;GACb,CACF;AAED,eAAa;AAGb,OAAK,cAAc,UAAU,SAAS,IAAI,IAAI,KAAK,CAAC;AAEpD,eAAa,SAAS,gBAAgB;GACpC,QAAQ,aAAa;GACrB,cAAc,aAAa;GAC5B,CAAC;AAEF,SAAO;;;;;CAMT,uBAA+B;AAC7B,SAAO,KAAK,cAAc,MAAM;;;;;;;;;CAUlC,aACE,QACA,QACS;AACT,OAAK,MAAM,gBAAgB,KAAK,cAAc,MAAM,QAAQ,CAC1D,KACED,8BACE,aAAa,QACb,QACA,aAAa,QAAQ,SACtB,EAGD;OAAI,WAAW,UAAa,aAAa,WAAW,OAClD,QAAO;;AAIb,SAAO;;;;;CAMT,UAAgB;AAEd,OAAK,MAAM,UAAU,MAAKV,gBAAiB,MAAM,CAC/C,OAAKD,yBAA0B,OAAO;AAGxC,OAAK,cAAc,+BAAe,IAAI,KAAK,CAAC;AAC5C,QAAKC,gBAAiB,OAAO;AAC7B,QAAKJ,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Hotkey, HotkeyCallback, HotkeyMeta, ParsedHotkey, RegisterableHotkey } from "./hotkey.cjs";
|
|
2
|
-
import { ConflictBehavior } from "./manager.utils.cjs";
|
|
1
|
+
import { ConflictBehavior, Hotkey, HotkeyCallback, HotkeyMeta, ParsedHotkey, RegisterableHotkey } from "./hotkey.types.cjs";
|
|
3
2
|
import { Store } from "@tanstack/store";
|
|
4
3
|
|
|
5
4
|
//#region src/hotkey-manager.d.ts
|
|
@@ -40,6 +39,11 @@ interface HotkeyRegistration {
|
|
|
40
39
|
callback: HotkeyCallback;
|
|
41
40
|
/** Whether this registration has fired and needs reset (for requireReset) */
|
|
42
41
|
hasFired: boolean;
|
|
42
|
+
/** The concrete key/code that activated a requireReset registration. */
|
|
43
|
+
activeMatch?: {
|
|
44
|
+
key: string;
|
|
45
|
+
code: string;
|
|
46
|
+
};
|
|
43
47
|
/** The original hotkey string */
|
|
44
48
|
hotkey: Hotkey;
|
|
45
49
|
/** Unique identifier for this registration */
|
package/dist/hotkey-manager.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Hotkey, HotkeyCallback, HotkeyMeta, ParsedHotkey, RegisterableHotkey } from "./hotkey.js";
|
|
2
|
-
import { ConflictBehavior } from "./manager.utils.js";
|
|
1
|
+
import { ConflictBehavior, Hotkey, HotkeyCallback, HotkeyMeta, ParsedHotkey, RegisterableHotkey } from "./hotkey.types.js";
|
|
3
2
|
import { Store } from "@tanstack/store";
|
|
4
3
|
|
|
5
4
|
//#region src/hotkey-manager.d.ts
|
|
@@ -40,6 +39,11 @@ interface HotkeyRegistration {
|
|
|
40
39
|
callback: HotkeyCallback;
|
|
41
40
|
/** Whether this registration has fired and needs reset (for requireReset) */
|
|
42
41
|
hasFired: boolean;
|
|
42
|
+
/** The concrete key/code that activated a requireReset registration. */
|
|
43
|
+
activeMatch?: {
|
|
44
|
+
key: string;
|
|
45
|
+
code: string;
|
|
46
|
+
};
|
|
43
47
|
/** The original hotkey string */
|
|
44
48
|
hotkey: Hotkey;
|
|
45
49
|
/** Unique identifier for this registration */
|