@tanstack/solid-hotkeys 0.6.0 → 0.8.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 +1 -1
- package/dist/createHotkey.cjs +28 -10
- package/dist/createHotkey.cjs.map +1 -1
- package/dist/createHotkey.js +29 -11
- package/dist/createHotkey.js.map +1 -1
- package/dist/createHotkeySequence.cjs +27 -8
- package/dist/createHotkeySequence.cjs.map +1 -1
- package/dist/createHotkeySequence.js +28 -9
- package/dist/createHotkeySequence.js.map +1 -1
- package/dist/createHotkeySequences.cjs +110 -0
- package/dist/createHotkeySequences.cjs.map +1 -0
- package/dist/createHotkeySequences.d.cts +57 -0
- package/dist/createHotkeySequences.d.ts +57 -0
- package/dist/createHotkeySequences.js +110 -0
- package/dist/createHotkeySequences.js.map +1 -0
- package/dist/createHotkeys.cjs +1 -1
- package/dist/createHotkeys.cjs.map +1 -1
- package/dist/createHotkeys.js +2 -2
- package/dist/createHotkeys.js.map +1 -1
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +2 -2
- package/src/createHotkey.ts +34 -21
- package/src/createHotkeySequence.ts +33 -16
- package/src/createHotkeySequences.ts +176 -0
- package/src/createHotkeys.ts +2 -8
- package/src/index.ts +1 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
|
|
3
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
4
|
+
|
|
5
|
+
//#region src/createHotkeySequences.ts
|
|
6
|
+
/**
|
|
7
|
+
* SolidJS primitive for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
8
|
+
*
|
|
9
|
+
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions, or accessors,
|
|
10
|
+
* so you can react to variable-length lists.
|
|
11
|
+
*
|
|
12
|
+
* Options are merged in this order:
|
|
13
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
14
|
+
*
|
|
15
|
+
* Definitions with an empty `sequence` are skipped (no registration).
|
|
16
|
+
*
|
|
17
|
+
* @param sequences - Array of sequence definitions, or accessor returning them
|
|
18
|
+
* @param commonOptions - Shared options for all sequences, or accessor
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* function VimPalette() {
|
|
23
|
+
* createHotkeySequences([
|
|
24
|
+
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
25
|
+
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
26
|
+
* ])
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* function Dynamic(props) {
|
|
33
|
+
* createHotkeySequences(
|
|
34
|
+
* () => props.items.map((item) => ({
|
|
35
|
+
* sequence: item.chords,
|
|
36
|
+
* callback: item.action,
|
|
37
|
+
* options: { enabled: item.enabled },
|
|
38
|
+
* })),
|
|
39
|
+
* { preventDefault: true },
|
|
40
|
+
* )
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function createHotkeySequences(sequences, commonOptions = {}) {
|
|
45
|
+
const defaultOptions = useDefaultHotkeysOptions();
|
|
46
|
+
const manager = getSequenceManager();
|
|
47
|
+
const registrations = /* @__PURE__ */ new Map();
|
|
48
|
+
onCleanup(() => {
|
|
49
|
+
for (const { handle } of registrations.values()) if (handle.isActive) handle.unregister();
|
|
50
|
+
registrations.clear();
|
|
51
|
+
});
|
|
52
|
+
createEffect(() => {
|
|
53
|
+
const resolved = typeof sequences === "function" ? sequences() : sequences;
|
|
54
|
+
const resolvedCommonOptions = typeof commonOptions === "function" ? commonOptions() : commonOptions;
|
|
55
|
+
const nextKeys = /* @__PURE__ */ new Set();
|
|
56
|
+
const prepared = [];
|
|
57
|
+
for (let i = 0; i < resolved.length; i++) {
|
|
58
|
+
const def = resolved[i];
|
|
59
|
+
const resolvedDefOptions = def.options ?? {};
|
|
60
|
+
const mergedOptions = {
|
|
61
|
+
...defaultOptions.hotkeySequence,
|
|
62
|
+
...resolvedCommonOptions,
|
|
63
|
+
...resolvedDefOptions
|
|
64
|
+
};
|
|
65
|
+
if (def.sequence.length === 0) continue;
|
|
66
|
+
const sequenceString = formatHotkeySequence(def.sequence);
|
|
67
|
+
const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
|
|
68
|
+
if (!resolvedTarget) continue;
|
|
69
|
+
const registrationKey = `${i}:${sequenceString}`;
|
|
70
|
+
nextKeys.add(registrationKey);
|
|
71
|
+
prepared.push({
|
|
72
|
+
registrationKey,
|
|
73
|
+
def,
|
|
74
|
+
mergedOptions,
|
|
75
|
+
sequenceString,
|
|
76
|
+
resolvedTarget
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
|
|
80
|
+
if (record.handle.isActive) record.handle.unregister();
|
|
81
|
+
registrations.delete(key);
|
|
82
|
+
}
|
|
83
|
+
for (const p of prepared) {
|
|
84
|
+
const existing = registrations.get(p.registrationKey);
|
|
85
|
+
if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
|
|
86
|
+
existing.handle.callback = p.def.callback;
|
|
87
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
|
|
88
|
+
existing.handle.setOptions(optionsWithoutTarget);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (existing) {
|
|
92
|
+
if (existing.handle.isActive) existing.handle.unregister();
|
|
93
|
+
registrations.delete(p.registrationKey);
|
|
94
|
+
}
|
|
95
|
+
const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
|
|
96
|
+
const handle = manager.register(p.def.sequence, p.def.callback, {
|
|
97
|
+
...optionsWithoutTarget,
|
|
98
|
+
target: p.resolvedTarget
|
|
99
|
+
});
|
|
100
|
+
registrations.set(p.registrationKey, {
|
|
101
|
+
handle,
|
|
102
|
+
target: p.resolvedTarget
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
export { createHotkeySequences };
|
|
110
|
+
//# sourceMappingURL=createHotkeySequences.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createHotkeySequences.js","names":[],"sources":["../src/createHotkeySequences.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeySequenceOptions } from './createHotkeySequence'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\n/**\n * A single sequence definition for use with `createHotkeySequences`.\n */\nexport interface CreateHotkeySequenceDefinition {\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?: CreateHotkeySequenceOptions\n}\n\n/**\n * SolidJS primitive for registering multiple keyboard shortcut sequences at once (Vim-style).\n *\n * Uses the singleton SequenceManager. Accepts a dynamic array of definitions, or accessors,\n * so you can react to variable-length lists.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Definitions with an empty `sequence` are skipped (no registration).\n *\n * @param sequences - Array of sequence definitions, or accessor returning them\n * @param commonOptions - Shared options for all sequences, or accessor\n *\n * @example\n * ```tsx\n * function VimPalette() {\n * createHotkeySequences([\n * { sequence: ['G', 'G'], callback: () => scrollToTop() },\n * { sequence: ['D', 'D'], callback: () => deleteLine() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Dynamic(props) {\n * createHotkeySequences(\n * () => props.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 createHotkeySequences(\n sequences:\n | Array<CreateHotkeySequenceDefinition>\n | (() => Array<CreateHotkeySequenceDefinition>),\n commonOptions:\n | CreateHotkeySequenceOptions\n | (() => CreateHotkeySequenceOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: SequenceRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getSequenceManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolved = typeof sequences === 'function' ? sequences() : sequences\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeySequenceDefinition\n mergedOptions: CreateHotkeySequenceOptions\n sequenceString: string\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolved.length; i++) {\n const def = resolved[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkeySequence,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeySequenceOptions\n\n if (def.sequence.length === 0) {\n continue\n }\n\n const sequenceString = formatHotkeySequence(def.sequence)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${sequenceString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n sequenceString,\n resolvedTarget,\n })\n }\n\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.def.sequence, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,SAAgB,sBACd,WAGA,gBAE0C,EAAE,EACtC;CAMN,MAAM,iBAAiB,0BAA0B;CACjD,MAAM,UAAU,oBAAoB;CAEpC,MAAM,gCAAgB,IAAI,KAAiC;AAE3D,iBAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,oBAAmB;EACjB,MAAM,WAAW,OAAO,cAAc,aAAa,WAAW,GAAG;EACjE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;GACxC,MAAM,MAAM,SAAS;GACrB,MAAM,qBAAqB,IAAI,WAAW,EAAE;GAE5C,MAAM,gBAAgB;IACpB,GAAG,eAAe;IAClB,GAAG;IACH,GAAG;IACJ;AAED,OAAI,IAAI,SAAS,WAAW,EAC1B;GAGF,MAAM,iBAAiB,qBAAqB,IAAI,SAAS;GAEzD,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,YAAS,IAAI,gBAAgB;AAC7B,YAAS,KAAK;IACZ;IACA;IACA;IACA;IACA;IACD,CAAC;;AAGJ,OAAK,MAAM,CAAC,KAAK,WAAW,CAAC,GAAG,cAAc,SAAS,CAAC,CACtD,KAAI,CAAC,SAAS,IAAI,IAAI,EAAE;AACtB,OAAI,OAAO,OAAO,SAChB,QAAO,OAAO,YAAY;AAE5B,iBAAc,OAAO,IAAI;;AAI7B,OAAK,MAAM,KAAK,UAAU;GACxB,MAAM,WAAW,cAAc,IAAI,EAAE,gBAAgB;AACrD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,EAAE,gBAAgB;AACrE,aAAS,OAAO,WAAW,EAAE,IAAI;IACjC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;AACvD,aAAS,OAAO,WAAW,qBAAqB;AAChD;;AAGF,OAAI,UAAU;AACZ,QAAI,SAAS,OAAO,SAClB,UAAS,OAAO,YAAY;AAE9B,kBAAc,OAAO,EAAE,gBAAgB;;GAGzC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;GACvD,MAAM,SAAS,QAAQ,SAAS,EAAE,IAAI,UAAU,EAAE,IAAI,UAAU;IAC9D,GAAG;IACH,QAAQ,EAAE;IACX,CAAC;AACF,iBAAc,IAAI,EAAE,iBAAiB;IACnC;IACA,QAAQ,EAAE;IACX,CAAC;;GAEJ"}
|
package/dist/createHotkeys.cjs
CHANGED
|
@@ -63,7 +63,7 @@ function createHotkeys(hotkeys, commonOptions = {}) {
|
|
|
63
63
|
...resolvedDefOptions
|
|
64
64
|
};
|
|
65
65
|
const platform = mergedOptions.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
|
|
66
|
-
const hotkeyString =
|
|
66
|
+
const hotkeyString = (0, _tanstack_hotkeys.normalizeRegisterableHotkey)(def.hotkey, platform);
|
|
67
67
|
const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
|
|
68
68
|
if (!resolvedTarget) continue;
|
|
69
69
|
const registrationKey = `${i}:${hotkeyString}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHotkeys.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n
|
|
1
|
+
{"version":3,"file":"createHotkeys.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n getHotkeyManager,\n normalizeRegisterableHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeyOptions } from './createHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `createHotkeys`.\n */\nexport interface CreateHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: CreateHotkeyOptions\n}\n\n/**\n * SolidJS primitive for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * @param hotkeys - Array of hotkey definitions, or accessor returning them\n * @param commonOptions - Shared options applied to all hotkeys, or accessor\n *\n * @example\n * ```tsx\n * function Editor() {\n * createHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts(props) {\n * createHotkeys(\n * () => props.items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function createHotkeys(\n hotkeys:\n | Array<CreateHotkeyDefinition>\n | (() => Array<CreateHotkeyDefinition>),\n commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getHotkeyManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n // Clean up only when this component/scope disposes — not before every effect\n // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive\n // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and\n // re-registered them, flooding the manager store and could freeze the tab.\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeyDefinition\n mergedOptions: CreateHotkeyOptions\n hotkeyString: Hotkey\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolvedHotkeys.length; i++) {\n const def = resolvedHotkeys[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkey,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeyOptions\n\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString = normalizeRegisterableHotkey(def.hotkey, platform)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n hotkeyString,\n resolvedTarget,\n })\n }\n\n // Drop stale keys first so list index shifts don't briefly double-register\n // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.hotkeyString, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,cACd,SAGA,gBAAmE,EAAE,EAC/D;CAMN,MAAM,iBAAiBA,kDAA0B;CACjD,MAAM,mDAA4B;CAElC,MAAM,gCAAgB,IAAI,KAAiC;AAM3D,+BAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,kCAAmB;EACjB,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EACpE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;GAC/C,MAAM,MAAM,gBAAgB;GAC5B,MAAM,qBAAqB,IAAI,WAAW,EAAE;GAE5C,MAAM,gBAAgB;IACpB,GAAG,eAAe;IAClB,GAAG;IACH,GAAG;IACJ;GAED,MAAM,WAAW,cAAc,mDAA4B;GAC3D,MAAM,kEAA2C,IAAI,QAAQ,SAAS;GAEtE,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,YAAS,IAAI,gBAAgB;AAC7B,YAAS,KAAK;IACZ;IACA;IACA;IACA;IACA;IACD,CAAC;;AAKJ,OAAK,MAAM,CAAC,KAAK,WAAW,CAAC,GAAG,cAAc,SAAS,CAAC,CACtD,KAAI,CAAC,SAAS,IAAI,IAAI,EAAE;AACtB,OAAI,OAAO,OAAO,SAChB,QAAO,OAAO,YAAY;AAE5B,iBAAc,OAAO,IAAI;;AAI7B,OAAK,MAAM,KAAK,UAAU;GACxB,MAAM,WAAW,cAAc,IAAI,EAAE,gBAAgB;AACrD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,EAAE,gBAAgB;AACrE,aAAS,OAAO,WAAW,EAAE,IAAI;IACjC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;AACvD,aAAS,OAAO,WAAW,qBAAqB;AAChD;;AAGF,OAAI,UAAU;AACZ,QAAI,SAAS,OAAO,SAClB,UAAS,OAAO,YAAY;AAE9B,kBAAc,OAAO,EAAE,gBAAgB;;GAGzC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;GACvD,MAAM,SAAS,QAAQ,SAAS,EAAE,cAAc,EAAE,IAAI,UAAU;IAC9D,GAAG;IACH,QAAQ,EAAE;IACX,CAAC;AACF,iBAAc,IAAI,EAAE,iBAAiB;IACnC;IACA,QAAQ,EAAE;IACX,CAAC;;GAEJ"}
|
package/dist/createHotkeys.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
-
import { detectPlatform,
|
|
2
|
+
import { detectPlatform, getHotkeyManager, normalizeRegisterableHotkey } from "@tanstack/hotkeys";
|
|
3
3
|
import { createEffect, onCleanup } from "solid-js";
|
|
4
4
|
|
|
5
5
|
//#region src/createHotkeys.ts
|
|
@@ -63,7 +63,7 @@ function createHotkeys(hotkeys, commonOptions = {}) {
|
|
|
63
63
|
...resolvedDefOptions
|
|
64
64
|
};
|
|
65
65
|
const platform = mergedOptions.platform ?? detectPlatform();
|
|
66
|
-
const hotkeyString =
|
|
66
|
+
const hotkeyString = normalizeRegisterableHotkey(def.hotkey, platform);
|
|
67
67
|
const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
|
|
68
68
|
if (!resolvedTarget) continue;
|
|
69
69
|
const registrationKey = `${i}:${hotkeyString}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHotkeys.js","names":[],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n
|
|
1
|
+
{"version":3,"file":"createHotkeys.js","names":[],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n getHotkeyManager,\n normalizeRegisterableHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeyOptions } from './createHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `createHotkeys`.\n */\nexport interface CreateHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: CreateHotkeyOptions\n}\n\n/**\n * SolidJS primitive for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * @param hotkeys - Array of hotkey definitions, or accessor returning them\n * @param commonOptions - Shared options applied to all hotkeys, or accessor\n *\n * @example\n * ```tsx\n * function Editor() {\n * createHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts(props) {\n * createHotkeys(\n * () => props.items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function createHotkeys(\n hotkeys:\n | Array<CreateHotkeyDefinition>\n | (() => Array<CreateHotkeyDefinition>),\n commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getHotkeyManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n // Clean up only when this component/scope disposes — not before every effect\n // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive\n // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and\n // re-registered them, flooding the manager store and could freeze the tab.\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeyDefinition\n mergedOptions: CreateHotkeyOptions\n hotkeyString: Hotkey\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolvedHotkeys.length; i++) {\n const def = resolvedHotkeys[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkey,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeyOptions\n\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString = normalizeRegisterableHotkey(def.hotkey, platform)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n hotkeyString,\n resolvedTarget,\n })\n }\n\n // Drop stale keys first so list index shifts don't briefly double-register\n // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.hotkeyString, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,cACd,SAGA,gBAAmE,EAAE,EAC/D;CAMN,MAAM,iBAAiB,0BAA0B;CACjD,MAAM,UAAU,kBAAkB;CAElC,MAAM,gCAAgB,IAAI,KAAiC;AAM3D,iBAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,oBAAmB;EACjB,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EACpE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;GAC/C,MAAM,MAAM,gBAAgB;GAC5B,MAAM,qBAAqB,IAAI,WAAW,EAAE;GAE5C,MAAM,gBAAgB;IACpB,GAAG,eAAe;IAClB,GAAG;IACH,GAAG;IACJ;GAED,MAAM,WAAW,cAAc,YAAY,gBAAgB;GAC3D,MAAM,eAAe,4BAA4B,IAAI,QAAQ,SAAS;GAEtE,MAAM,iBACJ,YAAY,gBACP,cAAc,UAAU,OACzB,OAAO,aAAa,cAClB,WACA;AAER,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,YAAS,IAAI,gBAAgB;AAC7B,YAAS,KAAK;IACZ;IACA;IACA;IACA;IACA;IACD,CAAC;;AAKJ,OAAK,MAAM,CAAC,KAAK,WAAW,CAAC,GAAG,cAAc,SAAS,CAAC,CACtD,KAAI,CAAC,SAAS,IAAI,IAAI,EAAE;AACtB,OAAI,OAAO,OAAO,SAChB,QAAO,OAAO,YAAY;AAE5B,iBAAc,OAAO,IAAI;;AAI7B,OAAK,MAAM,KAAK,UAAU;GACxB,MAAM,WAAW,cAAc,IAAI,EAAE,gBAAgB;AACrD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,EAAE,gBAAgB;AACrE,aAAS,OAAO,WAAW,EAAE,IAAI;IACjC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;AACvD,aAAS,OAAO,WAAW,qBAAqB;AAChD;;AAGF,OAAI,UAAU;AACZ,QAAI,SAAS,OAAO,SAClB,UAAS,OAAO,YAAY;AAE9B,kBAAc,OAAO,EAAE,gBAAgB;;GAGzC,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB,EAAE;GACvD,MAAM,SAAS,QAAQ,SAAS,EAAE,cAAc,EAAE,IAAI,UAAU;IAC9D,GAAG;IACH,QAAQ,EAAE;IACX,CAAC;AACF,iBAAc,IAAI,EAAE,iBAAiB;IACnC;IACA,QAAQ,EAAE;IACX,CAAC;;GAEJ"}
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,7 @@ const require_createHeldKeys = require('./createHeldKeys.cjs');
|
|
|
6
6
|
const require_createHeldKeyCodes = require('./createHeldKeyCodes.cjs');
|
|
7
7
|
const require_createKeyHold = require('./createKeyHold.cjs');
|
|
8
8
|
const require_createHotkeySequence = require('./createHotkeySequence.cjs');
|
|
9
|
+
const require_createHotkeySequences = require('./createHotkeySequences.cjs');
|
|
9
10
|
const require_createHotkeyRecorder = require('./createHotkeyRecorder.cjs');
|
|
10
11
|
const require_createHotkeySequenceRecorder = require('./createHotkeySequenceRecorder.cjs');
|
|
11
12
|
|
|
@@ -16,6 +17,7 @@ exports.createHotkey = require_createHotkey.createHotkey;
|
|
|
16
17
|
exports.createHotkeyRecorder = require_createHotkeyRecorder.createHotkeyRecorder;
|
|
17
18
|
exports.createHotkeySequence = require_createHotkeySequence.createHotkeySequence;
|
|
18
19
|
exports.createHotkeySequenceRecorder = require_createHotkeySequenceRecorder.createHotkeySequenceRecorder;
|
|
20
|
+
exports.createHotkeySequences = require_createHotkeySequences.createHotkeySequences;
|
|
19
21
|
exports.createHotkeys = require_createHotkeys.createHotkeys;
|
|
20
22
|
exports.createKeyHold = require_createKeyHold.createKeyHold;
|
|
21
23
|
exports.useDefaultHotkeysOptions = require_HotkeysProvider.useDefaultHotkeysOptions;
|
package/dist/index.d.cts
CHANGED
|
@@ -5,7 +5,8 @@ import { CreateHotkeyDefinition, createHotkeys } from "./createHotkeys.cjs";
|
|
|
5
5
|
import { createHeldKeys } from "./createHeldKeys.cjs";
|
|
6
6
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.cjs";
|
|
7
7
|
import { createKeyHold } from "./createKeyHold.cjs";
|
|
8
|
+
import { CreateHotkeySequenceDefinition, createHotkeySequences } from "./createHotkeySequences.cjs";
|
|
8
9
|
import { SolidHotkeyRecorder, createHotkeyRecorder } from "./createHotkeyRecorder.cjs";
|
|
9
10
|
import { SolidHotkeySequenceRecorder, createHotkeySequenceRecorder } from "./createHotkeySequenceRecorder.cjs";
|
|
10
11
|
export * from "@tanstack/hotkeys";
|
|
11
|
-
export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, SolidHotkeySequenceRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeySequenceRecorder, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
|
12
|
+
export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceDefinition, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, SolidHotkeySequenceRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeySequenceRecorder, createHotkeySequences, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,8 @@ import { CreateHotkeyDefinition, createHotkeys } from "./createHotkeys.js";
|
|
|
5
5
|
import { createHeldKeys } from "./createHeldKeys.js";
|
|
6
6
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
7
7
|
import { createKeyHold } from "./createKeyHold.js";
|
|
8
|
+
import { CreateHotkeySequenceDefinition, createHotkeySequences } from "./createHotkeySequences.js";
|
|
8
9
|
import { SolidHotkeyRecorder, createHotkeyRecorder } from "./createHotkeyRecorder.js";
|
|
9
10
|
import { SolidHotkeySequenceRecorder, createHotkeySequenceRecorder } from "./createHotkeySequenceRecorder.js";
|
|
10
11
|
export * from "@tanstack/hotkeys";
|
|
11
|
-
export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, SolidHotkeySequenceRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeySequenceRecorder, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
|
12
|
+
export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceDefinition, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, SolidHotkeySequenceRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeySequenceRecorder, createHotkeySequences, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
package/dist/index.js
CHANGED
|
@@ -5,9 +5,10 @@ import { createHeldKeys } from "./createHeldKeys.js";
|
|
|
5
5
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
6
6
|
import { createKeyHold } from "./createKeyHold.js";
|
|
7
7
|
import { createHotkeySequence } from "./createHotkeySequence.js";
|
|
8
|
+
import { createHotkeySequences } from "./createHotkeySequences.js";
|
|
8
9
|
import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
|
|
9
10
|
import { createHotkeySequenceRecorder } from "./createHotkeySequenceRecorder.js";
|
|
10
11
|
|
|
11
12
|
export * from "@tanstack/hotkeys"
|
|
12
13
|
|
|
13
|
-
export { HotkeysProvider, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeySequenceRecorder, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
|
14
|
+
export { HotkeysProvider, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeySequenceRecorder, createHotkeySequences, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "SolidJS adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@tanstack/solid-store": "^0.9.2",
|
|
46
|
-
"@tanstack/hotkeys": "0.
|
|
46
|
+
"@tanstack/hotkeys": "0.6.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@solidjs/testing-library": "^0.8.10",
|
package/src/createHotkey.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { createEffect, onCleanup } from 'solid-js'
|
|
2
2
|
import {
|
|
3
3
|
detectPlatform,
|
|
4
|
-
formatHotkey,
|
|
5
4
|
getHotkeyManager,
|
|
6
|
-
|
|
5
|
+
normalizeRegisterableHotkey,
|
|
7
6
|
} from '@tanstack/hotkeys'
|
|
8
7
|
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
9
8
|
import type {
|
|
@@ -89,6 +88,17 @@ export function createHotkey(
|
|
|
89
88
|
const manager = getHotkeyManager()
|
|
90
89
|
|
|
91
90
|
let registration: HotkeyRegistrationHandle | null = null
|
|
91
|
+
let lastHotkeyString: Hotkey | null = null
|
|
92
|
+
let lastTarget: HTMLElement | Document | Window | null = null
|
|
93
|
+
|
|
94
|
+
onCleanup(() => {
|
|
95
|
+
if (registration?.isActive) {
|
|
96
|
+
registration.unregister()
|
|
97
|
+
registration = null
|
|
98
|
+
}
|
|
99
|
+
lastHotkeyString = null
|
|
100
|
+
lastTarget = null
|
|
101
|
+
})
|
|
92
102
|
|
|
93
103
|
createEffect(() => {
|
|
94
104
|
// Resolve reactive values
|
|
@@ -102,12 +112,7 @@ export function createHotkey(
|
|
|
102
112
|
|
|
103
113
|
// Normalize to hotkey string
|
|
104
114
|
const platform = mergedOptions.platform ?? detectPlatform()
|
|
105
|
-
const hotkeyString
|
|
106
|
-
typeof resolvedHotkey === 'string'
|
|
107
|
-
? resolvedHotkey
|
|
108
|
-
: (formatHotkey(
|
|
109
|
-
rawHotkeyToParsedHotkey(resolvedHotkey, platform),
|
|
110
|
-
) as Hotkey)
|
|
115
|
+
const hotkeyString = normalizeRegisterableHotkey(resolvedHotkey, platform)
|
|
111
116
|
|
|
112
117
|
// Resolve target: when explicitly provided (even as null), use it and skip if null.
|
|
113
118
|
// When not provided, default to document. Matches React's ref handling.
|
|
@@ -119,36 +124,44 @@ export function createHotkey(
|
|
|
119
124
|
: null
|
|
120
125
|
|
|
121
126
|
if (!resolvedTarget) {
|
|
127
|
+
if (registration?.isActive) {
|
|
128
|
+
registration.unregister()
|
|
129
|
+
registration = null
|
|
130
|
+
}
|
|
131
|
+
lastHotkeyString = null
|
|
132
|
+
lastTarget = null
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Extract options without target (target is handled separately)
|
|
137
|
+
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
138
|
+
|
|
139
|
+
if (
|
|
140
|
+
registration?.isActive &&
|
|
141
|
+
lastHotkeyString === hotkeyString &&
|
|
142
|
+
lastTarget === resolvedTarget
|
|
143
|
+
) {
|
|
144
|
+
registration.callback = callback
|
|
145
|
+
registration.setOptions(optionsWithoutTarget)
|
|
122
146
|
return
|
|
123
147
|
}
|
|
124
148
|
|
|
125
|
-
// Unregister previous registration if it exists
|
|
126
149
|
if (registration?.isActive) {
|
|
127
150
|
registration.unregister()
|
|
128
151
|
registration = null
|
|
129
152
|
}
|
|
130
153
|
|
|
131
|
-
// Extract options without target (target is handled separately)
|
|
132
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
133
|
-
|
|
134
|
-
// Register the hotkey
|
|
135
154
|
registration = manager.register(hotkeyString, callback, {
|
|
136
155
|
...optionsWithoutTarget,
|
|
137
156
|
target: resolvedTarget,
|
|
138
157
|
})
|
|
139
158
|
|
|
140
|
-
// Update callback and options on every effect run
|
|
141
159
|
if (registration.isActive) {
|
|
142
160
|
registration.callback = callback
|
|
143
161
|
registration.setOptions(optionsWithoutTarget)
|
|
144
162
|
}
|
|
145
163
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (registration?.isActive) {
|
|
149
|
-
registration.unregister()
|
|
150
|
-
registration = null
|
|
151
|
-
}
|
|
152
|
-
})
|
|
164
|
+
lastHotkeyString = hotkeyString
|
|
165
|
+
lastTarget = resolvedTarget
|
|
153
166
|
})
|
|
154
167
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createEffect, onCleanup } from 'solid-js'
|
|
2
|
-
import { getSequenceManager } from '@tanstack/hotkeys'
|
|
2
|
+
import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
|
|
3
3
|
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
4
4
|
import type {
|
|
5
5
|
HotkeyCallback,
|
|
@@ -72,6 +72,17 @@ export function createHotkeySequence(
|
|
|
72
72
|
const manager = getSequenceManager()
|
|
73
73
|
|
|
74
74
|
let registration: SequenceRegistrationHandle | null = null
|
|
75
|
+
let lastSequenceKey: string | null = null
|
|
76
|
+
let lastTarget: HTMLElement | Document | Window | null = null
|
|
77
|
+
|
|
78
|
+
onCleanup(() => {
|
|
79
|
+
if (registration?.isActive) {
|
|
80
|
+
registration.unregister()
|
|
81
|
+
registration = null
|
|
82
|
+
}
|
|
83
|
+
lastSequenceKey = null
|
|
84
|
+
lastTarget = null
|
|
85
|
+
})
|
|
75
86
|
|
|
76
87
|
createEffect(() => {
|
|
77
88
|
// Resolve reactive values
|
|
@@ -87,10 +98,6 @@ export function createHotkeySequence(
|
|
|
87
98
|
// Extract options without target (target is handled separately)
|
|
88
99
|
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
89
100
|
|
|
90
|
-
if (resolvedSequence.length === 0) {
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
|
|
94
101
|
// Resolve target: when explicitly provided (even as null), use it and skip if null.
|
|
95
102
|
// When not provided, default to document. Matches createHotkey.
|
|
96
103
|
const resolvedTarget =
|
|
@@ -100,34 +107,44 @@ export function createHotkeySequence(
|
|
|
100
107
|
? document
|
|
101
108
|
: null
|
|
102
109
|
|
|
103
|
-
if (!resolvedTarget) {
|
|
110
|
+
if (resolvedSequence.length === 0 || !resolvedTarget) {
|
|
111
|
+
if (registration?.isActive) {
|
|
112
|
+
registration.unregister()
|
|
113
|
+
registration = null
|
|
114
|
+
}
|
|
115
|
+
lastSequenceKey = null
|
|
116
|
+
lastTarget = null
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const sequenceKey = formatHotkeySequence(resolvedSequence)
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
registration?.isActive &&
|
|
124
|
+
lastSequenceKey === sequenceKey &&
|
|
125
|
+
lastTarget === resolvedTarget
|
|
126
|
+
) {
|
|
127
|
+
registration.callback = callback
|
|
128
|
+
registration.setOptions(optionsWithoutTarget)
|
|
104
129
|
return
|
|
105
130
|
}
|
|
106
131
|
|
|
107
|
-
// Unregister previous registration if it exists
|
|
108
132
|
if (registration?.isActive) {
|
|
109
133
|
registration.unregister()
|
|
110
134
|
registration = null
|
|
111
135
|
}
|
|
112
136
|
|
|
113
|
-
// Register the sequence
|
|
114
137
|
registration = manager.register(resolvedSequence, callback, {
|
|
115
138
|
...mergedOptions,
|
|
116
139
|
target: resolvedTarget,
|
|
117
140
|
})
|
|
118
141
|
|
|
119
|
-
// Sync callback and options on every effect run
|
|
120
142
|
if (registration.isActive) {
|
|
121
143
|
registration.callback = callback
|
|
122
144
|
registration.setOptions(optionsWithoutTarget)
|
|
123
145
|
}
|
|
124
146
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (registration?.isActive) {
|
|
128
|
-
registration.unregister()
|
|
129
|
-
registration = null
|
|
130
|
-
}
|
|
131
|
-
})
|
|
147
|
+
lastSequenceKey = sequenceKey
|
|
148
|
+
lastTarget = resolvedTarget
|
|
132
149
|
})
|
|
133
150
|
}
|