@asyra/input-system 0.2.5 → 0.5.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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -48
  3. package/dist/event-mappings.d.ts +37 -2
  4. package/dist/event-mappings.js +1 -78
  5. package/dist/index.d.ts +5 -2
  6. package/dist/index.js +5 -3
  7. package/dist/input-system.d.ts +11 -0
  8. package/dist/input-system.js +133 -11
  9. package/dist/registry.d.ts +39 -0
  10. package/dist/registry.js +57 -0
  11. package/dist/subscribe.js +14 -2
  12. package/package.json +22 -4
  13. package/.turbo/turbo-test$colon$ci.log +0 -132
  14. package/.turbo/turbo-test$colon$local.log +0 -52
  15. package/.turbo/turbo-test.log +0 -0
  16. package/CHANGELOG.md +0 -46
  17. package/coverage/base.css +0 -224
  18. package/coverage/block-navigation.js +0 -87
  19. package/coverage/clover.xml +0 -573
  20. package/coverage/constants.ts.html +0 -94
  21. package/coverage/coverage-final.json +0 -8963
  22. package/coverage/event-mappings.ts.html +0 -370
  23. package/coverage/favicon.png +0 -0
  24. package/coverage/index.html +0 -206
  25. package/coverage/index.ts.html +0 -106
  26. package/coverage/input-system.ts.html +0 -1216
  27. package/coverage/keymap.ts.html +0 -616
  28. package/coverage/matcher.ts.html +0 -187
  29. package/coverage/prettify.css +0 -1
  30. package/coverage/prettify.js +0 -2
  31. package/coverage/sort-arrow-sprite.png +0 -0
  32. package/coverage/sorter.js +0 -196
  33. package/coverage/subscribe.ts.html +0 -109
  34. package/dist/__tests__/input-system.test.d.ts +0 -1
  35. package/dist/__tests__/input-system.test.js +0 -279
  36. package/dist/matcher.d.ts +0 -16
  37. package/dist/matcher.js +0 -32
  38. package/src/__tests__/input-system.test.ts +0 -398
  39. package/src/constants.ts +0 -3
  40. package/src/event-mappings.ts +0 -95
  41. package/src/index.ts +0 -7
  42. package/src/input-system.ts +0 -377
  43. package/src/keymap.ts +0 -177
  44. package/src/matcher.ts +0 -34
  45. package/src/subscribe.ts +0 -8
  46. package/test-setup.ts +0 -6
  47. package/tsconfig.json +0 -22
  48. package/vitest.config.ts +0 -8
