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
/
keymap-manager.coffee
674 lines (591 loc) · 27.2 KB
/
keymap-manager.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
_ = require "underscore-plus"
CSON = require 'season'
Grim = require 'grim'
fs = require 'fs-plus'
{observeCurrentKeyboardLayout} = require 'keyboard-layout'
path = require 'path'
EmitterMixin = require('emissary').Emitter
{File} = require 'pathwatcher'
{Emitter, Disposable, CompositeDisposable} = require 'event-kit'
KeyBinding = require './key-binding'
CommandEvent = require './command-event'
{normalizeKeystrokes, keystrokeForKeyboardEvent, isAtomModifier, keydownEvent} = require './helpers'
Platforms = ['darwin', 'freebsd', 'linux', 'sunos', 'win32']
OtherPlatforms = Platforms.filter (platform) -> platform isnt process.platform
# Extended: Allows commands to be associated with keystrokes in a
# context-sensitive way. In Atom, you can access a global instance of this
# object via `atom.keymap`.
#
# Key bindings are plain JavaScript objects containing **CSS selectors** as
# their top level keys, then **keystroke patterns** mapped to commands.
#
# ```cson
# '.workspace':
# 'ctrl-l': 'package:do-something'
# 'ctrl-z': 'package:do-something-else'
# '.mini.editor':
# 'enter': 'core:confirm'
# ```
#
# When a keystroke sequence matches a binding in a given context, a custom DOM
# event with a type based on the command is dispatched on the target of the
# keyboard event.
#
# To match a keystroke sequence, the keymap starts at the target element for the
# keyboard event. It looks for key bindings associated with selectors that match
# the target element. If multiple match, the most specific is selected. If there
# is a tie in specificity, the most recently added binding wins. If no bindings
# are found for the events target, the search is repeated again for the target's
# parent node and so on recursively until a binding is found or we traverse off
# the top of the document.
#
# When a binding is found, its command event is always dispatched on the
# original target of the keyboard event, even if the matching element is higher
# up in the DOM. In addition, `.preventDefault()` is called on the keyboard
# event to prevent the browser from taking action. `.preventDefault` is only
# called if a matching binding is found.
#
# Command event objects have a non-standard method called `.abortKeyBinding()`.
# If your command handler is invoked but you programmatically determine that no
# action can be taken and you want to allow other bindings to be matched, call
# `.abortKeyBinding()` on the event object. An example of where this is useful
# is binding snippet expansion to `tab`. If `snippets:expand` is invoked when
# the cursor does not follow a valid snippet prefix, we abort the binding and
# allow `tab` to be handled by the default handler, which inserts whitespace.
#
# Multi-keystroke bindings are possible. If a sequence of one or more keystrokes
# *partially* matches a multi-keystroke binding, the keymap enters a pending
# state. The pending state is terminated on the next keystroke, or after
# {::partialMatchTimeout} milliseconds has elapsed. When the pending state is
# terminated via a timeout or a keystroke that leads to no matches, the longest
# ambiguous bindings that caused the pending state are temporarily disabled and
# the previous keystrokes are replayed. If there is ambiguity again during the
# replay, the next longest bindings are disabled and the keystrokes are replayed
# again.
module.exports =
class KeymapManager
EmitterMixin.includeInto(this)
###
Section: Class Methods
###
# Public: Create a keydown DOM event for testing purposes.
#
# * `key` The key or keyIdentifier of the event. For example, `'a'`, `'1'`,
# `'escape'`, `'backspace'`, etc.
# * `options` (optional) An {Object} containing any of the following:
# * `ctrl` A {Boolean} indicating the ctrl modifier key
# * `alt` A {Boolean} indicating the alt modifier key
# * `shift` A {Boolean} indicating the shift modifier key
# * `cmd` A {Boolean} indicating the cmd modifier key
# * `which` A {Number} indicating `which` value of the event. See
# the docs for KeyboardEvent for more information.
# * `target` The target element of the event.
@keydownEvent: (key, options) -> keydownEvent(key, options)
###
Section: Properties
###
# Public: The number of milliseconds allowed before pending states caused
# by partial matches of multi-keystroke bindings are terminated.
partialMatchTimeout: 1000
defaultTarget: null
pendingPartialMatches: null
pendingStateTimeoutHandle: null
dvorakQwertyWorkaroundEnabled: false
###
Section: Construction and Destruction
###
# Public: Create a new KeymapManager.
#
# * `options` An {Object} containing properties to assign to the keymap. You
# can pass custom properties to be used by extension methods. The
# following properties are also supported:
# * `defaultTarget` This will be used as the target of events whose target
# is `document.body` to allow for a catch-all element when nothing is focused.
constructor: (options={}) ->
@[key] = value for key, value of options
@emitter = new Emitter
@keyBindings = []
@queuedKeyboardEvents = []
@queuedKeystrokes = []
@watchSubscriptions = {}
@enableDvorakQwertyWorkaroundIfNeeded()
# Public: Unwatch all watched paths.
destroy: ->
@keyboardLayoutSubscription.dispose()
for filePath, subscription of @watchSubscriptions
subscription.dispose()
undefined
enableDvorakQwertyWorkaroundIfNeeded: ->
@keyboardLayoutSubscription = observeCurrentKeyboardLayout (layoutId) =>
@dvorakQwertyWorkaroundEnabled = layoutId is 'com.apple.keylayout.DVORAK-QWERTYCMD'
###
Section: Event Subscription
###
# Public: Invoke the given callback when one or more keystrokes completely
# match a key binding.
#
# * `callback` {Function} to be called when keystrokes match a binding.
# * `event` {Object} with the following keys:
# * `keystrokes` {String} of keystrokes that matched the binding.
# * `binding` {KeyBinding} that the keystrokes matched.
# * `keyboardEventTarget` DOM element that was the target of the most
# recent keyboard event.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidMatchBinding: (callback) ->
@emitter.on 'did-match-binding', callback
# Public: Invoke the given callback when one or more keystrokes partially
# match a binding.
#
# * `callback` {Function} to be called when keystrokes partially match a
# binding.
# * `event` {Object} with the following keys:
# * `keystrokes` {String} of keystrokes that matched the binding.
# * `partiallyMatchedBindings` {KeyBinding}s that the keystrokes partially
# matched.
# * `keyboardEventTarget` DOM element that was the target of the most
# recent keyboard event.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidPartiallyMatchBindings: (callback) ->
@emitter.on 'did-partially-match-binding', callback
# Public: Invoke the given callback when one or more keystrokes fail to match
# any bindings.
#
# * `callback` {Function} to be called when keystrokes fail to match any
# bindings.
# * `event` {Object} with the following keys:
# * `keystrokes` {String} of keystrokes that matched the binding.
# * `keyboardEventTarget` DOM element that was the target of the most
# recent keyboard event.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidFailToMatchBinding: (callback) ->
@emitter.on 'did-fail-to-match-binding', callback
# Invoke the given callback when a keymap file is reloaded.
#
# * `callback` {Function} to be called when a keymap file is reloaded.
# * `event` {Object} with the following keys:
# * `path` {String} representing the path of the reloaded keymap file.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidReloadKeymap: (callback) ->
@emitter.on 'did-reload-keymap', callback
# Invoke the given callback when a keymap file is unloaded.
#
# * `callback` {Function} to be called when a keymap file is unloaded.
# * `event` {Object} with the following keys:
# * `path` {String} representing the path of the unloaded keymap file.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidUnloadKeymap: (callback) ->
@emitter.on 'did-unload-keymap', callback
# Public: Invoke the given callback when a keymap file not able to be loaded.
#
# * `callback` {Function} to be called when a keymap file is unloaded.
# * `error` {Object} with the following keys:
# * `message` {String} the error message.
# * `stack` {String} the error stack trace.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidFailToReadFile: (callback) ->
@emitter.on 'did-fail-to-read-file', callback
on: (eventName) ->
switch eventName
when 'matched'
Grim.deprecate("Call KeymapManager::onDidMatchBinding instead")
when 'matched-partially'
Grim.deprecate("Call KeymapManager::onDidPartiallyMatchBinding instead")
when 'match-failed'
Grim.deprecate("Call KeymapManager::onDidFailToMatchBinding instead")
when 'reloaded-key-bindings'
Grim.deprecate("Call KeymapManager::onDidReloadKeymap instead")
when 'unloaded-key-bindings'
Grim.deprecate("Call KeymapManager::onDidUnloadKeymap instead")
else
Grim.deprecate("Use explicit event subscription methods instead")
EmitterMixin::on.apply(this, arguments)
###
Section: Adding and Removing Bindings
###
# Public: Add sets of key bindings grouped by CSS selector.
#
# * `source` A {String} (usually a path) uniquely identifying the given bindings
# so they can be removed later.
# * `bindings` An {Object} whose top-level keys point at sub-objects mapping
# keystroke patterns to commands.
add: (source, keyBindingsBySelector) ->
addedKeyBindings = []
for selector, keyBindings of keyBindingsBySelector
# Verify selector is valid before registering any bindings
try
document.body.webkitMatchesSelector(selector.replace(/!important/g, ''))
catch e
console.warn("Encountered an invalid selector adding key bindings from '#{source}': '#{selector}'")
return
for keystrokes, command of keyBindings
if normalizedKeystrokes = normalizeKeystrokes(keystrokes)
keyBinding = new KeyBinding(source, command, normalizedKeystrokes, selector)
addedKeyBindings.push(keyBinding)
@keyBindings.push(keyBinding)
else
console.warn "Invalid keystroke sequence for binding: `#{keystrokes}: #{command}` in #{source}"
new Disposable =>
for keyBinding in addedKeyBindings
index = @keyBindings.indexOf(keyBinding)
@keyBindings.splice(index, 1) unless index is -1
remove: (source) ->
Grim.deprecate("Call .dispose() on the Disposable returned from KeymapManager::add instead")
@removeBindingsFromSource(source)
removeBindingsFromSource: (source) ->
@keyBindings = @keyBindings.filter (keyBinding) -> keyBinding.source isnt source
undefined
###
Section: Accessing Bindings
###
# Public: Get all current key bindings.
#
# Returns an {Array} of {KeyBinding}s.
getKeyBindings: ->
@keyBindings.slice()
# Public: Get the key bindings for a given command and optional target.
#
# * `params` An {Object} whose keys constrain the binding search:
# * `keystrokes` A {String} representing one or more keystrokes, such as
# 'ctrl-x ctrl-s'
# * `command` A {String} representing the name of a command, such as
# 'editor:backspace'
# * `target` An optional DOM element constraining the search. If this
# parameter is supplied, the call will only return bindings that
# can be invoked by a KeyboardEvent originating from the target element.
#
# Returns an {Array} of key bindings.
findKeyBindings: (params={}) ->
{keystrokes, command, target, keyBindings} = params
bindings = keyBindings ? @keyBindings
if command?
bindings = bindings.filter (binding) -> binding.command is command
if keystrokes?
bindings = bindings.filter (binding) -> binding.keystrokes is keystrokes
if target?
candidateBindings = bindings
bindings = []
element = target
while element? and element isnt document
matchingBindings = candidateBindings
.filter (binding) -> element.webkitMatchesSelector(binding.selector)
.sort (a, b) -> a.compare(b)
bindings.push(matchingBindings...)
element = element.parentElement
bindings
###
Section: Managing Keymap Files
###
# Public: Load the key bindings from the given path.
#
# * `path` A {String} containing a path to a file or a directory. If the path is
# a directory, all files inside it will be loaded.
# * `options` An {Object} containing the following optional keys:
# * `watch` If `true`, the keymap will also reload the file at the given
# path whenever it changes. This option cannot be used with directory paths.
loadKeymap: (bindingsPath, options) ->
checkIfDirectory = options?.checkIfDirectory ? true
if checkIfDirectory and fs.isDirectorySync(bindingsPath)
for filePath in fs.listSync(bindingsPath, ['.cson', '.json'])
if @filePathMatchesPlatform(filePath)
@loadKeymap(filePath, checkIfDirectory: false)
else
@addKeymap(bindingsPath, @readKeymap(bindingsPath, options?.suppressErrors))
@watchKeymap(bindingsPath) if options?.watch
undefined
# Public: Cause the keymap to reload the key bindings file at the given path
# whenever it changes.
#
# This method doesn't perform the initial load of the key bindings file. If
# that's what you're looking for, call {::loadKeymap} with `watch: true`.
#
# * `path` A {String} containing a path to a file or a directory. If the path is
# a directory, all files inside it will be loaded.
watchKeymap: (filePath) ->
if not @watchSubscriptions[filePath]? or @watchSubscriptions[filePath].disposed
file = new File(filePath)
reloadKeymap = => @reloadKeymap(filePath)
@watchSubscriptions[filePath] = new CompositeDisposable(
file.onDidChange(reloadKeymap)
file.onDidRename(reloadKeymap)
file.onDidDelete(reloadKeymap)
)
undefined
# Called by the path watcher callback to reload a file at the given path. If
# we can't read the file cleanly, we don't proceed with the reload.
reloadKeymap: (filePath) ->
if fs.isFileSync(filePath)
if bindings = @readKeymap(filePath, true)
@removeBindingsFromSource(filePath)
@addKeymap(filePath, bindings)
@emit 'reloaded-key-bindings', filePath
@emitter.emit 'did-reload-keymap', {path: filePath}
else
@removeBindingsFromSource(filePath)
@emit 'unloaded-key-bindings', filePath
@emitter.emit 'did-unload-keymap', {path: filePath}
readKeymap: (filePath, suppressErrors) ->
if suppressErrors
try
CSON.readFileSync(filePath)
catch error
console.warn("Failed to reload key bindings file: #{filePath}", error.stack ? error)
@emitter.emit 'did-fail-to-read-file', error
undefined
else
CSON.readFileSync(filePath)
# Determine if the given path should be loaded on this platform. If the
# filename has the pattern '<platform>.cson' or 'foo.<platform>.cson' and
# <platform> does not match the current platform, returns false. Otherwise
# returns true.
filePathMatchesPlatform: (filePath) ->
otherPlatforms = @getOtherPlatforms()
for component in path.basename(filePath).split('.')[0...-1]
return false if component in otherPlatforms
true
###
Section: Managing Keyboard Events
###
# Public: Dispatch a custom event associated with the matching key binding for
# the given `KeyboardEvent` if one can be found.
#
# If a matching binding is found on the event's target or one of its
# ancestors, `.preventDefault()` is called on the keyboard event and the
# binding's command is emitted as a custom event on the matching element.
#
# If the matching binding's command is 'native!', the method will terminate
# without calling `.preventDefault()` on the keyboard event, allowing the
# browser to handle it as normal.
#
# If the matching binding's command is 'unset!', the search will continue from
# the current element's parent.
#
# If the matching binding's command is 'abort!', the search will terminate
# without dispatching a command event.
#
# If the event's target is `document.body`, it will be treated as if its
# target is `.defaultTarget` if that property is assigned on the keymap.
#
# * `event` A `KeyboardEvent` of type 'keydown'
handleKeyboardEvent: (event, replaying) ->
keystroke = @keystrokeForKeyboardEvent(event)
if @queuedKeystrokes.length > 0 and isAtomModifier(keystroke)
event.preventDefault()
return
@queuedKeyboardEvents.push(event)
@queuedKeystrokes.push(keystroke)
keystrokes = @queuedKeystrokes.join(' ')
# If the event's target is document.body, assign it to defaultTarget instead
# to provide a catch-all element when nothing is focused.
target = event.target
target = @defaultTarget if event.target is document.body and @defaultTarget?
# First screen for any bindings that match the current keystrokes,
# regardless of their current selector. Matching strings is cheaper than
# matching selectors.
{partialMatchCandidates, exactMatchCandidates} = @findMatchCandidates(keystrokes)
partialMatches = @findPartialMatches(partialMatchCandidates, target)
# Determine if the current keystrokes match any bindings *exactly*. If we
# do find and exact match, the next step depends on whether we have any
# partial matches. If we have no partial matches, we dispatch the command
# immediately. Otherwise we break and allow ourselves to enter the pending
# state with a timeout.
if exactMatchCandidates.length > 0
currentTarget = target
while currentTarget? and currentTarget isnt document
exactMatches = @findExactMatches(exactMatchCandidates, currentTarget)
for exactMatch in exactMatches
if exactMatch.command is 'native!'
@clearQueuedKeystrokes()
return
if exactMatch.command is 'abort!'
@clearQueuedKeystrokes()
event.preventDefault()
return
if exactMatch.command is 'unset!'
break
foundMatch = true
break if partialMatches.length > 0
@clearQueuedKeystrokes()
@cancelPendingState()
if @dispatchCommandEvent(exactMatch.command, target, event)
event = {keystrokes, binding: exactMatch, keyboardEventTarget: target}
@emit 'matched', event
@emitter.emit 'did-match-binding', event
return
currentTarget = currentTarget.parentElement
# If we're at this point in the method, we either found no matches for the
# currently queued keystrokes or we found a match, but we need to enter a
# pending state due to partial matches. We only enable the timeout of the
# pending state if we found an exact match on this or a previously queued
# keystroke.
if partialMatches.length > 0
event.preventDefault()
enableTimeout = foundMatch ? @pendingStateTimeoutHandle?
@enterPendingState(partialMatches, enableTimeout)
event = {keystrokes, partiallyMatchedBindings: partialMatches, keyboardEventTarget: target}
@emit 'matched-partially', event
@emitter.emit 'did-partially-match-binding', event
else
event = {keystrokes, keyboardEventTarget: target}
@emit 'match-failed', event
@emitter.emit 'did-fail-to-match-binding', event
@terminatePendingState()
# Public: Translate a keydown event to a keystroke string.
#
# * `event` A `KeyboardEvent` of type 'keydown'
#
# Returns a {String} describing the keystroke.
keystrokeForKeyboardEvent: (event) ->
keystrokeForKeyboardEvent(event, @dvorakQwertyWorkaroundEnabled)
###
Section: Private
###
# For testing purposes
getOtherPlatforms: -> OtherPlatforms
# Finds all key bindings whose keystrokes match the given keystrokes. Returns
# both partial and exact matches.
findMatchCandidates: (keystrokes) ->
partialMatchCandidates = []
exactMatchCandidates = []
keystrokesWithSpace = keystrokes + ' '
for binding in @keyBindings when binding.enabled
if binding.keystrokes is keystrokes
exactMatchCandidates.push(binding)
else if binding.keystrokes.indexOf(keystrokesWithSpace) is 0
partialMatchCandidates.push(binding)
{partialMatchCandidates, exactMatchCandidates}
# Determine which of the given bindings have selectors matching the target or
# one of its ancestors. This is used by {::handleKeyboardEvent} to determine
# if there are any partial matches for the keyboard event.
findPartialMatches: (partialMatchCandidates, target) ->
partialMatches = []
ignoreKeystrokes = new Set
while partialMatchCandidates.length > 0 and target? and target isnt document
partialMatchCandidates = partialMatchCandidates.filter (binding) ->
if binding.command is 'unset!'
ignoreKeystrokes.add(binding.keystrokes)
else if not ignoreKeystrokes.has(binding.keystrokes) and target.webkitMatchesSelector(binding.selector)
partialMatches.push(binding)
false
else
true
target = target.parentElement
partialMatches.sort (a, b) -> b.keystrokeCount - a.keystrokeCount
# Find the matching bindings among the given candidates for the given target,
# ordered by specificity. Does not traverse up the target's ancestors. This is
# used by {::handleKeyboardEvent} to find a matching binding when there are no
# partially-matching bindings.
findExactMatches: (exactMatchCandidates, target) ->
exactMatches = exactMatchCandidates
.filter (binding) -> target.webkitMatchesSelector(binding.selector)
.sort (a, b) -> a.compare(b)
clearQueuedKeystrokes: ->
@queuedKeyboardEvents = []
@queuedKeystrokes = []
enterPendingState: (pendingPartialMatches, enableTimeout) ->
@cancelPendingState() if @pendingStateTimeoutHandle?
@pendingPartialMatches = pendingPartialMatches
if enableTimeout
@pendingStateTimeoutHandle = setTimeout(@terminatePendingState.bind(this), @partialMatchTimeout)
cancelPendingState: ->
clearTimeout(@pendingStateTimeoutHandle)
@pendingStateTimeoutHandle = null
@pendingPartialMatches = null
# This is called by {::handleKeyboardEvent} when no matching bindings are
# found for the currently queued keystrokes or by the pending state timeout.
# It disables the longest of the pending partially matching bindings, then
# replays the queued keyboard events to allow any bindings with shorter
# keystroke sequences to be matched unambiguously.
terminatePendingState: ->
unless @pendingPartialMatches?
@clearQueuedKeystrokes()
return
bindingsToDisable = @pendingPartialMatches
eventsToReplay = @queuedKeyboardEvents
@cancelPendingState()
@clearQueuedKeystrokes()
binding.enabled = false for binding in bindingsToDisable
@handleKeyboardEvent(event, true) for event in eventsToReplay
binding.enabled = true for binding in bindingsToDisable
# After we match a binding, we call this method to dispatch a custom event
# based on the binding's command.
dispatchCommandEvent: (command, target, keyboardEvent) ->
# Here we use prototype chain injection to add CommandEvent methods to this
# custom event to support aborting key bindings and simulated bubbling for
# detached targets.
commandEvent = new CustomEvent(command, bubbles: true, cancelable: true)
commandEvent.__proto__ = CommandEvent::
commandEvent.originalEvent = keyboardEvent
if document.contains(target)
target.dispatchEvent(commandEvent)
else
@simulateBubblingOnDetachedTarget(target, commandEvent)
{keyBindingAborted} = commandEvent
keyboardEvent.preventDefault() unless keyBindingAborted
not keyBindingAborted
# Chromium does not bubble events dispatched on detached targets, which makes
# testing a pain in the ass. This method simulates bubbling manually.
simulateBubblingOnDetachedTarget: (target, commandEvent) ->
Object.defineProperty(commandEvent, 'target', get: -> target)
Object.defineProperty(commandEvent, 'currentTarget', get: -> currentTarget)
currentTarget = target
while currentTarget?
currentTarget.dispatchEvent(commandEvent)
break if commandEvent.propagationStopped
break if currentTarget is window
currentTarget = currentTarget.parentNode ? window
# Deprecated: Use {::add} instead.
addKeymap: (source, bindings) ->
# Grim.deprecate("Use KeymapManager::add instead.")
@add(source, bindings)
# Deprecated: Use {::remove} instead.
removeKeymap: (source) ->
# Grim.deprecate("Use KeymapManager::remove instead.")
@remove(source)
# Deprecated: Handle a jQuery keyboard event. Use {::handleKeyboardEvent} with
# a raw keyboard event instead.
handleKeyEvent: (event) ->
Grim.deprecate("Use KeymapManager::handleKeyboardEvent instead.")
originalEvent = event.originalEvent ? event
Object.defineProperty(originalEvent, 'target', get: -> event.target) unless originalEvent.target?
@handleKeyboardEvent(originalEvent)
not originalEvent.defaultPrevented
# Deprecated: Translate a jQuery keyboard event to a keystroke string. Use
# {::keystrokeForKeyboardEvent} with a raw KeyboardEvent instead.
keystrokeStringForEvent: (event) ->
Grim.deprecate("Use KeymapManager::keystrokeForKeyboardEvent instead.")
@keystrokeForKeyboardEvent(event.originalEvent ? event)
# Deprecated: Use {::addKeymap} with a map from selectors to key
# bindings.
bindKeys: (source, selector, keyBindings) ->
Grim.deprecate("Use KeymapManager::addKeymap instead.")
keyBindingsBySelector = {}
keyBindingsBySelector[selector] = keyBindings
@addKeymap(source, keyBindingsBySelector)
# Deprecated: Use {::findKeyBindings} with the 'command' param.
keyBindingsForCommand: (command) ->
Grim.deprecate("Use KeymapManager::findKeyBindings instead.")
@findKeyBindings({command})
# Deprecated: Use {::findKeyBindings} with the 'keystrokes' param.
keyBindingsForKeystroke: (keystroke) ->
Grim.deprecate("Use KeymapManager::findKeyBindings instead.")
@findKeyBindings({keystrokes: keystroke})
# Deprecated: Use {::findKeyBindings} with the 'target' param.
keyBindingsMatchingElement: (target, keyBindings) ->
Grim.deprecate("Use KeymapManager::findKeyBindings instead.")
@findKeyBindings({target: target[0] ? target, keyBindings})
# Deprecated: Use {::findKeyBindings} with the 'command' and 'target'
# params
keyBindingsForCommandMatchingElement: (command, target) ->
Grim.deprecate("Use KeymapManager::findKeyBindings instead.")
@findKeyBindings({command, target: target[0] ? target})
# Deprecated: Use {::findKeyBindings} with the 'keystrokes' and 'target'
# params
keyBindingsForKeystrokeMatchingElement: (keystrokes, target) ->
Grim.deprecate("Use KeymapManager::findKeyBindings instead.")
@findKeyBindings({keystrokes, target: target[0] ? target})