@tanstack/angular-hotkeys 0.12.1 → 0.14.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.
@@ -1,44 +0,0 @@
1
- import { InjectHotkeySequenceOptions } from "./injectHotkeySequence.js";
2
- import { HotkeyCallback, HotkeySequence } from "@tanstack/hotkeys";
3
- //#region src/injectHotkeySequences.d.ts
4
- /**
5
- * A single sequence definition for use with `injectHotkeySequences`.
6
- */
7
- export interface InjectHotkeySequenceDefinition {
8
- /** Array of hotkey strings that form the sequence */
9
- sequence: HotkeySequence | (() => 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?: InjectHotkeySequenceOptions | (() => InjectHotkeySequenceOptions);
14
- }
15
- /**
16
- * Angular inject-based API for registering multiple keyboard shortcut sequences at once (Vim-style).
17
- *
18
- * Uses the singleton SequenceManager. Call in an injection context (e.g. constructor).
19
- * Uses `effect()` to track reactive dependencies when definitions or options are getters.
20
- *
21
- * Options are merged in this order:
22
- * provideHotkeys defaults < commonOptions < per-definition options
23
- *
24
- * Definitions with an empty `sequence` are skipped. Disabled sequences (`enabled: false`)
25
- * remain registered so they stay visible in devtools; the core manager suppresses execution.
26
- *
27
- * @param sequences - Array of sequence definitions, or getter returning them
28
- * @param commonOptions - Shared options for all sequences, or getter
29
- *
30
- * @example
31
- * ```ts
32
- * @Component({ ... })
33
- * export class VimComponent {
34
- * constructor() {
35
- * injectHotkeySequences([
36
- * { sequence: ['G', 'G'], callback: () => this.goTop() },
37
- * { sequence: ['D', 'D'], callback: () => this.deleteLine() },
38
- * ])
39
- * }
40
- * }
41
- * ```
42
- */
43
- export declare function injectHotkeySequences(sequences: Array<InjectHotkeySequenceDefinition> | (() => Array<InjectHotkeySequenceDefinition>), commonOptions?: InjectHotkeySequenceOptions | (() => InjectHotkeySequenceOptions)): void;
44
- //#endregion
@@ -1,104 +0,0 @@
1
- import { injectDefaultHotkeysOptions } from "./hotkeys-provider.js";
2
- import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
3
- import { DestroyRef, effect, inject } from "@angular/core";
4
-
5
- //#region src/injectHotkeySequences.ts
6
- /**
7
- * Angular inject-based API for registering multiple keyboard shortcut sequences at once (Vim-style).
8
- *
9
- * Uses the singleton SequenceManager. Call in an injection context (e.g. constructor).
10
- * Uses `effect()` to track reactive dependencies when definitions or options are getters.
11
- *
12
- * Options are merged in this order:
13
- * provideHotkeys defaults < commonOptions < per-definition options
14
- *
15
- * Definitions with an empty `sequence` are skipped. Disabled sequences (`enabled: false`)
16
- * remain registered so they stay visible in devtools; the core manager suppresses execution.
17
- *
18
- * @param sequences - Array of sequence definitions, or getter returning them
19
- * @param commonOptions - Shared options for all sequences, or getter
20
- *
21
- * @example
22
- * ```ts
23
- * @Component({ ... })
24
- * export class VimComponent {
25
- * constructor() {
26
- * injectHotkeySequences([
27
- * { sequence: ['G', 'G'], callback: () => this.goTop() },
28
- * { sequence: ['D', 'D'], callback: () => this.deleteLine() },
29
- * ])
30
- * }
31
- * }
32
- * ```
33
- */
34
- function injectHotkeySequences(sequences, commonOptions = {}) {
35
- const defaultOptions = injectDefaultHotkeysOptions();
36
- const manager = getSequenceManager();
37
- const destroyRef = inject(DestroyRef);
38
- const registrations = /* @__PURE__ */ new Map();
39
- destroyRef.onDestroy(() => {
40
- for (const { handle } of registrations.values()) if (handle.isActive) handle.unregister();
41
- registrations.clear();
42
- });
43
- effect(() => {
44
- const resolvedSequences = typeof sequences === "function" ? sequences() : sequences;
45
- const resolvedCommonOptions = typeof commonOptions === "function" ? commonOptions() : commonOptions;
46
- const nextKeys = /* @__PURE__ */ new Set();
47
- const prepared = [];
48
- for (let i = 0; i < resolvedSequences.length; i++) {
49
- const def = resolvedSequences[i];
50
- const resolvedSequence = typeof def.sequence === "function" ? def.sequence() : def.sequence;
51
- const resolvedDefOptions = typeof def.options === "function" ? def.options() : def.options ?? {};
52
- const { enabled = true, ...sequenceOpts } = {
53
- ...defaultOptions.hotkeySequence,
54
- ...resolvedCommonOptions,
55
- ...resolvedDefOptions
56
- };
57
- if (resolvedSequence.length === 0) continue;
58
- const resolvedTarget = "target" in sequenceOpts ? sequenceOpts.target ?? null : typeof document !== "undefined" ? document : null;
59
- if (!resolvedTarget) continue;
60
- const registrationKey = `${i}:${formatHotkeySequence(resolvedSequence)}`;
61
- nextKeys.add(registrationKey);
62
- prepared.push({
63
- registrationKey,
64
- def,
65
- resolvedSequence,
66
- enabled,
67
- sequenceOpts,
68
- resolvedTarget
69
- });
70
- }
71
- for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
72
- if (record.handle.isActive) record.handle.unregister();
73
- registrations.delete(key);
74
- }
75
- for (const p of prepared) {
76
- const existing = registrations.get(p.registrationKey);
77
- if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
78
- existing.handle.callback = p.def.callback;
79
- const { target: _target, ...optionsWithoutTarget } = p.sequenceOpts;
80
- existing.handle.setOptions({
81
- ...optionsWithoutTarget,
82
- enabled: p.enabled
83
- });
84
- continue;
85
- }
86
- if (existing) {
87
- if (existing.handle.isActive) existing.handle.unregister();
88
- registrations.delete(p.registrationKey);
89
- }
90
- const handle = manager.register(p.resolvedSequence, p.def.callback, {
91
- ...p.sequenceOpts,
92
- enabled: p.enabled,
93
- target: p.resolvedTarget
94
- });
95
- registrations.set(p.registrationKey, {
96
- handle,
97
- target: p.resolvedTarget
98
- });
99
- }
100
- });
101
- }
102
-
103
- //#endregion
104
- export { injectHotkeySequences };