@asyra/input-system 0.5.1 → 0.5.2

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 ADDED
@@ -0,0 +1,72 @@
1
+ # @asyra/input-system
2
+
3
+ ## 0.5.2
4
+
5
+ ### Patch Changes
6
+
7
+ - c714696: Make InputSystem import and construction DOM-neutral, add explicit browser host
8
+ and pointer-target lifecycle APIs, and provide symmetric listener cleanup.
9
+
10
+ ## 0.5.1
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [889f7b4]
15
+ - @asyra/reactive-events@0.5.1
16
+
17
+ ## 0.5.0
18
+
19
+ ### Minor Changes
20
+
21
+ - Exceptional synchronized minor release for the fixed 19-package Framework set.
22
+
23
+ ### Patch Changes
24
+
25
+ - Updated dependencies
26
+ - @asyra/reactive-events@0.5.0
27
+ - @asyra/utils@0.5.0
28
+
29
+ ## 0.2.5
30
+
31
+ ### Patch Changes
32
+
33
+ - Auto-patch all packages.
34
+ - Updated dependencies
35
+ - @asyra/reactive-events@0.2.5
36
+ - @asyra/utils@0.2.5
37
+
38
+ ## 0.2.4
39
+
40
+ ### Patch Changes
41
+
42
+ - Auto-patch all packages.
43
+ - Updated dependencies
44
+ - @asyra/reactive-events@0.2.4
45
+ - @asyra/utils@0.2.4
46
+
47
+ ## 0.2.3
48
+
49
+ ### Patch Changes
50
+
51
+ - Auto-patch all packages.
52
+ - Updated dependencies
53
+ - @asyra/reactive-events@0.2.3
54
+ - @asyra/utils@0.2.3
55
+
56
+ ## 0.2.2
57
+
58
+ ### Patch Changes
59
+
60
+ - Auto-patch all packages.
61
+ - Updated dependencies
62
+ - @asyra/reactive-events@0.2.2
63
+ - @asyra/utils@0.2.2
64
+
65
+ ## 0.2.1
66
+
67
+ ### Patch Changes
68
+
69
+ - Auto-patch all packages.
70
+ - Updated dependencies
71
+ - @asyra/reactive-events@0.2.1
72
+ - @asyra/utils@0.2.1
package/README.md CHANGED
@@ -1,83 +1,50 @@
1
- # Input System
1
+ # `@asyra/input-system`
2
2
 
3
- A powerful and flexible keyboard and mouse event listener plugin designed for modern web applications. This library simplifies the handling of complex input combinations, making it easier to manage user interactions in your projects.
3
+ Environment-neutral semantic input registration with explicit browser host attachment.
4
4
 
5
- ## Features
5
+ ## Requirements
6
6
 
7
- - **Cross-Platform Support**: Works seamlessly on Windows, Mac, and Linux.
8
- - **Customizable Key Combinations**: Define your own keyboard shortcuts and mouse actions.
9
- - **Event Listeners**: Easily attach callbacks to specific actions.
10
- - **TypeScript Support**: Built with TypeScript for better type safety and developer experience.
7
+ - Node.js 24.x
8
+ - Yarn 4.3.1 for this repository's maintained workflows
11
9
 
12
- ## Installation
13
-
14
- You can install the Input System package via npm or yarn:
15
-
16
- ### Using npm
10
+ ## Install
17
11
 
18
12
  ```bash
19
13
  npm install @asyra/input-system
20
14
  ```
21
15
 
