rx-hotkeys 2.2.1 → 2.3.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/README.md +1 -1
- package/dist/hotkeys.d.ts +2 -1
- package/dist/hotkeys.d.ts.map +1 -1
- package/dist/hotkeys.js +6 -4
- package/dist/hotkeys.test.js +51 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -128,7 +128,7 @@ Registers a key sequence shortcut.
|
|
|
128
128
|
* `config`: The KeySequenceConfig object.
|
|
129
129
|
* Returns the shortcut ID if successful, undefined otherwise.
|
|
130
130
|
|
|
131
|
-
`setContext(contextName: string | null):
|
|
131
|
+
`setContext(contextName: string | null): boolean`
|
|
132
132
|
|
|
133
133
|
Sets the active context. Only shortcuts matching this context or global shortcuts (no context) will trigger.
|
|
134
134
|
|
package/dist/hotkeys.d.ts
CHANGED
|
@@ -84,8 +84,9 @@ export declare class Hotkeys {
|
|
|
84
84
|
* will be active and can be triggered.
|
|
85
85
|
* @param contextName - The name of the context (e.g., "modal", "editor", "global").
|
|
86
86
|
* Pass `null` to activate shortcuts with no context or to deactivate context-specific shortcuts.
|
|
87
|
+
* @returns boolean
|
|
87
88
|
*/
|
|
88
|
-
setContext(contextName: string | null):
|
|
89
|
+
setContext(contextName: string | null): boolean;
|
|
89
90
|
/**
|
|
90
91
|
* Gets the current active context.
|
|
91
92
|
* @returns The current context name as a string, or `null` if no context is set.
|
package/dist/hotkeys.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hotkeys.d.ts","sourceRoot":"","sources":["../src/hotkeys.ts"],"names":[],"mappings":"AAAA,OAAO,EACuC,UAAU,EAEvD,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAIxC,oBAAY,aAAa;IACrB,WAAW,gBAAgB;IAC3B,QAAQ,aAAa;CACxB;AAED,UAAU,kBAAkB;IACxB,EAAE,EAAE,M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lC;;;;OAIG;IACI,YAAY,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;IAa1C;;;;;;;;;;;;;;;;;OAiBG;IACH,IAAW,gBAAgB,IAAI,UAAU,CAAC,MAAM,GAAG,IAAI,CAAC,CAEvD;IAED;;;;OAIG;IACI,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAIvC,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,iBAAiB;IAkBzB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,MAAM,GAAG,SAAS;IAgFvE;;;;;;;;;;;;;;;;;;;OAmBG;IACI,WAAW,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,GAAG,SAAS;IAkHjE;;;;;;OAMG;IACI,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAYlC;;;;;;OAMG;IACI,kBAAkB,IAAI;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,IAAI,EAAE,aAAa,CAAA;KAAC,EAAE;IAa/G;;;;;OAKG;IACI,OAAO,IAAI,IAAI;CAOzB"}
|
package/dist/hotkeys.js
CHANGED
|
@@ -63,21 +63,23 @@ export class Hotkeys {
|
|
|
63
63
|
* will be active and can be triggered.
|
|
64
64
|
* @param contextName - The name of the context (e.g., "modal", "editor", "global").
|
|
65
65
|
* Pass `null` to activate shortcuts with no context or to deactivate context-specific shortcuts.
|
|
66
|
+
* @returns boolean
|
|
66
67
|
*/
|
|
67
68
|
setContext(contextName) {
|
|
68
|
-
|
|
69
|
+
const currentContext = this.activeContext$.getValue();
|
|
70
|
+
if (currentContext === contextName) {
|
|
69
71
|
if (this.debugMode) {
|
|
70
72
|
// Optional: Log that no change is happening, or simply do nothing.
|
|
71
73
|
console.log(`${Hotkeys.LOG_PREFIX} setContext called with the same context "${contextName}". No change made.`);
|
|
72
74
|
}
|
|
73
|
-
return; // Context is the same, so no further action is needed.
|
|
75
|
+
return false; // Context is the same, so no further action is needed.
|
|
74
76
|
}
|
|
75
77
|
// If we reach here, the context is actually changing.
|
|
76
|
-
const oldContext = this.activeContext$.getValue(); // For more informative logging
|
|
77
78
|
if (this.debugMode) {
|
|
78
|
-
console.log(`${Hotkeys.LOG_PREFIX} Context changed from "${
|
|
79
|
+
console.log(`${Hotkeys.LOG_PREFIX} Context changed from "${currentContext}" to "${contextName}".`);
|
|
79
80
|
}
|
|
80
81
|
this.activeContext$.next(contextName);
|
|
82
|
+
return true;
|
|
81
83
|
}
|
|
82
84
|
/**
|
|
83
85
|
* Gets the current active context.
|
package/dist/hotkeys.test.js
CHANGED
|
@@ -152,16 +152,59 @@ describe("Hotkeys Library (Node.js Test Runner)", () => {
|
|
|
152
152
|
activeContextNextSpy.mock.restore();
|
|
153
153
|
consoleLogMock.mock.restore();
|
|
154
154
|
});
|
|
155
|
+
it(`should log "no change" if context is set to the same value (debug mode on)`, () => {
|
|
156
|
+
keyManager.setContext("sameCtx"); // Set initial context
|
|
157
|
+
const consoleLogMock = mock.method(console, "log");
|
|
158
|
+
keyManager.setDebugMode(true);
|
|
159
|
+
consoleLogMock.mock.resetCalls(); // Clear previous logs
|
|
160
|
+
keyManager.setContext("sameCtx"); // Attempt to set the same context
|
|
161
|
+
const noChangeLogCall = consoleLogMock.mock.calls.find(call => call.arguments[0].includes(`setContext called with the same context "sameCtx". No change made.`));
|
|
162
|
+
assert.ok(noChangeLogCall, "No-change log not found or incorrect for same context.");
|
|
163
|
+
const changedLogCall = consoleLogMock.mock.calls.find(call => call.arguments[0].includes("Context changed from"));
|
|
164
|
+
assert.strictEqual(changedLogCall, undefined, "Context changed log should not appear for same context.");
|
|
165
|
+
consoleLogMock.mock.restore();
|
|
166
|
+
});
|
|
155
167
|
it("should toggle debug mode and log its state", () => {
|
|
156
168
|
const consoleLogMock = mock.method(console, "log");
|
|
157
169
|
keyManager.setDebugMode(true);
|
|
158
170
|
assert.ok(consoleLogMock.mock.calls.some(call => call.arguments[0].includes("Debug mode enabled")));
|
|
159
171
|
consoleLogMock.mock.resetCalls();
|
|
160
172
|
keyManager.setDebugMode(false);
|
|
161
|
-
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>", consoleLogMock.mock.calls);
|
|
162
173
|
assert.ok(consoleLogMock.mock.calls.some(call => call.arguments[0].includes("Debug mode disabled")));
|
|
163
174
|
consoleLogMock.mock.restore();
|
|
164
175
|
});
|
|
176
|
+
describe("setContext return value", () => {
|
|
177
|
+
it("should return true when context is changed from null to a new value", () => {
|
|
178
|
+
assert.strictEqual(keyManager.getContext(), null, "Initial context should be null");
|
|
179
|
+
const result = keyManager.setContext("newContext");
|
|
180
|
+
assert.strictEqual(result, true, "setContext should return true for a change from null");
|
|
181
|
+
assert.strictEqual(keyManager.getContext(), "newContext");
|
|
182
|
+
});
|
|
183
|
+
it("should return true when context is changed from one value to another", () => {
|
|
184
|
+
keyManager.setContext("initialContext"); // Set a non-null initial context
|
|
185
|
+
const result = keyManager.setContext("changedContext");
|
|
186
|
+
assert.strictEqual(result, true, "setContext should return true for a value-to-value change");
|
|
187
|
+
assert.strictEqual(keyManager.getContext(), "changedContext");
|
|
188
|
+
});
|
|
189
|
+
it("should return false when context is set to the same value (non-null)", () => {
|
|
190
|
+
keyManager.setContext("sameContext");
|
|
191
|
+
const result = keyManager.setContext("sameContext");
|
|
192
|
+
assert.strictEqual(result, false, "setContext should return false for no change (non-null)");
|
|
193
|
+
assert.strictEqual(keyManager.getContext(), "sameContext");
|
|
194
|
+
});
|
|
195
|
+
it("should return true when context is changed from a value to null", () => {
|
|
196
|
+
keyManager.setContext("initialContext");
|
|
197
|
+
const result = keyManager.setContext(null);
|
|
198
|
+
assert.strictEqual(result, true, "setContext should return true for a change to null");
|
|
199
|
+
assert.strictEqual(keyManager.getContext(), null);
|
|
200
|
+
});
|
|
201
|
+
it("should return false when context is set to null and already null", () => {
|
|
202
|
+
keyManager.setContext(null); // Ensure context is null
|
|
203
|
+
const result = keyManager.setContext(null);
|
|
204
|
+
assert.strictEqual(result, false, "setContext should return false for no change (already null)");
|
|
205
|
+
assert.strictEqual(keyManager.getContext(), null);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
165
208
|
describe("onContextChange$ observable", () => {
|
|
166
209
|
it("should emit initial context to new subscriber", () => {
|
|
167
210
|
const manager = new Hotkeys("initial", false);
|
|
@@ -176,14 +219,14 @@ describe("Hotkeys Library (Node.js Test Runner)", () => {
|
|
|
176
219
|
const spy = createMockFn();
|
|
177
220
|
const sub = keyManager.onContextChange$.subscribe(spy); // Subscribes, gets initial null
|
|
178
221
|
spy.mockClear(); // Clear initial emission
|
|
179
|
-
keyManager.setContext("newContext");
|
|
180
|
-
assert.strictEqual(spy.calledCount, 1);
|
|
222
|
+
assert.ok(keyManager.setContext("newContext"), "setContext should have returned true for change");
|
|
223
|
+
assert.strictEqual(spy.calledCount, 1, "onContextChange$ did not emit on first change");
|
|
181
224
|
assert.strictEqual(spy.lastArgs[0], "newContext");
|
|
182
|
-
keyManager.setContext("anotherContext");
|
|
183
|
-
assert.strictEqual(spy.calledCount, 2);
|
|
225
|
+
assert.ok(keyManager.setContext("anotherContext"), "setContext should have returned true for second change");
|
|
226
|
+
assert.strictEqual(spy.calledCount, 2, "onContextChange$ did not emit on second change");
|
|
184
227
|
assert.strictEqual(spy.lastArgs[0], "anotherContext");
|
|
185
|
-
keyManager.setContext(null);
|
|
186
|
-
assert.strictEqual(spy.calledCount, 3);
|
|
228
|
+
assert.ok(keyManager.setContext(null), "setContext should have returned true for change to null");
|
|
229
|
+
assert.strictEqual(spy.calledCount, 3, "onContextChange$ did not emit on change to null");
|
|
187
230
|
assert.strictEqual(spy.lastArgs[0], null);
|
|
188
231
|
sub.unsubscribe();
|
|
189
232
|
});
|
|
@@ -192,7 +235,7 @@ describe("Hotkeys Library (Node.js Test Runner)", () => {
|
|
|
192
235
|
const spy = createMockFn();
|
|
193
236
|
const sub = keyManager.onContextChange$.subscribe(spy); // Subscribes, gets "testContext"
|
|
194
237
|
spy.mockClear(); // Clear initial emission
|
|
195
|
-
keyManager.setContext("testContext"); // Set same context
|
|
238
|
+
assert.strictEqual(keyManager.setContext("testContext"), false, "setContext should have returned false for no change"); // Set same context
|
|
196
239
|
assert.strictEqual(spy.calledCount, 0, "Observable should not emit if context value is the same.");
|
|
197
240
|
sub.unsubscribe();
|
|
198
241
|
});
|