Add keymap.toObject()

This commit is contained in:
probablycorey
2013-08-17 09:54:07 -07:00
committed by Corey Johnson & Nathan Sobo
parent 1074642e8f
commit 37c17075ea
2 changed files with 21 additions and 0 deletions

View File

@@ -256,6 +256,18 @@ describe "Keymap", ->
it "returns false to prevent the browser from transferring focus", ->
expect(keymap.handleKeyEvent(keydownEvent('U+0009', target: fragment[0]))).toBe false
describe ".toObject()", ->
it "returns a hash of all commands and their keybindings", ->
keymap.bindKeys 'body', 'a': 'letter'
keymap.bindKeys '.editor', 'b': 'letter'
keymap.bindKeys '.editor', '1': 'number'
keymap.bindKeys '.editor', 'meta-alt-1': 'number-with-modifiers'
expect(keymap.toObject()).toEqual
'letter': ['b', 'a']
'number': ['1']
'number-with-modifiers': ['alt-meta-1']
describe ".bindKeys(selector, bindings)", ->
it "normalizes the key patterns in the hash to put the modifiers in alphabetical order", ->
fooHandler = jasmine.createSpy('fooHandler')

View File

@@ -169,6 +169,15 @@ class Keymap
keyBindings = keyBindings.concat(bindingSet.keyBindingsForCommand(command))
keyBindings
toObject: ->
keyBindingsForCommands = {}
for bindingSet in @bindingSets
for keystroke, command of bindingSet.commandsByKeystrokes
keyBindingsForCommands[command] ?= []
keyBindingsForCommands[command].push keystroke
keyBindingsForCommands
isAscii: (charCode) ->
0 <= charCode <= 127