mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-21 07:58:04 +03:00
Keymap normalizes key patterns so modifier keys can be listed in a random order.
This commit is contained in:
parent
68bd79f8c2
commit
846846abe8
@ -125,7 +125,16 @@ describe "Keymap", ->
|
||||
expect(bazHandler).toHaveBeenCalled()
|
||||
|
||||
describe ".bindKeys(selector, fnOrMap)", ->
|
||||
describe "when called with a function", ->
|
||||
describe "when called with a selector and a hash", ->
|
||||
it "normalizes the key patterns in the hash to put the modifiers in alphabetical order", ->
|
||||
fooHandler = jasmine.createSpy('fooHandler')
|
||||
fragment.on 'foo', fooHandler
|
||||
keymap.bindKeys '*', 'ctrl-alt-delete': 'foo'
|
||||
result = keymap.handleKeyEvent(keydownEvent('delete', ctrlKey: true, altKey: true, target: fragment[0]))
|
||||
expect(result).toBe(false)
|
||||
expect(fooHandler).toHaveBeenCalled()
|
||||
|
||||
describe "when called with a selector and a function", ->
|
||||
it "calls the given function when selector matches", ->
|
||||
handler = jasmine.createSpy 'handler'
|
||||
keymap.bindKeys '.child-node', handler
|
||||
|
@ -15,6 +15,7 @@ class BindingSet
|
||||
if _.isFunction(mapOrFunction)
|
||||
mapOrFunction
|
||||
else
|
||||
mapOrFunction = @normalizeKeystrokePatterns(mapOrFunction)
|
||||
(event) =>
|
||||
for pattern, command of mapOrFunction
|
||||
return command if @eventMatchesPattern(event, pattern)
|
||||
@ -23,3 +24,16 @@ class BindingSet
|
||||
eventMatchesPattern: (event, pattern) ->
|
||||
pattern = pattern.replace(/^<|>$/g, '')
|
||||
event.keystroke == pattern
|
||||
|
||||
normalizeKeystrokePatterns: (map) ->
|
||||
normalizedMap = {}
|
||||
for pattern, event of map
|
||||
normalizedMap[@normalizeKeystrokePattern(pattern)] = event
|
||||
normalizedMap
|
||||
|
||||
normalizeKeystrokePattern: (pattern) ->
|
||||
keys = pattern.split('-')
|
||||
modifiers = keys[0...-1]
|
||||
modifiers.sort()
|
||||
[modifiers..., _.last(keys)].join('-')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user