@tanstack/preact-hotkeys 0.11.0 → 0.12.1
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/dist/HotkeysProvider.d.ts +6 -13
- package/dist/HotkeysProvider.js +1 -2
- package/dist/useHeldKeyCodes.d.ts +2 -4
- package/dist/useHeldKeyCodes.js +3 -3
- package/dist/useHeldKeys.d.ts +2 -4
- package/dist/useHeldKeys.js +3 -3
- package/dist/useHotkey.d.ts +3 -6
- package/dist/useHotkey.js +3 -3
- package/dist/useHotkeyHint.d.ts +2 -5
- package/dist/useHotkeyHint.js +1 -2
- package/dist/useHotkeyRecorder.d.ts +3 -6
- package/dist/useHotkeyRecorder.js +1 -2
- package/dist/useHotkeyRegistrations.d.ts +3 -6
- package/dist/useHotkeyRegistrations.js +1 -2
- package/dist/useHotkeySequence.d.ts +3 -6
- package/dist/useHotkeySequence.js +1 -2
- package/dist/useHotkeySequenceRecorder.d.ts +3 -6
- package/dist/useHotkeySequenceRecorder.js +1 -2
- package/dist/useHotkeySequences.d.ts +3 -6
- package/dist/useHotkeySequences.js +1 -2
- package/dist/useHotkeys.d.ts +3 -6
- package/dist/useHotkeys.js +1 -2
- package/dist/useKeyHold.d.ts +2 -5
- package/dist/useKeyHold.js +1 -2
- package/dist/utils.js +1 -2
- package/package.json +11 -16
- package/dist/HotkeysProvider.cjs +0 -26
- package/dist/HotkeysProvider.cjs.map +0 -1
- package/dist/HotkeysProvider.d.cts +0 -29
- package/dist/HotkeysProvider.js.map +0 -1
- package/dist/index.cjs +0 -35
- package/dist/index.d.cts +0 -14
- package/dist/useHeldKeyCodes.cjs +0 -37
- package/dist/useHeldKeyCodes.cjs.map +0 -1
- package/dist/useHeldKeyCodes.d.cts +0 -31
- package/dist/useHeldKeyCodes.js.map +0 -1
- package/dist/useHeldKeys.cjs +0 -33
- package/dist/useHeldKeys.cjs.map +0 -1
- package/dist/useHeldKeys.d.cts +0 -27
- package/dist/useHeldKeys.js.map +0 -1
- package/dist/useHotkey.cjs +0 -125
- package/dist/useHotkey.cjs.map +0 -1
- package/dist/useHotkey.d.cts +0 -75
- package/dist/useHotkey.js.map +0 -1
- package/dist/useHotkeyHint.cjs +0 -18
- package/dist/useHotkeyHint.cjs.map +0 -1
- package/dist/useHotkeyHint.d.cts +0 -14
- package/dist/useHotkeyHint.js.map +0 -1
- package/dist/useHotkeyRecorder.cjs +0 -71
- package/dist/useHotkeyRecorder.cjs.map +0 -1
- package/dist/useHotkeyRecorder.d.cts +0 -57
- package/dist/useHotkeyRecorder.js.map +0 -1
- package/dist/useHotkeyRegistrations.cjs +0 -29
- package/dist/useHotkeyRegistrations.cjs.map +0 -1
- package/dist/useHotkeyRegistrations.d.cts +0 -26
- package/dist/useHotkeyRegistrations.js.map +0 -1
- package/dist/useHotkeySequence.cjs +0 -120
- package/dist/useHotkeySequence.cjs.map +0 -1
- package/dist/useHotkeySequence.d.cts +0 -59
- package/dist/useHotkeySequence.js.map +0 -1
- package/dist/useHotkeySequenceRecorder.cjs +0 -39
- package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
- package/dist/useHotkeySequenceRecorder.d.cts +0 -19
- package/dist/useHotkeySequenceRecorder.js.map +0 -1
- package/dist/useHotkeySequences.cjs +0 -126
- package/dist/useHotkeySequences.cjs.map +0 -1
- package/dist/useHotkeySequences.d.cts +0 -63
- package/dist/useHotkeySequences.js.map +0 -1
- package/dist/useHotkeys.cjs +0 -124
- package/dist/useHotkeys.cjs.map +0 -1
- package/dist/useHotkeys.d.cts +0 -63
- package/dist/useHotkeys.js.map +0 -1
- package/dist/useKeyHold.cjs +0 -53
- package/dist/useKeyHold.cjs.map +0 -1
- package/dist/useKeyHold.d.cts +0 -47
- package/dist/useKeyHold.js.map +0 -1
- package/dist/utils.cjs +0 -12
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/HotkeysProvider.tsx +0 -56
- package/src/index.ts +0 -18
- package/src/useHeldKeyCodes.ts +0 -33
- package/src/useHeldKeys.ts +0 -29
- package/src/useHotkey.ts +0 -191
- package/src/useHotkeyHint.ts +0 -19
- package/src/useHotkeyRecorder.ts +0 -101
- package/src/useHotkeyRegistrations.ts +0 -51
- package/src/useHotkeySequence.ts +0 -192
- package/src/useHotkeySequenceRecorder.ts +0 -64
- package/src/useHotkeySequences.ts +0 -192
- package/src/useHotkeys.ts +0 -193
- package/src/useKeyHold.ts +0 -52
- package/src/utils.ts +0 -8
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
2
|
-
const require_utils = require('./utils.cjs');
|
|
3
|
-
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
4
|
-
let preact_hooks = require("preact/hooks");
|
|
5
|
-
|
|
6
|
-
//#region src/useHotkeySequence.ts
|
|
7
|
-
/**
|
|
8
|
-
* Preact hook for registering a keyboard shortcut sequence (Vim-style).
|
|
9
|
-
*
|
|
10
|
-
* This hook allows you to register multi-key sequences like 'g g' or 'd d'
|
|
11
|
-
* that trigger when the full sequence is pressed within a timeout.
|
|
12
|
-
*
|
|
13
|
-
* Each step may include modifiers. You can chain the same modifier across
|
|
14
|
-
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
15
|
-
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
16
|
-
* advance the sequence or reset progress.
|
|
17
|
-
*
|
|
18
|
-
* @param sequence - Array of hotkey strings that form the sequence
|
|
19
|
-
* @param callback - Function to call when the sequence is completed
|
|
20
|
-
* @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
|
|
21
|
-
* and only suppresses firing; the hook updates the existing handle instead of unregistering.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```tsx
|
|
25
|
-
* function VimEditor() {
|
|
26
|
-
* // 'g g' to go to top
|
|
27
|
-
* useHotkeySequence(['G', 'G'], () => {
|
|
28
|
-
* scrollToTop()
|
|
29
|
-
* })
|
|
30
|
-
*
|
|
31
|
-
* // 'd d' to delete line
|
|
32
|
-
* useHotkeySequence(['D', 'D'], () => {
|
|
33
|
-
* deleteLine()
|
|
34
|
-
* })
|
|
35
|
-
*
|
|
36
|
-
* // 'd i w' to delete inner word
|
|
37
|
-
* useHotkeySequence(['D', 'I', 'W'], () => {
|
|
38
|
-
* deleteInnerWord()
|
|
39
|
-
* }, { timeout: 500 })
|
|
40
|
-
*
|
|
41
|
-
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
42
|
-
* useHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
43
|
-
* nextAction()
|
|
44
|
-
* })
|
|
45
|
-
*
|
|
46
|
-
* return <div>...</div>
|
|
47
|
-
* }
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
function useHotkeySequence(sequence, callback, options = {}) {
|
|
51
|
-
const mergedOptions = {
|
|
52
|
-
...require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequence,
|
|
53
|
-
...options
|
|
54
|
-
};
|
|
55
|
-
const manager = (0, _tanstack_hotkeys.getSequenceManager)();
|
|
56
|
-
const registrationRef = (0, preact_hooks.useRef)(null);
|
|
57
|
-
const callbackRef = (0, preact_hooks.useRef)(callback);
|
|
58
|
-
const optionsRef = (0, preact_hooks.useRef)(mergedOptions);
|
|
59
|
-
const managerRef = (0, preact_hooks.useRef)(manager);
|
|
60
|
-
const sequenceRef = (0, preact_hooks.useRef)(sequence);
|
|
61
|
-
callbackRef.current = callback;
|
|
62
|
-
optionsRef.current = mergedOptions;
|
|
63
|
-
managerRef.current = manager;
|
|
64
|
-
sequenceRef.current = sequence;
|
|
65
|
-
const prevTargetRef = (0, preact_hooks.useRef)(null);
|
|
66
|
-
const prevSequenceRef = (0, preact_hooks.useRef)(null);
|
|
67
|
-
const hotkeySequenceString = (0, _tanstack_hotkeys.formatHotkeySequence)(sequence);
|
|
68
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions;
|
|
69
|
-
(0, preact_hooks.useEffect)(() => {
|
|
70
|
-
if (sequenceRef.current.length === 0) {
|
|
71
|
-
if (registrationRef.current?.isActive) {
|
|
72
|
-
registrationRef.current.unregister();
|
|
73
|
-
registrationRef.current = null;
|
|
74
|
-
}
|
|
75
|
-
prevTargetRef.current = null;
|
|
76
|
-
prevSequenceRef.current = null;
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const resolvedTarget = require_utils.isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
|
|
80
|
-
if (!resolvedTarget) {
|
|
81
|
-
if (registrationRef.current?.isActive) {
|
|
82
|
-
registrationRef.current.unregister();
|
|
83
|
-
registrationRef.current = null;
|
|
84
|
-
}
|
|
85
|
-
prevTargetRef.current = null;
|
|
86
|
-
prevSequenceRef.current = null;
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
|
|
90
|
-
const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
|
|
91
|
-
if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
|
|
92
|
-
registrationRef.current.unregister();
|
|
93
|
-
registrationRef.current = null;
|
|
94
|
-
}
|
|
95
|
-
if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequenceRef.current, (event, context) => callbackRef.current(event, context), {
|
|
96
|
-
...optionsRef.current,
|
|
97
|
-
target: resolvedTarget
|
|
98
|
-
});
|
|
99
|
-
prevTargetRef.current = resolvedTarget;
|
|
100
|
-
prevSequenceRef.current = hotkeySequenceString;
|
|
101
|
-
});
|
|
102
|
-
(0, preact_hooks.useEffect)(() => {
|
|
103
|
-
return () => {
|
|
104
|
-
if (registrationRef.current?.isActive) {
|
|
105
|
-
registrationRef.current.unregister();
|
|
106
|
-
registrationRef.current = null;
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
}, []);
|
|
110
|
-
(0, preact_hooks.useEffect)(() => {
|
|
111
|
-
if (registrationRef.current?.isActive) {
|
|
112
|
-
registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
|
|
113
|
-
registrationRef.current.setOptions(optionsWithoutTarget);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
//#endregion
|
|
119
|
-
exports.useHotkeySequence = useHotkeySequence;
|
|
120
|
-
//# sourceMappingURL=useHotkeySequence.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)\n * and only suppresses firing; the hook updates the existing handle instead of unregistering.\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * useHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n const sequenceRef = useRef(sequence)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n sequenceRef.current = sequence\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequenceRef.current.length === 0) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequenceRef.current,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n })\n\n useEffect(() => {\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [])\n\n // Publish option changes after commit, never during render.\n useEffect(() => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,qDAA8B;CAGpC,MAAM,2CAA4D,KAAK;CAGvE,MAAM,uCAAqB,SAAS;CACpC,MAAM,sCAAoB,cAAc;CACxC,MAAM,sCAAoB,QAAQ;CAClC,MAAM,uCAAqB,SAAS;AAGpC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;AACrB,aAAY,UAAU;CAGtB,MAAM,yCAA+D,KAAK;CAC1E,MAAM,2CAAwC,KAAK;CAGnD,MAAM,mEAA4C,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,mCAAgB;AACd,MAAI,YAAY,QAAQ,WAAW,GAAG;AACpC,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;AAE5B,iBAAc,UAAU;AACxB,mBAAgB,UAAU;AAC1B;;EAIF,MAAM,iBAAiBC,oBAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,gBAAgB;AACnB,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;AAE5B,iBAAc,UAAU;AACxB,mBAAgB,UAAU;AAC1B;;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,kBACJ,gBAAgB,YAAY,QAC5B,gBAAgB,YAAY;AAG9B,MACE,gBAAgB,SAAS,aACxB,iBAAiB,kBAClB;AACA,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAI5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,YAAY,UACX,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;GAC1B;AAEF,mCAAgB;AAEd,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,EAAE,CAAC;AAGN,mCAAgB;AACd,MAAI,gBAAgB,SAAS,UAAU;AACrC,mBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,mBAAgB,QAAQ,WAAW,qBAAqB;;GAE1D"}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
|
|
2
|
-
import { RefObject } from "preact";
|
|
3
|
-
|
|
4
|
-
//#region src/useHotkeySequence.d.ts
|
|
5
|
-
interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
6
|
-
/**
|
|
7
|
-
* The DOM element to attach the event listener to.
|
|
8
|
-
* Can be a Preact ref, direct DOM element, or null.
|
|
9
|
-
* Defaults to document.
|
|
10
|
-
*/
|
|
11
|
-
target?: RefObject<HTMLElement | null> | HTMLElement | Document | Window | null;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Preact hook for registering a keyboard shortcut sequence (Vim-style).
|
|
15
|
-
*
|
|
16
|
-
* This hook allows you to register multi-key sequences like 'g g' or 'd d'
|
|
17
|
-
* that trigger when the full sequence is pressed within a timeout.
|
|
18
|
-
*
|
|
19
|
-
* Each step may include modifiers. You can chain the same modifier across
|
|
20
|
-
* steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
|
|
21
|
-
* Control, Alt, or Meta pressed alone) are ignored while matching—they do not
|
|
22
|
-
* advance the sequence or reset progress.
|
|
23
|
-
*
|
|
24
|
-
* @param sequence - Array of hotkey strings that form the sequence
|
|
25
|
-
* @param callback - Function to call when the sequence is completed
|
|
26
|
-
* @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
|
|
27
|
-
* and only suppresses firing; the hook updates the existing handle instead of unregistering.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```tsx
|
|
31
|
-
* function VimEditor() {
|
|
32
|
-
* // 'g g' to go to top
|
|
33
|
-
* useHotkeySequence(['G', 'G'], () => {
|
|
34
|
-
* scrollToTop()
|
|
35
|
-
* })
|
|
36
|
-
*
|
|
37
|
-
* // 'd d' to delete line
|
|
38
|
-
* useHotkeySequence(['D', 'D'], () => {
|
|
39
|
-
* deleteLine()
|
|
40
|
-
* })
|
|
41
|
-
*
|
|
42
|
-
* // 'd i w' to delete inner word
|
|
43
|
-
* useHotkeySequence(['D', 'I', 'W'], () => {
|
|
44
|
-
* deleteInnerWord()
|
|
45
|
-
* }, { timeout: 500 })
|
|
46
|
-
*
|
|
47
|
-
* // Same modifier on consecutive steps (bare Shift between chords is ignored)
|
|
48
|
-
* useHotkeySequence(['Shift+R', 'Shift+T'], () => {
|
|
49
|
-
* nextAction()
|
|
50
|
-
* })
|
|
51
|
-
*
|
|
52
|
-
* return <div>...</div>
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
declare function useHotkeySequence(sequence: HotkeySequence, callback: HotkeyCallback, options?: UseHotkeySequenceOptions): void;
|
|
57
|
-
//#endregion
|
|
58
|
-
export { UseHotkeySequenceOptions, useHotkeySequence };
|
|
59
|
-
//# sourceMappingURL=useHotkeySequence.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)\n * and only suppresses firing; the hook updates the existing handle instead of unregistering.\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * useHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n const sequenceRef = useRef(sequence)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n sequenceRef.current = sequence\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequenceRef.current.length === 0) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequenceRef.current,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n })\n\n useEffect(() => {\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [])\n\n // Publish option changes after commit, never during render.\n useEffect(() => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,UAAU,oBAAoB;CAGpC,MAAM,kBAAkB,OAA0C,KAAK;CAGvE,MAAM,cAAc,OAAO,SAAS;CACpC,MAAM,aAAa,OAAO,cAAc;CACxC,MAAM,aAAa,OAAO,QAAQ;CAClC,MAAM,cAAc,OAAO,SAAS;AAGpC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;AACrB,aAAY,UAAU;CAGtB,MAAM,gBAAgB,OAA+C,KAAK;CAC1E,MAAM,kBAAkB,OAAsB,KAAK;CAGnD,MAAM,uBAAuB,qBAAqB,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,iBAAgB;AACd,MAAI,YAAY,QAAQ,WAAW,GAAG;AACpC,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;AAE5B,iBAAc,UAAU;AACxB,mBAAgB,UAAU;AAC1B;;EAIF,MAAM,iBAAiB,MAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,gBAAgB;AACnB,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;AAE5B,iBAAc,UAAU;AACxB,mBAAgB,UAAU;AAC1B;;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,kBACJ,gBAAgB,YAAY,QAC5B,gBAAgB,YAAY;AAG9B,MACE,gBAAgB,SAAS,aACxB,iBAAiB,kBAClB;AACA,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAI5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,YAAY,UACX,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;GAC1B;AAEF,iBAAgB;AAEd,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,EAAE,CAAC;AAGN,iBAAgB;AACd,MAAI,gBAAgB,SAAS,UAAU;AACrC,mBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,mBAAgB,QAAQ,WAAW,qBAAqB;;GAE1D"}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
2
|
-
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
3
|
-
let preact_hooks = require("preact/hooks");
|
|
4
|
-
let _tanstack_preact_store = require("@tanstack/preact-store");
|
|
5
|
-
|
|
6
|
-
//#region src/useHotkeySequenceRecorder.ts
|
|
7
|
-
/**
|
|
8
|
-
* Preact hook for recording multi-chord sequences (Vim-style shortcuts).
|
|
9
|
-
*/
|
|
10
|
-
function useHotkeySequenceRecorder(options) {
|
|
11
|
-
const mergedOptions = {
|
|
12
|
-
...require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequenceRecorder,
|
|
13
|
-
...options
|
|
14
|
-
};
|
|
15
|
-
const recorderRef = (0, preact_hooks.useRef)(null);
|
|
16
|
-
if (!recorderRef.current) recorderRef.current = new _tanstack_hotkeys.HotkeySequenceRecorder(mergedOptions);
|
|
17
|
-
recorderRef.current.setOptions(mergedOptions);
|
|
18
|
-
const isRecording = (0, _tanstack_preact_store.useSelector)(recorderRef.current.store, (state) => state.isRecording);
|
|
19
|
-
const steps = (0, _tanstack_preact_store.useSelector)(recorderRef.current.store, (state) => state.steps);
|
|
20
|
-
const recordedSequence = (0, _tanstack_preact_store.useSelector)(recorderRef.current.store, (state) => state.recordedSequence);
|
|
21
|
-
(0, preact_hooks.useEffect)(() => {
|
|
22
|
-
return () => {
|
|
23
|
-
recorderRef.current?.destroy();
|
|
24
|
-
};
|
|
25
|
-
}, []);
|
|
26
|
-
return {
|
|
27
|
-
isRecording,
|
|
28
|
-
steps,
|
|
29
|
-
recordedSequence,
|
|
30
|
-
startRecording: () => recorderRef.current?.start(),
|
|
31
|
-
stopRecording: () => recorderRef.current?.stop(),
|
|
32
|
-
cancelRecording: () => recorderRef.current?.cancel(),
|
|
33
|
-
commitRecording: () => recorderRef.current?.commit()
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
//#endregion
|
|
38
|
-
exports.useHotkeySequenceRecorder = useHotkeySequenceRecorder;
|
|
39
|
-
//# sourceMappingURL=useHotkeySequenceRecorder.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeySequenceRecorder.cjs","names":["useDefaultHotkeysOptions","HotkeySequenceRecorder"],"sources":["../src/useHotkeySequenceRecorder.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { useSelector } from '@tanstack/preact-store'\nimport { HotkeySequenceRecorder } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeySequence,\n HotkeySequenceRecorderOptions,\n} from '@tanstack/hotkeys'\n\nexport interface PreactHotkeySequenceRecorder {\n isRecording: boolean\n steps: HotkeySequence\n recordedSequence: HotkeySequence | null\n startRecording: () => void\n stopRecording: () => void\n cancelRecording: () => void\n commitRecording: () => void\n}\n\n/**\n * Preact hook for recording multi-chord sequences (Vim-style shortcuts).\n */\nexport function useHotkeySequenceRecorder(\n options: HotkeySequenceRecorderOptions,\n): PreactHotkeySequenceRecorder {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequenceRecorder,\n ...options,\n } as HotkeySequenceRecorderOptions\n\n const recorderRef = useRef<HotkeySequenceRecorder | null>(null)\n\n if (!recorderRef.current) {\n recorderRef.current = new HotkeySequenceRecorder(mergedOptions)\n }\n\n recorderRef.current.setOptions(mergedOptions)\n\n const isRecording = useSelector(\n recorderRef.current.store,\n (state) => state.isRecording,\n )\n const steps = useSelector(recorderRef.current.store, (state) => state.steps)\n const recordedSequence = useSelector(\n recorderRef.current.store,\n (state) => state.recordedSequence,\n )\n\n useEffect(() => {\n return () => {\n recorderRef.current?.destroy()\n }\n }, [])\n\n return {\n isRecording,\n steps,\n recordedSequence,\n startRecording: () => recorderRef.current?.start(),\n stopRecording: () => recorderRef.current?.stop(),\n cancelRecording: () => recorderRef.current?.cancel(),\n commitRecording: () => recorderRef.current?.commit(),\n }\n}\n"],"mappings":";;;;;;;;;AAsBA,SAAgB,0BACd,SAC8B;CAC9B,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,uCAAoD,KAAK;AAE/D,KAAI,CAAC,YAAY,QACf,aAAY,UAAU,IAAIC,yCAAuB,cAAc;AAGjE,aAAY,QAAQ,WAAW,cAAc;CAE7C,MAAM,sDACJ,YAAY,QAAQ,QACnB,UAAU,MAAM,YAClB;CACD,MAAM,gDAAoB,YAAY,QAAQ,QAAQ,UAAU,MAAM,MAAM;CAC5E,MAAM,2DACJ,YAAY,QAAQ,QACnB,UAAU,MAAM,iBAClB;AAED,mCAAgB;AACd,eAAa;AACX,eAAY,SAAS,SAAS;;IAE/B,EAAE,CAAC;AAEN,QAAO;EACL;EACA;EACA;EACA,sBAAsB,YAAY,SAAS,OAAO;EAClD,qBAAqB,YAAY,SAAS,MAAM;EAChD,uBAAuB,YAAY,SAAS,QAAQ;EACpD,uBAAuB,YAAY,SAAS,QAAQ;EACrD"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { HotkeySequence, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
|
|
2
|
-
|
|
3
|
-
//#region src/useHotkeySequenceRecorder.d.ts
|
|
4
|
-
interface PreactHotkeySequenceRecorder {
|
|
5
|
-
isRecording: boolean;
|
|
6
|
-
steps: HotkeySequence;
|
|
7
|
-
recordedSequence: HotkeySequence | null;
|
|
8
|
-
startRecording: () => void;
|
|
9
|
-
stopRecording: () => void;
|
|
10
|
-
cancelRecording: () => void;
|
|
11
|
-
commitRecording: () => void;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Preact hook for recording multi-chord sequences (Vim-style shortcuts).
|
|
15
|
-
*/
|
|
16
|
-
declare function useHotkeySequenceRecorder(options: HotkeySequenceRecorderOptions): PreactHotkeySequenceRecorder;
|
|
17
|
-
//#endregion
|
|
18
|
-
export { PreactHotkeySequenceRecorder, useHotkeySequenceRecorder };
|
|
19
|
-
//# sourceMappingURL=useHotkeySequenceRecorder.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeySequenceRecorder.js","names":[],"sources":["../src/useHotkeySequenceRecorder.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { useSelector } from '@tanstack/preact-store'\nimport { HotkeySequenceRecorder } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeySequence,\n HotkeySequenceRecorderOptions,\n} from '@tanstack/hotkeys'\n\nexport interface PreactHotkeySequenceRecorder {\n isRecording: boolean\n steps: HotkeySequence\n recordedSequence: HotkeySequence | null\n startRecording: () => void\n stopRecording: () => void\n cancelRecording: () => void\n commitRecording: () => void\n}\n\n/**\n * Preact hook for recording multi-chord sequences (Vim-style shortcuts).\n */\nexport function useHotkeySequenceRecorder(\n options: HotkeySequenceRecorderOptions,\n): PreactHotkeySequenceRecorder {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequenceRecorder,\n ...options,\n } as HotkeySequenceRecorderOptions\n\n const recorderRef = useRef<HotkeySequenceRecorder | null>(null)\n\n if (!recorderRef.current) {\n recorderRef.current = new HotkeySequenceRecorder(mergedOptions)\n }\n\n recorderRef.current.setOptions(mergedOptions)\n\n const isRecording = useSelector(\n recorderRef.current.store,\n (state) => state.isRecording,\n )\n const steps = useSelector(recorderRef.current.store, (state) => state.steps)\n const recordedSequence = useSelector(\n recorderRef.current.store,\n (state) => state.recordedSequence,\n )\n\n useEffect(() => {\n return () => {\n recorderRef.current?.destroy()\n }\n }, [])\n\n return {\n isRecording,\n steps,\n recordedSequence,\n startRecording: () => recorderRef.current?.start(),\n stopRecording: () => recorderRef.current?.stop(),\n cancelRecording: () => recorderRef.current?.cancel(),\n commitRecording: () => recorderRef.current?.commit(),\n }\n}\n"],"mappings":";;;;;;;;;AAsBA,SAAgB,0BACd,SAC8B;CAC9B,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,cAAc,OAAsC,KAAK;AAE/D,KAAI,CAAC,YAAY,QACf,aAAY,UAAU,IAAI,uBAAuB,cAAc;AAGjE,aAAY,QAAQ,WAAW,cAAc;CAE7C,MAAM,cAAc,YAClB,YAAY,QAAQ,QACnB,UAAU,MAAM,YAClB;CACD,MAAM,QAAQ,YAAY,YAAY,QAAQ,QAAQ,UAAU,MAAM,MAAM;CAC5E,MAAM,mBAAmB,YACvB,YAAY,QAAQ,QACnB,UAAU,MAAM,iBAClB;AAED,iBAAgB;AACd,eAAa;AACX,eAAY,SAAS,SAAS;;IAE/B,EAAE,CAAC;AAEN,QAAO;EACL;EACA;EACA;EACA,sBAAsB,YAAY,SAAS,OAAO;EAClD,qBAAqB,YAAY,SAAS,MAAM;EAChD,uBAAuB,YAAY,SAAS,QAAQ;EACpD,uBAAuB,YAAY,SAAS,QAAQ;EACrD"}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
2
|
-
const require_utils = require('./utils.cjs');
|
|
3
|
-
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
4
|
-
let preact_hooks = require("preact/hooks");
|
|
5
|
-
|
|
6
|
-
//#region src/useHotkeySequences.ts
|
|
7
|
-
/**
|
|
8
|
-
* Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
9
|
-
*
|
|
10
|
-
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
|
|
11
|
-
* register variable-length lists without violating the rules of hooks.
|
|
12
|
-
*
|
|
13
|
-
* Options are merged in this order:
|
|
14
|
-
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
15
|
-
*
|
|
16
|
-
* Callbacks and options are synced on every render to avoid stale closures.
|
|
17
|
-
*
|
|
18
|
-
* Definitions with an empty `sequence` are skipped (no registration).
|
|
19
|
-
*
|
|
20
|
-
* @param definitions - Array of sequence definitions to register
|
|
21
|
-
* @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
|
|
22
|
-
* Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
|
|
23
|
-
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
24
|
-
* via `setOptions` (no unregister/re-register churn).
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```tsx
|
|
28
|
-
* function VimPalette() {
|
|
29
|
-
* useHotkeySequences([
|
|
30
|
-
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
31
|
-
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
32
|
-
* { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
|
|
33
|
-
* ])
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```tsx
|
|
39
|
-
* function DynamicSequences({ items }) {
|
|
40
|
-
* useHotkeySequences(
|
|
41
|
-
* items.map((item) => ({
|
|
42
|
-
* sequence: item.chords,
|
|
43
|
-
* callback: item.action,
|
|
44
|
-
* options: { enabled: item.enabled },
|
|
45
|
-
* })),
|
|
46
|
-
* { preventDefault: true },
|
|
47
|
-
* )
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
function useHotkeySequences(definitions, commonOptions = {}) {
|
|
52
|
-
const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequence;
|
|
53
|
-
const manager = (0, _tanstack_hotkeys.getSequenceManager)();
|
|
54
|
-
const registrationsRef = (0, preact_hooks.useRef)(/* @__PURE__ */ new Map());
|
|
55
|
-
const definitionsRef = (0, preact_hooks.useRef)(definitions);
|
|
56
|
-
const sequenceStringsRef = (0, preact_hooks.useRef)([]);
|
|
57
|
-
const commonOptionsRef = (0, preact_hooks.useRef)(commonOptions);
|
|
58
|
-
const defaultOptionsRef = (0, preact_hooks.useRef)(defaultOptions);
|
|
59
|
-
const managerRef = (0, preact_hooks.useRef)(manager);
|
|
60
|
-
const sequenceStrings = definitions.map((def) => (0, _tanstack_hotkeys.formatHotkeySequence)(def.sequence));
|
|
61
|
-
definitionsRef.current = definitions;
|
|
62
|
-
sequenceStringsRef.current = sequenceStrings;
|
|
63
|
-
commonOptionsRef.current = commonOptions;
|
|
64
|
-
defaultOptionsRef.current = defaultOptions;
|
|
65
|
-
managerRef.current = manager;
|
|
66
|
-
(0, preact_hooks.useEffect)(() => {
|
|
67
|
-
const prevRegistrations = registrationsRef.current;
|
|
68
|
-
const nextRegistrations = /* @__PURE__ */ new Map();
|
|
69
|
-
const rows = [];
|
|
70
|
-
for (let i = 0; i < definitionsRef.current.length; i++) {
|
|
71
|
-
const def = definitionsRef.current[i];
|
|
72
|
-
const seqStr = sequenceStringsRef.current[i];
|
|
73
|
-
const seq = def.sequence;
|
|
74
|
-
if (seq.length === 0) continue;
|
|
75
|
-
const mergedOptions = {
|
|
76
|
-
...defaultOptionsRef.current,
|
|
77
|
-
...commonOptionsRef.current,
|
|
78
|
-
...def.options
|
|
79
|
-
};
|
|
80
|
-
const resolvedTarget = require_utils.isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
|
|
81
|
-
if (!resolvedTarget) continue;
|
|
82
|
-
const registrationKey = `${i}:${seqStr}`;
|
|
83
|
-
rows.push({
|
|
84
|
-
registrationKey,
|
|
85
|
-
def,
|
|
86
|
-
seq,
|
|
87
|
-
seqStr,
|
|
88
|
-
mergedOptions,
|
|
89
|
-
resolvedTarget
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
const nextKeys = new Set(rows.map((r) => r.registrationKey));
|
|
93
|
-
for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
|
|
94
|
-
for (const row of rows) {
|
|
95
|
-
const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row;
|
|
96
|
-
const existing = prevRegistrations.get(registrationKey);
|
|
97
|
-
if (existing?.handle.isActive && existing.target === resolvedTarget) {
|
|
98
|
-
existing.handle.callback = def.callback;
|
|
99
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions;
|
|
100
|
-
existing.handle.setOptions(optionsWithoutTarget);
|
|
101
|
-
nextRegistrations.set(registrationKey, existing);
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
if (existing?.handle.isActive) existing.handle.unregister();
|
|
105
|
-
const handle = managerRef.current.register(seq, def.callback, {
|
|
106
|
-
...mergedOptions,
|
|
107
|
-
target: resolvedTarget
|
|
108
|
-
});
|
|
109
|
-
nextRegistrations.set(registrationKey, {
|
|
110
|
-
handle,
|
|
111
|
-
target: resolvedTarget
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
registrationsRef.current = nextRegistrations;
|
|
115
|
-
});
|
|
116
|
-
(0, preact_hooks.useEffect)(() => {
|
|
117
|
-
return () => {
|
|
118
|
-
for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
|
|
119
|
-
registrationsRef.current = /* @__PURE__ */ new Map();
|
|
120
|
-
};
|
|
121
|
-
}, []);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
//#endregion
|
|
125
|
-
exports.useHotkeySequences = useHotkeySequences;
|
|
126
|
-
//# sourceMappingURL=useHotkeySequences.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeySequences.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeySequences.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeySequenceOptions } from './useHotkeySequence'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\n/**\n * A single sequence definition for use with `useHotkeySequences`.\n */\nexport interface UseHotkeySequenceDefinition {\n /** Array of hotkey strings that form the sequence */\n sequence: HotkeySequence\n /** The function to call when the sequence is completed */\n callback: HotkeyCallback\n /** Per-sequence options (merged on top of commonOptions) */\n options?: UseHotkeySequenceOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).\n *\n * Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can\n * register variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * Definitions with an empty `sequence` are skipped (no registration).\n *\n * @param definitions - Array of sequence definitions to register\n * @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).\n * Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function VimPalette() {\n * useHotkeySequences([\n * { sequence: ['G', 'G'], callback: () => scrollToTop() },\n * { sequence: ['D', 'D'], callback: () => deleteLine() },\n * { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function DynamicSequences({ items }) {\n * useHotkeySequences(\n * items.map((item) => ({\n * sequence: item.chords,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeySequences(\n definitions: Array<UseHotkeySequenceDefinition>,\n commonOptions: UseHotkeySequenceOptions = {},\n): void {\n type RegistrationRecord = {\n handle: SequenceRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkeySequence\n const manager = getSequenceManager()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const definitionsRef = useRef(definitions)\n const sequenceStringsRef = useRef<Array<string>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const sequenceStrings = definitions.map((def) =>\n formatHotkeySequence(def.sequence),\n )\n\n definitionsRef.current = definitions\n sequenceStringsRef.current = sequenceStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof definitionsRef.current)[number]\n seq: HotkeySequence\n seqStr: string\n mergedOptions: UseHotkeySequenceOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < definitionsRef.current.length; i++) {\n const def = definitionsRef.current[i]!\n const seqStr = sequenceStringsRef.current[i]!\n const seq = def.sequence\n if (seq.length === 0) {\n continue\n }\n\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeySequenceOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${seqStr}`\n rows.push({\n registrationKey,\n def,\n seq,\n seqStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n // Publish callback/options only after commit, never during render.\n existing.handle.callback = def.callback\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(seq, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,SAAgB,mBACd,aACA,gBAA0C,EAAE,EACtC;CAMN,MAAM,iBAAiBA,kDAA0B,CAAC;CAClD,MAAM,qDAA8B;CAEpC,MAAM,4DAA2D,IAAI,KAAK,CAAC;CAC3E,MAAM,0CAAwB,YAAY;CAC1C,MAAM,8CAA2C,EAAE,CAAC;CACpD,MAAM,4CAA0B,cAAc;CAC9C,MAAM,6CAA2B,eAAe;CAChD,MAAM,sCAAoB,QAAQ;CAElC,MAAM,kBAAkB,YAAY,KAAK,oDAClB,IAAI,SAAS,CACnC;AAED,gBAAe,UAAU;AACzB,oBAAmB,UAAU;AAC7B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAErB,mCAAgB;EACd,MAAM,oBAAoB,iBAAiB;EAC3C,MAAM,oCAAoB,IAAI,KAAiC;EAE/D,MAAM,OAOD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,QAAQ,KAAK;GACtD,MAAM,MAAM,eAAe,QAAQ;GACnC,MAAM,SAAS,mBAAmB,QAAQ;GAC1C,MAAM,MAAM,IAAI;AAChB,OAAI,IAAI,WAAW,EACjB;GAGF,MAAM,gBAAgB;IACpB,GAAG,kBAAkB;IACrB,GAAG,iBAAiB;IACpB,GAAG,IAAI;IACR;GAED,MAAM,iBAAiBC,oBAAM,cAAc,OAAO,GAC9C,cAAc,OAAO,UACpB,cAAc,WACd,OAAO,aAAa,cAAc,WAAW;AAElD,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,QAAK,KAAK;IACR;IACA;IACA;IACA;IACA;IACA;IACD,CAAC;;EAGJ,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,gBAAgB,CAAC;AAE5D,OAAK,MAAM,CAAC,KAAK,WAAW,kBAC1B,KAAI,CAAC,SAAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,KAAK,eAAe,mBAAmB;GAErE,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AAEnE,aAAS,OAAO,WAAW,IAAI;IAC/B,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AACrD,aAAS,OAAO,WAAW,qBAAqB;AAChD,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,KAAK,IAAI,UAAU;IAC5D,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;GAC3B;AAEF,mCAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC"}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { UseHotkeySequenceOptions } from "./useHotkeySequence.cjs";
|
|
2
|
-
import { HotkeyCallback, HotkeySequence } from "@tanstack/hotkeys";
|
|
3
|
-
|
|
4
|
-
//#region src/useHotkeySequences.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* A single sequence definition for use with `useHotkeySequences`.
|
|
7
|
-
*/
|
|
8
|
-
interface UseHotkeySequenceDefinition {
|
|
9
|
-
/** Array of hotkey strings that form the sequence */
|
|
10
|
-
sequence: HotkeySequence;
|
|
11
|
-
/** The function to call when the sequence is completed */
|
|
12
|
-
callback: HotkeyCallback;
|
|
13
|
-
/** Per-sequence options (merged on top of commonOptions) */
|
|
14
|
-
options?: UseHotkeySequenceOptions;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
18
|
-
*
|
|
19
|
-
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
|
|
20
|
-
* register variable-length lists without violating the rules of hooks.
|
|
21
|
-
*
|
|
22
|
-
* Options are merged in this order:
|
|
23
|
-
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
24
|
-
*
|
|
25
|
-
* Callbacks and options are synced on every render to avoid stale closures.
|
|
26
|
-
*
|
|
27
|
-
* Definitions with an empty `sequence` are skipped (no registration).
|
|
28
|
-
*
|
|
29
|
-
* @param definitions - Array of sequence definitions to register
|
|
30
|
-
* @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
|
|
31
|
-
* Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
|
|
32
|
-
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
33
|
-
* via `setOptions` (no unregister/re-register churn).
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```tsx
|
|
37
|
-
* function VimPalette() {
|
|
38
|
-
* useHotkeySequences([
|
|
39
|
-
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
40
|
-
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
41
|
-
* { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
|
|
42
|
-
* ])
|
|
43
|
-
* }
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```tsx
|
|
48
|
-
* function DynamicSequences({ items }) {
|
|
49
|
-
* useHotkeySequences(
|
|
50
|
-
* items.map((item) => ({
|
|
51
|
-
* sequence: item.chords,
|
|
52
|
-
* callback: item.action,
|
|
53
|
-
* options: { enabled: item.enabled },
|
|
54
|
-
* })),
|
|
55
|
-
* { preventDefault: true },
|
|
56
|
-
* )
|
|
57
|
-
* }
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare function useHotkeySequences(definitions: Array<UseHotkeySequenceDefinition>, commonOptions?: UseHotkeySequenceOptions): void;
|
|
61
|
-
//#endregion
|
|
62
|
-
export { UseHotkeySequenceDefinition, useHotkeySequences };
|
|
63
|
-
//# sourceMappingURL=useHotkeySequences.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeySequences.js","names":[],"sources":["../src/useHotkeySequences.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeySequenceOptions } from './useHotkeySequence'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\n/**\n * A single sequence definition for use with `useHotkeySequences`.\n */\nexport interface UseHotkeySequenceDefinition {\n /** Array of hotkey strings that form the sequence */\n sequence: HotkeySequence\n /** The function to call when the sequence is completed */\n callback: HotkeyCallback\n /** Per-sequence options (merged on top of commonOptions) */\n options?: UseHotkeySequenceOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).\n *\n * Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can\n * register variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * Definitions with an empty `sequence` are skipped (no registration).\n *\n * @param definitions - Array of sequence definitions to register\n * @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).\n * Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function VimPalette() {\n * useHotkeySequences([\n * { sequence: ['G', 'G'], callback: () => scrollToTop() },\n * { sequence: ['D', 'D'], callback: () => deleteLine() },\n * { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function DynamicSequences({ items }) {\n * useHotkeySequences(\n * items.map((item) => ({\n * sequence: item.chords,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeySequences(\n definitions: Array<UseHotkeySequenceDefinition>,\n commonOptions: UseHotkeySequenceOptions = {},\n): void {\n type RegistrationRecord = {\n handle: SequenceRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkeySequence\n const manager = getSequenceManager()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const definitionsRef = useRef(definitions)\n const sequenceStringsRef = useRef<Array<string>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const sequenceStrings = definitions.map((def) =>\n formatHotkeySequence(def.sequence),\n )\n\n definitionsRef.current = definitions\n sequenceStringsRef.current = sequenceStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof definitionsRef.current)[number]\n seq: HotkeySequence\n seqStr: string\n mergedOptions: UseHotkeySequenceOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < definitionsRef.current.length; i++) {\n const def = definitionsRef.current[i]!\n const seqStr = sequenceStringsRef.current[i]!\n const seq = def.sequence\n if (seq.length === 0) {\n continue\n }\n\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeySequenceOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${seqStr}`\n rows.push({\n registrationKey,\n def,\n seq,\n seqStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n // Publish callback/options only after commit, never during render.\n existing.handle.callback = def.callback\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(seq, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,SAAgB,mBACd,aACA,gBAA0C,EAAE,EACtC;CAMN,MAAM,iBAAiB,0BAA0B,CAAC;CAClD,MAAM,UAAU,oBAAoB;CAEpC,MAAM,mBAAmB,uBAAwC,IAAI,KAAK,CAAC;CAC3E,MAAM,iBAAiB,OAAO,YAAY;CAC1C,MAAM,qBAAqB,OAAsB,EAAE,CAAC;CACpD,MAAM,mBAAmB,OAAO,cAAc;CAC9C,MAAM,oBAAoB,OAAO,eAAe;CAChD,MAAM,aAAa,OAAO,QAAQ;CAElC,MAAM,kBAAkB,YAAY,KAAK,QACvC,qBAAqB,IAAI,SAAS,CACnC;AAED,gBAAe,UAAU;AACzB,oBAAmB,UAAU;AAC7B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAErB,iBAAgB;EACd,MAAM,oBAAoB,iBAAiB;EAC3C,MAAM,oCAAoB,IAAI,KAAiC;EAE/D,MAAM,OAOD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,QAAQ,KAAK;GACtD,MAAM,MAAM,eAAe,QAAQ;GACnC,MAAM,SAAS,mBAAmB,QAAQ;GAC1C,MAAM,MAAM,IAAI;AAChB,OAAI,IAAI,WAAW,EACjB;GAGF,MAAM,gBAAgB;IACpB,GAAG,kBAAkB;IACrB,GAAG,iBAAiB;IACpB,GAAG,IAAI;IACR;GAED,MAAM,iBAAiB,MAAM,cAAc,OAAO,GAC9C,cAAc,OAAO,UACpB,cAAc,WACd,OAAO,aAAa,cAAc,WAAW;AAElD,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,QAAK,KAAK;IACR;IACA;IACA;IACA;IACA;IACA;IACD,CAAC;;EAGJ,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,gBAAgB,CAAC;AAE5D,OAAK,MAAM,CAAC,KAAK,WAAW,kBAC1B,KAAI,CAAC,SAAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,KAAK,eAAe,mBAAmB;GAErE,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AAEnE,aAAS,OAAO,WAAW,IAAI;IAC/B,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AACrD,aAAS,OAAO,WAAW,qBAAqB;AAChD,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,KAAK,IAAI,UAAU;IAC5D,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;GAC3B;AAEF,iBAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC"}
|
package/dist/useHotkeys.cjs
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
2
|
-
const require_utils = require('./utils.cjs');
|
|
3
|
-
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
4
|
-
let preact_hooks = require("preact/hooks");
|
|
5
|
-
|
|
6
|
-
//#region src/useHotkeys.ts
|
|
7
|
-
/**
|
|
8
|
-
* Preact hook for registering multiple keyboard hotkeys at once.
|
|
9
|
-
*
|
|
10
|
-
* Uses the singleton HotkeyManager for efficient event handling.
|
|
11
|
-
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
12
|
-
* with variable-length lists without violating the rules of hooks.
|
|
13
|
-
*
|
|
14
|
-
* Options are merged in this order:
|
|
15
|
-
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
16
|
-
*
|
|
17
|
-
* Callbacks and options are synced on every render to avoid stale closures.
|
|
18
|
-
*
|
|
19
|
-
* @param hotkeys - Array of hotkey definitions to register
|
|
20
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).
|
|
21
|
-
* Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row
|
|
22
|
-
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
23
|
-
* via `setOptions` (no unregister/re-register churn).
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```tsx
|
|
27
|
-
* function Editor() {
|
|
28
|
-
* useHotkeys([
|
|
29
|
-
* { hotkey: 'Mod+S', callback: () => save() },
|
|
30
|
-
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
31
|
-
* { hotkey: 'Escape', callback: () => close() },
|
|
32
|
-
* ])
|
|
33
|
-
* }
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```tsx
|
|
38
|
-
* function MenuShortcuts({ items }) {
|
|
39
|
-
* // Dynamic hotkeys from data -- safe because it's a single hook call
|
|
40
|
-
* useHotkeys(
|
|
41
|
-
* items.map((item) => ({
|
|
42
|
-
* hotkey: item.shortcut,
|
|
43
|
-
* callback: item.action,
|
|
44
|
-
* options: { enabled: item.enabled },
|
|
45
|
-
* })),
|
|
46
|
-
* { preventDefault: true },
|
|
47
|
-
* )
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
function useHotkeys(hotkeys, commonOptions = {}) {
|
|
52
|
-
const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions().hotkey;
|
|
53
|
-
const manager = (0, _tanstack_hotkeys.getHotkeyManager)();
|
|
54
|
-
const platform = commonOptions.platform ?? defaultOptions?.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
|
|
55
|
-
const registrationsRef = (0, preact_hooks.useRef)(/* @__PURE__ */ new Map());
|
|
56
|
-
const hotkeysRef = (0, preact_hooks.useRef)(hotkeys);
|
|
57
|
-
const hotkeyStringsRef = (0, preact_hooks.useRef)([]);
|
|
58
|
-
const commonOptionsRef = (0, preact_hooks.useRef)(commonOptions);
|
|
59
|
-
const defaultOptionsRef = (0, preact_hooks.useRef)(defaultOptions);
|
|
60
|
-
const managerRef = (0, preact_hooks.useRef)(manager);
|
|
61
|
-
const hotkeyStrings = hotkeys.map((def) => (0, _tanstack_hotkeys.normalizeRegisterableHotkey)(def.hotkey, platform));
|
|
62
|
-
hotkeysRef.current = hotkeys;
|
|
63
|
-
hotkeyStringsRef.current = hotkeyStrings;
|
|
64
|
-
commonOptionsRef.current = commonOptions;
|
|
65
|
-
defaultOptionsRef.current = defaultOptions;
|
|
66
|
-
managerRef.current = manager;
|
|
67
|
-
(0, preact_hooks.useEffect)(() => {
|
|
68
|
-
const prevRegistrations = registrationsRef.current;
|
|
69
|
-
const nextRegistrations = /* @__PURE__ */ new Map();
|
|
70
|
-
const rows = [];
|
|
71
|
-
for (let i = 0; i < hotkeysRef.current.length; i++) {
|
|
72
|
-
const def = hotkeysRef.current[i];
|
|
73
|
-
const hotkeyStr = hotkeyStringsRef.current[i];
|
|
74
|
-
const mergedOptions = {
|
|
75
|
-
...defaultOptionsRef.current,
|
|
76
|
-
...commonOptionsRef.current,
|
|
77
|
-
...def.options
|
|
78
|
-
};
|
|
79
|
-
const resolvedTarget = require_utils.isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
|
|
80
|
-
if (!resolvedTarget) continue;
|
|
81
|
-
const registrationKey = `${i}:${hotkeyStr}`;
|
|
82
|
-
rows.push({
|
|
83
|
-
registrationKey,
|
|
84
|
-
def,
|
|
85
|
-
hotkeyStr,
|
|
86
|
-
mergedOptions,
|
|
87
|
-
resolvedTarget
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const nextKeys = new Set(rows.map((r) => r.registrationKey));
|
|
91
|
-
for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
|
|
92
|
-
for (const row of rows) {
|
|
93
|
-
const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } = row;
|
|
94
|
-
const existing = prevRegistrations.get(registrationKey);
|
|
95
|
-
if (existing?.handle.isActive && existing.target === resolvedTarget) {
|
|
96
|
-
existing.handle.callback = def.callback;
|
|
97
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions;
|
|
98
|
-
existing.handle.setOptions(optionsWithoutTarget);
|
|
99
|
-
nextRegistrations.set(registrationKey, existing);
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
if (existing?.handle.isActive) existing.handle.unregister();
|
|
103
|
-
const handle = managerRef.current.register(hotkeyStr, def.callback, {
|
|
104
|
-
...mergedOptions,
|
|
105
|
-
target: resolvedTarget
|
|
106
|
-
});
|
|
107
|
-
nextRegistrations.set(registrationKey, {
|
|
108
|
-
handle,
|
|
109
|
-
target: resolvedTarget
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
registrationsRef.current = nextRegistrations;
|
|
113
|
-
});
|
|
114
|
-
(0, preact_hooks.useEffect)(() => {
|
|
115
|
-
return () => {
|
|
116
|
-
for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
|
|
117
|
-
registrationsRef.current = /* @__PURE__ */ new Map();
|
|
118
|
-
};
|
|
119
|
-
}, []);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
//#endregion
|
|
123
|
-
exports.useHotkeys = useHotkeys;
|
|
124
|
-
//# sourceMappingURL=useHotkeys.cjs.map
|