@tanstack/hotkeys 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/dist/{manager.utils.js → _event-target.cjs} +5 -41
- package/dist/_event-target.cjs.map +1 -0
- package/dist/{manager.utils.cjs → _event-target.js} +3 -47
- package/dist/_event-target.js.map +1 -0
- package/dist/_keyboard-event.cjs +83 -0
- package/dist/_keyboard-event.cjs.map +1 -0
- package/dist/_keyboard-event.js +76 -0
- package/dist/_keyboard-event.js.map +1 -0
- package/dist/_match.cjs +49 -0
- package/dist/_match.cjs.map +1 -0
- package/dist/_match.js +49 -0
- package/dist/_match.js.map +1 -0
- package/dist/_named-keys.cjs +68 -0
- package/dist/_named-keys.cjs.map +1 -0
- package/dist/_named-keys.d.cts +8 -0
- package/dist/_named-keys.d.ts +8 -0
- package/dist/_named-keys.js +66 -0
- package/dist/_named-keys.js.map +1 -0
- package/dist/_recorder-chord.cjs +44 -0
- package/dist/_recorder-chord.cjs.map +1 -0
- package/dist/_recorder-chord.js +43 -0
- package/dist/_recorder-chord.js.map +1 -0
- package/dist/_recording-guard.cjs +59 -0
- package/dist/_recording-guard.cjs.map +1 -0
- package/dist/_recording-guard.js +56 -0
- package/dist/_recording-guard.js.map +1 -0
- package/dist/_registration.cjs +54 -0
- package/dist/_registration.cjs.map +1 -0
- package/dist/_registration.js +50 -0
- package/dist/_registration.js.map +1 -0
- package/dist/conflicts.cjs +55 -0
- package/dist/conflicts.cjs.map +1 -0
- package/dist/conflicts.d.cts +39 -0
- package/dist/conflicts.d.ts +39 -0
- package/dist/conflicts.js +55 -0
- package/dist/conflicts.js.map +1 -0
- package/dist/constants.cjs +46 -171
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +4 -129
- package/dist/constants.d.ts +4 -129
- package/dist/constants.js +48 -164
- package/dist/constants.js.map +1 -1
- package/dist/display-labels.cjs +119 -0
- package/dist/display-labels.cjs.map +1 -0
- package/dist/display-labels.d.cts +93 -0
- package/dist/display-labels.d.ts +93 -0
- package/dist/display-labels.js +113 -0
- package/dist/display-labels.js.map +1 -0
- package/dist/format.cjs +30 -36
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.cts +46 -7
- package/dist/format.d.ts +46 -7
- package/dist/format.js +31 -37
- package/dist/format.js.map +1 -1
- package/dist/hint.cjs +24 -0
- package/dist/hint.cjs.map +1 -0
- package/dist/hint.d.cts +23 -0
- package/dist/hint.d.ts +23 -0
- package/dist/hint.js +24 -0
- package/dist/hint.js.map +1 -0
- package/dist/hotkey-manager.cjs +53 -22
- package/dist/hotkey-manager.cjs.map +1 -1
- package/dist/hotkey-manager.d.cts +6 -2
- package/dist/hotkey-manager.d.ts +6 -2
- package/dist/hotkey-manager.js +49 -18
- package/dist/hotkey-manager.js.map +1 -1
- package/dist/hotkey-recorder.cjs +66 -10
- package/dist/hotkey-recorder.cjs.map +1 -1
- package/dist/hotkey-recorder.d.cts +5 -2
- package/dist/hotkey-recorder.d.ts +5 -2
- package/dist/hotkey-recorder.js +65 -9
- package/dist/hotkey-recorder.js.map +1 -1
- package/dist/hotkey-sequence-recorder.cjs +85 -22
- package/dist/hotkey-sequence-recorder.cjs.map +1 -1
- package/dist/hotkey-sequence-recorder.d.cts +9 -2
- package/dist/hotkey-sequence-recorder.d.ts +9 -2
- package/dist/hotkey-sequence-recorder.js +84 -21
- package/dist/hotkey-sequence-recorder.js.map +1 -1
- package/dist/{hotkey.d.ts → hotkey.types.d.cts} +55 -120
- package/dist/{hotkey.d.cts → hotkey.types.d.ts} +55 -120
- package/dist/index.cjs +18 -10
- package/dist/index.d.cts +14 -9
- package/dist/index.d.ts +14 -9
- package/dist/index.js +10 -6
- package/dist/key-state-tracker.cjs +24 -24
- package/dist/key-state-tracker.cjs.map +1 -1
- package/dist/key-state-tracker.js +24 -24
- package/dist/key-state-tracker.js.map +1 -1
- package/dist/key.types.d.cts +82 -0
- package/dist/key.types.d.ts +82 -0
- package/dist/match.cjs +21 -30
- package/dist/match.cjs.map +1 -1
- package/dist/match.d.cts +19 -3
- package/dist/match.d.ts +19 -3
- package/dist/match.js +19 -29
- package/dist/match.js.map +1 -1
- package/dist/parse.cjs +43 -26
- package/dist/parse.cjs.map +1 -1
- package/dist/parse.d.cts +10 -4
- package/dist/parse.d.ts +10 -4
- package/dist/parse.js +35 -19
- package/dist/parse.js.map +1 -1
- package/dist/platform.cjs +55 -0
- package/dist/platform.cjs.map +1 -0
- package/dist/platform.d.cts +44 -0
- package/dist/platform.d.ts +44 -0
- package/dist/platform.js +53 -0
- package/dist/platform.js.map +1 -0
- package/dist/recorder-options.d.cts +34 -0
- package/dist/recorder-options.d.ts +34 -0
- package/dist/sequence-manager.cjs +54 -28
- package/dist/sequence-manager.cjs.map +1 -1
- package/dist/sequence-manager.d.cts +18 -3
- package/dist/sequence-manager.d.ts +18 -3
- package/dist/sequence-manager.js +49 -23
- package/dist/sequence-manager.js.map +1 -1
- package/dist/validate.cjs +6 -10
- package/dist/validate.cjs.map +1 -1
- package/dist/validate.d.cts +13 -2
- package/dist/validate.d.ts +13 -2
- package/dist/validate.js +7 -11
- package/dist/validate.js.map +1 -1
- package/package.json +1 -1
- package/src/{manager.utils.ts → _event-target.ts} +0 -74
- package/src/_keyboard-event.ts +101 -0
- package/src/_match.ts +96 -0
- package/src/_named-keys.ts +62 -0
- package/src/_recorder-chord.ts +76 -0
- package/src/_recording-guard.ts +63 -0
- package/src/_registration.ts +79 -0
- package/src/conflicts.ts +125 -0
- package/src/constants.ts +48 -196
- package/src/display-labels.ts +123 -0
- package/src/format.ts +111 -52
- package/src/hint.ts +42 -0
- package/src/hotkey-manager.ts +95 -40
- package/src/hotkey-recorder.ts +91 -15
- package/src/hotkey-sequence-recorder.ts +110 -28
- package/src/{hotkey.ts → hotkey.types.ts} +57 -224
- package/src/index.ts +11 -5
- package/src/key-state-tracker.ts +36 -24
- package/src/key.types.ts +268 -0
- package/src/match.ts +41 -90
- package/src/parse.ts +67 -26
- package/src/platform.ts +66 -0
- package/src/recorder-options.ts +34 -0
- package/src/sequence-manager.ts +77 -42
- package/src/validate.ts +29 -21
- package/dist/manager.utils.cjs.map +0 -1
- package/dist/manager.utils.d.cts +0 -13
- package/dist/manager.utils.d.ts +0 -13
- package/dist/manager.utils.js.map +0 -1
- package/dist/recorder-chord.cjs +0 -18
- package/dist/recorder-chord.cjs.map +0 -1
- package/dist/recorder-chord.js +0 -18
- package/dist/recorder-chord.js.map +0 -1
- package/src/recorder-chord.ts +0 -25
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hotkey-sequence-recorder.js","names":["#options","#platform","#idleTimer","#clearIdleTimer","#keydownHandler","#finishWithoutCommitCallback","#patchSteps","#scheduleIdleTimer","#addListener","#removeListener"],"sources":["../src/hotkey-sequence-recorder.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { detectPlatform } from './constants'\nimport { isInputElement } from './manager.utils'\nimport { hotkeyChordFromKeydown } from './recorder-chord'\nimport type { HotkeySequence } from './sequence-manager'\n\n/**\n * How the user can commit a recorded sequence from the keyboard.\n * - `'enter'`: plain Enter (no modifiers) commits when at least one step exists.\n * - `'none'`: only {@link HotkeySequenceRecorder.commit} finishes recording (or idle timeout if set).\n */\nexport type HotkeySequenceRecorderCommitKeys = 'enter' | 'none'\n\n/**\n * State interface for the HotkeySequenceRecorder.\n */\nexport interface HotkeySequenceRecorderState {\n /** Whether recording is currently active */\n isRecording: boolean\n /** Chords captured so far in the current recording session */\n steps: HotkeySequence\n /** The last successfully committed sequence, or null if none / after starting a new session */\n recordedSequence: HotkeySequence | null\n}\n\n/**\n * Options for configuring a HotkeySequenceRecorder instance.\n */\nexport interface HotkeySequenceRecorderOptions {\n /** Callback when a sequence is successfully recorded (including empty array when cleared) */\n onRecord: (sequence: HotkeySequence) => void\n /** Optional callback when recording is cancelled (Escape pressed) */\n onCancel?: () => void\n /** Optional callback when the sequence is cleared (Backspace/Delete with no steps) */\n onClear?: () => void\n /**\n * Whether plain Enter commits the current steps. Ignored when {@link commitKeys} is `'none'`.\n * @default true\n */\n commitOnEnter?: boolean\n /**\n * Keyboard commit mode. When `'none'`, use {@link HotkeySequenceRecorder.commit} (and optional idle timeout).\n * @default 'enter'\n */\n commitKeys?: HotkeySequenceRecorderCommitKeys\n /**\n * Milliseconds of inactivity after the **last completed chord** before auto-committing.\n * The timer does not run while waiting for the first chord (`steps.length === 0`).\n */\n idleTimeoutMs?: number\n /**\n * Whether to ignore keyboard events from input-like elements (text inputs,\n * textarea, select, contenteditable). When true, typing in inputs passes\n * through normally instead of being captured as a sequence recording.\n * Escape always works regardless of this setting.\n * @default true\n */\n ignoreInputs?: boolean\n}\n\nconst defaultHotkeySequenceRecorderOptions: Pick<\n HotkeySequenceRecorderOptions,\n 'commitOnEnter' | 'commitKeys'\n> = {\n commitOnEnter: true,\n commitKeys: 'enter',\n}\n\nfunction resolvedCommitOnEnter(\n options: HotkeySequenceRecorderOptions,\n): boolean {\n if (options.commitKeys === 'none') {\n return false\n }\n return options.commitOnEnter !== false\n}\n\n/**\n * Framework-agnostic class for recording multi-chord sequences (Vim-style shortcuts).\n *\n * Each step is captured like a single hotkey chord. Press **Enter** (no modifiers) to commit\n * when {@link HotkeySequenceRecorderOptions.commitKeys} is `'enter'` (default), **Escape** to cancel,\n * **Backspace/Delete** to remove the last step or clear when empty.\n */\nexport class HotkeySequenceRecorder {\n readonly store: Store<HotkeySequenceRecorderState> =\n new Store<HotkeySequenceRecorderState>({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n })\n\n #keydownHandler: ((event: KeyboardEvent) => void) | null = null\n #options: HotkeySequenceRecorderOptions\n #platform: 'mac' | 'windows' | 'linux'\n #idleTimer: ReturnType<typeof setTimeout> | null = null\n\n constructor(options: HotkeySequenceRecorderOptions) {\n this.#options = {\n ...defaultHotkeySequenceRecorderOptions,\n ...options,\n }\n this.#platform = detectPlatform()\n }\n\n setOptions(options: Partial<HotkeySequenceRecorderOptions>): void {\n this.#options = {\n ...defaultHotkeySequenceRecorderOptions,\n ...this.#options,\n ...options,\n }\n }\n\n #clearIdleTimer(): void {\n if (this.#idleTimer !== null) {\n clearTimeout(this.#idleTimer)\n this.#idleTimer = null\n }\n }\n\n #scheduleIdleTimer(): void {\n this.#clearIdleTimer()\n const ms = this.#options.idleTimeoutMs\n if (ms === undefined || ms <= 0) {\n return\n }\n const steps = this.store.state.steps\n if (steps.length === 0) {\n return\n }\n this.#idleTimer = setTimeout(() => {\n this.#idleTimer = null\n if (!this.#keydownHandler) {\n return\n }\n if (this.store.state.steps.length >= 1) {\n this.commit()\n }\n }, ms)\n }\n\n #patchSteps(updater: (prev: HotkeySequence) => HotkeySequence): void {\n this.store.setState((s) => ({\n ...s,\n steps: updater(s.steps),\n }))\n }\n\n start(): void {\n if (this.#keydownHandler) {\n return\n }\n\n this.#clearIdleTimer()\n this.store.setState(() => ({\n isRecording: true,\n steps: [],\n recordedSequence: null,\n }))\n\n const handler = (event: KeyboardEvent) => {\n if (!this.#keydownHandler) {\n return\n }\n\n // If ignoreInputs is enabled (default) and focus is in an input element,\n // let the event pass through so the user can type normally.\n // Escape is the exception — it should always cancel recording.\n if (this.#options.ignoreInputs !== false) {\n const activeEl =\n typeof document !== 'undefined' ? document.activeElement : null\n if (isInputElement(activeEl) && event.key !== 'Escape') {\n return\n }\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n if (event.key === 'Escape') {\n this.cancel()\n return\n }\n\n if (event.key === 'Backspace' || event.key === 'Delete') {\n if (\n !event.ctrlKey &&\n !event.shiftKey &&\n !event.altKey &&\n !event.metaKey\n ) {\n const steps = this.store.state.steps\n if (steps.length === 0) {\n this.#options.onClear?.()\n this.#options.onRecord([])\n this.#finishWithoutCommitCallback()\n return\n }\n this.#patchSteps((prev) => prev.slice(0, -1))\n const next = this.store.state.steps\n if (next.length === 0) {\n this.#clearIdleTimer()\n } else {\n this.#scheduleIdleTimer()\n }\n return\n }\n }\n\n const enterCommits =\n resolvedCommitOnEnter(this.#options) &&\n event.key === 'Enter' &&\n !event.ctrlKey &&\n !event.shiftKey &&\n !event.altKey &&\n !event.metaKey\n\n if (enterCommits && this.store.state.steps.length >= 1) {\n this.commit()\n return\n }\n\n if (enterCommits && this.store.state.steps.length === 0) {\n return\n }\n\n const finalHotkey = hotkeyChordFromKeydown(event, this.#platform)\n if (finalHotkey === null) {\n return\n }\n\n this.#patchSteps((prev) => [...prev, finalHotkey])\n this.#scheduleIdleTimer()\n }\n\n this.#keydownHandler = handler\n this.#addListener(handler)\n }\n\n /**\n * Commit the current steps as a sequence. No-op if fewer than one step.\n */\n commit(): void {\n const steps = this.store.state.steps\n if (steps.length < 1) {\n return\n }\n\n const sequence = [...steps]\n\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: sequence,\n }))\n\n this.#options.onRecord(sequence)\n }\n\n #finishWithoutCommitCallback(): void {\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n }\n\n stop(): void {\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n }\n\n cancel(): void {\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n this.#options.onCancel?.()\n }\n\n #addListener(handler: (event: KeyboardEvent) => void): void {\n if (typeof document === 'undefined') {\n return\n }\n document.addEventListener('keydown', handler, true)\n }\n\n #removeListener(handler: (event: KeyboardEvent) => void): void {\n if (typeof document === 'undefined') {\n return\n }\n document.removeEventListener('keydown', handler, true)\n }\n\n destroy(): void {\n this.stop()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n }\n}\n"],"mappings":";;;;;;AA4DA,MAAM,uCAGF;CACF,eAAe;CACf,YAAY;CACb;AAED,SAAS,sBACP,SACS;AACT,KAAI,QAAQ,eAAe,OACzB,QAAO;AAET,QAAO,QAAQ,kBAAkB;;;;;;;;;AAUnC,IAAa,yBAAb,MAAoC;CAQlC,kBAA2D;CAC3D;CACA;CACA,aAAmD;CAEnD,YAAY,SAAwC;eAXlD,IAAI,MAAmC;GACrC,aAAa;GACb,OAAO,EAAE;GACT,kBAAkB;GACnB,CAAC;AAQF,QAAKA,UAAW;GACd,GAAG;GACH,GAAG;GACJ;AACD,QAAKC,WAAY,gBAAgB;;CAGnC,WAAW,SAAuD;AAChE,QAAKD,UAAW;GACd,GAAG;GACH,GAAG,MAAKA;GACR,GAAG;GACJ;;CAGH,kBAAwB;AACtB,MAAI,MAAKE,cAAe,MAAM;AAC5B,gBAAa,MAAKA,UAAW;AAC7B,SAAKA,YAAa;;;CAItB,qBAA2B;AACzB,QAAKC,gBAAiB;EACtB,MAAM,KAAK,MAAKH,QAAS;AACzB,MAAI,OAAO,UAAa,MAAM,EAC5B;AAGF,MADc,KAAK,MAAM,MAAM,MACrB,WAAW,EACnB;AAEF,QAAKE,YAAa,iBAAiB;AACjC,SAAKA,YAAa;AAClB,OAAI,CAAC,MAAKE,eACR;AAEF,OAAI,KAAK,MAAM,MAAM,MAAM,UAAU,EACnC,MAAK,QAAQ;KAEd,GAAG;;CAGR,YAAY,SAAyD;AACnE,OAAK,MAAM,UAAU,OAAO;GAC1B,GAAG;GACH,OAAO,QAAQ,EAAE,MAAM;GACxB,EAAE;;CAGL,QAAc;AACZ,MAAI,MAAKA,eACP;AAGF,QAAKD,gBAAiB;AACtB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,OAAO,EAAE;GACT,kBAAkB;GACn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"file":"hotkey-sequence-recorder.js","names":["#options","#platform","#idleTimer","#clearIdleTimer","#keydownHandler","#events","#patchSteps","#scheduleIdleTimer","#addListener","#removeListener"],"sources":["../src/hotkey-sequence-recorder.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { findHotkeyConflicts } from './conflicts'\nimport {\n beginRecording,\n captureRecordingEvent,\n endRecording,\n} from './_recording-guard'\nimport { chordRejection, hotkeyChordFromKeydown } from './_recorder-chord'\nimport { normalizeKeyboardEvent } from './_keyboard-event'\nimport { isModifierKey, parseHotkey } from './parse'\nimport { validateHotkey } from './validate'\nimport { detectPlatform } from './platform'\nimport { shouldIgnoreInputEvent } from './_event-target'\nimport type {\n HotkeySequenceRecorderValidationContext,\n RecorderOptions,\n} from './recorder-options'\nimport type { HotkeySequence } from './sequence-manager'\n\n/**\n * How the user can commit a recorded sequence from the keyboard.\n * - `'enter'`: plain Enter (no modifiers) commits when at least one step exists.\n * - `'none'`: only {@link HotkeySequenceRecorder.commit} finishes recording (or idle timeout if set).\n */\nexport type HotkeySequenceRecorderCommitKeys = 'enter' | 'none'\n\n/**\n * State interface for the HotkeySequenceRecorder.\n */\nexport interface HotkeySequenceRecorderState {\n /** Whether recording is currently active */\n isRecording: boolean\n /** Chords captured so far in the current recording session */\n steps: HotkeySequence\n /** The last successfully committed sequence, or null if none / after starting a new session */\n recordedSequence: HotkeySequence | null\n}\n\n/**\n * Options for configuring a HotkeySequenceRecorder instance.\n */\nexport interface HotkeySequenceRecorderOptions extends RecorderOptions {\n /** Validate the completed sequence. Rejection preserves the steps for editing. */\n validate?: (\n sequence: HotkeySequence,\n context: HotkeySequenceRecorderValidationContext,\n ) => boolean | string\n /** Callback when a sequence is successfully recorded */\n onRecord: (sequence: HotkeySequence) => void\n /** Optional callback when recording is cancelled (Escape pressed) */\n onCancel?: () => void\n /** Optional callback when the sequence is cleared (Backspace/Delete with no steps) */\n onClear?: () => void\n /**\n * Whether plain Enter commits the current steps. Ignored when {@link commitKeys} is `'none'`.\n * @default true\n */\n commitOnEnter?: boolean\n /**\n * Keyboard commit mode. When `'none'`, use {@link HotkeySequenceRecorder.commit} (and optional idle timeout).\n * @default 'enter'\n */\n commitKeys?: HotkeySequenceRecorderCommitKeys\n /**\n * Milliseconds of inactivity after the **last completed chord** before auto-committing.\n * The timer does not run while waiting for the first chord (`steps.length === 0`).\n */\n idleTimeoutMs?: number\n /**\n * Whether to ignore keyboard events from input-like elements (text inputs,\n * textarea, select, contenteditable). When true, typing in inputs passes\n * through normally instead of being captured as a sequence recording.\n * Escape always works regardless of this setting.\n * @default true\n */\n ignoreInputs?: boolean\n}\n\nconst defaultHotkeySequenceRecorderOptions: Pick<\n HotkeySequenceRecorderOptions,\n 'commitOnEnter' | 'commitKeys'\n> = {\n commitOnEnter: true,\n commitKeys: 'enter',\n}\n\n/** Resolves the Enter policy, with commitKeys: none overriding the legacy boolean. */\nfunction resolvedCommitOnEnter(\n options: HotkeySequenceRecorderOptions,\n): boolean {\n if (options.commitKeys === 'none') {\n return false\n }\n return options.commitOnEnter !== false\n}\n\n/**\n * Framework-agnostic class for recording multi-chord sequences (Vim-style shortcuts).\n *\n * Each step is captured like a single hotkey chord. Press **Enter** (no modifiers) to commit\n * when {@link HotkeySequenceRecorderOptions.commitKeys} is `'enter'` (default), **Escape** to cancel,\n * **Backspace/Delete** to remove the last step or clear when empty.\n */\nexport class HotkeySequenceRecorder {\n readonly store: Store<HotkeySequenceRecorderState> =\n new Store<HotkeySequenceRecorderState>({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n })\n\n #keydownHandler: ((event: KeyboardEvent) => void) | null = null\n #options: HotkeySequenceRecorderOptions\n #platform: 'mac' | 'windows' | 'linux'\n #events: Array<KeyboardEvent> = []\n #idleTimer: ReturnType<typeof setTimeout> | null = null\n\n constructor(options: HotkeySequenceRecorderOptions) {\n this.#options = {\n ...defaultHotkeySequenceRecorderOptions,\n ...options,\n }\n this.#platform = detectPlatform()\n }\n\n /** Merges current callbacks and options without discarding recorded steps. */\n setOptions(options: Partial<HotkeySequenceRecorderOptions>): void {\n this.#options = {\n ...defaultHotkeySequenceRecorderOptions,\n ...this.#options,\n ...options,\n }\n }\n\n /** Cancels a pending idle commit so it cannot fire after editing or stopping. */\n #clearIdleTimer(): void {\n if (this.#idleTimer !== null) {\n clearTimeout(this.#idleTimer)\n this.#idleTimer = null\n }\n }\n\n /** Restarts optional idle commit after a step is recorded or edited. */\n #scheduleIdleTimer(): void {\n this.#clearIdleTimer()\n const ms = this.#options.idleTimeoutMs\n if (ms === undefined || ms <= 0) {\n return\n }\n const steps = this.store.state.steps\n if (steps.length === 0) {\n return\n }\n this.#idleTimer = setTimeout(() => {\n this.#idleTimer = null\n if (!this.#keydownHandler) {\n return\n }\n if (this.store.state.steps.length >= 1) {\n this.commit()\n }\n }, ms)\n }\n\n /** Publishes edited steps while preserving the remaining recorder state. */\n #patchSteps(updater: (prev: HotkeySequence) => HotkeySequence): void {\n this.store.setState((s) => ({\n ...s,\n steps: updater(s.steps),\n }))\n }\n\n /** Starts a fresh recording; repeated starts during an active session are ignored. */\n start(): void {\n if (this.#keydownHandler) {\n return\n }\n\n this.#clearIdleTimer()\n this.#events = []\n beginRecording(this)\n this.store.setState(() => ({\n isRecording: true,\n steps: [],\n recordedSequence: null,\n }))\n\n const handler = (event: KeyboardEvent) => {\n if (!this.#keydownHandler) {\n return\n }\n\n // If ignoreInputs is enabled (default) and focus is in an input element,\n // let the event pass through so the user can type normally.\n // Escape is the exception — it should always cancel recording.\n if (\n this.#options.ignoreInputs !== false &&\n event.key !== 'Escape' &&\n shouldIgnoreInputEvent(event, document, document)\n )\n return\n\n const platform = this.#options.platform ?? this.#platform\n if (normalizeKeyboardEvent(event, platform).isComposing) return\n captureRecordingEvent(event)\n event.preventDefault()\n event.stopPropagation()\n if (event.repeat) return\n\n if (event.key === 'Escape') {\n this.cancel()\n return\n }\n\n if (event.key === 'Backspace' || event.key === 'Delete') {\n if (\n !event.ctrlKey &&\n !event.shiftKey &&\n !event.altKey &&\n !event.metaKey\n ) {\n const steps = this.store.state.steps\n if (steps.length === 0) {\n this.stop()\n this.#options.onClear?.()\n return\n }\n this.#events.pop()\n this.#patchSteps((prev) => prev.slice(0, -1))\n const next = this.store.state.steps\n if (next.length === 0) {\n this.#clearIdleTimer()\n } else {\n this.#scheduleIdleTimer()\n }\n return\n }\n }\n\n const enterCommits =\n resolvedCommitOnEnter(this.#options) &&\n event.key === 'Enter' &&\n !event.ctrlKey &&\n !event.shiftKey &&\n !event.altKey &&\n !event.metaKey\n\n if (enterCommits && this.store.state.steps.length >= 1) {\n this.commit()\n return\n }\n\n if (enterCommits && this.store.state.steps.length === 0) {\n return\n }\n\n if (isModifierKey(event.key) || event.key === 'AltGraph') return\n const rejection = chordRejection(event, { ...this.#options, platform })\n if (rejection) {\n this.#options.onReject?.(rejection)\n return\n }\n const finalHotkey = hotkeyChordFromKeydown(\n event,\n platform,\n this.#options.recordBy,\n )\n if (finalHotkey === null) {\n return\n }\n\n const validation = validateHotkey(finalHotkey)\n if (!validation.valid) {\n this.#options.onReject?.({\n reason: 'invalid',\n message: validation.errors.join('; '),\n hotkey: finalHotkey,\n })\n return\n }\n this.#events.push(event)\n this.#patchSteps((prev) => [...prev, finalHotkey])\n this.#scheduleIdleTimer()\n }\n\n this.#keydownHandler = handler\n this.#addListener(handler)\n }\n\n /**\n * Commit the current steps as a sequence. No-op if fewer than one step.\n */\n commit(): void {\n const steps = this.store.state.steps\n if (steps.length < 1) {\n return\n }\n\n const sequence = [...steps]\n const platform = this.#options.platform ?? this.#platform\n this.#clearIdleTimer()\n const accepted = this.#options.validate?.(sequence, {\n events: [...this.#events],\n parsedSequence: sequence.map((step) => parseHotkey(step, platform)),\n })\n if (accepted !== undefined && accepted !== true) {\n this.#options.onReject?.({\n reason: 'validation',\n message:\n typeof accepted === 'string'\n ? accepted\n : 'This sequence is not allowed.',\n sequence,\n })\n return\n }\n if (this.#options.detectConflicts) {\n const conflicts = findHotkeyConflicts(sequence, {\n ...(typeof this.#options.detectConflicts === 'object'\n ? this.#options.detectConflicts\n : {}),\n platform,\n events: this.#events,\n })\n if (conflicts.length) {\n this.#options.onReject?.({\n reason: 'conflict',\n message: 'This sequence conflicts with a registered binding.',\n sequence,\n conflicts,\n })\n return\n }\n }\n endRecording(this)\n this.#events = []\n\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: sequence,\n }))\n\n this.#options.onRecord(sequence)\n }\n\n /** Stops and discards in-progress steps without invoking onCancel. */\n stop(): void {\n endRecording(this)\n this.#events = []\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n }\n\n /** Stops, discards in-progress steps, and notifies onCancel. */\n cancel(): void {\n endRecording(this)\n this.#events = []\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n this.#clearIdleTimer()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n this.#options.onCancel?.()\n }\n\n /** Captures keydowns before application handlers, with an SSR-safe document check. */\n #addListener(handler: (event: KeyboardEvent) => void): void {\n if (typeof document === 'undefined') {\n return\n }\n document.addEventListener('keydown', handler, true)\n }\n\n /** Detaches the capture listener when recording ends. */\n #removeListener(handler: (event: KeyboardEvent) => void): void {\n if (typeof document === 'undefined') {\n return\n }\n document.removeEventListener('keydown', handler, true)\n }\n\n destroy(): void {\n this.stop()\n this.store.setState(() => ({\n isRecording: false,\n steps: [],\n recordedSequence: null,\n }))\n }\n}\n"],"mappings":";;;;;;;;;;;AA8EA,MAAM,uCAGF;CACF,eAAe;CACf,YAAY;CACb;;AAGD,SAAS,sBACP,SACS;AACT,KAAI,QAAQ,eAAe,OACzB,QAAO;AAET,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AAS,SAAS;;;CAIlC,OAAa;AACX,eAAa,KAAK;AAClB,QAAKK,SAAU,EAAE;AACjB,MAAI,MAAKD,gBAAiB;AACxB,SAAKK,eAAgB,MAAKL,eAAgB;AAC1C,SAAKA,iBAAkB;;AAEzB,QAAKD,gBAAiB;AACtB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,OAAO,EAAE;GACT,kBAAkB;GACnB,EAAE;;;CAIL,SAAe;AACb,eAAa,KAAK;AAClB,QAAKE,SAAU,EAAE;AACjB,MAAI,MAAKD,gBAAiB;AACxB,SAAKK,eAAgB,MAAKL,eAAgB;AAC1C,SAAKA,iBAAkB;;AAEzB,QAAKD,gBAAiB;AACtB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,OAAO,EAAE;GACT,kBAAkB;GACnB,EAAE;AACH,QAAKH,QAAS,YAAY;;;CAI5B,aAAa,SAA+C;AAC1D,MAAI,OAAO,aAAa,YACtB;AAEF,WAAS,iBAAiB,WAAW,SAAS,KAAK;;;CAIrD,gBAAgB,SAA+C;AAC7D,MAAI,OAAO,aAAa,YACtB;AAEF,WAAS,oBAAoB,WAAW,SAAS,KAAK;;CAGxD,UAAgB;AACd,OAAK,MAAM;AACX,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,OAAO,EAAE;GACT,kBAAkB;GACnB,EAAE"}
|
|
@@ -1,66 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* - Control/Ctrl: The Control key
|
|
5
|
-
* - Shift: The Shift key
|
|
6
|
-
* - Alt/Option: The Alt key (Option on macOS)
|
|
7
|
-
* - Command/Cmd: The Command key (macOS only)
|
|
8
|
-
* - CommandOrControl/Mod: Command on macOS, Control on other platforms
|
|
9
|
-
*/
|
|
10
|
-
type Modifier = 'Control' | 'Ctrl' | 'Shift' | 'Alt' | 'Option' | 'Command' | 'Cmd' | 'CommandOrControl' | 'Mod';
|
|
11
|
-
/**
|
|
12
|
-
* Canonical modifier names that map to KeyboardEvent properties.
|
|
13
|
-
*/
|
|
14
|
-
type CanonicalModifier = 'Control' | 'Shift' | 'Alt' | 'Meta';
|
|
15
|
-
/**
|
|
16
|
-
* Letter keys A-Z (case-insensitive in matching).
|
|
17
|
-
*/
|
|
18
|
-
type LetterKey = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
19
|
-
/**
|
|
20
|
-
* Number keys 0-9.
|
|
21
|
-
*/
|
|
22
|
-
type NumberKey = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
23
|
-
/**
|
|
24
|
-
* Function keys F1-F12.
|
|
25
|
-
*/
|
|
26
|
-
type FunctionKey = 'F1' | 'F2' | 'F3' | 'F4' | 'F5' | 'F6' | 'F7' | 'F8' | 'F9' | 'F10' | 'F11' | 'F12';
|
|
27
|
-
/**
|
|
28
|
-
* Navigation keys for cursor movement.
|
|
29
|
-
*/
|
|
30
|
-
type NavigationKey = 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Home' | 'End' | 'PageUp' | 'PageDown';
|
|
31
|
-
/**
|
|
32
|
-
* Editing and special keys.
|
|
33
|
-
*/
|
|
34
|
-
type EditingKey = 'Enter' | 'Escape' | 'Space' | 'Tab' | 'Backspace' | 'Delete';
|
|
35
|
-
/**
|
|
36
|
-
* Punctuation keys commonly used in keyboard shortcuts.
|
|
37
|
-
* These are the literal characters as they appear in KeyboardEvent.key
|
|
38
|
-
* (layout-dependent, typically US keyboard layout).
|
|
39
|
-
*/
|
|
40
|
-
type PunctuationKey = '/' | '[' | ']' | '\\' | '=' | '-' | ',' | '.' | ';' | '`';
|
|
41
|
-
/**
|
|
42
|
-
* Keys that don't change their value when Shift is pressed.
|
|
43
|
-
* These keys produce the same `KeyboardEvent.key` value whether Shift is held or not.
|
|
44
|
-
*
|
|
45
|
-
* Excludes NumberKey (Shift+1 produces '!' on US layout) and PunctuationKey
|
|
46
|
-
* (Shift+',' produces '<' on US layout).
|
|
47
|
-
*
|
|
48
|
-
* Used in hotkey type definitions to prevent layout-dependent issues when Shift
|
|
49
|
-
* is part of the modifier combination.
|
|
50
|
-
*/
|
|
51
|
-
type NonPunctuationKey = LetterKey | NumberKey | EditingKey | NavigationKey | FunctionKey;
|
|
52
|
-
/**
|
|
53
|
-
* All supported non-modifier keys.
|
|
54
|
-
*/
|
|
55
|
-
type Key = NonPunctuationKey | PunctuationKey;
|
|
56
|
-
/**
|
|
57
|
-
* Includes both modifier keys and regular keys.
|
|
58
|
-
*/
|
|
59
|
-
type IndividualKey = CanonicalModifier | Key;
|
|
1
|
+
import { CanonicalModifier, Key, LogicalKey } from "./key.types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hotkey.types.d.ts
|
|
60
4
|
/**
|
|
61
5
|
* Single modifier + key combinations.
|
|
62
6
|
* Uses canonical modifiers (4) + Mod (1) = 5 modifiers.
|
|
63
|
-
* Shift combinations exclude PunctuationKey to avoid layout-dependent issues.
|
|
64
7
|
*
|
|
65
8
|
* The `Mod` modifier is platform-adaptive:
|
|
66
9
|
* - **macOS**: Resolves to `Meta` (Command key ⌘)
|
|
@@ -69,10 +12,9 @@ type IndividualKey = CanonicalModifier | Key;
|
|
|
69
12
|
* This enables cross-platform hotkey definitions that automatically adapt to the platform.
|
|
70
13
|
* For example, `Mod+S` becomes `Command+S` on Mac and `Ctrl+S` on Windows/Linux.
|
|
71
14
|
*/
|
|
72
|
-
type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${
|
|
15
|
+
type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${Key}` | `Meta+${Key}` | `Mod+${Key}`;
|
|
73
16
|
/**
|
|
74
17
|
* Two modifier + key combinations.
|
|
75
|
-
* Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
|
|
76
18
|
*
|
|
77
19
|
* **Platform-adaptive `Mod` combinations:**
|
|
78
20
|
* - `Mod+Alt` and `Mod+Shift` are included (safe on all platforms)
|
|
@@ -80,10 +22,9 @@ type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${NonPunctu
|
|
|
80
22
|
* - `Mod+Control` duplicates `Control` on Windows/Linux (Mod = Control)
|
|
81
23
|
* - `Mod+Meta` duplicates `Meta` on macOS (Mod = Meta)
|
|
82
24
|
*/
|
|
83
|
-
type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${
|
|
25
|
+
type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${Key}` | `Control+Meta+${Key}` | `Alt+Shift+${Key}` | `Alt+Meta+${Key}` | `Shift+Meta+${Key}` | `Mod+Alt+${Key}` | `Mod+Shift+${Key}`;
|
|
84
26
|
/**
|
|
85
27
|
* Three modifier + key combinations.
|
|
86
|
-
* Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
|
|
87
28
|
*
|
|
88
29
|
* **Platform-adaptive `Mod` combinations:**
|
|
89
30
|
* - `Mod+Alt+Shift` is included (safe on all platforms)
|
|
@@ -91,10 +32,9 @@ type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${NonPunctuationK
|
|
|
91
32
|
* - `Mod+Control+Shift` duplicates `Control` on Windows/Linux (Mod = Control)
|
|
92
33
|
* - `Mod+Shift+Meta` duplicates `Meta` on macOS (Mod = Meta)
|
|
93
34
|
*/
|
|
94
|
-
type ThreeModifierHotkey = `Control+Alt+Shift+${
|
|
35
|
+
type ThreeModifierHotkey = `Control+Alt+Shift+${Key}` | `Control+Alt+Meta+${Key}` | `Control+Shift+Meta+${Key}` | `Alt+Shift+Meta+${Key}` | `Mod+Alt+Shift+${Key}`;
|
|
95
36
|
/**
|
|
96
37
|
* Four modifier + key combinations.
|
|
97
|
-
* Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
|
|
98
38
|
*
|
|
99
39
|
* Only the canonical `Control+Alt+Shift+Meta` combination is included.
|
|
100
40
|
*
|
|
@@ -105,7 +45,7 @@ type ThreeModifierHotkey = `Control+Alt+Shift+${NonPunctuationKey}` | `Control+A
|
|
|
105
45
|
* - `Mod+Control+Alt+Shift` → duplicates `Control` on Windows/Linux
|
|
106
46
|
* - `Mod+Alt+Shift+Meta` → duplicates `Meta` on macOS
|
|
107
47
|
*/
|
|
108
|
-
type FourModifierHotkey = `Control+Alt+Shift+Meta+${
|
|
48
|
+
type FourModifierHotkey = `Control+Alt+Shift+Meta+${Key}`;
|
|
109
49
|
/**
|
|
110
50
|
* A type-safe hotkey string.
|
|
111
51
|
*
|
|
@@ -159,38 +99,35 @@ type FourModifierHotkey = `Control+Alt+Shift+Meta+${NonPunctuationKey}`;
|
|
|
159
99
|
* ```
|
|
160
100
|
*/
|
|
161
101
|
type Hotkey = Key | SingleModifierHotkey | TwoModifierHotkey | ThreeModifierHotkey | FourModifierHotkey;
|
|
162
|
-
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
* This interface provides a flexible fallback when the `Hotkey` type doesn't
|
|
166
|
-
* fit your use case. You can pass a `ParsedHotkey` directly to hotkey functions
|
|
167
|
-
* instead of a hotkey string, allowing for more dynamic or complex scenarios
|
|
168
|
-
* that aren't covered by the type-safe `Hotkey` union.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* // Type-safe hotkey string
|
|
173
|
-
* useHotkey('Mod+S', handler)
|
|
174
|
-
*
|
|
175
|
-
* // Fallback: parsed hotkey for dynamic scenarios
|
|
176
|
-
* const parsed = parseHotkey(userInput)
|
|
177
|
-
* useHotkey(parsed, handler) // Works even if userInput isn't in Hotkey type
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
interface ParsedHotkey {
|
|
181
|
-
/** The non-modifier key (e.g., 'S', 'Escape', 'F1', '/', '['). Can be any string for flexibility. */
|
|
182
|
-
key: Key | (string & {});
|
|
183
|
-
/** Whether the Control key is required */
|
|
102
|
+
/** Resolved modifier flags shared by both parsed binding variants. */
|
|
103
|
+
interface ParsedModifiers {
|
|
104
|
+
/** Whether Control is required. */
|
|
184
105
|
ctrl: boolean;
|
|
185
|
-
/** Whether
|
|
106
|
+
/** Whether Shift is required. */
|
|
186
107
|
shift: boolean;
|
|
187
|
-
/** Whether
|
|
108
|
+
/** Whether Alt is required. */
|
|
188
109
|
alt: boolean;
|
|
189
|
-
/** Whether
|
|
110
|
+
/** Whether Meta (Command) is required. */
|
|
190
111
|
meta: boolean;
|
|
191
|
-
/**
|
|
112
|
+
/** Required modifiers in canonical order. */
|
|
192
113
|
modifiers: Array<CanonicalModifier>;
|
|
193
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* A parsed binding matches either a logical key or a physical code, never both.
|
|
117
|
+
* Check `code !== undefined` to narrow to a physical binding. Its logical
|
|
118
|
+
* character is unknown without a keyboard event and is not stored in `key`.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* parseHotkey('Alt+S') // { key: 'S', alt: true, ... }
|
|
122
|
+
* parseHotkey('Alt+[KeyS]') // { code: 'KeyS', alt: true, ... } — no key property
|
|
123
|
+
*/
|
|
124
|
+
type ParsedHotkey = ParsedModifiers & ({
|
|
125
|
+
key: LogicalKey | (string & {});
|
|
126
|
+
code?: never;
|
|
127
|
+
} | {
|
|
128
|
+
code: string;
|
|
129
|
+
key?: never;
|
|
130
|
+
});
|
|
194
131
|
/**
|
|
195
132
|
* A raw hotkey object for programmatic registration.
|
|
196
133
|
*
|
|
@@ -209,11 +146,13 @@ interface ParsedHotkey {
|
|
|
209
146
|
* useHotkey({ key: 'Escape' }, handler) // Escape (no modifiers)
|
|
210
147
|
* useHotkey({ key: 'A', shift: true, meta: true }, handler) // Shift+Meta+A
|
|
211
148
|
* useHotkey({ key: 'S', mod: true, shift: true }, handler) // Mod+Shift+S
|
|
149
|
+
* useHotkey({ code: 'KeyW', mod: true }, handler) // Physical Mod+KeyW
|
|
150
|
+
* useHotkey({ code: 'NumpadAdd', mod: true }, handler) // Physical numpad plus
|
|
212
151
|
* ```
|
|
213
152
|
*/
|
|
214
|
-
|
|
153
|
+
/** Modifier flags shared by logical-key and physical-code registrations. */
|
|
154
|
+
interface RawModifiers {
|
|
215
155
|
/** The non-modifier key (e.g., 'S', 'Escape', 'F1'). */
|
|
216
|
-
key: Key | (string & {});
|
|
217
156
|
/** Platform-adaptive modifier: Command on macOS, Control on Windows/Linux. Defaults to false. */
|
|
218
157
|
mod?: boolean;
|
|
219
158
|
/** Whether the Control key is required. Defaults to false. */
|
|
@@ -225,33 +164,18 @@ interface RawHotkey {
|
|
|
225
164
|
/** Whether the Meta (Command) key is required. Defaults to false. */
|
|
226
165
|
meta?: boolean;
|
|
227
166
|
}
|
|
167
|
+
type RawHotkey = ({
|
|
168
|
+
key: LogicalKey | (string & {});
|
|
169
|
+
code?: never;
|
|
170
|
+
} & RawModifiers) | ({
|
|
171
|
+
code: string;
|
|
172
|
+
key?: never;
|
|
173
|
+
} & RawModifiers);
|
|
228
174
|
/**
|
|
229
175
|
* A hotkey that can be passed to `HotkeyManager.register()` and `useHotkey()`.
|
|
230
176
|
* Either a type-safe string (`Hotkey`) or a raw object (`RawHotkey`).
|
|
231
177
|
*/
|
|
232
178
|
type RegisterableHotkey = Hotkey | RawHotkey;
|
|
233
|
-
/**
|
|
234
|
-
* Options for formatting hotkeys for display.
|
|
235
|
-
*/
|
|
236
|
-
interface FormatDisplayOptions {
|
|
237
|
-
/** The target platform. Defaults to auto-detection. */
|
|
238
|
-
platform?: 'mac' | 'windows' | 'linux';
|
|
239
|
-
/** Whether to use symbols for the display. Defaults to true. */
|
|
240
|
-
useSymbols?: boolean;
|
|
241
|
-
/** Override the separator between display tokens. Defaults to platform-specific formatting when null/undefined. */
|
|
242
|
-
separatorToken?: string | null;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Result of validating a hotkey string.
|
|
246
|
-
*/
|
|
247
|
-
interface ValidationResult {
|
|
248
|
-
/** Whether the hotkey is valid (can still have warnings) */
|
|
249
|
-
valid: boolean;
|
|
250
|
-
/** Warning messages about potential issues */
|
|
251
|
-
warnings: Array<string>;
|
|
252
|
-
/** Error messages about invalid syntax */
|
|
253
|
-
errors: Array<string>;
|
|
254
|
-
}
|
|
255
179
|
/**
|
|
256
180
|
* Context passed to hotkey callbacks along with the keyboard event.
|
|
257
181
|
*/
|
|
@@ -278,7 +202,7 @@ interface HotkeyCallbackContext {
|
|
|
278
202
|
type HotkeyCallback = (event: KeyboardEvent, context: HotkeyCallbackContext) => void;
|
|
279
203
|
/**
|
|
280
204
|
* Metadata for hotkey and sequence registrations.
|
|
281
|
-
* Includes `name` and `
|
|
205
|
+
* Includes `name`, `description`, and `group` by default. Extend via declaration merging:
|
|
282
206
|
*
|
|
283
207
|
* @example
|
|
284
208
|
* ```ts
|
|
@@ -295,7 +219,18 @@ interface HotkeyMeta {
|
|
|
295
219
|
name?: string;
|
|
296
220
|
/** Description of what this hotkey does */
|
|
297
221
|
description?: string;
|
|
222
|
+
/** Optional display group. Does not affect matching or scope. */
|
|
223
|
+
group?: string;
|
|
298
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Behavior when registering a hotkey/sequence that conflicts with an existing registration.
|
|
227
|
+
*
|
|
228
|
+
* - `'warn'` - Log a warning to the console but allow both registrations (default)
|
|
229
|
+
* - `'error'` - Throw an error and prevent the new registration
|
|
230
|
+
* - `'replace'` - Unregister the existing registration and register the new one
|
|
231
|
+
* - `'allow'` - Allow multiple registrations without warning
|
|
232
|
+
*/
|
|
233
|
+
type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
|
|
299
234
|
//#endregion
|
|
300
|
-
export {
|
|
301
|
-
//# sourceMappingURL=hotkey.d.
|
|
235
|
+
export { ConflictBehavior, FourModifierHotkey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyMeta, ParsedHotkey, ParsedModifiers, RawHotkey, RawModifiers, RegisterableHotkey, SingleModifierHotkey, ThreeModifierHotkey, TwoModifierHotkey };
|
|
236
|
+
//# sourceMappingURL=hotkey.types.d.cts.map
|
|
@@ -1,66 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* - Control/Ctrl: The Control key
|
|
5
|
-
* - Shift: The Shift key
|
|
6
|
-
* - Alt/Option: The Alt key (Option on macOS)
|
|
7
|
-
* - Command/Cmd: The Command key (macOS only)
|
|
8
|
-
* - CommandOrControl/Mod: Command on macOS, Control on other platforms
|
|
9
|
-
*/
|
|
10
|
-
type Modifier = 'Control' | 'Ctrl' | 'Shift' | 'Alt' | 'Option' | 'Command' | 'Cmd' | 'CommandOrControl' | 'Mod';
|
|
11
|
-
/**
|
|
12
|
-
* Canonical modifier names that map to KeyboardEvent properties.
|
|
13
|
-
*/
|
|
14
|
-
type CanonicalModifier = 'Control' | 'Shift' | 'Alt' | 'Meta';
|
|
15
|
-
/**
|
|
16
|
-
* Letter keys A-Z (case-insensitive in matching).
|
|
17
|
-
*/
|
|
18
|
-
type LetterKey = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
19
|
-
/**
|
|
20
|
-
* Number keys 0-9.
|
|
21
|
-
*/
|
|
22
|
-
type NumberKey = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
23
|
-
/**
|
|
24
|
-
* Function keys F1-F12.
|
|
25
|
-
*/
|
|
26
|
-
type FunctionKey = 'F1' | 'F2' | 'F3' | 'F4' | 'F5' | 'F6' | 'F7' | 'F8' | 'F9' | 'F10' | 'F11' | 'F12';
|
|
27
|
-
/**
|
|
28
|
-
* Navigation keys for cursor movement.
|
|
29
|
-
*/
|
|
30
|
-
type NavigationKey = 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Home' | 'End' | 'PageUp' | 'PageDown';
|
|
31
|
-
/**
|
|
32
|
-
* Editing and special keys.
|
|
33
|
-
*/
|
|
34
|
-
type EditingKey = 'Enter' | 'Escape' | 'Space' | 'Tab' | 'Backspace' | 'Delete';
|
|
35
|
-
/**
|
|
36
|
-
* Punctuation keys commonly used in keyboard shortcuts.
|
|
37
|
-
* These are the literal characters as they appear in KeyboardEvent.key
|
|
38
|
-
* (layout-dependent, typically US keyboard layout).
|
|
39
|
-
*/
|
|
40
|
-
type PunctuationKey = '/' | '[' | ']' | '\\' | '=' | '-' | ',' | '.' | ';' | '`';
|
|
41
|
-
/**
|
|
42
|
-
* Keys that don't change their value when Shift is pressed.
|
|
43
|
-
* These keys produce the same `KeyboardEvent.key` value whether Shift is held or not.
|
|
44
|
-
*
|
|
45
|
-
* Excludes NumberKey (Shift+1 produces '!' on US layout) and PunctuationKey
|
|
46
|
-
* (Shift+',' produces '<' on US layout).
|
|
47
|
-
*
|
|
48
|
-
* Used in hotkey type definitions to prevent layout-dependent issues when Shift
|
|
49
|
-
* is part of the modifier combination.
|
|
50
|
-
*/
|
|
51
|
-
type NonPunctuationKey = LetterKey | NumberKey | EditingKey | NavigationKey | FunctionKey;
|
|
52
|
-
/**
|
|
53
|
-
* All supported non-modifier keys.
|
|
54
|
-
*/
|
|
55
|
-
type Key = NonPunctuationKey | PunctuationKey;
|
|
56
|
-
/**
|
|
57
|
-
* Includes both modifier keys and regular keys.
|
|
58
|
-
*/
|
|
59
|
-
type IndividualKey = CanonicalModifier | Key;
|
|
1
|
+
import { CanonicalModifier, Key, LogicalKey } from "./key.types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hotkey.types.d.ts
|
|
60
4
|
/**
|
|
61
5
|
* Single modifier + key combinations.
|
|
62
6
|
* Uses canonical modifiers (4) + Mod (1) = 5 modifiers.
|
|
63
|
-
* Shift combinations exclude PunctuationKey to avoid layout-dependent issues.
|
|
64
7
|
*
|
|
65
8
|
* The `Mod` modifier is platform-adaptive:
|
|
66
9
|
* - **macOS**: Resolves to `Meta` (Command key ⌘)
|
|
@@ -69,10 +12,9 @@ type IndividualKey = CanonicalModifier | Key;
|
|
|
69
12
|
* This enables cross-platform hotkey definitions that automatically adapt to the platform.
|
|
70
13
|
* For example, `Mod+S` becomes `Command+S` on Mac and `Ctrl+S` on Windows/Linux.
|
|
71
14
|
*/
|
|
72
|
-
type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${
|
|
15
|
+
type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${Key}` | `Meta+${Key}` | `Mod+${Key}`;
|
|
73
16
|
/**
|
|
74
17
|
* Two modifier + key combinations.
|
|
75
|
-
* Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
|
|
76
18
|
*
|
|
77
19
|
* **Platform-adaptive `Mod` combinations:**
|
|
78
20
|
* - `Mod+Alt` and `Mod+Shift` are included (safe on all platforms)
|
|
@@ -80,10 +22,9 @@ type SingleModifierHotkey = `Control+${Key}` | `Alt+${Key}` | `Shift+${NonPunctu
|
|
|
80
22
|
* - `Mod+Control` duplicates `Control` on Windows/Linux (Mod = Control)
|
|
81
23
|
* - `Mod+Meta` duplicates `Meta` on macOS (Mod = Meta)
|
|
82
24
|
*/
|
|
83
|
-
type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${
|
|
25
|
+
type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${Key}` | `Control+Meta+${Key}` | `Alt+Shift+${Key}` | `Alt+Meta+${Key}` | `Shift+Meta+${Key}` | `Mod+Alt+${Key}` | `Mod+Shift+${Key}`;
|
|
84
26
|
/**
|
|
85
27
|
* Three modifier + key combinations.
|
|
86
|
-
* Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
|
|
87
28
|
*
|
|
88
29
|
* **Platform-adaptive `Mod` combinations:**
|
|
89
30
|
* - `Mod+Alt+Shift` is included (safe on all platforms)
|
|
@@ -91,10 +32,9 @@ type TwoModifierHotkey = `Control+Alt+${Key}` | `Control+Shift+${NonPunctuationK
|
|
|
91
32
|
* - `Mod+Control+Shift` duplicates `Control` on Windows/Linux (Mod = Control)
|
|
92
33
|
* - `Mod+Shift+Meta` duplicates `Meta` on macOS (Mod = Meta)
|
|
93
34
|
*/
|
|
94
|
-
type ThreeModifierHotkey = `Control+Alt+Shift+${
|
|
35
|
+
type ThreeModifierHotkey = `Control+Alt+Shift+${Key}` | `Control+Alt+Meta+${Key}` | `Control+Shift+Meta+${Key}` | `Alt+Shift+Meta+${Key}` | `Mod+Alt+Shift+${Key}`;
|
|
95
36
|
/**
|
|
96
37
|
* Four modifier + key combinations.
|
|
97
|
-
* Shift combinations exclude Numbers and PunctuationKeys to avoid layout-dependent issues.
|
|
98
38
|
*
|
|
99
39
|
* Only the canonical `Control+Alt+Shift+Meta` combination is included.
|
|
100
40
|
*
|
|
@@ -105,7 +45,7 @@ type ThreeModifierHotkey = `Control+Alt+Shift+${NonPunctuationKey}` | `Control+A
|
|
|
105
45
|
* - `Mod+Control+Alt+Shift` → duplicates `Control` on Windows/Linux
|
|
106
46
|
* - `Mod+Alt+Shift+Meta` → duplicates `Meta` on macOS
|
|
107
47
|
*/
|
|
108
|
-
type FourModifierHotkey = `Control+Alt+Shift+Meta+${
|
|
48
|
+
type FourModifierHotkey = `Control+Alt+Shift+Meta+${Key}`;
|
|
109
49
|
/**
|
|
110
50
|
* A type-safe hotkey string.
|
|
111
51
|
*
|
|
@@ -159,38 +99,35 @@ type FourModifierHotkey = `Control+Alt+Shift+Meta+${NonPunctuationKey}`;
|
|
|
159
99
|
* ```
|
|
160
100
|
*/
|
|
161
101
|
type Hotkey = Key | SingleModifierHotkey | TwoModifierHotkey | ThreeModifierHotkey | FourModifierHotkey;
|
|
162
|
-
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
* This interface provides a flexible fallback when the `Hotkey` type doesn't
|
|
166
|
-
* fit your use case. You can pass a `ParsedHotkey` directly to hotkey functions
|
|
167
|
-
* instead of a hotkey string, allowing for more dynamic or complex scenarios
|
|
168
|
-
* that aren't covered by the type-safe `Hotkey` union.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* // Type-safe hotkey string
|
|
173
|
-
* useHotkey('Mod+S', handler)
|
|
174
|
-
*
|
|
175
|
-
* // Fallback: parsed hotkey for dynamic scenarios
|
|
176
|
-
* const parsed = parseHotkey(userInput)
|
|
177
|
-
* useHotkey(parsed, handler) // Works even if userInput isn't in Hotkey type
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
interface ParsedHotkey {
|
|
181
|
-
/** The non-modifier key (e.g., 'S', 'Escape', 'F1', '/', '['). Can be any string for flexibility. */
|
|
182
|
-
key: Key | (string & {});
|
|
183
|
-
/** Whether the Control key is required */
|
|
102
|
+
/** Resolved modifier flags shared by both parsed binding variants. */
|
|
103
|
+
interface ParsedModifiers {
|
|
104
|
+
/** Whether Control is required. */
|
|
184
105
|
ctrl: boolean;
|
|
185
|
-
/** Whether
|
|
106
|
+
/** Whether Shift is required. */
|
|
186
107
|
shift: boolean;
|
|
187
|
-
/** Whether
|
|
108
|
+
/** Whether Alt is required. */
|
|
188
109
|
alt: boolean;
|
|
189
|
-
/** Whether
|
|
110
|
+
/** Whether Meta (Command) is required. */
|
|
190
111
|
meta: boolean;
|
|
191
|
-
/**
|
|
112
|
+
/** Required modifiers in canonical order. */
|
|
192
113
|
modifiers: Array<CanonicalModifier>;
|
|
193
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* A parsed binding matches either a logical key or a physical code, never both.
|
|
117
|
+
* Check `code !== undefined` to narrow to a physical binding. Its logical
|
|
118
|
+
* character is unknown without a keyboard event and is not stored in `key`.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* parseHotkey('Alt+S') // { key: 'S', alt: true, ... }
|
|
122
|
+
* parseHotkey('Alt+[KeyS]') // { code: 'KeyS', alt: true, ... } — no key property
|
|
123
|
+
*/
|
|
124
|
+
type ParsedHotkey = ParsedModifiers & ({
|
|
125
|
+
key: LogicalKey | (string & {});
|
|
126
|
+
code?: never;
|
|
127
|
+
} | {
|
|
128
|
+
code: string;
|
|
129
|
+
key?: never;
|
|
130
|
+
});
|
|
194
131
|
/**
|
|
195
132
|
* A raw hotkey object for programmatic registration.
|
|
196
133
|
*
|
|
@@ -209,11 +146,13 @@ interface ParsedHotkey {
|
|
|
209
146
|
* useHotkey({ key: 'Escape' }, handler) // Escape (no modifiers)
|
|
210
147
|
* useHotkey({ key: 'A', shift: true, meta: true }, handler) // Shift+Meta+A
|
|
211
148
|
* useHotkey({ key: 'S', mod: true, shift: true }, handler) // Mod+Shift+S
|
|
149
|
+
* useHotkey({ code: 'KeyW', mod: true }, handler) // Physical Mod+KeyW
|
|
150
|
+
* useHotkey({ code: 'NumpadAdd', mod: true }, handler) // Physical numpad plus
|
|
212
151
|
* ```
|
|
213
152
|
*/
|
|
214
|
-
|
|
153
|
+
/** Modifier flags shared by logical-key and physical-code registrations. */
|
|
154
|
+
interface RawModifiers {
|
|
215
155
|
/** The non-modifier key (e.g., 'S', 'Escape', 'F1'). */
|
|
216
|
-
key: Key | (string & {});
|
|
217
156
|
/** Platform-adaptive modifier: Command on macOS, Control on Windows/Linux. Defaults to false. */
|
|
218
157
|
mod?: boolean;
|
|
219
158
|
/** Whether the Control key is required. Defaults to false. */
|
|
@@ -225,33 +164,18 @@ interface RawHotkey {
|
|
|
225
164
|
/** Whether the Meta (Command) key is required. Defaults to false. */
|
|
226
165
|
meta?: boolean;
|
|
227
166
|
}
|
|
167
|
+
type RawHotkey = ({
|
|
168
|
+
key: LogicalKey | (string & {});
|
|
169
|
+
code?: never;
|
|
170
|
+
} & RawModifiers) | ({
|
|
171
|
+
code: string;
|
|
172
|
+
key?: never;
|
|
173
|
+
} & RawModifiers);
|
|
228
174
|
/**
|
|
229
175
|
* A hotkey that can be passed to `HotkeyManager.register()` and `useHotkey()`.
|
|
230
176
|
* Either a type-safe string (`Hotkey`) or a raw object (`RawHotkey`).
|
|
231
177
|
*/
|
|
232
178
|
type RegisterableHotkey = Hotkey | RawHotkey;
|
|
233
|
-
/**
|
|
234
|
-
* Options for formatting hotkeys for display.
|
|
235
|
-
*/
|
|
236
|
-
interface FormatDisplayOptions {
|
|
237
|
-
/** The target platform. Defaults to auto-detection. */
|
|
238
|
-
platform?: 'mac' | 'windows' | 'linux';
|
|
239
|
-
/** Whether to use symbols for the display. Defaults to true. */
|
|
240
|
-
useSymbols?: boolean;
|
|
241
|
-
/** Override the separator between display tokens. Defaults to platform-specific formatting when null/undefined. */
|
|
242
|
-
separatorToken?: string | null;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Result of validating a hotkey string.
|
|
246
|
-
*/
|
|
247
|
-
interface ValidationResult {
|
|
248
|
-
/** Whether the hotkey is valid (can still have warnings) */
|
|
249
|
-
valid: boolean;
|
|
250
|
-
/** Warning messages about potential issues */
|
|
251
|
-
warnings: Array<string>;
|
|
252
|
-
/** Error messages about invalid syntax */
|
|
253
|
-
errors: Array<string>;
|
|
254
|
-
}
|
|
255
179
|
/**
|
|
256
180
|
* Context passed to hotkey callbacks along with the keyboard event.
|
|
257
181
|
*/
|
|
@@ -278,7 +202,7 @@ interface HotkeyCallbackContext {
|
|
|
278
202
|
type HotkeyCallback = (event: KeyboardEvent, context: HotkeyCallbackContext) => void;
|
|
279
203
|
/**
|
|
280
204
|
* Metadata for hotkey and sequence registrations.
|
|
281
|
-
* Includes `name` and `
|
|
205
|
+
* Includes `name`, `description`, and `group` by default. Extend via declaration merging:
|
|
282
206
|
*
|
|
283
207
|
* @example
|
|
284
208
|
* ```ts
|
|
@@ -295,7 +219,18 @@ interface HotkeyMeta {
|
|
|
295
219
|
name?: string;
|
|
296
220
|
/** Description of what this hotkey does */
|
|
297
221
|
description?: string;
|
|
222
|
+
/** Optional display group. Does not affect matching or scope. */
|
|
223
|
+
group?: string;
|
|
298
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Behavior when registering a hotkey/sequence that conflicts with an existing registration.
|
|
227
|
+
*
|
|
228
|
+
* - `'warn'` - Log a warning to the console but allow both registrations (default)
|
|
229
|
+
* - `'error'` - Throw an error and prevent the new registration
|
|
230
|
+
* - `'replace'` - Unregister the existing registration and register the new one
|
|
231
|
+
* - `'allow'` - Allow multiple registrations without warning
|
|
232
|
+
*/
|
|
233
|
+
type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
|
|
299
234
|
//#endregion
|
|
300
|
-
export {
|
|
301
|
-
//# sourceMappingURL=hotkey.d.
|
|
235
|
+
export { ConflictBehavior, FourModifierHotkey, Hotkey, HotkeyCallback, HotkeyCallbackContext, HotkeyMeta, ParsedHotkey, ParsedModifiers, RawHotkey, RawModifiers, RegisterableHotkey, SingleModifierHotkey, ThreeModifierHotkey, TwoModifierHotkey };
|
|
236
|
+
//# sourceMappingURL=hotkey.types.d.ts.map
|