1
- // @ts -ignore No DefinitelyTyped package exists
1
+ // @ts -ignore todo - No DefinitelyTyped package exists for this
2
2
import eventTypes from 'dom-event-types'
3
+ import { Key } from 'readline'
4
+
5
+ const keyCodesByKeyName = {
6
+ backspace : 8 ,
7
+ tab : 9 ,
8
+ enter : 13 ,
9
+ esc : 27 ,
10
+ space : 32 ,
11
+ pageup : 33 ,
12
+ pagedown : 34 ,
13
+ end : 35 ,
14
+ home : 36 ,
15
+ left : 37 ,
16
+ up : 38 ,
17
+ right : 39 ,
18
+ down : 40 ,
19
+ insert : 45 ,
20
+ delete : 46
21
+ } as const
22
+
23
+ // modifiers to keep an eye on
24
+ const ignorableKeyModifiers = [ 'stop' , 'prevent' , 'self' , 'exact' ]
25
+ const systemKeyModifiers = [ 'ctrl' , 'shift' , 'alt' , 'meta' ] as const
26
+ const mouseKeyModifiers = [ 'left' , 'middle' , 'right' ] as const
27
+
28
+ type KeyName = keyof typeof keyCodesByKeyName
29
+ type Modifier =
30
+ | typeof systemKeyModifiers [ number ]
31
+ | typeof mouseKeyModifiers [ number ]
3
32
4
33
interface TriggerOptions {
5
34
code ?: String
@@ -10,24 +39,19 @@ interface TriggerOptions {
10
39
11
40
interface EventParams {
12
41
eventType : string
13
- modifiers : KeyNameArray
42
+ modifiers : KeyName [ ]
14
43
options ?: TriggerOptions
15
44
}
16
45
17
- // modifiers to keep an eye on
18
- const ignorableKeyModifiers = [ 'stop' , 'prevent' , 'self' , 'exact' ]
19
- const systemKeyModifiers = [ 'ctrl' , 'shift' , 'alt' , 'meta' ]
20
- const mouseKeyModifiers = [ 'left' , 'middle' , 'right' ]
21
-
22
46
/**
23
47
* Groups modifiers into lists
24
48
*/
25
- function generateModifiers ( modifiers : KeyNameArray , isOnClick : boolean ) {
26
- const keyModifiers : KeyNameArray = [ ]
27
- const systemModifiers : string [ ] = [ ]
49
+ function generateModifiers ( modifiers : KeyName [ ] , isOnClick : boolean ) {
50
+ const keyModifiers : KeyName [ ] = [ ]
51
+ const systemModifiers : Modifier [ ] = [ ]
28
52
29
53
for ( let i = 0 ; i < modifiers . length ; i ++ ) {
30
- const modifier = modifiers [ i ]
54
+ const modifier : KeyName | Modifier = modifiers [ i ]
31
55
32
56
// addEventListener() options, e.g. .passive & .capture, that we dont need to handle
33
57
if ( ignorableKeyModifiers . includes ( modifier ) ) {
@@ -36,10 +60,15 @@ function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
36
60
// modifiers that require special conversion
37
61
// if passed a left/right key modifier with onClick, add it here as well.
38
62
if (
39
- systemKeyModifiers . includes ( modifier ) ||
40
- ( mouseKeyModifiers . includes ( modifier ) && isOnClick )
63
+ systemKeyModifiers . includes (
64
+ modifier as Exclude < typeof modifier , KeyName >
65
+ ) ||
66
+ ( mouseKeyModifiers . includes (
67
+ modifier as Exclude < typeof modifier , KeyName >
68
+ ) &&
69
+ isOnClick )
41
70
) {
42
- systemModifiers . push ( modifier )
71
+ systemModifiers . push ( modifier as Modifier )
43
72
} else {
44
73
keyModifiers . push ( modifier )
45
74
}
@@ -51,25 +80,6 @@ function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
51
80
}
52
81
}
53
82
54
- export type KeyNameArray = Array < keyof typeof keyCodesByKeyName >
55
- export const keyCodesByKeyName = {
56
- backspace : 8 ,
57
- tab : 9 ,
58
- enter : 13 ,
59
- esc : 27 ,
60
- space : 32 ,
61
- pageup : 33 ,
62
- pagedown : 34 ,
63
- end : 35 ,
64
- home : 36 ,
65
- left : 37 ,
66
- up : 38 ,
67
- right : 39 ,
68
- down : 40 ,
69
- insert : 45 ,
70
- delete : 46
71
- }
72
-
73
83
function getEventProperties ( eventParams : EventParams ) {
74
84
let { modifiers, options = { } , eventType } = eventParams
75
85
@@ -155,7 +165,7 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
155
165
156
166
const eventParams : EventParams = {
157
167
eventType,
158
- modifiers : modifiers as KeyNameArray ,
168
+ modifiers : modifiers as KeyName [ ] ,
159
169
options
160
170
}
161
171
const event : Event & TriggerOptions = createEvent ( eventParams )
@@ -178,4 +188,4 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
178
188
return event
179
189
}
180
190
181
- export { TriggerOptions , createDOMEvent }
191
+ export { TriggerOptions , createDOMEvent , keyCodesByKeyName , KeyName }
0 commit comments