@tanstack/svelte-hotkeys 0.22.0 → 0.22.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/HotkeysCtx.d.ts +3 -2
- package/dist/HotkeysCtx.js +29 -16
- package/dist/HotkeysProvider.svelte +1 -16
- package/dist/createHotkeys.svelte.js +4 -4
- package/dist/getAppHotkeys.svelte.js +16 -12
- package/dist/getHotkeyHint.svelte.js +17 -3
- package/dist/internal.svelte.d.ts +21 -4
- package/dist/internal.svelte.js +48 -5
- package/package.json +2 -2
package/dist/HotkeysCtx.d.ts
CHANGED
|
@@ -22,8 +22,9 @@ export interface HotkeysContextOptions extends HotkeysProviderScopeProps {
|
|
|
22
22
|
/** Merged with the parent context's defaults. */
|
|
23
23
|
defaultOptions?: HotkeysProviderOptions;
|
|
24
24
|
/**
|
|
25
|
-
* Every app hotkey, from `defineAppHotkeys`.
|
|
26
|
-
*
|
|
25
|
+
* Every app hotkey, from `defineAppHotkeys`. Listed by `getAppHotkeyList`
|
|
26
|
+
* whether or not they are registered. It can start as `undefined` and arrive
|
|
27
|
+
* later, such as from a lazy import.
|
|
27
28
|
*/
|
|
28
29
|
appHotkeys?: AppHotkeys;
|
|
29
30
|
/**
|
package/dist/HotkeysCtx.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { publishAppHotkeysStore } from '@tanstack/hotkeys';
|
|
2
1
|
import { createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults, } from '@tanstack/hotkeys/adapter';
|
|
3
|
-
import { createContext
|
|
4
|
-
import { activateScopeWhileMounted } from './internal.svelte.js';
|
|
2
|
+
import { createContext } from 'svelte';
|
|
3
|
+
import { activateScopeWhileMounted, syncAppHotkeysStore, } from './internal.svelte.js';
|
|
5
4
|
export const DEFAULT_OPTIONS = {};
|
|
6
5
|
const [useHotkeysContext, setHotkeysContextValue] = createContext();
|
|
7
6
|
/**
|
|
@@ -31,20 +30,27 @@ export function setHotkeysContext(options = {}) {
|
|
|
31
30
|
/** Sets the context from a getter, so default options stay reactive. */
|
|
32
31
|
export function setHotkeysContextSource(source) {
|
|
33
32
|
const parent = getHotkeysContext();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
let store = null;
|
|
34
|
+
// Created when app hotkeys first arrive. That can be after the context is
|
|
35
|
+
// set, such as when a lazy import loads the declarations. A reactive reader
|
|
36
|
+
// tracks the options through `source()`, so it re-runs when they arrive.
|
|
37
|
+
const ownStore = () => {
|
|
38
|
+
const { appHotkeys, customHotkeys, defaultOptions } = source();
|
|
39
|
+
if (!store && appHotkeys) {
|
|
40
|
+
store = createAppHotkeysStore(appHotkeys, {
|
|
41
|
+
customHotkeys,
|
|
42
|
+
// App hotkeys compare hotkeys, such as for conflicts, on this
|
|
43
|
+
// platform.
|
|
44
|
+
platform: defaultOptions?.platform ?? parent?.defaultOptions.platform,
|
|
45
|
+
onCustomHotkeysChange: (custom) => source().onCustomHotkeysChange?.(custom),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return store;
|
|
49
|
+
};
|
|
46
50
|
const context = setHotkeysContextValue({
|
|
47
|
-
|
|
51
|
+
get appHotkeysStore() {
|
|
52
|
+
return ownStore() ?? parent?.appHotkeysStore ?? null;
|
|
53
|
+
},
|
|
48
54
|
get defaultOptions() {
|
|
49
55
|
const { defaultOptions, scope } = source();
|
|
50
56
|
return mergeHotkeysProviderDefaults(parent?.defaultOptions, defaultOptions, scope);
|
|
@@ -53,6 +59,13 @@ export function setHotkeysContextSource(source) {
|
|
|
53
59
|
return (parent?.enabled ?? true) && source().enabled !== false;
|
|
54
60
|
},
|
|
55
61
|
});
|
|
62
|
+
// Only this context's own store: a nested context without app hotkeys
|
|
63
|
+
// reads its parent's store and never changes it.
|
|
64
|
+
syncAppHotkeysStore(ownStore, () => ({
|
|
65
|
+
appHotkeys: source().appHotkeys,
|
|
66
|
+
customHotkeys: source().customHotkeys,
|
|
67
|
+
platform: context.defaultOptions.platform,
|
|
68
|
+
}));
|
|
56
69
|
// Compared by value, so a new `exclusive` object does not reactivate the
|
|
57
70
|
// scope and lose its precedence.
|
|
58
71
|
activateScopeWhileMounted(() => {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
enabled,
|
|
14
14
|
}: HotkeysProviderProps = $props()
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
setHotkeysContextSource(() => ({
|
|
17
17
|
defaultOptions,
|
|
18
18
|
appHotkeys,
|
|
19
19
|
customHotkeys,
|
|
@@ -22,21 +22,6 @@
|
|
|
22
22
|
exclusive,
|
|
23
23
|
enabled,
|
|
24
24
|
}))
|
|
25
|
-
|
|
26
|
-
// Keep declarations current (for example after hot module replacement).
|
|
27
|
-
$effect(() => {
|
|
28
|
-
if (appHotkeys) context.appHotkeysStore?.setAppHotkeys(appHotkeys)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
// Apply the platform and saved custom hotkeys whose value changes, such as
|
|
32
|
-
// data that loads after the first render. Only a provider with appHotkeys
|
|
33
|
-
// owns a store.
|
|
34
|
-
$effect(() => {
|
|
35
|
-
const platform = context.defaultOptions.platform
|
|
36
|
-
if (appHotkeys) {
|
|
37
|
-
context.appHotkeysStore?.setOptions({ customHotkeys, platform })
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
25
|
</script>
|
|
41
26
|
|
|
42
27
|
{@render children()}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { createHotkeyBindings, prepareHotkeyDefinitions, } from '@tanstack/hotkeys/adapter';
|
|
2
2
|
import { getHotkeysContext } from './HotkeysCtx.js';
|
|
3
|
-
import {
|
|
3
|
+
import { createAppHotkeysSource, resolveMaybeGetter, } from './internal.svelte.js';
|
|
4
4
|
/**
|
|
5
5
|
* Registers definitions in an effect. With an element, every hotkey listens on
|
|
6
6
|
* that element; otherwise each uses its own target (document by default).
|
|
7
7
|
*/
|
|
8
8
|
function bindHotkeys(hotkeys, commonOptions, element) {
|
|
9
9
|
const context = getHotkeysContext();
|
|
10
|
-
const
|
|
10
|
+
const appHotkeys = createAppHotkeysSource(context);
|
|
11
11
|
const bindings = createHotkeyBindings({ afterDelegatedHandlers: true });
|
|
12
12
|
$effect(() => {
|
|
13
13
|
if (typeof document === 'undefined') {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
16
|
// Track app hotkeys so registrations move with custom hotkeys.
|
|
17
|
-
|
|
17
|
+
appHotkeys.subscribe();
|
|
18
18
|
const definitions = resolveMaybeGetter(hotkeys).map((definition) => ({
|
|
19
19
|
hotkey: resolveMaybeGetter(definition.hotkey),
|
|
20
20
|
callback: definition.callback,
|
|
@@ -23,7 +23,7 @@ function bindHotkeys(hotkeys, commonOptions, element) {
|
|
|
23
23
|
: undefined,
|
|
24
24
|
}));
|
|
25
25
|
bindings.update(prepareHotkeyDefinitions(definitions, {
|
|
26
|
-
appHotkeysStore:
|
|
26
|
+
appHotkeysStore: appHotkeys.store,
|
|
27
27
|
defaults: context?.defaultOptions.hotkey,
|
|
28
28
|
commonOptions: element
|
|
29
29
|
? { ...resolveMaybeGetter(commonOptions), target: element }
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { getHotkeysContext } from './HotkeysCtx.js';
|
|
2
2
|
import { getHotkeyRegistrations } from './getHotkeyRegistrations.svelte.js';
|
|
3
|
-
import {
|
|
3
|
+
import { createAppHotkeysSource } from './internal.svelte.js';
|
|
4
4
|
function requireStore(name) {
|
|
5
|
-
const
|
|
6
|
-
|
|
5
|
+
const source = createAppHotkeysSource(getHotkeysContext());
|
|
6
|
+
const initial = source.store;
|
|
7
|
+
if (!initial) {
|
|
7
8
|
throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with appHotkeys`);
|
|
8
9
|
}
|
|
9
10
|
return {
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
// The nearest provider's store, which changes if a nested provider gets
|
|
12
|
+
// its app hotkeys later.
|
|
13
|
+
store: () => (source.store ??
|
|
14
|
+
initial),
|
|
15
|
+
subscribe: source.subscribe,
|
|
12
16
|
};
|
|
13
17
|
}
|
|
14
18
|
/**
|
|
@@ -28,7 +32,7 @@ export function getAppHotkeys() {
|
|
|
28
32
|
return {
|
|
29
33
|
get current() {
|
|
30
34
|
subscribe();
|
|
31
|
-
return store.appHotkeys;
|
|
35
|
+
return store().appHotkeys;
|
|
32
36
|
},
|
|
33
37
|
};
|
|
34
38
|
}
|
|
@@ -43,7 +47,7 @@ export function getAppHotkeyList() {
|
|
|
43
47
|
return {
|
|
44
48
|
get current() {
|
|
45
49
|
subscribe();
|
|
46
|
-
return store.getList(registrations.hotkeys);
|
|
50
|
+
return store().getList(registrations.hotkeys);
|
|
47
51
|
},
|
|
48
52
|
};
|
|
49
53
|
}
|
|
@@ -57,11 +61,11 @@ export function getCustomHotkeys() {
|
|
|
57
61
|
return {
|
|
58
62
|
get current() {
|
|
59
63
|
subscribe();
|
|
60
|
-
return store.customHotkeys;
|
|
64
|
+
return store().customHotkeys;
|
|
61
65
|
},
|
|
62
|
-
setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
|
|
63
|
-
resetCustomHotkey: (id) => store.resetCustomHotkey(id),
|
|
64
|
-
resetCustomHotkeys: () => store.resetCustomHotkeys(),
|
|
65
|
-
findConflicts: (id, hotkey) => store.findConflicts(id, hotkey),
|
|
66
|
+
setCustomHotkey: (id, hotkey) => store().setCustomHotkey(id, hotkey),
|
|
67
|
+
resetCustomHotkey: (id) => store().resetCustomHotkey(id),
|
|
68
|
+
resetCustomHotkeys: () => store().resetCustomHotkeys(),
|
|
69
|
+
findConflicts: (id, hotkey) => store().findConflicts(id, hotkey),
|
|
66
70
|
};
|
|
67
71
|
}
|
|
@@ -14,12 +14,14 @@ import { resolveMaybeGetter } from './internal.svelte.js';
|
|
|
14
14
|
export function getHotkeyHint(hotkey, options = {}) {
|
|
15
15
|
const tracker = getKeyStateTracker();
|
|
16
16
|
const manager = getHotkeyManager();
|
|
17
|
-
const
|
|
17
|
+
const context = getHotkeysContext();
|
|
18
|
+
// Read each time: a provider creates its store when its app hotkeys arrive.
|
|
19
|
+
const getStore = () => context?.appHotkeysStore ?? null;
|
|
18
20
|
// Resolve current reactive arguments without caching a stale hotkey.
|
|
19
21
|
const read = () => {
|
|
20
22
|
const value = resolveMaybeGetter(hotkey);
|
|
21
23
|
const resolved = isAppHotkey(value)
|
|
22
|
-
? resolveAppHotkey(value,
|
|
24
|
+
? resolveAppHotkey(value, getStore()).hotkey
|
|
23
25
|
: value;
|
|
24
26
|
return (resolved !== null &&
|
|
25
27
|
matchesHeldModifiers(resolved, tracker.store.state.heldKeys, resolveMaybeGetter(options)) &&
|
|
@@ -27,7 +29,7 @@ export function getHotkeyHint(hotkey, options = {}) {
|
|
|
27
29
|
};
|
|
28
30
|
let previous = false;
|
|
29
31
|
// Notify Svelte only when visibility changes, not for every unrelated held key.
|
|
30
|
-
const
|
|
32
|
+
const track = (store) => createSubscriber((update) => {
|
|
31
33
|
previous = read();
|
|
32
34
|
const check = () => {
|
|
33
35
|
const next = read();
|
|
@@ -45,6 +47,18 @@ export function getHotkeyHint(hotkey, options = {}) {
|
|
|
45
47
|
custom?.unsubscribe();
|
|
46
48
|
};
|
|
47
49
|
});
|
|
50
|
+
// One subscriber for each store, so a store that arrives later is tracked.
|
|
51
|
+
const withoutStore = track(null);
|
|
52
|
+
const subscribers = new WeakMap();
|
|
53
|
+
const subscribe = () => {
|
|
54
|
+
const store = getStore();
|
|
55
|
+
if (!store)
|
|
56
|
+
return withoutStore();
|
|
57
|
+
let tracked = subscribers.get(store);
|
|
58
|
+
if (!tracked)
|
|
59
|
+
subscribers.set(store, (tracked = track(store)));
|
|
60
|
+
tracked();
|
|
61
|
+
};
|
|
48
62
|
return {
|
|
49
63
|
/** Reads visibility and subscribes the current reactive consumer when needed. */
|
|
50
64
|
get visible() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AppHotkeysStore } from '@tanstack/hotkeys';
|
|
1
|
+
import type { AppHotkeys, AppHotkeysStore, CustomHotkeys, HotkeyPlatform } from '@tanstack/hotkeys';
|
|
2
2
|
export type MaybeGetter<T> = T | (() => T);
|
|
3
3
|
interface SubscribableStore<TState> {
|
|
4
4
|
state: TState;
|
|
@@ -9,10 +9,27 @@ interface SubscribableStore<TState> {
|
|
|
9
9
|
export declare function resolveMaybeGetter<T>(value: MaybeGetter<T>): T;
|
|
10
10
|
export declare function createStoreSubscriber<TState>(store: SubscribableStore<TState>): () => void;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* The nearest provider's app hotkeys store, read from the context each time.
|
|
13
|
+
* A provider creates its store when its app hotkeys arrive, which can be after
|
|
14
|
+
* a hotkey is created. `subscribe` makes the current reactive consumer re-run
|
|
15
|
+
* when app hotkeys or custom hotkeys change.
|
|
14
16
|
*/
|
|
15
|
-
export declare function
|
|
17
|
+
export declare function createAppHotkeysSource(context: {
|
|
18
|
+
readonly appHotkeysStore: AppHotkeysStore | null;
|
|
19
|
+
} | null): {
|
|
20
|
+
readonly store: AppHotkeysStore | null;
|
|
21
|
+
subscribe: () => void;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Keeps a provider's own app hotkeys store current while the component is
|
|
25
|
+
* mounted: publishes it for devtools, and applies changed declarations, saved
|
|
26
|
+
* custom hotkeys and platform.
|
|
27
|
+
*/
|
|
28
|
+
export declare function syncAppHotkeysStore(getStore: () => AppHotkeysStore | null, getOptions: () => {
|
|
29
|
+
appHotkeys?: AppHotkeys;
|
|
30
|
+
customHotkeys?: CustomHotkeys | null;
|
|
31
|
+
platform?: HotkeyPlatform;
|
|
32
|
+
}): void;
|
|
16
33
|
/**
|
|
17
34
|
* Activates a provider's scope while the component is mounted, and again
|
|
18
35
|
* whenever the key from `getHotkeysProviderScopeKey` changes.
|
package/dist/internal.svelte.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { publishAppHotkeysStore } from '@tanstack/hotkeys';
|
|
1
2
|
import { activateHotkeysProviderScope } from '@tanstack/hotkeys/adapter';
|
|
2
3
|
import { createSubscriber } from 'svelte/reactivity';
|
|
3
4
|
export function resolveMaybeGetter(value) {
|
|
@@ -11,13 +12,55 @@ export function createStoreSubscriber(store) {
|
|
|
11
12
|
: () => subscription.unsubscribe();
|
|
12
13
|
});
|
|
13
14
|
}
|
|
14
|
-
const noop = () => { };
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
16
|
+
* The nearest provider's app hotkeys store, read from the context each time.
|
|
17
|
+
* A provider creates its store when its app hotkeys arrive, which can be after
|
|
18
|
+
* a hotkey is created. `subscribe` makes the current reactive consumer re-run
|
|
19
|
+
* when app hotkeys or custom hotkeys change.
|
|
18
20
|
*/
|
|
19
|
-
export function
|
|
20
|
-
|
|
21
|
+
export function createAppHotkeysSource(context) {
|
|
22
|
+
const subscribers = new WeakMap();
|
|
23
|
+
return {
|
|
24
|
+
get store() {
|
|
25
|
+
return context?.appHotkeysStore ?? null;
|
|
26
|
+
},
|
|
27
|
+
subscribe() {
|
|
28
|
+
const store = context?.appHotkeysStore;
|
|
29
|
+
if (!store)
|
|
30
|
+
return;
|
|
31
|
+
let subscribe = subscribers.get(store);
|
|
32
|
+
if (!subscribe) {
|
|
33
|
+
subscribe = createStoreSubscriber(store.store);
|
|
34
|
+
subscribers.set(store, subscribe);
|
|
35
|
+
}
|
|
36
|
+
subscribe();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Keeps a provider's own app hotkeys store current while the component is
|
|
42
|
+
* mounted: publishes it for devtools, and applies changed declarations, saved
|
|
43
|
+
* custom hotkeys and platform.
|
|
44
|
+
*/
|
|
45
|
+
export function syncAppHotkeysStore(getStore, getOptions) {
|
|
46
|
+
// Derived, so an effect runs again only when its own value changes, not for
|
|
47
|
+
// every option the getters read.
|
|
48
|
+
const store = $derived(getStore());
|
|
49
|
+
const appHotkeys = $derived(getOptions().appHotkeys);
|
|
50
|
+
const customHotkeys = $derived(getOptions().customHotkeys);
|
|
51
|
+
const platform = $derived(getOptions().platform);
|
|
52
|
+
// Publish the store so devtools can list declared hotkeys.
|
|
53
|
+
$effect(() => (store ? publishAppHotkeysStore(store) : undefined));
|
|
54
|
+
// Keep declarations current (for example after hot module replacement).
|
|
55
|
+
$effect(() => {
|
|
56
|
+
if (appHotkeys)
|
|
57
|
+
store?.setAppHotkeys(appHotkeys);
|
|
58
|
+
});
|
|
59
|
+
// Apply the platform and saved custom hotkeys whose value changes, such as
|
|
60
|
+
// data that loads after the first render.
|
|
61
|
+
$effect(() => {
|
|
62
|
+
store?.setOptions({ customHotkeys, platform });
|
|
63
|
+
});
|
|
21
64
|
}
|
|
22
65
|
/**
|
|
23
66
|
* Activates a provider's scope while the component is mounted, and again
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-hotkeys",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.1",
|
|
4
4
|
"description": "Svelte adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Kunal Rao",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"skills"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@tanstack/hotkeys": "0.27.
|
|
44
|
+
"@tanstack/hotkeys": "0.27.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@sveltejs/package": "^3.0.2",
|