@asyra/input-system 0.5.5 → 0.6.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/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @asyra/input-system
2
2
 
3
+ ## Pending - Office input and spatial materials
4
+
5
+ ```update-note
6
+ {
7
+ "change": "office-input-and-spatial-materials",
8
+ "owner": "Framework - @asyra/input-system",
9
+ "summary": "Support held keyboard control or optional spatial material effects used by the Office preview, preserving existing defaults.",
10
+ "sources": [
11
+ "packages/input-system/src/__tests__/held-keyboard.test.ts",
12
+ "packages/input-system/src/__tests__/input-system.test.ts",
13
+ "packages/input-system/src/__tests__/runtime-reset.test.ts",
14
+ "packages/input-system/src/event-mappings.ts",
15
+ "packages/input-system/src/input-system.ts"
16
+ ],
17
+ "impact": "integration",
18
+ "guides": [
19
+ "docs/public/updates/held-input-and-spatial-materials-r1.md"
20
+ ],
21
+ "reason": "Optional APIs require explicit consumer registration and preservation of app-owned policies; see the adoption guide."
22
+ }
23
+ ```
24
+
3
25
  ## 0.5.5
4
26
 
5
27
  ### Patch Changes
@@ -38,6 +38,13 @@ export interface InputEventCombo {
38
38
  type: InputType;
39
39
  keys: KeyboardKey[];
40
40
  modifiers?: ModifierKey[];
41
+ /**
42
+ * Keyboard-only held observation: keys are alternatives, not an exact chord.
43
+ * Reports down/up/cancel with live modifiers; callback owns modifier admission.
44
+ * `modifiers` is a chord-mode filter and is not applied in held mode.
45
+ */
46
+ keyboardMode?: 'held';
41
47
  detail?: DetailType;
42
- callback?: (raw: RawInputEvent) => void;
48
+ /** Return false to ignore this input before dispatch; policy belongs to the caller. */
49
+ callback?: ((raw: RawInputEvent) => void) | ((raw: RawInputEvent) => boolean);
43
50
  }
@@ -3,6 +3,7 @@ import { InputSystemRegistry } from './registry.js';
3
3
  type Callback = (raw: RawInputEvent) => void | Promise<void>;
4
4
  type Combinations = Record<string, string[]>;
