@tanstack/solid-hotkeys 0.13.0 → 0.14.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.
@@ -1,43 +0,0 @@
1
- import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
- import { HotkeySequenceRecorder } from "@tanstack/hotkeys";
3
- import { createEffect, onCleanup } from "solid-js";
4
- import { useSelector } from "@tanstack/solid-store";
5
-
6
- //#region src/createHotkeySequenceRecorder.ts
7
- /**
8
- * SolidJS primitive for recording multi-chord sequences (Vim-style shortcuts).
9
- */
10
- function createHotkeySequenceRecorder(options) {
11
- const defaultOptions = useDefaultHotkeysOptionsSource();
12
- const resolvedOptions = typeof options === "function" ? options() : options;
13
- const mergedOptions = {
14
- ...defaultOptions().hotkeySequenceRecorder,
15
- ...resolvedOptions
16
- };
17
- const recorder = new HotkeySequenceRecorder(mergedOptions);
18
- const isRecording = useSelector(recorder.store, (state) => state.isRecording);
19
- const steps = useSelector(recorder.store, (state) => state.steps);
20
- const recordedSequence = useSelector(recorder.store, (state) => state.recordedSequence);
21
- createEffect(() => {
22
- const resolved = typeof options === "function" ? options() : options;
23
- recorder.setOptions({
24
- ...defaultOptions().hotkeySequenceRecorder,
25
- ...resolved
26
- });
27
- });
28
- onCleanup(() => {
29
- recorder.destroy();
30
- });
31
- return {
32
- isRecording,
33
- steps,
34
- recordedSequence,
35
- startRecording: () => recorder.start(),
36
- stopRecording: () => recorder.stop(),
37
- cancelRecording: () => recorder.cancel(),
38
- commitRecording: () => recorder.commit()
39
- };
40
- }
41
-
42
- //#endregion
43
- export { createHotkeySequenceRecorder };
@@ -1,54 +0,0 @@
1
- import { CreateHotkeySequenceOptions } from "./createHotkeySequence.js";
2
- import { HotkeyCallback, HotkeySequence } from "@tanstack/hotkeys";
3
- //#region src/createHotkeySequences.d.ts
4
- /**
5
- * A single sequence definition for use with `createHotkeySequences`.
6
- */
7
- export interface CreateHotkeySequenceDefinition {
8
- /** Array of hotkey strings that form the sequence */
9
- sequence: HotkeySequence;
10
- /** The function to call when the sequence is completed */
11
- callback: HotkeyCallback;
12
- /** Per-sequence options (merged on top of commonOptions) */
13
- options?: CreateHotkeySequenceOptions;
14
- }
15
- /**
16
- * SolidJS primitive for registering multiple keyboard shortcut sequences at once (Vim-style).
17
- *
18
- * Uses the singleton SequenceManager. Accepts a dynamic array of definitions, or accessors,
19
- * so you can react to variable-length lists.
20
- *
21
- * Options are merged in this order:
22
- * HotkeysProvider defaults < commonOptions < per-definition options
23
- *
24
- * Definitions with an empty `sequence` are skipped (no registration).
25
- *
26
- * @param sequences - Array of sequence definitions, or accessor returning them
27
- * @param commonOptions - Shared options for all sequences, or accessor
28
- *
29
- * @example
30
- * ```tsx
31
- * function VimPalette() {
32
- * createHotkeySequences([
33
- * { sequence: ['G', 'G'], callback: () => scrollToTop() },
34
- * { sequence: ['D', 'D'], callback: () => deleteLine() },
35
- * ])
36
- * }
37
- * ```
38
- *
39
- * @example
40
- * ```tsx
41
- * function Dynamic(props) {
42
- * createHotkeySequences(
43
- * () => props.items.map((item) => ({
44
- * sequence: item.chords,
45
- * callback: item.action,
46
- * options: { enabled: item.enabled },
47
- * })),
48
- * { preventDefault: true },
49
- * )
50
- * }
51
- * ```
52
- */
53
- export declare function createHotkeySequences(sequences: Array<CreateHotkeySequenceDefinition> | (() => Array<CreateHotkeySequenceDefinition>), commonOptions?: CreateHotkeySequenceOptions | (() => CreateHotkeySequenceOptions)): void;
54
- //#endregion
@@ -1,111 +0,0 @@
1
- import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
- import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
3
- import { createEffect, onCleanup, untrack } 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 = useDefaultHotkeysOptionsSource();
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
- untrack(() => {
80
- for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
81
- if (record.handle.isActive) record.handle.unregister();
82
- registrations.delete(key);
83
- }
84
- for (const p of prepared) {
85
- const existing = registrations.get(p.registrationKey);
86
- if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
87
- existing.handle.callback = p.def.callback;
88
- const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
89
- existing.handle.setOptions(optionsWithoutTarget);
90
- continue;
91
- }
92
- if (existing) {
93
- if (existing.handle.isActive) existing.handle.unregister();
94
- registrations.delete(p.registrationKey);
95
- }
96
- const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
97
- const handle = manager.register(p.def.sequence, p.def.callback, {
98
- ...optionsWithoutTarget,
99
- target: p.resolvedTarget
100
- });
101
- registrations.set(p.registrationKey, {
102
- handle,
103
- target: p.resolvedTarget
104
- });
105
- }
106
- });
107
- });
108
- }
109
-
110
- //#endregion
111
- export { createHotkeySequences };