This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathResolvers.ts
186 lines (149 loc) · 4.88 KB
/
Resolvers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { IKeyInfo, IKeyMap, KeyboardLayoutManager } from "./KeyboardLayout"
/**
* Interface describing a 'key resolver' - a strategy
* for resolving a keyboard event to a vim-facing input event.
*
* Key resolvers are intended to be chained together.
*
* If a key resolver returns null, it will prevent processing that key.
*/
export type KeyResolver = (evt: KeyboardEvent, previousResolution: string | null) => string | null
const keysToIgnore = [
"Shift",
"Control",
"Alt",
"AltGraph",
"CapsLock",
"Pause",
"ScrollLock",
"AudioVolumeUp",
"AudioVolumeDown",
]
export const ignoreMetaKeyResolver = (
evt: KeyboardEvent,
previousResolution: string | null,
): string | null => {
if (keysToIgnore.indexOf(evt.key) >= 0) {
return null
} else {
return evt.key
}
}
const keysToRemap: { [key: string]: string } = {
Backspace: "bs",
Escape: "esc",
Enter: "enter",
Tab: "tab", // Tab
ArrowLeft: "left", // ArrowLeft
ArrowUp: "up", // ArrowUp
ArrowRight: "right", // ArrowRight
ArrowDown: "down", // ArrowDown
Insert: "insert",
" ": "space",
}
export const remapResolver = (
evt: KeyboardEvent,
previousResolution: string | null,
): string | null => {
return keysToRemap[evt.key] ? keysToRemap[evt.key] : previousResolution
}
export const getMetaKeyResolver = () => {
const keyboardLayout: KeyboardLayoutManager = new KeyboardLayoutManager()
let keyMap = keyboardLayout.getCurrentKeyMap()
keyboardLayout.onKeyMapChanged.subscribe(() => {
keyMap = keyboardLayout.getCurrentKeyMap()
})
return (evt: KeyboardEvent, previousResolution: string | null): null | string => {
const isCharacterFromShiftKey = isShiftCharacter(keyMap, evt)
const isCharacterFromAltGraphKey = isAltGraphCharacter(keyMap, evt)
let mappedKey = previousResolution
if (mappedKey === "<") {
mappedKey = "lt"
}
const metaPressed = evt.metaKey
let controlPressed = false
// On Windows, when the AltGr key is pressed, _both_
// the evt.ctrlKey and evt.altKey are set to true.
if (evt.ctrlKey && !isCharacterFromAltGraphKey) {
mappedKey = "c-" + previousResolution + ""
controlPressed = true
evt.preventDefault()
}
if (evt.shiftKey && (!isCharacterFromShiftKey || controlPressed || metaPressed)) {
mappedKey = "s-" + mappedKey
}
if (evt.altKey && !isCharacterFromAltGraphKey) {
mappedKey = "a-" + mappedKey
evt.preventDefault()
}
if (metaPressed) {
mappedKey = "m-" + mappedKey
evt.preventDefault()
}
if (mappedKey.length > 1) {
mappedKey = "<" + mappedKey.toLowerCase() + ">"
}
return mappedKey
}
}
export const createMetaKeyResolver = (keyMap: IKeyMap) => {
return (evt: KeyboardEvent, previousResolution: string | null): null | string => {
const isCharacterFromShiftKey = isShiftCharacter(keyMap, evt)
const isCharacterFromAltGraphKey = isAltGraphCharacter(keyMap, evt)
let mappedKey = previousResolution
if (mappedKey === "<") {
mappedKey = "lt"
}
const metaPressed = evt.metaKey
let controlPressed = false
// On Windows, when the AltGr key is pressed, _both_
// the evt.ctrlKey and evt.altKey are set to true.
if (evt.ctrlKey && !isCharacterFromAltGraphKey) {
mappedKey = "c-" + previousResolution + ""
controlPressed = true
evt.preventDefault()
}
if (evt.shiftKey && (!isCharacterFromShiftKey || controlPressed || metaPressed)) {
mappedKey = "s-" + mappedKey
}
if (evt.altKey && !isCharacterFromAltGraphKey) {
mappedKey = "a-" + mappedKey
evt.preventDefault()
}
if (metaPressed) {
mappedKey = "m-" + mappedKey
evt.preventDefault()
}
if (mappedKey.length > 1) {
mappedKey = "<" + mappedKey.toLowerCase() + ">"
}
return mappedKey
}
}
const isShiftCharacter = (keyMap: IKeyMap, evt: KeyboardEvent): boolean => {
const { key, code } = evt
const mappedKey: IKeyInfo = keyMap[code]
if (!mappedKey) {
return false
}
if (code === "Space") {
return false
}
if (mappedKey.withShift === key || mappedKey.withAltGraphShift === key) {
return true
} else {
return false
}
}
const isAltGraphCharacter = (keyMap: IKeyMap, evt: KeyboardEvent): boolean => {
const { key, code } = evt
const mappedKey: IKeyInfo = keyMap[code]
if (!mappedKey) {
return false
}
if (mappedKey.withAltGraph === key || mappedKey.withAltGraphShift === key) {
return true
} else {
return false
}
}