@@ -1,398 +0,0 @@
1
- import {
2
- describe,
3
- it,
4
- expect,
5
- vi,
6
- beforeEach,
7
- type MockInstance,
8
- type Mock
9
- } from 'vitest'
10
- import { InputSystem } from '../input-system'
11
-
12
- import {
13
- InputSystemEvents,
14
- InputType,
15
- RawInputEvent,
16
- ModifierKey,
17
- PointerEventData
18
- } from '@asyra/utils'
19
- import keyMap from '../keymap'
20
- import { CLEAR_KEY_TIME } from '../constants'
21
- import { InputEventMappings } from '../event-mappings'
22
- import * as EventMappingsModule from '../event-mappings'
23
-
24
- vi.mock('../event-mappings', async (importOriginal) => {
25
- const actual = await importOriginal<typeof import('../event-mappings')>()
26
- return {
27
- ...actual
28
- }
29
- })
30
-
31
- describe('InputSystem', () => {
32
- let inputSystem: InputSystem
33
- let addEventListenerSpy: MockInstance
34
- let preventDefaultSpy: Mock
35
- let clearTimeoutSpy: MockInstance
36
- let setTimeoutSpy: MockInstance
37
- let mockHTMLElement: HTMLElement
38
-
39
- beforeEach(() => {
40
- vi.clearAllMocks()
41
-
42
- // Mock window event listeners
43
- addEventListenerSpy = vi.spyOn(window, 'addEventListener')
44
- preventDefaultSpy = vi.fn()
45
- clearTimeoutSpy = vi.spyOn(global, 'clearTimeout')
46
- setTimeoutSpy = vi.spyOn(global, 'setTimeout')
47
-
48
- // Mock HTMLElement for event.target
49
- mockHTMLElement = {
50
- tagName: 'div',
51
- isContentEditable: false
52
- // Add other properties as needed by the code under test
53
- } as unknown as HTMLElement
54
-
55
- inputSystem = new InputSystem()
56
- })
57
-
58
- // Test constructor and setupListeners
59
- it('should set up event listeners on window during construction', () => {
60
- expect(addEventListenerSpy).toHaveBeenCalledWith(
61
- 'keydown',
62
- expect.any(Function)
63
- )
64
- expect(addEventListenerSpy).toHaveBeenCalledWith(
65
- 'keyup',
66
- expect.any(Function)
67
- )
68
- expect(addEventListenerSpy).toHaveBeenCalledWith(
69
- 'mousedown',
70
- expect.any(Function)
71
- )
72
- expect(addEventListenerSpy).toHaveBeenCalledWith(
73
- 'mouseup',
74
- expect.any(Function)
75
- )
76
- expect(addEventListenerSpy).toHaveBeenCalledWith(
77
- 'mousemove',
78
- expect.any(Function)
79
- )
80
- expect(addEventListenerSpy).toHaveBeenCalledWith(
81
- 'wheel',
82
- expect.any(Function),
83
- { passive: false }
84
- )
85
- expect(addEventListenerSpy).toHaveBeenCalledWith(
86
- 'contextmenu',
87
- expect.any(Function)
88
- )
89
- })
90
-
91
- // Test handleKeyDown
92
- it('should add key to activeKeys and prevent default if not input active', () => {
93
- const event = new KeyboardEvent('keydown', { code: 'KeyA' })
94
- Object.defineProperty(event, 'target', { value: mockHTMLElement }) // Not an input field
95
- event.preventDefault = preventDefaultSpy
96
-
97
- inputSystem['handleKeyDown'](event)
98
-
99
- expect(inputSystem['activeKeys'].has(keyMap.keys.KeyA)).toBe(true)
100
- expect(preventDefaultSpy).toHaveBeenCalled()
101
- })
102
-
103
- it('should not prevent default if input field is active', () => {
104
- const event = new KeyboardEvent('keydown', { code: 'KeyA' })
105
- const inputElement = document.createElement('input')
106
- Object.defineProperty(event, 'target', { value: inputElement })
107
- event.preventDefault = preventDefaultSpy
108
-
109
- inputSystem['handleKeyDown'](event)
110
-
111
- expect(inputSystem['activeKeys'].has(keyMap.keys.KeyA)).toBe(true)
112
- expect(preventDefaultSpy).not.toHaveBeenCalled()
113
- })
114
-
115
- it('should start timer for non-modifier keys', () => {
116
- vi.spyOn(keyMap, 'isModifierKeys').mockReturnValue(false)
117
- const event = new KeyboardEvent('keydown', { code: 'KeyA' })
118
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
119
- event.preventDefault = preventDefaultSpy
120
-
121
- inputSystem['handleKeyDown'](event)
122
-
123
- expect(setTimeoutSpy).toHaveBeenCalledTimes(1)
124
- expect(setTimeoutSpy).toHaveBeenCalledWith(
125
- expect.any(Function),
126
- CLEAR_KEY_TIME
127
- )
128
- })
129
-
130
- it('should not start timer for modifier keys', () => {
131
- vi.spyOn(keyMap, 'isModifierKeys').mockReturnValue(true)
132
- const event = new KeyboardEvent('keydown', { code: 'ShiftLeft' })
133
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
134
- event.preventDefault = preventDefaultSpy
135
-
136
- inputSystem['handleKeyDown'](event)
137
-
138
- expect(setTimeoutSpy).not.toHaveBeenCalled()
139
- })
140
-
141
- // Test handleKeyUp
142
- it('should remove key from activeKeys and clear timer', () => {
143
- const key = keyMap.keys.KeyA
144
- inputSystem['activeKeys'].add(key)
145
- const timerId = 123
146
- inputSystem['timers'].set(key, timerId as unknown as NodeJS.Timeout)
147
- const event = new KeyboardEvent('keyup', { code: 'KeyA' })
148
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
149
- event.preventDefault = preventDefaultSpy
150
-
151
- inputSystem['handleKeyUp'](event)
152
-
153
- expect(inputSystem['activeKeys'].has(key)).toBe(false)
154
- expect(clearTimeoutSpy).toHaveBeenCalledWith(timerId)
155
- expect(inputSystem['timers'].has(key)).toBe(false)
156
- expect(preventDefaultSpy).toHaveBeenCalled()
157
- })
158
-
159
- // Test handleMouseDown
160
- it('should add mouse button key to activeKeys and set startPos', () => {
161
- const event = new MouseEvent('mousedown', {
162
- button: 0,
163
- clientX: 10,
164
- clientY: 20
165
- })
166
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
167
- inputSystem['handleMouseDown'](event)
168
-
169
- expect(inputSystem['activeKeys'].has('leftMouseDown')).toBe(true)
170
- expect(inputSystem['_startPos']).toEqual({ clientX: 10, clientY: 20 })
171
- })
172
-
173
- // Test handleMouseUp
174
- it('should add mouse up key, remove mouse down key, and trigger checkCombinations', () => {
175
- inputSystem['activeKeys'].add('leftMouseDown')
176
- const event = new MouseEvent('mouseup', {
177
- button: 0,
178
- clientX: 10,
179
- clientY: 20
180
- })
181
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
182
- inputSystem['handleMouseUp'](event)
183
- expect(inputSystem['activeKeys'].has('leftMouseUp')).toBe(false)
184
- expect(inputSystem['activeKeys'].has('leftMouseDown')).toBe(false)
185
- })
186
-
187
- // Test handleMouseMove
188
- it('should add mouse move key and trigger checkCombinations if dragging beyond threshold', () => {
189
- inputSystem['_startPos'] = { clientX: 0, clientY: 0 }
190
- const event = new MouseEvent('mousemove', {
191
- button: 0,
192
- clientX: 10,
193
- clientY: 10
194
- }) // Distance > CLICK_THRESHOLD (5)
195
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
196
- inputSystem['handleMouseMove'](event)
197
- expect(inputSystem['activeKeys'].has('leftMouseMove')).toBe(false)
198
- })
199
-
200
- it('should not add mouse move key if dragging within threshold', () => {
201
- inputSystem['_startPos'] = { clientX: 0, clientY: 0 }
202
- const event = new MouseEvent('mousemove', {
203
- button: 0,
204
- clientX: 1,
205
- clientY: 1
206
- }) // Distance < CLICK_THRESHOLD (5)
207
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
208
- inputSystem['handleMouseMove'](event)
209
-
210
- expect(inputSystem['activeKeys'].has('leftMouseMove')).toBe(false)
211
- })
212
-
213
- // Test handleWheel
214
- it('should prevent default and trigger checkCombinations for wheel event', () => {
215
- vi.spyOn(keyMap, 'isSpecialEvent').mockReturnValue(true) // Mock as special event
216
- const event = new WheelEvent('wheel', {
217
- deltaX: 10,
218
- deltaY: 20,
219
- clientX: 50,
220
- clientY: 60
221
- })
222
- Object.defineProperty(event, 'target', { value: mockHTMLElement })
223
- event.preventDefault = preventDefaultSpy
224
-
225
- inputSystem['handleWheel'](event)
226
-
227
- expect(preventDefaultSpy).toHaveBeenCalled()
228
- expect(inputSystem['activeKeys'].has('wheel')).toBe(false) // Should be removed immediately
229
- })
230
-
231
- // Test getActiveModifiers
232
- it('should return active modifier keys', () => {
233
- inputSystem['activeKeys'].add(keyMap.keys.ShiftLeft)
234
- inputSystem['activeKeys'].add(keyMap.keys.KeyA)
235
- inputSystem['activeKeys'].add(keyMap.keys.ControlLeft)
236
-
237
- // Explicitly mock to ensure it works in this test context
238
- vi.spyOn(keyMap, 'isModifierKeys').mockImplementation((key: string) =>
239
- ['Shift', 'Ctrl'].includes(key)
240
- )
241
-
242
- const modifiers = inputSystem.getActiveModifiers(inputSystem['activeKeys'])
243
- expect(modifiers).toEqual(['shift', 'ctrl'])
244
- })
245
-
246
- // Test getAllModifiers
247
- it('should return all modifier keys status', () => {
248
- const activeModifiers: ModifierKey[] = [ModifierKey.SHIFT, ModifierKey.ALT]
249
- const allModifiers = inputSystem.getAllModifiers(activeModifiers)
250
- expect(allModifiers).toEqual({
251
- meta: false,
252
- ctrl: false,
253
- alt: true,
254
- shift: true
255
- })
256
- })
257
-
258
- // Test checkCombinations and triggerAction
259
- it('should trigger action for matching combination', () => {
260
- const triggerActionSpy = vi.spyOn(
261
- inputSystem,
262
- 'triggerAction' as keyof InputSystem
263
- )
264
- inputSystem['activeKeys'].add(keyMap.keys.KeyA)
265
- const INPUT_KEYBOARD_A = 'INPUT_KEYBOARD_A' as InputSystemEvents
266
- vi.mocked(EventMappingsModule).InputEventMappings = {
267
- ...InputEventMappings,
268
- [INPUT_KEYBOARD_A]: [
269
- { type: InputType.KEYBOARD, keys: [keyMap.keys.KeyA], modifiers: [] }
270
- ]
271
- }
272
-
273
- inputSystem['checkCombinations'](InputType.KEYBOARD)
274
-
275
- expect(triggerActionSpy).toHaveBeenCalledTimes(1)
276
- expect(triggerActionSpy).toHaveBeenCalledWith(
277
- 'INPUT_KEYBOARD_A' as InputSystemEvents,
278
- expect.objectContaining({
279
- type: InputType.KEYBOARD,
280
- keys: [keyMap.keys.KeyA]
281
- })
282
- )
283
- })
284
-
285
- it('should not trigger action for non-matching combination', () => {
286
- const triggerActionSpy = vi.spyOn(
287
- inputSystem,
288
- 'triggerAction' as keyof InputSystem
289
- )
290
- inputSystem['activeKeys'].add('KeyB') // Active key is B
291
- const INPUT_KEYBOARD_A = 'INPUT_KEYBOARD_A' as InputSystemEvents
292
- vi.mocked(EventMappingsModule).InputEventMappings = {
293
- ...InputEventMappings,
294
- [INPUT_KEYBOARD_A]: [
295
- { type: InputType.KEYBOARD, keys: [keyMap.keys.KeyA], modifiers: [] } // Mapping is for A
296
- ]
297
- }
298
-
299
- inputSystem['checkCombinations'](InputType.KEYBOARD)
300
-
301
- expect(triggerActionSpy).not.toHaveBeenCalled()
302
- })
303
-
304
- it('should trigger action with detail if provided in combo', () => {
305
- const triggerActionSpy = vi.spyOn(
306
- inputSystem,
307
- 'triggerAction' as keyof InputSystem
308
- )
309
- inputSystem['activeKeys'].add(keyMap.keys.KeyA)
310
- const mockDetail = { some: 'detail' }
311
- const INPUT_KEYBOARD_A = 'INPUT_KEYBOARD_A' as InputSystemEvents
312
- vi.mocked(EventMappingsModule).InputEventMappings = {
313
- ...InputEventMappings,
314
- [INPUT_KEYBOARD_A]: [
315
- {
316
- type: InputType.KEYBOARD,
317
- keys: [keyMap.keys.KeyA],
318
- modifiers: [],
319
- detail: mockDetail
320
- }
321
- ]
322
- }
323
-
324
- inputSystem['checkCombinations'](InputType.KEYBOARD)
325
-
326
- expect(triggerActionSpy).toHaveBeenCalledWith(
327
- 'INPUT_KEYBOARD_A' as InputSystemEvents,
328
- expect.objectContaining({
329
- detail: mockDetail
330
- })
331
- )
332
- })
333
-
334
- it('should call registered listeners when triggerAction is called', () => {
335
- const listener1 = vi.fn()
336
- const listener2 = vi.fn()
337
- const INPUT_KEYBOARD_A = 'INPUT_KEYBOARD_A' as InputSystemEvents
338
- inputSystem.on(INPUT_KEYBOARD_A, listener1)
339
- inputSystem.on(INPUT_KEYBOARD_A, listener2)
340
-
341
- const rawEvent: RawInputEvent = {
342
- type: InputType.KEYBOARD,
343
- keys: [keyMap.keys.KeyA],
344
- modifiers: { meta: false, ctrl: false, alt: false, shift: false },
345
- pointer: {} as PointerEventData
346
- }
347
- inputSystem['triggerAction'](INPUT_KEYBOARD_A, rawEvent)
348
-
349
- expect(listener1).toHaveBeenCalledTimes(1)
350
- expect(listener1).toHaveBeenCalledWith(rawEvent)
351
- expect(listener2).toHaveBeenCalledTimes(1)
352
- expect(listener2).toHaveBeenCalledWith(rawEvent)
353
- })
354
-
355
- it('should handle async listeners correctly', async () => {
356
- const asyncListener = vi.fn(() => Promise.resolve())
357
- inputSystem.on('INPUT_KEYBOARD_A' as InputSystemEvents, asyncListener)
358
-
359
- const rawEvent: RawInputEvent = {
360
- type: InputType.KEYBOARD,
361
- keys: [keyMap.keys.KeyA],
362
- modifiers: { meta: false, ctrl: false, alt: false, shift: false },
363
- pointer: {} as PointerEventData
364
- }
365
- await inputSystem['triggerAction'](
366
- 'INPUT_KEYBOARD_A' as InputSystemEvents,
367
- rawEvent
368
- )
369
-
370
- expect(asyncListener).toHaveBeenCalledTimes(1)
371
- })
372
-
373
- it('should catch errors in async listeners', async () => {
374
- const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {
375
- /* no-op */
376
- })
377
- const asyncListener = vi.fn(() => Promise.reject('Test Error'))
378
- inputSystem.on('INPUT_KEYBOARD_A' as InputSystemEvents, asyncListener)
379
-
380
- const rawEvent: RawInputEvent = {
381
- type: InputType.KEYBOARD,
382
- keys: [keyMap.keys.KeyA],
383
- modifiers: { meta: false, ctrl: false, alt: false, shift: false },
384
- pointer: {} as PointerEventData
385
- }
386
- await inputSystem['triggerAction'](
387
- 'INPUT_KEYBOARD_A' as InputSystemEvents,
388
- rawEvent
389
- )
390
-
391
- expect(asyncListener).toHaveBeenCalledTimes(1)
392
- expect(errorSpy).toHaveBeenCalledWith(
393
- 'Error in async input system callback:',
394
- 'Test Error'
395
- )
396
- errorSpy.mockRestore()
397
- })
398
- })
package/src/constants.ts DELETED
@@ -1,3 +0,0 @@
1
- export const CLICK_THRESHOLD = 3
2
-
3
- export const CLEAR_KEY_TIME = 100
@@ -1,95 +0,0 @@
1
- import {
2
- InputType,
3
- KeyboardKey,
4
- PointerKey,
5
- ModifierKey,
6
- InputSystemEvents,
7
- PrimaryToolType,
8
- DetailType
9
- } from '@asyra/utils'
10
- import keyMap from './keymap'
11
-
12
- export interface InputEventCombo {
13
- type: InputType
14
- keys: KeyboardKey[]
15
- modifiers?: ModifierKey[]
16
- detail?: DetailType
17
- }
18
-
19
- export const InputEventMappings: Record<InputSystemEvents, InputEventCombo[]> =
20
- {
21
- [InputSystemEvents.INPUT_DRAG_START]: [
22
- { type: InputType.POINTER, keys: [PointerKey.LEFT_MOUSE_DOWN] }
23
- ],
24
- [InputSystemEvents.INPUT_DRAG_UPDATE]: [
25
- {
26
- type: InputType.POINTER,
27
- keys: [PointerKey.LEFT_MOUSE_DOWN, PointerKey.LEFT_MOUSE_MOVE]
28
- }
29
- ],
30
- [InputSystemEvents.INPUT_DRAG_END]: [
31
- { type: InputType.POINTER, keys: [PointerKey.LEFT_MOUSE_UP] }
32
- ],
33
- [InputSystemEvents.INPUT_MOUSE_MOVE]: [
34
- {
35
- type: InputType.POINTER,
36
- keys: [PointerKey.LEFT_MOUSE_MOVE]
37
- }
38
- ],
39
- [InputSystemEvents.INPUT_WHEEL_SCROLL]: [
40
- { type: InputType.WHEEL, keys: [PointerKey.WHEEL] }
41
- ],
42
- [InputSystemEvents.INPUT_SHORTCUT_ARROW]: [
43
- {
44
- type: InputType.KEYBOARD,
45
- keys: [keyMap.keys.ArrowUp]
46
- },
47
- {
48
- type: InputType.KEYBOARD,
49
- keys: [keyMap.keys.ArrowDown]
50
- },
51
- {
52
- type: InputType.KEYBOARD,
53
- keys: [keyMap.keys.ArrowLeft]
54
- },
55
- {
56
- type: InputType.KEYBOARD,
57
- keys: [keyMap.keys.ArrowRight]
58
- }
59
- ],
60
- [InputSystemEvents.INPUT_SHORTCUT_UNDOREDO]: [
61
- {
62
- type: InputType.KEYBOARD,
63
- keys: [keyMap.keys.KeyZ],
64
- modifiers: [ModifierKey.META]
65
- },
66
- {
67
- type: InputType.KEYBOARD,
68
- keys: [keyMap.keys.KeyZ],
69
- modifiers: [ModifierKey.CTRL]
70
- }
71
- ],
72
- [InputSystemEvents.INPUT_SHORTCUT_ZOOM_PRESET]: [
73
- {
74
- type: InputType.KEYBOARD,
75
- keys: [keyMap.keys.Digit1],
76
- modifiers: [ModifierKey.META]
77
- }
78
- ],
79
- [InputSystemEvents.INPUT_SHORTCUT_SWITCH_PRIMARY_TOOL]: [
80
- {
81
- type: InputType.KEYBOARD,
82
- keys: [keyMap.keys.KeyR],
83
- detail: {
84
- primaryTool: PrimaryToolType.RECTANGLE
85
- }
86
- },
87
- {
88
- type: InputType.KEYBOARD,
89
- keys: [keyMap.keys.KeyV],
90
- detail: {
91
- primaryTool: PrimaryToolType.SELECT
92
- }
93
- }
94
- ]
95
- }
package/src/index.ts DELETED
@@ -1,7 +0,0 @@
1
- import inputSystem, { InputSystem } from './input-system'
2
- import { initInputSystemSubscribe } from './subscribe'
3
-
4
- initInputSystemSubscribe()
5
-
6
- export { InputSystem }
7
- export default inputSystem