22
- ### Using yarn
23
-
24
- ```bash
25
- yarn add @asyra/input-system
16
+ ```ts
17
+ import inputSystem, { keyMap } from '@asyra/input-system'
26
18
  ```
27
19
 
28
- ## Usage
20
+ Use only the package root and the explicitly documented public subpaths.
29
21
 
30
- The default browser singleton listens for raw input. Apps define named events by
31
- registering typed combinations, then attach and release listeners by callback
32
- identity:
22
+ ## Owns
33
23
 
34
- ```typescript
35
- import inputSystem, { keyMap } from '@asyra/input-system'
36
- import { InputType, ModifierKey, PointerKey } from '@asyra/utils'
37
-
38
- inputSystem.registry.registerKeyCombinations({
39
- UNDO: [
40
- {
41
- type: InputType.KEYBOARD,
42
- keys: [keyMap.keys.KeyZ],
43
- modifiers: [ModifierKey.META]
44
- }
45
- ],
46
- DRAG_START: [
47
- {
48
- type: InputType.POINTER,
49
- keys: [PointerKey.LEFT_MOUSE_DOWN]
50
- }
51
- ]
52
- })
53
-
54
- const handleUndo = () => console.log('Undo triggered')
55
-
56
- inputSystem.on('UNDO', handleUndo)
57
- inputSystem.off('UNDO', handleUndo)
58
- ```
24
+ - normalized keyboard, pointer, wheel, and mapped-event routing
25
+ - instance-owned browser attachment, switching, detach, reset, and disposal
26
+
27
+ ## Does not own
59
28
 
60
- Event names and combinations are app-owned. Register each event name once for a
61
- given registry. `off(...)` removes only the supplied listener and returns
62
- `false` when that listener is not registered.
29
+ - Feature decisions, scene mutations, render-layer behavior, context-menu policy, or unconditional native-menu suppression
63
30
 
64
- ## Contributing
31
+ ## Start here
65
32
 
66
- This repository is not accepting external issues or pull requests at this time.
67
- You are welcome to fork the package and adapt it for your own application; see
68
- the repository root README for the current contribution policy.
33
+ Import or construct it without browser globals; attach a browser host only when the runtime actually needs browser input.
69
34
 
70
- ## License
35
+ ## Lifecycle and composition
71
36
 
72
- This project is licensed under the MIT License.
37
+ Attachment adds listeners to the selected host and target. Reattachment is idempotent and target switching removes prior listeners first. `reset()` preserves attachment; `dispose()` detaches and clears transient state. Node-safe construction does not imply Headless Core support.
73
38
 
74
- ## Release support
39
+ ## Learn more
75
40
 
76
- The `@asyra/input-system` `0.2.5` ESM artifact supports Node.js 24.x. Use only
77
- package-root exports. See the
78
- [Framework release support contract](../../docs/ai/framework/RELEASE_SUPPORT.md).
41
+ - [Complete package guide](https://github.com/karote00/asyra/blob/main/docs/public/reference/packages/input-system.md)
42
+ - [Framework release support](https://github.com/karote00/asyra/blob/main/docs/ai/framework/RELEASE_SUPPORT.md)
79
43
 
80
- ## Acknowledgments
44
+ ## Support and policy
81
45
 
82
- - Inspired by various input handling libraries and frameworks.
83
- - Thanks to the open-source community for their contributions and support.
46
+ This repository does not accept external issues or contributions. You may use,
47
+ inspect, and fork the package under the [MIT License](https://github.com/karote00/asyra/blob/main/LICENSE).
48
+ Follow the [security policy](https://github.com/karote00/asyra/blob/main/SECURITY.md) for
49
+ security-sensitive reports and the [root policy](https://github.com/karote00/asyra) for the
50
+ current support boundary.
@@ -3,7 +3,8 @@ 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 _previousWatchedElement;
6
+ private browserHost;
7
+ private pointerTarget;
7
8
  private combinations;
8
9
  private keyMap;
9
10
  private activeKeys;
@@ -14,7 +15,12 @@ declare class InputSystem {
14
15
  private pointerCaptureId;
15
16
  registry: InputSystemRegistry;
16
17
  constructor();
17
- private setupListeners;
18
+ attachBrowserHost(host: Window, pointerTarget?: Window | HTMLElement): void;
19
+ detachBrowserHost(): void;
20
+ private addKeyboardListeners;
21
+ private removeKeyboardListeners;
22
+ private addPointerListeners;
23
+ private removePointerListeners;
18
24
  setCombinations(combinations: Combinations): void;
19
25
  on(action: string, callback: Callback): this;
20
26
  off(action: string, callback: Callback): boolean;
@@ -39,6 +45,7 @@ declare class InputSystem {
39
45
  private clearPointerState;
40
46
  private isPointerKey;
41
47
  getAllModifiers(modifiers: ModifierKey[]): ModifierKeys;
48
+ private clearTransientState;
42
49
  dispose(): void;
43
50
  reset(): void;
44
51
  }
@@ -31,7 +31,8 @@ const getMouseButtonFromButtonsMask = (buttons) => {
31
31
  return MouseButton.NONE;
32
32
  };
33
33
  class InputSystem {
34
- _previousWatchedElement;
34
+ browserHost;
35
+ pointerTarget;
35
36
  combinations = {};
36
37
  keyMap;
37
38
  activeKeys;
@@ -42,7 +43,8 @@ class InputSystem {
42
43
  pointerCaptureId;
43
44
  registry;
44
45
  constructor() {
45
- this._previousWatchedElement = window;
46
+ this.browserHost = null;
47
+ this.pointerTarget = null;
46
48
  this.keyMap = keymap;
47
49
  this.activeKeys = new Set();
48
50
  this.listeners = new Map();
@@ -51,16 +53,59 @@ class InputSystem {
51
53
  this.pointerInputBlocked = false;
52
54
  this.pointerCaptureId = null;
53
55
  this.registry = new InputSystemRegistry();
54
- this.setupListeners();
55
56
  }
56
- setupListeners() {
57
- window.addEventListener('keydown', this.handleKeyDown);
58
- window.addEventListener('keyup', this.handleKeyUp);
59
- window.addEventListener('mousedown', this.handleMouseDown);
60
- window.addEventListener('mouseup', this.handleMouseUp);
61
- window.addEventListener('mousemove', this.handleMouseMove);
62
- window.addEventListener('dblclick', this.handleDoubleClick);
63
- window.addEventListener('wheel', this.handleWheel, WHEEL_EVENT_OPTIONS);
57
+ attachBrowserHost(host, pointerTarget = host) {
58
+ if (this.browserHost === host && this.pointerTarget === pointerTarget) {
59
+ return;
60
+ }
61
+ if (this.pointerTarget && this.pointerTarget !== pointerTarget) {
62
+ this.removePointerListeners(this.pointerTarget);
63
+ this.pointerTarget = null;
64
+ }
65
+ if (this.browserHost && this.browserHost !== host) {
66
+ this.removeKeyboardListeners(this.browserHost);
67
+ this.browserHost = null;
68
+ }
69
+ if (!this.browserHost) {
70
+ this.addKeyboardListeners(host);
71
+ this.browserHost = host;
72
+ }
73
+ if (!this.pointerTarget) {
74
+ this.addPointerListeners(pointerTarget);
75
+ this.pointerTarget = pointerTarget;
76
+ }
77
+ }
78
+ detachBrowserHost() {
79
+ if (this.pointerTarget) {
80
+ this.removePointerListeners(this.pointerTarget);
81
+ this.pointerTarget = null;
82
+ }
83
+ if (this.browserHost) {
84
+ this.removeKeyboardListeners(this.browserHost);
85
+ this.browserHost = null;
86
+ }
87
+ }
88
+ addKeyboardListeners(host) {
89
+ host.addEventListener('keydown', this.handleKeyDown);
90
+ host.addEventListener('keyup', this.handleKeyUp);
91
+ }
92
+ removeKeyboardListeners(host) {
93
+ host.removeEventListener('keydown', this.handleKeyDown);
94
+ host.removeEventListener('keyup', this.handleKeyUp);
95
+ }
96
+ addPointerListeners(target) {
97
+ target.addEventListener('mousedown', this.handleMouseDown);
98
+ target.addEventListener('mouseup', this.handleMouseUp);
99
+ target.addEventListener('mousemove', this.handleMouseMove);
100
+ target.addEventListener('dblclick', this.handleDoubleClick);
101
+ target.addEventListener('wheel', this.handleWheel, WHEEL_EVENT_OPTIONS);
102
+ }
103
+ removePointerListeners(target) {
104
+ target.removeEventListener('mousedown', this.handleMouseDown);
105
+ target.removeEventListener('mouseup', this.handleMouseUp);
106
+ target.removeEventListener('mousemove', this.handleMouseMove);
107
+ target.removeEventListener('dblclick', this.handleDoubleClick);
108
+ target.removeEventListener('wheel', this.handleWheel, WHEEL_EVENT_OPTIONS);
64
109
  }
65
110
  setCombinations(combinations) {
66
111
  this.combinations = combinations;
@@ -88,19 +133,11 @@ class InputSystem {
88
133
  return true;
89
134
  }
90
135
  switchWatchedElement(watchedElement) {
91
- this._previousWatchedElement.removeEventListener('mousedown', this.handleMouseDown);
92
- this._previousWatchedElement.removeEventListener('mouseup', this.handleMouseUp);
93
- this._previousWatchedElement.removeEventListener('mousemove', this.handleMouseMove);
94
- this._previousWatchedElement.removeEventListener('dblclick', this.handleDoubleClick);
95
- this._previousWatchedElement.removeEventListener('wheel', this.handleWheel, WHEEL_EVENT_OPTIONS);
96
- watchedElement.addEventListener('mousedown', this.handleMouseDown);
97
- watchedElement.addEventListener('mouseup', this.handleMouseUp);
98
- watchedElement.addEventListener('mousemove', this.handleMouseMove);
99
- watchedElement.addEventListener('dblclick', this.handleDoubleClick);
100
- watchedElement.addEventListener('wheel', this.handleWheel, {
101
- passive: false
102
- });
103
- this._previousWatchedElement = watchedElement;
136
+ const host = watchedElement.ownerDocument.defaultView;
137
+ if (!host) {
138
+ throw new Error('[InputSystem] Watched element must belong to a document with a Window');
139
+ }
140
+ this.attachBrowserHost(host, watchedElement);
104
141
  }
105
142
  startTimer(key) {
106
143
  if (this.timers.has(key)) {
@@ -382,15 +419,22 @@ class InputSystem {
382
419
  });
383
420
  return allModifiers;
384
421
  }
385
- dispose() {
422
+ clearTransientState() {
386
423
  this.listeners.clear();
387
424
  this.timers.forEach((timer) => clearTimeout(timer));
388
425
  this.timers.clear();
389
426
  this.activeKeys.clear();
390
427
  this.combinations = {};
428
+ this._startPos = null;
429
+ this.pointerInputBlocked = false;
430
+ this.pointerCaptureId = null;
431
+ }
432
+ dispose() {
433
+ this.detachBrowserHost();
434
+ this.clearTransientState();
391
435
  }
392
436
  reset() {
393
- this.dispose();
437
+ this.clearTransientState();
394
438
  }
395
439
  }
396
440
  export { InputSystem };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyra/input-system",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Keyboard and Mouse event listener plugin",
5
5
  "author": "Asa Tsai",
6
6
  "license": "MIT",
@@ -43,4 +43,4 @@
43
43
  "@asyra/reactive-events": "0.5.1",
44
44
  "@asyra/utils": "0.5.0"
45
45
  }
46
- }
46
+ }