@tanstack/ember-hotkeys 0.8.1 → 0.9.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.
@@ -2,20 +2,21 @@ import { DefaultHotkeysOptions } from "./types.js";
2
2
  import { UseHotkey } from "./useHotkey.js";
3
3
  import { UseHotkeys } from "./useHotkeys.js";
4
4
  import { MaybeGetter } from "./utils.js";
5
- import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
6
5
  import { OnHotkeys } from "./onHotkeys.js";
7
6
  import { OnHotkey } from "./onHotkey.js";
8
7
  //#region src/createHotkeysScope.d.ts
9
- /** Helpers, modifiers, and recorder factories with shared defaults. Owners retain their own cleanup. */
8
+ /**
9
+ * Helpers and modifiers with shared defaults. Owners retain their own cleanup.
10
+ * Pass the scope to `useHotkeyRecorder` to apply its recorder defaults.
11
+ */
10
12
  export interface EmberHotkeysScope {
11
13
  onHotkey: typeof OnHotkey;
12
14
  onHotkeys: typeof OnHotkeys;
13
15
  useHotkey: typeof UseHotkey;
14
16
  useHotkeys: typeof UseHotkeys;
15
- useHotkeyRecorder: typeof useHotkeyRecorder;
16
17
  }
17
18
  /**
18
- * Creates contextual helpers, modifiers, and recorders with shared, optionally reactive defaults.
19
+ * Creates contextual helpers and modifiers with shared, optionally reactive defaults.
19
20
  * Pass the scope to child components through arguments to share configuration.
20
21
  */
21
22
  export declare function createHotkeysScope(defaultOptions?: MaybeGetter<DefaultHotkeysOptions>): EmberHotkeysScope;
@@ -1,18 +1,17 @@
1
1
  import { UseHotkey } from "./useHotkey.js";
2
2
  import { UseHotkeys } from "./useHotkeys.js";
3
- import { read } from "./utils.js";
4
- import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
3
+ import { read, scopeDefaults } from "./utils.js";
5
4
  import { OnHotkey } from "./onHotkey.js";
6
5
  import { OnHotkeys } from "./onHotkeys.js";
7
- import { definedOptions, withDefaults } from "@tanstack/hotkeys/adapter";
6
+ import { definedOptions } from "@tanstack/hotkeys/adapter";
8
7
 
9
8
  //#region src/createHotkeysScope.ts
10
9
  /**
11
- * Creates contextual helpers, modifiers, and recorders with shared, optionally reactive defaults.
10
+ * Creates contextual helpers and modifiers with shared, optionally reactive defaults.
12
11
  * Pass the scope to child components through arguments to share configuration.
13
12
  */
