@tanstack/svelte-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/createHotkeyRecorder.svelte.js +22 -35
- package/dist/createHotkeySequenceRecorder.svelte.js +28 -41
- package/dist/getHeldKeyCodesMap.svelte.js +4 -13
- package/dist/getHeldKeys.svelte.js +4 -13
- package/dist/getHotkeyRegistrations.svelte.js +8 -17
- package/dist/getIsKeyHeld.svelte.js +6 -19
- package/package.json +12 -10
- package/src/HotkeysCtx.ts +0 -61
- package/src/HotkeysProvider.svelte +0 -12
- package/src/createHotkey.svelte.ts +0 -179
- package/src/createHotkeyRecorder.svelte.ts +0 -121
- package/src/createHotkeySequence.svelte.ts +0 -207
- package/src/createHotkeySequenceRecorder.svelte.ts +0 -97
- package/src/createHotkeySequences.svelte.ts +0 -274
- package/src/createHotkeys.svelte.ts +0 -276
- package/src/getHeldKeyCodesMap.svelte.ts +0 -44
- package/src/getHeldKeys.svelte.ts +0 -40
- package/src/getHotkeyHint.svelte.ts +0 -51
- package/src/getHotkeyRegistrations.svelte.ts +0 -60
- package/src/getIsKeyHeld.svelte.ts +0 -68
- package/src/index.ts +0 -22
- package/src/internal.svelte.ts +0 -26
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _SvelteHotkeyRecorderState_recorder, _SvelteHotkeyRecorderState_subscribe;
|
|
13
1
|
import { HotkeyRecorder } from '@tanstack/hotkeys';
|
|
14
2
|
import { onDestroy } from 'svelte';
|
|
15
3
|
import { getDefaultHotkeysOptions } from './HotkeysCtx';
|
|
@@ -51,40 +39,39 @@ import { createStoreSubscriber, resolveMaybeGetter } from './internal.svelte';
|
|
|
51
39
|
* ```
|
|
52
40
|
*/
|
|
53
41
|
class SvelteHotkeyRecorderState {
|
|
42
|
+
#recorder;
|
|
43
|
+
#subscribe;
|
|
54
44
|
constructor(options) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
58
|
-
this.startRecording = () => {
|
|
59
|
-
__classPrivateFieldGet(this, _SvelteHotkeyRecorderState_recorder, "f").start();
|
|
60
|
-
};
|
|
61
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
62
|
-
this.stopRecording = () => {
|
|
63
|
-
__classPrivateFieldGet(this, _SvelteHotkeyRecorderState_recorder, "f").stop();
|
|
64
|
-
};
|
|
65
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
66
|
-
this.cancelRecording = () => {
|
|
67
|
-
__classPrivateFieldGet(this, _SvelteHotkeyRecorderState_recorder, "f").cancel();
|
|
68
|
-
};
|
|
69
|
-
__classPrivateFieldSet(this, _SvelteHotkeyRecorderState_recorder, new HotkeyRecorder(options), "f");
|
|
70
|
-
__classPrivateFieldSet(this, _SvelteHotkeyRecorderState_subscribe, createStoreSubscriber(__classPrivateFieldGet(this, _SvelteHotkeyRecorderState_recorder, "f").store), "f");
|
|
45
|
+
this.#recorder = new HotkeyRecorder(options);
|
|
46
|
+
this.#subscribe = createStoreSubscriber(this.#recorder.store);
|
|
71
47
|
}
|
|
72
48
|
get isRecording() {
|
|
73
|
-
|
|
74
|
-
return
|
|
49
|
+
this.#subscribe();
|
|
50
|
+
return this.#recorder.store.state.isRecording;
|
|
75
51
|
}
|
|
76
52
|
get recordedHotkey() {
|
|
77
|
-
|
|
78
|
-
return
|
|
53
|
+
this.#subscribe();
|
|
54
|
+
return this.#recorder.store.state.recordedHotkey;
|
|
79
55
|
}
|
|
80
56
|
setOptions(options) {
|
|
81
|
-
|
|
57
|
+
this.#recorder.setOptions(options);
|
|
82
58
|
}
|
|
59
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
60
|
+
startRecording = () => {
|
|
61
|
+
this.#recorder.start();
|
|
62
|
+
};
|
|
63
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
64
|
+
stopRecording = () => {
|
|
65
|
+
this.#recorder.stop();
|
|
66
|
+
};
|
|
67
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
68
|
+
cancelRecording = () => {
|
|
69
|
+
this.#recorder.cancel();
|
|
70
|
+
};
|
|
83
71
|
destroy() {
|
|
84
|
-
|
|
72
|
+
this.#recorder.destroy();
|
|
85
73
|
}
|
|
86
74
|
}
|
|
87
|
-
_SvelteHotkeyRecorderState_recorder = new WeakMap(), _SvelteHotkeyRecorderState_subscribe = new WeakMap();
|
|
88
75
|
export function createHotkeyRecorder(options) {
|
|
89
76
|
const recorder = new SvelteHotkeyRecorderState({
|
|
90
77
|
...getDefaultHotkeysOptions().hotkeyRecorder,
|
|
@@ -1,62 +1,49 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _SvelteHotkeySequenceRecorderState_recorder, _SvelteHotkeySequenceRecorderState_subscribe;
|
|
13
1
|
import { HotkeySequenceRecorder } from '@tanstack/hotkeys';
|
|
14
2
|
import { onDestroy } from 'svelte';
|
|
15
3
|
import { getDefaultHotkeysOptions } from './HotkeysCtx';
|
|
16
4
|
import { createStoreSubscriber, resolveMaybeGetter } from './internal.svelte';
|
|
17
5
|
class SvelteHotkeySequenceRecorderState {
|
|
6
|
+
#recorder;
|
|
7
|
+
#subscribe;
|
|
18
8
|
constructor(options) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
22
|
-
this.startRecording = () => {
|
|
23
|
-
__classPrivateFieldGet(this, _SvelteHotkeySequenceRecorderState_recorder, "f").start();
|
|
24
|
-
};
|
|
25
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
26
|
-
this.stopRecording = () => {
|
|
27
|
-
__classPrivateFieldGet(this, _SvelteHotkeySequenceRecorderState_recorder, "f").stop();
|
|
28
|
-
};
|
|
29
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
30
|
-
this.cancelRecording = () => {
|
|
31
|
-
__classPrivateFieldGet(this, _SvelteHotkeySequenceRecorderState_recorder, "f").cancel();
|
|
32
|
-
};
|
|
33
|
-
// Keep the recorder receiver when used directly as an event handler.
|
|
34
|
-
this.commitRecording = () => {
|
|
35
|
-
__classPrivateFieldGet(this, _SvelteHotkeySequenceRecorderState_recorder, "f").commit();
|
|
36
|
-
};
|
|
37
|
-
__classPrivateFieldSet(this, _SvelteHotkeySequenceRecorderState_recorder, new HotkeySequenceRecorder(options), "f");
|
|
38
|
-
__classPrivateFieldSet(this, _SvelteHotkeySequenceRecorderState_subscribe, createStoreSubscriber(__classPrivateFieldGet(this, _SvelteHotkeySequenceRecorderState_recorder, "f").store), "f");
|
|
9
|
+
this.#recorder = new HotkeySequenceRecorder(options);
|
|
10
|
+
this.#subscribe = createStoreSubscriber(this.#recorder.store);
|
|
39
11
|
}
|
|
40
12
|
get isRecording() {
|
|
41
|
-
|
|
42
|
-
return
|
|
13
|
+
this.#subscribe();
|
|
14
|
+
return this.#recorder.store.state.isRecording;
|
|
43
15
|
}
|
|
44
16
|
get steps() {
|
|
45
|
-
|
|
46
|
-
return
|
|
17
|
+
this.#subscribe();
|
|
18
|
+
return this.#recorder.store.state.steps;
|
|
47
19
|
}
|
|
48
20
|
get recordedSequence() {
|
|
49
|
-
|
|
50
|
-
return
|
|
21
|
+
this.#subscribe();
|
|
22
|
+
return this.#recorder.store.state.recordedSequence;
|
|
51
23
|
}
|
|
52
24
|
setOptions(options) {
|
|
53
|
-
|
|
25
|
+
this.#recorder.setOptions(options);
|
|
54
26
|
}
|
|
27
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
28
|
+
startRecording = () => {
|
|
29
|
+
this.#recorder.start();
|
|
30
|
+
};
|
|
31
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
32
|
+
stopRecording = () => {
|
|
33
|
+
this.#recorder.stop();
|
|
34
|
+
};
|
|
35
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
36
|
+
cancelRecording = () => {
|
|
37
|
+
this.#recorder.cancel();
|
|
38
|
+
};
|
|
39
|
+
// Keep the recorder receiver when used directly as an event handler.
|
|
40
|
+
commitRecording = () => {
|
|
41
|
+
this.#recorder.commit();
|
|
42
|
+
};
|
|
55
43
|
destroy() {
|
|
56
|
-
|
|
44
|
+
this.#recorder.destroy();
|
|
57
45
|
}
|
|
58
46
|
}
|
|
59
|
-
_SvelteHotkeySequenceRecorderState_recorder = new WeakMap(), _SvelteHotkeySequenceRecorderState_subscribe = new WeakMap();
|
|
60
47
|
/**
|
|
61
48
|
* Svelte helper for recording multi-chord sequences (Vim-style shortcuts).
|
|
62
49
|
*/
|
|
@@ -1,22 +1,13 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var _HeldKeyCodesMapState_tracker, _HeldKeyCodesMapState_subscribe;
|
|
7
1
|
import { getKeyStateTracker } from '@tanstack/hotkeys';
|
|
8
2
|
import { createStoreSubscriber } from './internal.svelte';
|
|
9
3
|
class HeldKeyCodesMapState {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
_HeldKeyCodesMapState_subscribe.set(this, createStoreSubscriber(__classPrivateFieldGet(this, _HeldKeyCodesMapState_tracker, "f").store));
|
|
13
|
-
}
|
|
4
|
+
#tracker = getKeyStateTracker();
|
|
5
|
+
#subscribe = createStoreSubscriber(this.#tracker.store);
|
|
14
6
|
get codes() {
|
|
15
|
-
|
|
16
|
-
return
|
|
7
|
+
this.#subscribe();
|
|
8
|
+
return this.#tracker.store.state.heldCodes;
|
|
17
9
|
}
|
|
18
10
|
}
|
|
19
|
-
_HeldKeyCodesMapState_tracker = new WeakMap(), _HeldKeyCodesMapState_subscribe = new WeakMap();
|
|
20
11
|
/**
|
|
21
12
|
* Svelte function that returns reactive access to the map of currently held key names
|
|
22
13
|
* to their physical `event.code` values.
|
|
@@ -1,22 +1,13 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var _HeldKeysState_tracker, _HeldKeysState_subscribe;
|
|
7
1
|
import { getKeyStateTracker } from '@tanstack/hotkeys';
|
|
8
2
|
import { createStoreSubscriber } from './internal.svelte';
|
|
9
3
|
class HeldKeysState {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
_HeldKeysState_subscribe.set(this, createStoreSubscriber(__classPrivateFieldGet(this, _HeldKeysState_tracker, "f").store));
|
|
13
|
-
}
|
|
4
|
+
#tracker = getKeyStateTracker();
|
|
5
|
+
#subscribe = createStoreSubscriber(this.#tracker.store);
|
|
14
6
|
get keys() {
|
|
15
|
-
|
|
16
|
-
return
|
|
7
|
+
this.#subscribe();
|
|
8
|
+
return this.#tracker.store.state.heldKeys;
|
|
17
9
|
}
|
|
18
10
|
}
|
|
19
|
-
_HeldKeysState_tracker = new WeakMap(), _HeldKeysState_subscribe = new WeakMap();
|
|
20
11
|
/**
|
|
21
12
|
* Svelte function that returns reactive access to currently held keyboard keys.
|
|
22
13
|
*
|
|
@@ -1,28 +1,19 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var _HotkeyRegistrationsState_hotkeyManager, _HotkeyRegistrationsState_sequenceManager, _HotkeyRegistrationsState_subscribeHotkeys, _HotkeyRegistrationsState_subscribeSequences;
|
|
7
1
|
import { getHotkeyManager, getSequenceManager, toHotkeyRegistrationView, } from '@tanstack/hotkeys';
|
|
8
2
|
import { createStoreSubscriber } from './internal.svelte';
|
|
9
3
|
class HotkeyRegistrationsState {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
_HotkeyRegistrationsState_subscribeSequences.set(this, createStoreSubscriber(__classPrivateFieldGet(this, _HotkeyRegistrationsState_sequenceManager, "f").registrations));
|
|
15
|
-
}
|
|
4
|
+
#hotkeyManager = getHotkeyManager();
|
|
5
|
+
#sequenceManager = getSequenceManager();
|
|
6
|
+
#subscribeHotkeys = createStoreSubscriber(this.#hotkeyManager.registrations);
|
|
7
|
+
#subscribeSequences = createStoreSubscriber(this.#sequenceManager.registrations);
|
|
16
8
|
get hotkeys() {
|
|
17
|
-
|
|
18
|
-
return Array.from(
|
|
9
|
+
this.#subscribeHotkeys();
|
|
10
|
+
return Array.from(this.#hotkeyManager.registrations.state.values()).map(toHotkeyRegistrationView);
|
|
19
11
|
}
|
|
20
12
|
get sequences() {
|
|
21
|
-
|
|
22
|
-
return Array.from(
|
|
13
|
+
this.#subscribeSequences();
|
|
14
|
+
return Array.from(this.#sequenceManager.registrations.state.values());
|
|
23
15
|
}
|
|
24
16
|
}
|
|
25
|
-
_HotkeyRegistrationsState_hotkeyManager = new WeakMap(), _HotkeyRegistrationsState_sequenceManager = new WeakMap(), _HotkeyRegistrationsState_subscribeHotkeys = new WeakMap(), _HotkeyRegistrationsState_subscribeSequences = new WeakMap();
|
|
26
17
|
/**
|
|
27
18
|
* Svelte function that returns reactive access to all hotkey and sequence
|
|
28
19
|
* registrations from the singleton managers.
|
|
@@ -1,30 +1,17 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _HeldKeyState_tracker, _HeldKeyState_normalizedKey, _HeldKeyState_subscribe;
|
|
13
1
|
import { getKeyStateTracker } from '@tanstack/hotkeys';
|
|
14
2
|
import { createStoreSubscriber } from './internal.svelte';
|
|
15
3
|
class HeldKeyState {
|
|
4
|
+
#tracker = getKeyStateTracker();
|
|
5
|
+
#normalizedKey;
|
|
6
|
+
#subscribe = createStoreSubscriber(this.#tracker.store);
|
|
16
7
|
constructor(key) {
|
|
17
|
-
|
|
18
|
-
_HeldKeyState_normalizedKey.set(this, void 0);
|
|
19
|
-
_HeldKeyState_subscribe.set(this, createStoreSubscriber(__classPrivateFieldGet(this, _HeldKeyState_tracker, "f").store));
|
|
20
|
-
__classPrivateFieldSet(this, _HeldKeyState_normalizedKey, key.toLowerCase(), "f");
|
|
8
|
+
this.#normalizedKey = key.toLowerCase();
|
|
21
9
|
}
|
|
22
10
|
get held() {
|
|
23
|
-
|
|
24
|
-
return
|
|
11
|
+
this.#subscribe();
|
|
12
|
+
return this.#tracker.store.state.heldKeys.some((heldKey) => heldKey.toLowerCase() === this.#normalizedKey);
|
|
25
13
|
}
|
|
26
14
|
}
|
|
27
|
-
_HeldKeyState_tracker = new WeakMap(), _HeldKeyState_normalizedKey = new WeakMap(), _HeldKeyState_subscribe = new WeakMap();
|
|
28
15
|
/**
|
|
29
16
|
* Svelte function that returns reactive access to whether a specific key is currently being held.
|
|
30
17
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "Svelte adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Kunal Rao",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"shortcuts"
|
|
24
24
|
],
|
|
25
25
|
"type": "module",
|
|
26
|
-
"types": "dist/index.d.ts",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
27
|
"module": "dist/index.js",
|
|
28
28
|
"svelte": "./dist/index.js",
|
|
29
29
|
"exports": {
|
|
@@ -36,29 +36,31 @@
|
|
|
36
36
|
},
|
|
37
37
|
"sideEffects": false,
|
|
38
38
|
"files": [
|
|
39
|
-
"dist"
|
|
40
|
-
"src"
|
|
39
|
+
"dist"
|
|
41
40
|
],
|
|
42
41
|
"dependencies": {
|
|
43
|
-
"@tanstack/hotkeys": "0.
|
|
42
|
+
"@tanstack/hotkeys": "0.10.1"
|
|
44
43
|
},
|
|
45
44
|
"devDependencies": {
|
|
46
|
-
"@sveltejs/package": "^2.5.
|
|
45
|
+
"@sveltejs/package": "^2.5.8",
|
|
47
46
|
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
48
|
-
"@typescript-eslint/parser": "^8.
|
|
49
|
-
"eslint-plugin-svelte": "^3.
|
|
50
|
-
"svelte": "^5.
|
|
47
|
+
"@typescript-eslint/parser": "^8.70.1",
|
|
48
|
+
"eslint-plugin-svelte": "^3.23.0",
|
|
49
|
+
"svelte": "^5.57.1"
|
|
51
50
|
},
|
|
52
51
|
"peerDependencies": {
|
|
53
52
|
"svelte": "^5.25.0"
|
|
54
53
|
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=20"
|
|
56
|
+
},
|
|
55
57
|
"scripts": {
|
|
56
58
|
"clean": "premove ./dist ./coverage",
|
|
57
59
|
"test:eslint": "eslint ./src",
|
|
58
60
|
"test:lib": "vitest --passWithNoTests",
|
|
59
61
|
"test:lib:dev": "pnpm test:lib --watch",
|
|
60
62
|
"test:types": "tsc",
|
|
61
|
-
"test:build": "publint --strict",
|
|
63
|
+
"test:build": "publint --strict && node ../../scripts/verify-package.ts",
|
|
62
64
|
"build": "svelte-package --input ./src --output ./dist"
|
|
63
65
|
}
|
|
64
66
|
}
|
package/src/HotkeysCtx.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'svelte'
|
|
2
|
-
import type {
|
|
3
|
-
HotkeyRecorderOptions,
|
|
4
|
-
HotkeySequenceRecorderOptions,
|
|
5
|
-
} from '@tanstack/hotkeys'
|
|
6
|
-
import type { CreateHotkeyOptions } from './createHotkey.svelte'
|
|
7
|
-
import type { Snippet } from 'svelte'
|
|
8
|
-
import type { CreateHotkeySequenceOptions } from './createHotkeySequence.svelte'
|
|
9
|
-
|
|
10
|
-
export interface HotkeysProviderOptions {
|
|
11
|
-
hotkey?: Partial<CreateHotkeyOptions>
|
|
12
|
-
hotkeyRecorder?: Partial<HotkeyRecorderOptions>
|
|
13
|
-
hotkeySequenceRecorder?: Partial<HotkeySequenceRecorderOptions>
|
|
14
|
-
hotkeySequence?: Partial<CreateHotkeySequenceOptions>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface HotkeysProviderProps {
|
|
18
|
-
children: Snippet
|
|
19
|
-
defaultOptions?: HotkeysProviderOptions
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const DEFAULT_OPTIONS: HotkeysProviderOptions = {}
|
|
23
|
-
|
|
24
|
-
interface HotkeysContextValue {
|
|
25
|
-
defaultOptions: HotkeysProviderOptions
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const [useHotkeysContext, setHotkeysContextValue] =
|
|
29
|
-
createContext<HotkeysContextValue | null>()
|
|
30
|
-
|
|
31
|
-
export function setHotkeysContext(
|
|
32
|
-
defaultOptions: HotkeysProviderOptions = DEFAULT_OPTIONS,
|
|
33
|
-
): HotkeysContextValue {
|
|
34
|
-
return setHotkeysContextValue({
|
|
35
|
-
get defaultOptions() {
|
|
36
|
-
return defaultOptions
|
|
37
|
-
},
|
|
38
|
-
})!
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function setHotkeysContextSource(
|
|
42
|
-
defaultOptions: () => HotkeysProviderOptions,
|
|
43
|
-
): HotkeysContextValue {
|
|
44
|
-
return setHotkeysContextValue({
|
|
45
|
-
get defaultOptions() {
|
|
46
|
-
return defaultOptions()
|
|
47
|
-
},
|
|
48
|
-
})!
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function getHotkeysContext(): HotkeysContextValue | null {
|
|
52
|
-
try {
|
|
53
|
-
return useHotkeysContext()
|
|
54
|
-
} catch {
|
|
55
|
-
return null
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function getDefaultHotkeysOptions(): HotkeysProviderOptions {
|
|
60
|
-
return getHotkeysContext()?.defaultOptions ?? DEFAULT_OPTIONS
|
|
61
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { DEFAULT_OPTIONS, setHotkeysContextSource } from './HotkeysCtx'
|
|
3
|
-
|
|
4
|
-
import type { HotkeysProviderProps } from './HotkeysCtx'
|
|
5
|
-
|
|
6
|
-
let { children, defaultOptions = DEFAULT_OPTIONS }: HotkeysProviderProps =
|
|
7
|
-
$props()
|
|
8
|
-
|
|
9
|
-
setHotkeysContextSource(() => defaultOptions)
|
|
10
|
-
</script>
|
|
11
|
-
|
|
12
|
-
{@render children()}
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
detectPlatform,
|
|
3
|
-
getHotkeyManager,
|
|
4
|
-
normalizeRegisterableHotkey,
|
|
5
|
-
} from '@tanstack/hotkeys'
|
|
6
|
-
import { onDestroy } from 'svelte'
|
|
7
|
-
import { getDefaultHotkeysOptions } from './HotkeysCtx'
|
|
8
|
-
import { resolveMaybeGetter } from './internal.svelte'
|
|
9
|
-
import type {
|
|
10
|
-
Hotkey,
|
|
11
|
-
HotkeyCallback,
|
|
12
|
-
HotkeyOptions,
|
|
13
|
-
HotkeyRegistrationHandle,
|
|
14
|
-
RegisterableHotkey,
|
|
15
|
-
} from '@tanstack/hotkeys'
|
|
16
|
-
import type { MaybeGetter } from './internal.svelte'
|
|
17
|
-
import type { Attachment } from 'svelte/attachments'
|
|
18
|
-
|
|
19
|
-
export interface CreateHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
|
|
20
|
-
target?: Document | Window // not html elements, use attachment instead
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function registerHotkey(
|
|
24
|
-
target: HTMLElement | Document | Window,
|
|
25
|
-
hotkey: RegisterableHotkey,
|
|
26
|
-
callback: HotkeyCallback,
|
|
27
|
-
mergedOptions: CreateHotkeyOptions,
|
|
28
|
-
) {
|
|
29
|
-
return getHotkeyManager().register(
|
|
30
|
-
normalizeRegisterableHotkey(
|
|
31
|
-
hotkey,
|
|
32
|
-
mergedOptions.platform ?? detectPlatform(),
|
|
33
|
-
),
|
|
34
|
-
callback,
|
|
35
|
-
{
|
|
36
|
-
...mergedOptions,
|
|
37
|
-
target,
|
|
38
|
-
},
|
|
39
|
-
)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Register a global hotkey for the current component.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```svelte
|
|
47
|
-
* <script lang="ts">
|
|
48
|
-
* import { createHotkey } from '@tanstack/svelte-hotkeys'
|
|
49
|
-
*
|
|
50
|
-
* createHotkey('Mod+S', () => {
|
|
51
|
-
* console.log('Mod+S pressed')
|
|
52
|
-
* })
|
|
53
|
-
* </script>
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export function createHotkey(
|
|
57
|
-
hotkey: MaybeGetter<RegisterableHotkey>,
|
|
58
|
-
callback: HotkeyCallback,
|
|
59
|
-
options: MaybeGetter<CreateHotkeyOptions> = {},
|
|
60
|
-
): void {
|
|
61
|
-
let registration: HotkeyRegistrationHandle | null = null
|
|
62
|
-
let lastHotkeyStr: Hotkey | null = null
|
|
63
|
-
|
|
64
|
-
$effect(() => {
|
|
65
|
-
if (typeof document === 'undefined') {
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const resolvedHotkey = resolveMaybeGetter(hotkey)
|
|
70
|
-
const resolvedOptions = resolveMaybeGetter(options)
|
|
71
|
-
const mergedOptions = {
|
|
72
|
-
...getDefaultHotkeysOptions().hotkey,
|
|
73
|
-
...resolvedOptions,
|
|
74
|
-
} as CreateHotkeyOptions
|
|
75
|
-
|
|
76
|
-
const hotkeyStr = normalizeRegisterableHotkey(
|
|
77
|
-
resolvedHotkey,
|
|
78
|
-
mergedOptions.platform ?? detectPlatform(),
|
|
79
|
-
)
|
|
80
|
-
const { target: _t, ...optionsWithoutTarget } = mergedOptions
|
|
81
|
-
|
|
82
|
-
if (registration?.isActive && lastHotkeyStr === hotkeyStr) {
|
|
83
|
-
registration.callback = callback
|
|
84
|
-
registration.setOptions(optionsWithoutTarget)
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (registration?.isActive) {
|
|
89
|
-
registration.unregister()
|
|
90
|
-
registration = null
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
registration = registerHotkey(
|
|
94
|
-
document,
|
|
95
|
-
resolvedHotkey,
|
|
96
|
-
callback,
|
|
97
|
-
mergedOptions,
|
|
98
|
-
)
|
|
99
|
-
lastHotkeyStr = hotkeyStr
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
onDestroy(() => {
|
|
103
|
-
if (registration?.isActive) {
|
|
104
|
-
registration.unregister()
|
|
105
|
-
registration = null
|
|
106
|
-
}
|
|
107
|
-
})
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Create an attachment for element-scoped hotkeys.
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```svelte
|
|
115
|
-
* <script lang="ts">
|
|
116
|
-
* import { createHotkeyAttachment } from '@tanstack/svelte-hotkeys'
|
|
117
|
-
*
|
|
118
|
-
* let count = $state(0)
|
|
119
|
-
* const saveHotkey = createHotkeyAttachment('Mod+S', () => {
|
|
120
|
-
* count++
|
|
121
|
-
* })
|
|
122
|
-
*
|
|
123
|
-
* </script>
|
|
124
|
-
*
|
|
125
|
-
* <div tabindex="0" {@attach saveHotkey}>
|
|
126
|
-
* Count: {count}
|
|
127
|
-
* </div>
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
export function createHotkeyAttachment(
|
|
131
|
-
hotkey: MaybeGetter<RegisterableHotkey>,
|
|
132
|
-
callback: HotkeyCallback,
|
|
133
|
-
options: MaybeGetter<CreateHotkeyOptions> = {},
|
|
134
|
-
): Attachment<HTMLElement> {
|
|
135
|
-
return (element) => {
|
|
136
|
-
let registration: HotkeyRegistrationHandle | null = null
|
|
137
|
-
let lastHotkeyStr: Hotkey | null = null
|
|
138
|
-
|
|
139
|
-
$effect(() => {
|
|
140
|
-
const resolvedHotkey = resolveMaybeGetter(hotkey)
|
|
141
|
-
const resolvedOptions = resolveMaybeGetter(options)
|
|
142
|
-
const mergedOptions = {
|
|
143
|
-
...getDefaultHotkeysOptions().hotkey,
|
|
144
|
-
...resolvedOptions,
|
|
145
|
-
} as CreateHotkeyOptions
|
|
146
|
-
|
|
147
|
-
const hotkeyStr = normalizeRegisterableHotkey(
|
|
148
|
-
resolvedHotkey,
|
|
149
|
-
mergedOptions.platform ?? detectPlatform(),
|
|
150
|
-
)
|
|
151
|
-
const { target: _t, ...optionsWithoutTarget } = mergedOptions
|
|
152
|
-
|
|
153
|
-
if (registration?.isActive && lastHotkeyStr === hotkeyStr) {
|
|
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 = registerHotkey(
|
|
165
|
-
element,
|
|
166
|
-
resolvedHotkey,
|
|
167
|
-
callback,
|
|
168
|
-
mergedOptions,
|
|
169
|
-
)
|
|
170
|
-
lastHotkeyStr = hotkeyStr
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
return () => {
|
|
174
|
-
if (registration?.isActive) {
|
|
175
|
-
registration.unregister()
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|