5
5
  declare class InputSystem {
6
+ private heldKeyboardKeys;
6
7
  private browserHost;
7
8
  private pointerTarget;
8
9
  private combinations;
@@ -35,7 +36,12 @@ declare class InputSystem {
35
36
  private _isInputActive;
36
37
  private _hasTriggerBrowserShortcut;
37
38
  private handleKeyDown;
39
+ private releaseKey;
38
40
  private handleKeyUp;
41
+ private hasHeldInterest;
42
+ private dispatchHeldKeyboard;
43
+ private cancelHeldKeyboard;
44
+ private hiddenKeyboard;
39
45
  private handleMouseDown;
40
46
  private handleMouseUp;
41
47
  private handleMouseMove;
@@ -1,4 +1,4 @@
1
- import { MouseButton, SpecialEvent, InputType, arrEqual, DefaultPointerEventData, measureBrowserDragPhase } from '@asyra/utils';
1
+ import { MouseButton, SpecialEvent, InputType, ModifierKey, arrEqual, DefaultPointerEventData, measureBrowserDragPhase } from '@asyra/utils';
2
2
  import { InputFieldsList } from '@asyra/utils';
3
3
  import { PointerKey } from '@asyra/utils';
4
4
  import { CLICK_THRESHOLD, CLEAR_KEY_TIME } from './constants.js';
@@ -31,6 +31,7 @@ const getMouseButtonFromButtonsMask = (buttons) => {
31
31
  return MouseButton.NONE;
32
32
  };
33
33
  class InputSystem {
34
+ heldKeyboardKeys = new Set();
34
35
  browserHost;
35
36
  pointerTarget;
36
37
  combinations = {};
@@ -88,10 +89,13 @@ class InputSystem {
88
89
  }
89
90
  }
90
91
  addKeyboardListeners(host) {
91
- this.keyboardBindings().forEach(([name, listener]) => host.addEventListener(name, listener));
92
+ this.keyboardBindings().forEach((binding) => host.addEventListener(...binding));
93
+ host.document.addEventListener('visibilitychange', this.browserListener(this.hiddenKeyboard));
92
94
  }
93
95
  removeKeyboardListeners(host, attempt = (cleanup) => cleanup()) {
94
- this.keyboardBindings().forEach(([name, listener]) => attempt(() => host.removeEventListener(name, listener)));
96
+ this.keyboardBindings().forEach((binding) => attempt(() => host.removeEventListener(...binding)));
97
+ attempt(() => host.document.removeEventListener('visibilitychange', this.browserListener(this.hiddenKeyboard)));
98
+ attempt(this.cancelHeldKeyboard);
95
99
  }
96
100
  addPointerListeners(target) {
97
101
  this.pointerBindings().forEach((binding) => target.addEventListener(...binding));
@@ -102,7 +106,11 @@ class InputSystem {
102
106
  keyboardBindings() {
103
107
  return [
104
108
  ['keydown', this.browserListener(this.handleKeyDown)],
105
- ['keyup', this.browserListener(this.handleKeyUp)]
109
+ ['keyup', this.browserListener(this.releaseKey), true],
110
+ ['keyup', this.browserListener(this.handleKeyUp)],
111
+ ['blur', this.browserListener(this.cancelHeldKeyboard)],
112
+ ['focusin', this.browserListener(this.cancelHeldKeyboard)],
113
+ ['compositionstart', this.browserListener(this.cancelHeldKeyboard)]
106
114
  ];
107
115
  }
108
116
  pointerBindings() {
@@ -129,12 +137,14 @@ class InputSystem {
129
137
  }
130
138
  browserListener(listener) {
131
139
  const existing = this.browserListeners.get(listener);
132
- if (existing)
140
+ if (existing) {
133
141
  return existing;
142
+ }
134
143
  const generation = this.runtimeGeneration;
135
144
  const guarded = (event) => {
136
- if (generation === this.runtimeGeneration)
145
+ if (generation === this.runtimeGeneration) {
137
146
  listener(event);
147
+ }
138
148
  };
139
149
  this.browserListeners.set(listener, guarded);
140
150
  return guarded;
@@ -180,8 +190,9 @@ class InputSystem {
180
190
  }
181
191
  const generation = this.runtimeGeneration;
182
192
  const timer = setTimeout(() => {
183
- if (generation !== this.runtimeGeneration)
193
+ if (generation !== this.runtimeGeneration) {
184
194
  return;
195
+ }
185
196
  this.activeKeys.delete(key);
186
197
  this.timers.delete(key);
187
198
  }, CLEAR_KEY_TIME);
@@ -197,7 +208,7 @@ class InputSystem {
197
208
  }
198
209
  }
199
210
  _isInputActive(event) {
200
- return (InputFieldsList.includes(event.target.tagName.toLowerCase()) || event.target.isContentEditable);
211
+ return (InputFieldsList.includes(event.target?.tagName?.toLowerCase()) || event.target?.isContentEditable);
201
212
  }
202
213
  _hasTriggerBrowserShortcut(event) {
203
214
  const hasMeta = this.activeKeys.has('Meta');
@@ -206,10 +217,23 @@ class InputSystem {
206
217
  return hasMeta && hasNumber;
207
218
  }
208
219
  handleKeyDown = (event) => {
209
- if (!this._isInputActive(event) || this._hasTriggerBrowserShortcut(event)) {
220
+ const key = this.keyMap.mapKey(event.code);
221
+ const interested = this.hasHeldInterest(key);
222
+ if (event.isComposing) {
223
+ this.cancelHeldKeyboard();
224
+ }
225
+ else {
226
+ if (key && !this.keyMap.isModifierKeys(key)) {
227
+ this.heldKeyboardKeys.add(key);
228
+ }
229
+ if (this.dispatchHeldKeyboard(event, key, 'down')) {
230
+ event.preventDefault();
231
+ }
232
+ }
233
+ if (!interested &&
234
+ (!this._isInputActive(event) || this._hasTriggerBrowserShortcut(event))) {
210
235
  event.preventDefault();
211
236
  }
212
- const key = this.keyMap.mapKey(event.code);
213
237
  if (key) {
214
238
  this.activeKeys.add(key);
215
239
  if (!this.keyMap.isModifierKeys(key)) {
@@ -218,17 +242,96 @@ class InputSystem {
218
242
  this.checkCombinations(InputType.KEYBOARD);
219
243
  }
220
244
  };
245
+ // Release bookkeeping must survive editors stopping event propagation.
246
+ // Capture publishes opted-in held releases only; ordinary chord dispatch stays in bubble phase.
247
+ releaseKey = (event, notifyHeld = true) => {
248
+ const key = this.keyMap.mapKey(event.code);
249
+ if (key) {
250
+ this.activeKeys.delete(key);
251
+ this.clearTimer(key);
252
+ if (notifyHeld) {
253
+ this.heldKeyboardKeys.delete(key);
254
+ this.dispatchHeldKeyboard(event, key, 'up');
255
+ }
256
+ }
257
+ };
221
258
  handleKeyUp = (event) => {
222
- if (!this._isInputActive(event) || this._hasTriggerBrowserShortcut(event)) {
259
+ if (!this.hasHeldInterest(this.keyMap.mapKey(event.code)) &&
260
+ (!this._isInputActive(event) || this._hasTriggerBrowserShortcut(event))) {
223
261
  event.preventDefault();
224
262
  }
225
263
  const key = this.keyMap.mapKey(event.code);
226
264
  if (key) {
227
- this.activeKeys.delete(key);
228
- this.clearTimer(key);
265
+ this.releaseKey(event, false);
229
266
  this.checkCombinations(InputType.KEYBOARD);
230
267
  }
231
268
  };
269
+ hasHeldInterest(key) {
270
+ return this.registry
271
+ .getEventNames()
272
+ .some((name) => this.registry
273
+ .getCombinations(name)
274
+ ?.some((combo) => combo.type === InputType.KEYBOARD &&
275
+ combo.keyboardMode === 'held' &&
276
+ combo.keys.some((candidate) => candidate === key || this.heldKeyboardKeys.has(candidate))));
277
+ }
278
+ dispatchHeldKeyboard(event, key, phase) {
279
+ let accepted = false;
280
+ const raw = {
281
+ type: InputType.KEYBOARD,
282
+ keys: [...this.heldKeyboardKeys],
283
+ modifiers: {
284
+ shift: event?.shiftKey ?? false,
285
+ ctrl: event?.ctrlKey ?? false,
286
+ alt: event?.altKey ?? false,
287
+ meta: event?.metaKey ?? false
288
+ },
289
+ pointer: DefaultPointerEventData,
290
+ keyboard: {
291
+ phase,
292
+ changedKey: key ?? null,
293
+ target: event?.target ?? null,
294
+ repeat: event?.repeat ?? false,
295
+ isComposing: event?.isComposing ?? false
296
+ }
297
+ };
298
+ for (const name of this.registry.getEventNames()) {
299
+ for (const combo of this.registry.getCombinations(name) ?? []) {
300
+ if (combo.type !== InputType.KEYBOARD ||
301
+ combo.keyboardMode !== 'held') {
302
+ continue;
303
+ }
304
+ if (phase !== 'cancel' &&
305
+ !combo.keys.some((candidate) => candidate === key || this.heldKeyboardKeys.has(candidate))) {
306
+ continue;
307
+ }
308
+ const snapshot = {
309
+ ...raw,
310
+ keys: [...raw.keys],
311
+ modifiers: { ...raw.modifiers },
312
+ detail: combo.detail
313
+ };
314
+ if (combo.callback?.(snapshot) === false) {
315
+ continue;
316
+ }
317
+ accepted = true;
318
+ this.triggerAction(name, snapshot);
319
+ }
320
+ }
321
+ return accepted;
322
+ }
323
+ cancelHeldKeyboard = () => {
324
+ if (!this.heldKeyboardKeys.size) {
325
+ return;
326
+ }
327
+ this.heldKeyboardKeys.clear();
328
+ this.dispatchHeldKeyboard(null, undefined, 'cancel');
329
+ };
330
+ hiddenKeyboard = () => {
331
+ if (this.browserHost?.document.hidden) {
332
+ this.cancelHeldKeyboard();
333
+ }
334
+ };
232
335
  handleMouseDown = (event) => {
233
336
  if (this.pointerInputBlocked) {
234
337
  return;
@@ -314,11 +417,12 @@ class InputSystem {
314
417
  this.activeKeys.delete(PointerKey.MOUSE_DOUBLE_CLICK);
315
418
  };
316
419
  getMouseEventKey(button, state) {
317
- if (button === MouseButton.NONE)
420
+ if (button === MouseButton.NONE) {
318
421
  return;
422
+ }
319
423
  return `${button}Mouse${state}`;
320
424
  }
321
- checkCombinations(type, pointerData = DefaultPointerEventData) {
425
+ checkCombinations(type, pointerData = DefaultPointerEventData, activeModifiers = this.getActiveModifiers(this.activeKeys)) {
322
426
  const currentKeys = Array.from(this.activeKeys).filter((key) => {
323
427
  if (this.keyMap.isModifierKeys(key)) {
324
428
  return false;
@@ -331,13 +435,16 @@ class InputSystem {
331
435
  }
332
436
  return POINTER_KEYS.has(key) && key !== SpecialEvent.WHEEL;
333
437
  });
334
- const activeModifiers = this.getActiveModifiers(this.activeKeys);
335
438
  const allModifiers = this.getAllModifiers(activeModifiers);
336
439
  for (const eventName of this.registry.getEventNames()) {
337
440
  const combos = this.registry.getCombinations(eventName);
338
- if (!combos)
441
+ if (!combos) {
339
442
  continue;
443
+ }
340
444
  for (const combo of combos) {
445
+ if (combo.keyboardMode === 'held') {
446
+ continue;
447
+ }
341
448
  if (this.isExactMatch(type, currentKeys, combo, activeModifiers)) {
342
449
  const raw = {
343
450
  type,
@@ -348,9 +455,9 @@ class InputSystem {
348
455
  if (combo.detail) {
349
456
  raw.detail = combo.detail;
350
457
  }
351
- // Call callback before triggering action (if defined)
352
- if (combo.callback) {
353
- combo.callback(raw);
458
+ // Admission happens before any downstream queue or session receives input.
459
+ if (combo.callback?.(raw) === false) {
460
+ continue;
354
461
  }
355
462
  this.triggerAction(eventName, raw);
356
463
  }
@@ -359,8 +466,9 @@ class InputSystem {
359
466
  }
360
467
  isExactMatch(type, keys, combo, modifiers) {
361
468
  const isKeysMatch = arrEqual(keys, combo.keys);
362
- if (type !== combo.type || !isKeysMatch)
469
+ if (type !== combo.type || !isKeysMatch) {
363
470
  return false;
471
+ }
364
472
  if (isKeysMatch && (!combo.modifiers || !combo.modifiers.length)) {
365
473
  return true;
366
474
  }
@@ -397,7 +505,22 @@ class InputSystem {
397
505
  clientY: event.clientY,
398
506
  button: MouseButton.MIDDLE
399
507
  };
400
- this.checkCombinations(InputType.WHEEL, wheelData);
508
+ // Trackpad pinch supplies ctrlKey on the wheel event without a keydown.
509
+ // Keep these modifiers local to this event; never latch them into keys.
510
+ const modifiers = new Set(this.getActiveModifiers(this.activeKeys));
511
+ if (event.ctrlKey) {
512
+ modifiers.add(ModifierKey.CTRL);
513
+ }
514
+ if (event.metaKey) {
515
+ modifiers.add(ModifierKey.META);
516
+ }
517
+ if (event.shiftKey) {
518
+ modifiers.add(ModifierKey.SHIFT);
519
+ }
520
+ if (event.altKey) {
521
+ modifiers.add(ModifierKey.ALT);
522
+ }
523
+ this.checkCombinations(InputType.WHEEL, wheelData, [...modifiers]);
401
524
  // Remove wheel key immediately as scrolling is continuous
402
525
  this.activeKeys.delete(key);
403
526
  }
@@ -459,6 +582,7 @@ class InputSystem {
459
582
  this.timers.forEach((timer) => clearTimeout(timer));
460
583
  this.timers.clear();
461
584
  this.activeKeys.clear();
585
+ this.heldKeyboardKeys.clear();
462
586
  this.combinations = {};
463
587
  this._startPos = null;
464
588
  this.pointerInputBlocked = false;
@@ -482,17 +606,20 @@ class InputSystem {
482
606
  failures.push(error);
483
607
  }
484
608
  };
485
- if (this.pointerTarget)
609
+ if (this.pointerTarget) {
486
610
  this.removePointerListeners(this.pointerTarget, attempt);
487
- if (this.browserHost)
611
+ }
612
+ if (this.browserHost) {
488
613
  this.removeKeyboardListeners(this.browserHost, attempt);
614
+ }
489
615
  this.pointerTarget = null;
490
616
  this.browserHost = null;
491
617
  this.browserListeners.clear();
492
618
  this.clearTransientState();
493
619
  this.registry.clear();
494
- if (failures.length > 0)
620
+ if (failures.length > 0) {
495
621
  throw failures[0];
622
+ }
496
623
  }
497
624
  }
498
625
  export { InputSystem };
package/dist/keymap.js CHANGED
@@ -16,10 +16,12 @@ export class KeyMap {
16
16
  navigator.platform ||
17
17
  '').toLowerCase();
18
18
  const userAgent = navigator.userAgent.toLowerCase();
19
- if (platform.includes('mac') || userAgent.includes('mac'))
19
+ if (platform.includes('mac') || userAgent.includes('mac')) {
20
20
  return Platforms.MAC;
21
- if (platform.includes('win') || userAgent.includes('win'))
21
+ }
22
+ if (platform.includes('win') || userAgent.includes('win')) {
22
23
  return Platforms.WINDOWS;
24
+ }
23
25
  return Platforms.LINUX;
24
26
  }
25
27
  createKeyMap() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyra/input-system",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
4
4
  "description": "Keyboard and Mouse event listener plugin",
5
5
  "author": "Asa Tsai",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "git+https://github.com/karote00/asyra.git",
10
10
  "directory": "packages/input-system"
11
11
  },
12
- "homepage": "https://asyra-framework.vercel.app",
12
+ "homepage": "https://asyra-framework.pages.dev",
13
13
  "type": "module",
14
14
  "engines": {
15
15
  "node": "24.x"
@@ -38,7 +38,7 @@
38
38
  "has:test": "find . -type f \\( -name \"*.test.ts\" -o -name \"*.test.tsx\" \\) | grep -q .",
39
39
  "test": "yarn has:test && vitest || echo 'No test files found. Skipping test.'",
40
40
  "test:local": "vitest run",
41
- "test:ci": "yarn has:test && vitest run --coverage --reporter=default --reporter=junit || echo 'No test files found. Skipping test:ci.'"
41
+ "test:ci": "yarn has:test && vitest run --coverage --reporter=default --reporter=junit"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@vitest/coverage-v8": "^3.2.6",
@@ -46,7 +46,7 @@
46
46
  "vitest": "^3.2.6"
47
47
  },
48
48
  "dependencies": {
49
- "@asyra/reactive-events": "0.5.4",
50
- "@asyra/utils": "0.5.2"
49
+ "@asyra/reactive-events": "0.6.0",
50
+ "@asyra/utils": "0.6.0"
51
51
  }
52
52
  }