@tanstack/angular-hotkeys 0.5.1 → 0.7.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/dist/hotkeys-provider.cjs.map +1 -1
- package/dist/hotkeys-provider.d.cts +2 -1
- package/dist/hotkeys-provider.d.ts +2 -1
- package/dist/hotkeys-provider.js.map +1 -1
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/injectHotkey.cjs +36 -6
- package/dist/injectHotkey.cjs.map +1 -1
- package/dist/injectHotkey.d.cts +2 -0
- package/dist/injectHotkey.d.ts +2 -0
- package/dist/injectHotkey.js +37 -7
- package/dist/injectHotkey.js.map +1 -1
- package/dist/injectHotkeySequence.cjs +43 -11
- package/dist/injectHotkeySequence.cjs.map +1 -1
- package/dist/injectHotkeySequence.d.cts +9 -2
- package/dist/injectHotkeySequence.d.ts +9 -2
- package/dist/injectHotkeySequence.js +45 -13
- package/dist/injectHotkeySequence.js.map +1 -1
- package/dist/injectHotkeySequenceRecorder.cjs +58 -0
- package/dist/injectHotkeySequenceRecorder.cjs.map +1 -0
- package/dist/injectHotkeySequenceRecorder.d.cts +19 -0
- package/dist/injectHotkeySequenceRecorder.d.ts +19 -0
- package/dist/injectHotkeySequenceRecorder.js +58 -0
- package/dist/injectHotkeySequenceRecorder.js.map +1 -0
- package/dist/injectHotkeySequences.cjs +105 -0
- package/dist/injectHotkeySequences.cjs.map +1 -0
- package/dist/injectHotkeySequences.d.cts +47 -0
- package/dist/injectHotkeySequences.d.ts +47 -0
- package/dist/injectHotkeySequences.js +105 -0
- package/dist/injectHotkeySequences.js.map +1 -0
- package/package.json +2 -2
- package/src/hotkeys-provider.ts +5 -1
- package/src/index.ts +2 -0
- package/src/injectHotkey.ts +43 -11
- package/src/injectHotkeySequence.ts +66 -19
- package/src/injectHotkeySequenceRecorder.ts +103 -0
- package/src/injectHotkeySequences.ts +176 -0
package/src/injectHotkey.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { effect } from '@angular/core'
|
|
1
|
+
import { DestroyRef, effect, inject } from '@angular/core'
|
|
2
2
|
import {
|
|
3
3
|
detectPlatform,
|
|
4
4
|
formatHotkey,
|
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
Hotkey,
|
|
11
11
|
HotkeyCallback,
|
|
12
12
|
HotkeyOptions,
|
|
13
|
+
HotkeyRegistrationHandle,
|
|
13
14
|
RegisterableHotkey,
|
|
14
15
|
} from '@tanstack/hotkeys'
|
|
15
16
|
|
|
@@ -34,6 +35,8 @@ export interface InjectHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
|
|
|
34
35
|
* Call in an injection context (e.g. constructor or field initializer).
|
|
35
36
|
* Uses effect() to track reactive dependencies and update registration
|
|
36
37
|
* when options or the callback change.
|
|
38
|
+
* `enabled: false` keeps the registration (visible in devtools) and only suppresses firing; the same
|
|
39
|
+
* handle is updated instead of unregistering and re-registering when identity is unchanged.
|
|
37
40
|
*
|
|
38
41
|
* @param hotkey - The hotkey string (e.g. 'Mod+S', 'Escape') or getter function
|
|
39
42
|
* @param callback - The function to call when the hotkey is pressed
|
|
@@ -87,8 +90,22 @@ export function injectHotkey(
|
|
|
87
90
|
): void {
|
|
88
91
|
const defaultOptions = injectDefaultHotkeysOptions()
|
|
89
92
|
const manager = getHotkeyManager()
|
|
93
|
+
const destroyRef = inject(DestroyRef)
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
let registration: HotkeyRegistrationHandle | null = null
|
|
96
|
+
let lastHotkeyString: Hotkey | null = null
|
|
97
|
+
let lastTarget: HTMLElement | Document | Window | null = null
|
|
98
|
+
|
|
99
|
+
destroyRef.onDestroy(() => {
|
|
100
|
+
if (registration?.isActive) {
|
|
101
|
+
registration.unregister()
|
|
102
|
+
registration = null
|
|
103
|
+
}
|
|
104
|
+
lastHotkeyString = null
|
|
105
|
+
lastTarget = null
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
effect(() => {
|
|
92
109
|
// Resolve reactive values
|
|
93
110
|
const resolvedHotkey = typeof hotkey === 'function' ? hotkey() : hotkey
|
|
94
111
|
const resolvedOptions = typeof options === 'function' ? options() : options
|
|
@@ -117,29 +134,44 @@ export function injectHotkey(
|
|
|
117
134
|
: null
|
|
118
135
|
|
|
119
136
|
if (!resolvedTarget) {
|
|
137
|
+
if (registration?.isActive) {
|
|
138
|
+
registration.unregister()
|
|
139
|
+
registration = null
|
|
140
|
+
}
|
|
141
|
+
lastHotkeyString = null
|
|
142
|
+
lastTarget = null
|
|
120
143
|
return
|
|
121
144
|
}
|
|
122
145
|
|
|
123
146
|
// Extract options without target (target is handled separately)
|
|
124
147
|
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
125
148
|
|
|
126
|
-
|
|
127
|
-
|
|
149
|
+
if (
|
|
150
|
+
registration?.isActive &&
|
|
151
|
+
lastHotkeyString === hotkeyString &&
|
|
152
|
+
lastTarget === resolvedTarget
|
|
153
|
+
) {
|
|
154
|
+
registration.callback = callback
|
|
155
|
+
registration.setOptions(optionsWithoutTarget)
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (registration?.isActive) {
|
|
160
|
+
registration.unregister()
|
|
161
|
+
registration = null
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
registration = manager.register(hotkeyString, callback, {
|
|
128
165
|
...optionsWithoutTarget,
|
|
129
166
|
target: resolvedTarget,
|
|
130
167
|
})
|
|
131
168
|
|
|
132
|
-
// Update callback and options on every effect run
|
|
133
169
|
if (registration.isActive) {
|
|
134
170
|
registration.callback = callback
|
|
135
171
|
registration.setOptions(optionsWithoutTarget)
|
|
136
172
|
}
|
|
137
173
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (registration.isActive) {
|
|
141
|
-
registration.unregister()
|
|
142
|
-
}
|
|
143
|
-
})
|
|
174
|
+
lastHotkeyString = hotkeyString
|
|
175
|
+
lastTarget = resolvedTarget
|
|
144
176
|
})
|
|
145
177
|
}
|
|
@@ -1,18 +1,27 @@
|
|
|
1
|
-
import { effect } from '@angular/core'
|
|
2
|
-
import { getSequenceManager } from '@tanstack/hotkeys'
|
|
1
|
+
import { DestroyRef, effect, inject } from '@angular/core'
|
|
2
|
+
import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
|
|
3
3
|
import { injectDefaultHotkeysOptions } from './hotkeys-provider'
|
|
4
4
|
import type {
|
|
5
5
|
HotkeyCallback,
|
|
6
6
|
HotkeySequence,
|
|
7
7
|
SequenceOptions,
|
|
8
|
+
SequenceRegistrationHandle,
|
|
8
9
|
} from '@tanstack/hotkeys'
|
|
9
10
|
|
|
11
|
+
type SequenceTarget = Document | HTMLElement | Window
|
|
12
|
+
|
|
10
13
|
export interface InjectHotkeySequenceOptions extends Omit<
|
|
11
14
|
SequenceOptions,
|
|
12
|
-
'enabled'
|
|
15
|
+
'enabled' | 'target'
|
|
13
16
|
> {
|
|
14
17
|
/** Whether the sequence is enabled. Defaults to true. */
|
|
15
18
|
enabled?: boolean
|
|
19
|
+
/**
|
|
20
|
+
* The DOM element to attach the event listener to.
|
|
21
|
+
* Can be a direct DOM element, an accessor target, or null.
|
|
22
|
+
* Defaults to document when omitted.
|
|
23
|
+
*/
|
|
24
|
+
target?: HTMLElement | Document | Window | null
|
|
16
25
|
}
|
|
17
26
|
|
|
18
27
|
/**
|
|
@@ -28,7 +37,8 @@ export interface InjectHotkeySequenceOptions extends Omit<
|
|
|
28
37
|
*
|
|
29
38
|
* @param sequence - Array of hotkey strings that form the sequence (or getter function)
|
|
30
39
|
* @param callback - Function to call when the sequence is completed
|
|
31
|
-
* @param options - Options for the sequence behavior (or getter function)
|
|
40
|
+
* @param options - Options for the sequence behavior (or getter function). `enabled: false` still registers
|
|
41
|
+
* the sequence (visible in devtools); only execution is suppressed.
|
|
32
42
|
*
|
|
33
43
|
* @example
|
|
34
44
|
* ```ts
|
|
@@ -53,8 +63,23 @@ export function injectHotkeySequence(
|
|
|
53
63
|
| (() => InjectHotkeySequenceOptions) = {},
|
|
54
64
|
): void {
|
|
55
65
|
const defaultOptions = injectDefaultHotkeysOptions()
|
|
66
|
+
const manager = getSequenceManager()
|
|
67
|
+
const destroyRef = inject(DestroyRef)
|
|
68
|
+
|
|
69
|
+
let handle: SequenceRegistrationHandle | null = null
|
|
70
|
+
let lastSequenceKey: string | null = null
|
|
71
|
+
let lastTarget: SequenceTarget | null = null
|
|
56
72
|
|
|
57
|
-
|
|
73
|
+
destroyRef.onDestroy(() => {
|
|
74
|
+
if (handle?.isActive) {
|
|
75
|
+
handle.unregister()
|
|
76
|
+
handle = null
|
|
77
|
+
}
|
|
78
|
+
lastSequenceKey = null
|
|
79
|
+
lastTarget = null
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
effect(() => {
|
|
58
83
|
// Resolve reactive values
|
|
59
84
|
const resolvedSequence =
|
|
60
85
|
typeof sequence === 'function' ? sequence() : sequence
|
|
@@ -67,27 +92,49 @@ export function injectHotkeySequence(
|
|
|
67
92
|
|
|
68
93
|
const { enabled = true, ...sequenceOptions } = mergedOptions
|
|
69
94
|
|
|
70
|
-
|
|
95
|
+
const resolvedTarget =
|
|
96
|
+
'target' in sequenceOptions
|
|
97
|
+
? (sequenceOptions.target ?? null)
|
|
98
|
+
: typeof document !== 'undefined'
|
|
99
|
+
? document
|
|
100
|
+
: null
|
|
101
|
+
|
|
102
|
+
if (resolvedSequence.length === 0 || !resolvedTarget) {
|
|
103
|
+
if (handle?.isActive) {
|
|
104
|
+
handle.unregister()
|
|
105
|
+
handle = null
|
|
106
|
+
}
|
|
107
|
+
lastSequenceKey = null
|
|
108
|
+
lastTarget = null
|
|
71
109
|
return
|
|
72
110
|
}
|
|
73
111
|
|
|
74
|
-
const
|
|
112
|
+
const sequenceKey = formatHotkeySequence(resolvedSequence)
|
|
75
113
|
|
|
76
|
-
|
|
77
|
-
const registerOptions: SequenceOptions = {
|
|
114
|
+
const registerPayload: SequenceOptions = {
|
|
78
115
|
...sequenceOptions,
|
|
79
|
-
enabled
|
|
80
|
-
target:
|
|
81
|
-
sequenceOptions.target ??
|
|
82
|
-
(typeof document !== 'undefined' ? document : undefined),
|
|
116
|
+
enabled,
|
|
117
|
+
target: resolvedTarget,
|
|
83
118
|
}
|
|
119
|
+
const { target: _t, ...optionsWithoutTarget } = registerPayload
|
|
84
120
|
|
|
85
|
-
|
|
121
|
+
if (
|
|
122
|
+
handle?.isActive &&
|
|
123
|
+
lastSequenceKey === sequenceKey &&
|
|
124
|
+
lastTarget === resolvedTarget
|
|
125
|
+
) {
|
|
126
|
+
handle.callback = callback
|
|
127
|
+
handle.setOptions(optionsWithoutTarget)
|
|
128
|
+
return
|
|
129
|
+
}
|
|
86
130
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
131
|
+
if (handle?.isActive) {
|
|
132
|
+
handle.unregister()
|
|
133
|
+
handle = null
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
handle = manager.register(resolvedSequence, callback, registerPayload)
|
|
137
|
+
lastSequenceKey = sequenceKey
|
|
138
|
+
lastTarget = resolvedTarget
|
|
92
139
|
})
|
|
93
140
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DestroyRef,
|
|
3
|
+
computed,
|
|
4
|
+
effect,
|
|
5
|
+
inject,
|
|
6
|
+
linkedSignal,
|
|
7
|
+
untracked,
|
|
8
|
+
} from '@angular/core'
|
|
9
|
+
import { HotkeySequenceRecorder } from '@tanstack/hotkeys'
|
|
10
|
+
import { injectDefaultHotkeysOptions } from './hotkeys-provider'
|
|
11
|
+
import type { Atom, ReadonlyAtom } from '@tanstack/angular-store'
|
|
12
|
+
import type { Signal } from '@angular/core'
|
|
13
|
+
import type {
|
|
14
|
+
HotkeySequence,
|
|
15
|
+
HotkeySequenceRecorderOptions,
|
|
16
|
+
} from '@tanstack/hotkeys'
|
|
17
|
+
|
|
18
|
+
export interface AngularHotkeySequenceRecorder {
|
|
19
|
+
readonly isRecording: () => boolean
|
|
20
|
+
readonly steps: () => HotkeySequence
|
|
21
|
+
readonly recordedSequence: () => HotkeySequence | null
|
|
22
|
+
readonly startRecording: () => void
|
|
23
|
+
readonly stopRecording: () => void
|
|
24
|
+
readonly cancelRecording: () => void
|
|
25
|
+
readonly commitRecording: () => void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Angular inject-based API for recording multi-chord sequences.
|
|
30
|
+
*/
|
|
31
|
+
export function injectHotkeySequenceRecorder(
|
|
32
|
+
options:
|
|
33
|
+
| HotkeySequenceRecorderOptions
|
|
34
|
+
| (() => HotkeySequenceRecorderOptions),
|
|
35
|
+
): AngularHotkeySequenceRecorder {
|
|
36
|
+
const defaultOptions = injectDefaultHotkeysOptions()
|
|
37
|
+
const destroyRef = inject(DestroyRef)
|
|
38
|
+
|
|
39
|
+
const recorderSignal = computed(() =>
|
|
40
|
+
untracked(() => {
|
|
41
|
+
const resolvedOptions =
|
|
42
|
+
typeof options === 'function' ? options() : options
|
|
43
|
+
|
|
44
|
+
const mergedOptions = {
|
|
45
|
+
...defaultOptions.hotkeySequenceRecorder,
|
|
46
|
+
...resolvedOptions,
|
|
47
|
+
} as HotkeySequenceRecorderOptions
|
|
48
|
+
|
|
49
|
+
return new HotkeySequenceRecorder(mergedOptions)
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
const recorderStore = computed(() => untracked(() => recorderSignal().store))
|
|
54
|
+
const isRecording = injectLazyStore(
|
|
55
|
+
recorderStore,
|
|
56
|
+
(state) => state.isRecording,
|
|
57
|
+
)
|
|
58
|
+
const steps = injectLazyStore(recorderStore, (state) => state.steps)
|
|
59
|
+
const recordedSequence = injectLazyStore(
|
|
60
|
+
recorderStore,
|
|
61
|
+
(state) => state.recordedSequence,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
effect(() => {
|
|
65
|
+
const resolved = typeof options === 'function' ? options() : options
|
|
66
|
+
recorderSignal().setOptions({
|
|
67
|
+
...defaultOptions.hotkeySequenceRecorder,
|
|
68
|
+
...resolved,
|
|
69
|
+
} as HotkeySequenceRecorderOptions)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
destroyRef.onDestroy(() => {
|
|
73
|
+
recorderSignal().destroy()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
isRecording,
|
|
78
|
+
steps,
|
|
79
|
+
recordedSequence,
|
|
80
|
+
startRecording: () => recorderSignal().start(),
|
|
81
|
+
stopRecording: () => recorderSignal().stop(),
|
|
82
|
+
cancelRecording: () => recorderSignal().cancel(),
|
|
83
|
+
commitRecording: () => recorderSignal().commit(),
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function injectLazyStore<TState, TSelected = NoInfer<TState>>(
|
|
88
|
+
storeSignal: Signal<Atom<TState> | ReadonlyAtom<TState>>,
|
|
89
|
+
selector: (state: NoInfer<TState>) => TSelected,
|
|
90
|
+
): Signal<TSelected> {
|
|
91
|
+
const slice = linkedSignal(() => selector(storeSignal().get()))
|
|
92
|
+
|
|
93
|
+
effect((onCleanup) => {
|
|
94
|
+
const currentStore = storeSignal()
|
|
95
|
+
slice.set(selector(currentStore.get()))
|
|
96
|
+
const { unsubscribe } = currentStore.subscribe((s) => {
|
|
97
|
+
slice.set(selector(s))
|
|
98
|
+
})
|
|
99
|
+
onCleanup(() => unsubscribe())
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
return slice.asReadonly()
|
|
103
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { DestroyRef, effect, inject } from '@angular/core'
|
|
2
|
+
import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
|
|
3
|
+
import { injectDefaultHotkeysOptions } from './hotkeys-provider'
|
|
4
|
+
import type { InjectHotkeySequenceOptions } from './injectHotkeySequence'
|
|
5
|
+
import type {
|
|
6
|
+
HotkeyCallback,
|
|
7
|
+
HotkeySequence,
|
|
8
|
+
SequenceRegistrationHandle,
|
|
9
|
+
} from '@tanstack/hotkeys'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A single sequence definition for use with `injectHotkeySequences`.
|
|
13
|
+
*/
|
|
14
|
+
export interface InjectHotkeySequenceDefinition {
|
|
15
|
+
/** Array of hotkey strings that form the sequence */
|
|
16
|
+
sequence: HotkeySequence | (() => HotkeySequence)
|
|
17
|
+
/** The function to call when the sequence is completed */
|
|
18
|
+
callback: HotkeyCallback
|
|
19
|
+
/** Per-sequence options (merged on top of commonOptions) */
|
|
20
|
+
options?: InjectHotkeySequenceOptions | (() => InjectHotkeySequenceOptions)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Angular inject-based API for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
25
|
+
*
|
|
26
|
+
* Uses the singleton SequenceManager. Call in an injection context (e.g. constructor).
|
|
27
|
+
* Uses `effect()` to track reactive dependencies when definitions or options are getters.
|
|
28
|
+
*
|
|
29
|
+
* Options are merged in this order:
|
|
30
|
+
* provideHotkeys defaults < commonOptions < per-definition options
|
|
31
|
+
*
|
|
32
|
+
* Definitions with an empty `sequence` are skipped. Disabled sequences (`enabled: false`)
|
|
33
|
+
* remain registered so they stay visible in devtools; the core manager suppresses execution.
|
|
34
|
+
*
|
|
35
|
+
* @param sequences - Array of sequence definitions, or getter returning them
|
|
36
|
+
* @param commonOptions - Shared options for all sequences, or getter
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* @Component({ ... })
|
|
41
|
+
* export class VimComponent {
|
|
42
|
+
* constructor() {
|
|
43
|
+
* injectHotkeySequences([
|
|
44
|
+
* { sequence: ['G', 'G'], callback: () => this.goTop() },
|
|
45
|
+
* { sequence: ['D', 'D'], callback: () => this.deleteLine() },
|
|
46
|
+
* ])
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function injectHotkeySequences(
|
|
52
|
+
sequences:
|
|
53
|
+
| Array<InjectHotkeySequenceDefinition>
|
|
54
|
+
| (() => Array<InjectHotkeySequenceDefinition>),
|
|
55
|
+
commonOptions:
|
|
56
|
+
| InjectHotkeySequenceOptions
|
|
57
|
+
| (() => InjectHotkeySequenceOptions) = {},
|
|
58
|
+
): void {
|
|
59
|
+
type RegistrationRecord = {
|
|
60
|
+
handle: SequenceRegistrationHandle
|
|
61
|
+
target: Document | HTMLElement | Window
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const defaultOptions = injectDefaultHotkeysOptions()
|
|
65
|
+
const manager = getSequenceManager()
|
|
66
|
+
const destroyRef = inject(DestroyRef)
|
|
67
|
+
|
|
68
|
+
const registrations = new Map<string, RegistrationRecord>()
|
|
69
|
+
|
|
70
|
+
destroyRef.onDestroy(() => {
|
|
71
|
+
for (const { handle } of registrations.values()) {
|
|
72
|
+
if (handle.isActive) {
|
|
73
|
+
handle.unregister()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
registrations.clear()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
effect(() => {
|
|
80
|
+
const resolvedSequences =
|
|
81
|
+
typeof sequences === 'function' ? sequences() : sequences
|
|
82
|
+
const resolvedCommonOptions =
|
|
83
|
+
typeof commonOptions === 'function' ? commonOptions() : commonOptions
|
|
84
|
+
|
|
85
|
+
const nextKeys = new Set<string>()
|
|
86
|
+
const prepared: Array<{
|
|
87
|
+
registrationKey: string
|
|
88
|
+
def: InjectHotkeySequenceDefinition
|
|
89
|
+
resolvedSequence: HotkeySequence
|
|
90
|
+
enabled: boolean
|
|
91
|
+
sequenceOpts: Omit<InjectHotkeySequenceOptions, 'enabled'>
|
|
92
|
+
resolvedTarget: Document | HTMLElement | Window
|
|
93
|
+
}> = []
|
|
94
|
+
|
|
95
|
+
for (let i = 0; i < resolvedSequences.length; i++) {
|
|
96
|
+
const def = resolvedSequences[i]!
|
|
97
|
+
const resolvedSequence =
|
|
98
|
+
typeof def.sequence === 'function' ? def.sequence() : def.sequence
|
|
99
|
+
const resolvedDefOptions =
|
|
100
|
+
typeof def.options === 'function' ? def.options() : (def.options ?? {})
|
|
101
|
+
|
|
102
|
+
const mergedOptions = {
|
|
103
|
+
...defaultOptions.hotkeySequence,
|
|
104
|
+
...resolvedCommonOptions,
|
|
105
|
+
...resolvedDefOptions,
|
|
106
|
+
} as InjectHotkeySequenceOptions
|
|
107
|
+
|
|
108
|
+
const { enabled = true, ...sequenceOpts } = mergedOptions
|
|
109
|
+
|
|
110
|
+
if (resolvedSequence.length === 0) {
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const resolvedTarget =
|
|
115
|
+
'target' in sequenceOpts
|
|
116
|
+
? (sequenceOpts.target ?? null)
|
|
117
|
+
: typeof document !== 'undefined'
|
|
118
|
+
? document
|
|
119
|
+
: null
|
|
120
|
+
|
|
121
|
+
if (!resolvedTarget) {
|
|
122
|
+
continue
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const registrationKey = `${i}:${formatHotkeySequence(resolvedSequence)}`
|
|
126
|
+
nextKeys.add(registrationKey)
|
|
127
|
+
prepared.push({
|
|
128
|
+
registrationKey,
|
|
129
|
+
def,
|
|
130
|
+
resolvedSequence,
|
|
131
|
+
enabled,
|
|
132
|
+
sequenceOpts,
|
|
133
|
+
resolvedTarget,
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (const [key, record] of [...registrations.entries()]) {
|
|
138
|
+
if (!nextKeys.has(key)) {
|
|
139
|
+
if (record.handle.isActive) {
|
|
140
|
+
record.handle.unregister()
|
|
141
|
+
}
|
|
142
|
+
registrations.delete(key)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (const p of prepared) {
|
|
147
|
+
const existing = registrations.get(p.registrationKey)
|
|
148
|
+
if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
|
|
149
|
+
existing.handle.callback = p.def.callback
|
|
150
|
+
const { target: _target, ...optionsWithoutTarget } = p.sequenceOpts
|
|
151
|
+
existing.handle.setOptions({
|
|
152
|
+
...optionsWithoutTarget,
|
|
153
|
+
enabled: p.enabled,
|
|
154
|
+
})
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (existing) {
|
|
159
|
+
if (existing.handle.isActive) {
|
|
160
|
+
existing.handle.unregister()
|
|
161
|
+
}
|
|
162
|
+
registrations.delete(p.registrationKey)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const handle = manager.register(p.resolvedSequence, p.def.callback, {
|
|
166
|
+
...p.sequenceOpts,
|
|
167
|
+
enabled: p.enabled,
|
|
168
|
+
target: p.resolvedTarget,
|
|
169
|
+
})
|
|
170
|
+
registrations.set(p.registrationKey, {
|
|
171
|
+
handle,
|
|
172
|
+
target: p.resolvedTarget,
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
}
|