14
13
  function createHotkeysScope(defaultOptions = {}) {
15
- return {
14
+ const scope = {
16
15
  onHotkey: class extends OnHotkey {
17
16
  modify(...[element, args, options]) {
18
17
  super.modify(element, args, { ...definedOptions(read(defaultOptions).hotkey, options) });
@@ -32,9 +31,10 @@ function createHotkeysScope(defaultOptions = {}) {
32
31
  compute(...[args, options]) {
33
32
  super.compute(args, { ...definedOptions(read(defaultOptions).hotkey, options) });
34
33
  }
35
- },
36
- useHotkeyRecorder: ((owner, options) => useHotkeyRecorder(owner, () => withDefaults(read(defaultOptions).hotkeyRecorder, read(options))))
34
+ }
37
35
  };
36
+ scopeDefaults.set(scope, defaultOptions);
37
+ return scope;
38
38
  }
39
39
 
40
40
  //#endregion
package/dist/index.d.ts CHANGED
@@ -8,10 +8,10 @@ import { useKeyHold } from "./useKeyHold.js";
8
8
  import { useHotkeyHint } from "./useHotkeyHint.js";
9
9
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
10
10
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
11
- import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
12
- import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
13
11
  import { ElementHotkeyOptions, OnHotkeys } from "./onHotkeys.js";
14
12
  import { OnHotkey } from "./onHotkey.js";
15
13
  import { EmberHotkeysScope, createHotkeysScope } from "./createHotkeysScope.js";
14
+ import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
15
+ import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
16
16
  export * from "@tanstack/hotkeys";
17
17
  export { DefaultHotkeysOptions, type ElementHotkeyOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, EmberHotkeysScope, HotkeyRegistrationsResult, type MaybeGetter, createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
@@ -13,11 +13,12 @@ import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers
13
13
  function useHotkeyHint(owner, hotkey, options = {}) {
14
14
  const appHotkeys = trackPublishedAppHotkeys(owner);
15
15
  const manager = getHotkeyManager();
16
- const registrations = select(owner, manager.registrations, (state) => state);
16
+ const active = select(owner, manager.registrations, () => manager.hasActiveRegistration(read(hotkey)));
17
17
  return select(owner, getKeyStateTracker().store, (state) => {
18
+ const isActive = active.value;
18
19
  const value = read(hotkey);
19
20
  const resolved = isAppHotkey(value) ? resolveAppHotkey(value, appHotkeys()).hotkey : value;
20
- return resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, read(options)) && registrations.value.size > 0 && manager.hasActiveRegistration(value);
21
+ return resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, read(options)) && isActive;
21
22
  });
22
23
  }
23
24
 
@@ -1,4 +1,5 @@
1
1
  import { EmberHotkeyRecorder } from "./types.js";
2
+ import { EmberHotkeysScope } from "./createHotkeysScope.js";
2
3
  import { HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderOptions, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
3
4
  //#region src/useHotkeyRecorder.d.ts
4
5
  /**
@@ -7,10 +8,12 @@ import { HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderOptions, HotkeyS
7
8
  * With a larger `maxSteps`, each chord adds a step; the recording commits on
8
9
  * Enter (unless `commitKeys: 'none'`), after `idleTimeoutMs`, or at
9
10
  * `maxSteps`, and `onRecord` receives a chord (one step) or a sequence.
11
+ * Pass a scope from `createHotkeysScope` to apply its `hotkeyRecorder`
12
+ * defaults.
10
13
  */
11
- export declare function useHotkeyRecorder(owner: object, options: HotkeyChordRecorderOptions): EmberHotkeyRecorder<HotkeyChord>;
12
- export declare function useHotkeyRecorder(owner: object, options: HotkeySequenceRecorderOptions): EmberHotkeyRecorder;
13
- export declare function useHotkeyRecorder(owner: object, options: () => HotkeySequenceRecorderOptions): EmberHotkeyRecorder;
14
- export declare function useHotkeyRecorder(owner: object, options: () => HotkeyChordRecorderOptions): EmberHotkeyRecorder<HotkeyChord>;
15
- export declare function useHotkeyRecorder(owner: object, options: () => HotkeyRecorderOptions): EmberHotkeyRecorder;
14
+ export declare function useHotkeyRecorder(owner: object, options: HotkeyChordRecorderOptions, scope?: EmberHotkeysScope): EmberHotkeyRecorder<HotkeyChord>;
15
+ export declare function useHotkeyRecorder(owner: object, options: HotkeySequenceRecorderOptions, scope?: EmberHotkeysScope): EmberHotkeyRecorder;
16
+ export declare function useHotkeyRecorder(owner: object, options: () => HotkeySequenceRecorderOptions, scope?: EmberHotkeysScope): EmberHotkeyRecorder;
17
+ export declare function useHotkeyRecorder(owner: object, options: () => HotkeyChordRecorderOptions, scope?: EmberHotkeysScope): EmberHotkeyRecorder<HotkeyChord>;
18
+ export declare function useHotkeyRecorder(owner: object, options: () => HotkeyRecorderOptions, scope?: EmberHotkeysScope): EmberHotkeyRecorder;
16
19
  //#endregion
@@ -1,10 +1,11 @@
1
- import { read, select } from "./utils.js";
1
+ import { read, scopeDefaults, select } from "./utils.js";
2
2
  import { HotkeyRecorder } from "@tanstack/hotkeys";
3
3
  import { isDestroyed, isDestroying, registerDestructor } from "@ember/destroyable";
4
+ import { withDefaults } from "@tanstack/hotkeys/adapter";
4
5
 
5
6
  //#region src/useHotkeyRecorder.ts
6
- function useHotkeyRecorder(owner, options) {
7
- const recorder = new HotkeyRecorder(() => read(options));
7
+ function useHotkeyRecorder(owner, options, scope) {
8
+ const recorder = new HotkeyRecorder(() => withDefaults(scope && read(scopeDefaults.get(scope)).hotkeyRecorder, read(options)));
8
9
  const state = select(owner, recorder.store, (value) => value);
9
10
  registerDestructor(owner, () => recorder.destroy());
10
11
  return {
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import "./types.js";
1
2
  import "@tanstack/hotkeys";
2
3
  import "@tanstack/hotkeys/adapter";
3
4
  //#region src/utils.d.ts
package/dist/utils.js CHANGED
@@ -4,17 +4,25 @@ import { subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
4
4
  import { trackedObject } from "@ember/reactive/collections";
5
5
 
6
6
  //#region src/utils.ts
7
+ /** The defaults behind each `createHotkeysScope` result. */
8
+ const scopeDefaults = /* @__PURE__ */ new WeakMap();
7
9
  function read(value) {
8
10
  return typeof value === "function" ? value() : value;
9
11
  }
10
12
  function select(owner, store, selector) {
11
- const state = trackedObject({ snapshot: store.state });
13
+ const state = trackedObject({ version: 0 });
14
+ let last;
12
15
  const subscription = store.subscribe(() => {
13
- state.snapshot = store.state;
16
+ const next = selector(store.state);
17
+ if (!Object.is(next, last)) {
18
+ last = next;
19
+ state.version++;
20
+ }
14
21
  });
15
22
  registerDestructor(owner, () => subscription.unsubscribe());
16
23
  return { get value() {
17
- return selector(state.snapshot);
24
+ state.version;
25
+ return last = selector(store.state);
18
26
  } };
19
27
  }
20
28
  /**
@@ -33,4 +41,4 @@ function trackPublishedAppHotkeys(owner) {
33
41
  }
34
42
 
35
43
  //#endregion
36
- export { read, select, trackPublishedAppHotkeys };
44
+ export { read, scopeDefaults, select, trackPublishedAppHotkeys };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ember-hotkeys",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Ember adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@embroider/addon-shim": "^1.10.3",
43
- "@tanstack/hotkeys": "0.22.0",
43
+ "@tanstack/hotkeys": "0.22.1",
44
44
  "ember-modifier": "^4.2.2"
45
45
  },
46
46
  "devDependencies": {
@@ -21,6 +21,7 @@ sources:
21
21
  - src/onHotkey.ts
22
22
  - src/onHotkeys.ts
23
23
  - src/createHotkeysScope.ts
24
+ - src/useHotkeyRecorder.ts
24
25
  - src/useAppHotkeys.ts
25
26
  - TanStack/hotkeys:docs/framework/ember/quick-start.md
26
27
  - TanStack/hotkeys:docs/framework/ember/guides/app-hotkeys.md
@@ -60,13 +61,13 @@ export default class Editor extends Component {
60
61
  | `{{useHotkeys definitions}}` | Register several hotkeys, or a list built from data, in one helper. |
61
62
  | `{{onHotkey hotkey callback ...options}}` | Attach a hotkey to the element the modifier is on, so it fires while focus is inside that element. |
62
63
  | `{{onHotkeys definitions}}` | Attach several hotkeys to one element. |
63
- | `createHotkeysScope(defaults?)` | Create `useHotkey`, `useHotkeys`, `onHotkey`, `onHotkeys`, and `useHotkeyRecorder` that share default options. |
64
+ | `createHotkeysScope(defaults?)` | Create `useHotkey`, `useHotkeys`, `onHotkey`, and `onHotkeys` that share default options. |
64
65
  | `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys before the app boots, since Ember has no provider. |
65
66
  | `getHotkeyManager().activateScope(scope, options?)` | Turn on a scope, such as an exclusive one for a dialog; it returns the function that turns the scope off. |
66
67
  | `useAppHotkeys(this)` | Get the declared app hotkeys with the user's custom hotkeys applied. |
67
68
  | `useAppHotkeyList(this)` | List every app hotkey with its status, for a cheat sheet or settings screen. |
68
69
  | `useCustomHotkeys(this)` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
69
- | `useHotkeyRecorder(this, options)` | Record a chord or sequence the user presses, for a settings screen. |
70
+ | `useHotkeyRecorder(this, options, scope?)` | Record a chord or sequence the user presses, for a settings screen. Pass a `createHotkeysScope` result to apply its `hotkeyRecorder` defaults. |
70
71
  | `useHotkeyRegistrations(this)` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
71
72
  | `useHotkeyScopesState(this)` | Get the active scopes and the exclusive scope that owns the keyboard. |
72
73
  | `useHotkeyHint(this, hotkey, options?)` | Get whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
@@ -84,6 +85,7 @@ The helpers are template helpers, not JavaScript functions. State readers take t
84
85
  - **Build template sequences with `array`.** `{{useHotkey "G G" ...}}` registers a single key named `G G`, which never matches.
85
86
  - **`createHotkeysScope` isn't a hotkey scope.** It only sets defaults for its helpers. For a dialog, call `getHotkeyManager().activateScope(name, { exclusive: true })` in the constructor, pass the result to `registerDestructor(this, release)`, and put `scope=name` on the dialog's hotkeys. Give each open dialog its own name.
86
87
  - **Read app hotkeys through getters.** Template paths can't index ids that contain dots.
88
+ - **Scopes have no `useHotkeyRecorder`** since v0.9.0. Call `useHotkeyRecorder(this, options, this.hotkeys)`, so apps that set defaults but don't record don't ship the recorder.
87
89
 
88
90
  When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Ember](references/app-hotkeys.md).
89
91