Rename Keymap::toObject to Keymap::keystrokesByCommand

Also add spec for passing a selector
This commit is contained in:
Corey Johnson & Nathan Sobo
2013-08-20 10:59:44 -07:00
parent 79dd4320da
commit 0c9a1fdc80
5 changed files with 19 additions and 15 deletions

View File

@@ -256,18 +256,24 @@ 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()", ->
describe ".keystrokesByCommandForSelector(selector)", ->
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
expect(keymap.keystrokesByCommandForSelector()).toEqual
'letter': ['b', 'a']
'number': ['1']
'number-with-modifiers': ['alt-meta-1']
expect(keymap.keystrokesByCommandForSelector('.editor')).toEqual
'letter': ['b']
'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

@@ -4,7 +4,7 @@ _ = require 'underscore'
module.exports =
class ApplicationMenu
keyBindingsByCommand: null
keystrokesByCommand: null
version: null
devMode: null
menu: null
@@ -13,7 +13,7 @@ class ApplicationMenu
@menu = Menu.buildFromTemplate @defaultTemplate()
Menu.setApplicationMenu @menu
update: (@keyBindingsByCommand) ->
update: (@keystrokesByCommand) ->
template = @template()
@parseTemplate(template)
@menu = Menu.buildFromTemplate(template)
@@ -127,7 +127,7 @@ class ApplicationMenu
menu
acceleratorForCommand: (command) ->
keyBinding = @keyBindingsByCommand[command]?[0]
keyBinding = @keystrokesByCommand[command]?[0]
return null unless keyBinding
modifiers = keyBinding.split('-')

View File

@@ -134,8 +134,8 @@ class AtomApplication
else
@promptForPath()
ipc.once 'keymap-loaded', (processId, routingId, keyBindingsByCommand) =>
@applicationMenu.update(keyBindingsByCommand)
ipc.once 'update-application-menu', (processId, routingId, keystrokesByCommand) =>
@applicationMenu.update(keystrokesByCommand)
ipc.on 'command', (processId, routingId, command) =>
@emit(command)

View File

@@ -163,16 +163,14 @@ class Keymap
[modifiers..., key].join('-')
toObject: (selector)->
body = $('body')
keyBindingsForCommands = {}
keystrokesByCommandForSelector: (selector)->
keystrokesByCommand = {}
for bindingSet in @bindingSets
for keystroke, command of bindingSet.commandsByKeystrokes
continue if selector? and selector != bindingSet.selector
keyBindingsForCommands[command] ?= []
keyBindingsForCommands[command].push keystroke
keyBindingsForCommands
keystrokesByCommand[command] ?= []
keystrokesByCommand[command].push keystroke
keystrokesByCommand
isAscii: (charCode) ->
0 <= charCode <= 127

View File

@@ -55,7 +55,7 @@ window.startEditorWindow = ->
atom.activatePackages()
keymap.loadUserKeymaps()
atom.requireUserInitScript()
ipc.sendChannel 'keymap-loaded', keymap.toObject('body')
ipc.sendChannel 'update-application-menu', keymap.keystrokesByCommandForSelector('body')
$(window).on 'unload', -> unloadEditorWindow(); false
atom.show()
atom.focus()