Add keyBindingsForCommandMatchingElement to keymap

This commit is contained in:
Ben Ogle
2013-11-20 15:43:02 -08:00
parent a0fbec29c3
commit 819ac9ea68
2 changed files with 41 additions and 1 deletions

View File

@@ -324,3 +324,26 @@ describe "Keymap", ->
bindings = keymap.keyBindingsMatchingElement(fragment.find('.grandchild-node'))
expect(bindings).toHaveLength 3
expect(bindings[0].command).toEqual "command-and-grandchild-node"
describe ".keyBindingsForCommandMatchingElement(element)", ->
beforeEach ->
keymap.add 'nature',
'.green':
'ctrl-c': 'cultivate'
'.green-2':
'ctrl-o': 'cultivate'
'.brown':
'ctrl-h': 'harvest'
'.blue':
'ctrl-c': 'fly'
it "finds a keymap for an element", ->
el = $$ -> @div class: 'green'
bindings = keymap.keyBindingsForCommandMatchingElement('cultivate', el)
expect(bindings).toHaveLength 1
expect(bindings[0].keystroke).toEqual "ctrl-c"
it "no keymap an element without that map", ->
el = $$ -> @div class: 'brown'
bindings = keymap.keyBindingsForCommandMatchingElement('cultivate', el)
expect(bindings).toHaveLength 0

View File

@@ -43,12 +43,29 @@ class Keymap
keyBindings = @keyBindingsForKeystroke(keystroke)
@keyBindingsMatchingElement(element, keyBindings)
# Public: Returns a array of {KeyBinding}s (sorted by selector specificity)
# that match a command.
#
# * command:
# The string representing the command (tree-view:toggle)
# * element:
# The DOM node that will match a {KeyBinding}'s selector.
keyBindingsForCommandMatchingElement: (command, element) ->
keyBindings = @keyBindingsForCommand(command)
@keyBindingsMatchingElement(element, keyBindings)
# Public: Returns an array of {KeyBinding}s that match a keystroke
# * keystroke:
# The string representing the keys pressed (e.g. ctrl-P)
keyBindingsForKeystroke: (keystroke) ->
keystroke = KeyBinding.normalizeKeystroke(keystroke)
keyBindings = @keyBindings.filter (keyBinding) -> keyBinding.matches(keystroke)
@keyBindings.filter (keyBinding) -> keyBinding.matches(keystroke)
# Public: Returns an array of {KeyBinding}s that match a command
# * keystroke:
# The string representing the keys pressed (e.g. ctrl-P)
keyBindingsForCommand: (command) ->
@keyBindings.filter (keyBinding) -> keyBinding.command == command
# Public: Returns a array of {KeyBinding}s (sorted by selector specificity)
# whos selector matches the element.