@asyra/input-system 0.5.1 → 0.5.3
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 +28 -61
- package/dist/input-system.d.ts +9 -2
- package/dist/input-system.js +70 -26
- package/package.json +11 -5
package/README.md
CHANGED
|
@@ -1,83 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@asyra/input-system`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Environment-neutral semantic input registration with explicit browser host attachment.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Requirements
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
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
|
-
##
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
yarn add @asyra/input-system
|
|
16
|
+
```ts
|
|
17
|
+
import inputSystem, { keyMap } from '@asyra/input-system'
|
|
26
18
|
```
|
|
27
19
|
|
|
28
|
-
|
|
20
|
+
Use only the package root and the explicitly documented public subpaths.
|
|
29
21
|
|
|
30
|
-
|
|
31
|
-
registering typed combinations, then attach and release listeners by callback
|
|
32
|
-
identity:
|
|
22
|
+
## Owns
|
|
33
23
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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
|
-
##
|
|
31
|
+
## Start here
|
|
65
32
|
|
|
66
|
-
|
|
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
|
-
##
|
|
35
|
+
## Lifecycle and composition
|
|
71
36
|
|
|
72
|
-
|
|
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
|
-
##
|
|
39
|
+
## Learn more
|
|
75
40
|
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
##
|
|
44
|
+
## Support and policy
|
|
81
45
|
|
|
82
|
-
|
|
83
|
-
|
|
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.
|
package/dist/input-system.d.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
}
|
package/dist/input-system.js
CHANGED
|
@@ -31,7 +31,8 @@ const getMouseButtonFromButtonsMask = (buttons) => {
|
|
|
31
31
|
return MouseButton.NONE;
|
|
32
32
|
};
|
|
33
33
|
class InputSystem {
|
|
34
|
-
|
|
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.
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this.
|
|
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
|
-
|
|
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.
|
|
437
|
+
this.clearTransientState();
|
|
394
438
|
}
|
|
395
439
|
}
|
|
396
440
|
export { InputSystem };
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asyra/input-system",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Keyboard and Mouse event listener plugin",
|
|
5
5
|
"author": "Asa Tsai",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/karote00/asyra.git",
|
|
10
|
+
"directory": "packages/input-system"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://asyra-framework.vercel.app",
|
|
7
13
|
"type": "module",
|
|
8
14
|
"engines": {
|
|
9
15
|
"node": "24.x"
|
|
@@ -35,12 +41,12 @@
|
|
|
35
41
|
"test:ci": "yarn has:test && vitest run --coverage --reporter=default --reporter=junit || echo 'No test files found. Skipping test:ci.'"
|
|
36
42
|
},
|
|
37
43
|
"devDependencies": {
|
|
38
|
-
"@vitest/coverage-v8": "^2.
|
|
44
|
+
"@vitest/coverage-v8": "^3.2.6",
|
|
39
45
|
"typescript": "^5.7.2",
|
|
40
|
-
"vitest": "^2.
|
|
46
|
+
"vitest": "^3.2.6"
|
|
41
47
|
},
|
|
42
48
|
"dependencies": {
|
|
43
|
-
"@asyra/reactive-events": "
|
|
44
|
-
"@asyra/utils": "
|
|
49
|
+
"@asyra/reactive-events": "workspace:*",
|
|
50
|
+
"@asyra/utils": "workspace:*"
|
|
45
51
|
}
|
|
46
52
|
}
|