This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
helpers.coffee
242 lines (215 loc) · 6.5 KB
/
helpers.coffee
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{specificity} = require 'clear-cut'
[parser, fs, loophole, pegjs] = []
AtomModifiers = new Set
AtomModifiers.add(modifier) for modifier in ['ctrl', 'alt', 'shift', 'cmd', 'num']
KeyboardEventModifiers = new Set
KeyboardEventModifiers.add(modifier) for modifier in ['Control', 'Alt', 'Shift', 'Meta']
SpecificityCache = {}
WindowsAndLinuxCharCodeTranslations =
48:
shifted: 41 # ")"
unshifted: 48 # "0"
49:
shifted: 33 # "!"
unshifted: 49 # "1"
50:
shifted: 64 # "@"
unshifted: 50 # "2"
51:
shifted: 35 # "#"
unshifted: 51 # "3"
52:
shifted: 36 # "$"
unshifted: 52 # "4"
53:
shifted: 37 # "%"
unshifted: 53 # "5"
54:
shifted: 94 # "^"
unshifted: 54 # "6"
55:
shifted: 38 # "&"
unshifted: 55 # "7"
56:
shifted: 42 # "*"
unshifted: 56 # "8"
57:
shifted: 40 # "("
unshifted: 57 # "9"
186:
shifted: 58 # ":"
unshifted: 59 # ";"
187:
shifted: 43 # "+"
unshifted: 61 # "="
188:
shifted: 60 # "<"
unshifted: 44 # ","
189:
shifted: 95 # "_"
unshifted: 45 # "-"
190:
shifted: 62 # ">"
unshifted: 46 # "."
191:
shifted: 63 # "?"
unshifted: 47 # "/"
192:
shifted: 126 # "~"
unshifted: 96 # "`"
219:
shifted: 123 # "{"
unshifted: 91 # "["
220:
shifted: 124 # "|"
unshifted: 92 # "\"
221:
shifted: 125 # "}"
unshifted: 93 # "]"
222:
shifted: 34 # '"'
unshifted: 39 # "'"
NumPadToASCII =
79: 47 # "/"
74: 42 # "*"
77: 45 # "-"
75: 43 # "+"
78: 46 # "."
96: 48 # "0"
65: 49 # "1"
66: 50 # "2"
67: 51 # "3"
68: 52 # "4"
69: 53 # "5"
70: 54 # "6"
71: 55 # "7"
72: 56 # "8"
73: 57 # "9"
exports.normalizeKeystrokes = (keystrokes) ->
normalizedKeystrokes = []
for keystroke in keystrokes.split(/\s+/)
if normalizedKeystroke = normalizeKeystroke(keystroke)
normalizedKeystrokes.push(normalizedKeystroke)
else
return false
normalizedKeystrokes.join(' ')
exports.keystrokeForKeyboardEvent = (event) ->
unless KeyboardEventModifiers.has(event.keyIdentifier)
charCode = charCodeFromKeyIdentifier(event.keyIdentifier)
if charCode?
if process.platform in ['linux', 'win32']
charCode = translateCharCodeForWindowsAndLinuxChromiumBug(charCode, event.shiftKey)
if event.location is KeyboardEvent.DOM_KEY_LOCATION_NUMPAD
# This is a numpad number
charCode = numpadToASCII(charCode)
charCode = event.which if not isASCII(charCode) and isASCII(event.keyCode)
key = keyFromCharCode(charCode)
else
key = event.keyIdentifier.toLowerCase()
keystroke = []
keystroke.push 'ctrl' if event.ctrlKey
keystroke.push 'alt' if event.altKey
if event.shiftKey
# Don't push 'shift' when modifying symbolic characters like '{'
keystroke.push 'shift' unless /^[^A-Za-z]$/.test(key)
# Only upper case alphabetic characters like 'a'
key = key.toUpperCase() if /^[a-z]$/.test(key)
else
key = key.toLowerCase() if /^[A-Z]$/.test(key)
keystroke.push 'cmd' if event.metaKey
keystroke.push 'num' if event.location is KeyboardEvent.DOM_KEY_LOCATION_NUMPAD
keystroke.push(key) if key?
keystroke.join('-')
exports.calculateSpecificity = (selector) ->
SpecificityCache[selector] ?= specificity(selector)
exports.isAtomModifier = (key) ->
AtomModifiers.has(key)
exports.keydownEvent = (key, {ctrl, shift, alt, cmd, keyCode, target, location}={}) ->
event = document.createEvent('KeyboardEvent')
bubbles = true
cancelable = true
view = null
key = key.toUpperCase() if /^[a-z]$/.test(key)
if key.length is 1
keyIdentifier = "U+#{key.charCodeAt(0).toString(16)}"
else
switch key
when 'ctrl'
keyIdentifier = 'Control'
ctrl = true
when 'alt'
keyIdentifier = 'Alt'
alt = true
when 'shift'
keyIdentifier = 'Shift'
shift = true
when 'cmd'
keyIdentifier = 'Meta'
cmd = true
else
keyIdentifier = key[0].toUpperCase() + key[1..]
location ?= KeyboardEvent.DOM_KEY_LOCATION_STANDARD
event.initKeyboardEvent('keydown', bubbles, cancelable, view, keyIdentifier, location, ctrl, alt, shift, cmd)
Object.defineProperty(event, 'target', get: -> target) if target?
Object.defineProperty(event, 'keyCode', get: -> keyCode)
Object.defineProperty(event, 'which', get: -> keyCode)
event
normalizeKeystroke = (keystroke) ->
keys = parseKeystroke(keystroke)
primaryKey = null
modifiers = new Set
for key, i in keys
if AtomModifiers.has(key)
modifiers.add(key)
else
# only the last key can be a non-modifier
if i is keys.length - 1
primaryKey = key
else
return false
modifiers.add('shift') if /^[A-Z]$/.test(primaryKey)
primaryKey = primaryKey.toUpperCase() if modifiers.has('shift') and /^[a-z]$/.test(primaryKey)
keystroke = []
keystroke.push('ctrl') if modifiers.has('ctrl')
keystroke.push('alt') if modifiers.has('alt')
keystroke.push('shift') if modifiers.has('shift')
keystroke.push('cmd') if modifiers.has('cmd')
keystroke.push('num') if modifiers.has('num')
keystroke.push(primaryKey) if primaryKey?
keystroke.join('-')
parseKeystroke = (keystroke) ->
unless parser?
try
parser = require './keystroke'
catch e
fs ?= require 'fs'
loophole ?= require 'loophole'
pegjs ?= require 'pegjs'
keystrokeGrammar = fs.readFileSync(require.resolve('./keystroke.pegjs'), 'utf8')
loophole.allowUnsafeEval => parser = pegjs.buildParser(keystrokeGrammar)
parser.parse(keystroke)
charCodeFromKeyIdentifier = (keyIdentifier) ->
parseInt(keyIdentifier[2..], 16) if keyIdentifier.indexOf('U+') is 0
# Chromium includes incorrect keyIdentifier values on keypress events for
# certain symbols keys on Window and Linux.
#
# See https://code.google.com/p/chromium/issues/detail?id=51024
# See https://bugs.webkit.org/show_bug.cgi?id=19906
translateCharCodeForWindowsAndLinuxChromiumBug = (charCode, shift) ->
if translation = WindowsAndLinuxCharCodeTranslations[charCode]
if shift then translation.shifted else translation.unshifted
else
charCode
keyFromCharCode = (charCode) ->
switch charCode
when 8 then 'backspace'
when 9 then 'tab'
when 13 then 'enter'
when 27 then 'escape'
when 32 then 'space'
when 127 then 'delete'
else String.fromCharCode(charCode)
isASCII = (charCode) ->
0 <= charCode <= 127
numpadToASCII = (charCode) ->
NumPadToASCII[charCode] ? charCode