Skip to content

Commit

Permalink
Fix remaining multi-keystroke specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Sobo committed Jun 18, 2012
1 parent 85f695e commit 171a193
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 5 additions & 6 deletions spec/app/keymap-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ describe "Keymap", ->
fragment.on 'deleteChar', deleteCharHandler
fragment.on 'insertChar', insertCharHandler

it "adds a 'keystroke' string to the event object", ->
it "adds a 'keystrokes' string to the event object", ->
event = keydownEvent('x', altKey: true, metaKey: true)
keymap.handleKeyEvent(event)
expect(event.keystroke).toBe 'alt-meta-x'
expect(event.keystrokes).toBe 'alt-meta-x'

describe "when no binding matches the event's keystroke", ->
it "returns true, so the event continues to propagate", ->
Expand All @@ -54,7 +54,7 @@ describe "Keymap", ->
expect(insertCharHandler).toHaveBeenCalled()
commandEvent = insertCharHandler.argsForCall[0][0]
expect(commandEvent.keyEvent).toBe event
expect(event.keystroke).toBe 'x'
expect(event.keystrokes).toBe 'x'

describe "when the event's target node *descends* from a selector with a matching binding", ->
it "triggers the command event associated with that binding on the target node and returns false", ->
Expand Down Expand Up @@ -158,7 +158,7 @@ describe "Keymap", ->
expect(closeOtherWindowsHandler).toHaveBeenCalled()

describe "when a second keystroke added to the first doesn't match any bindings", ->
fit "clears the queued keystrokes without triggering any events", ->
it "clears the queued keystrokes without triggering any events", ->
expect(keymap.handleKeyEvent(keydownEvent('x', target: fragment[0], ctrlKey: true))).toBeFalsy()
expect(keymap.handleKeyEvent(keydownEvent('c', target: fragment[0]))).toBeFalsy()
expect(quitHandler).not.toHaveBeenCalled()
Expand All @@ -167,7 +167,7 @@ describe "Keymap", ->
expect(keymap.handleKeyEvent(keydownEvent('c', target: fragment[0]))).toBeTruthy()

describe "when the event's target node descends from multiple nodes that match selectors with a partial binding match", ->
fit "allows any of the bindings to be triggered upon a second keystroke, favoring the most specific selector", ->
it "allows any of the bindings to be triggered upon a second keystroke, favoring the most specific selector", ->
keymap.bindKeys ".grandchild-node", 'ctrl-x ctrl-c': 'more-specific-quit'
grandchildNode = fragment.find('.grandchild-node')[0]
moreSpecificQuitHandler = jasmine.createSpy('moreSpecificQuitHandler')
Expand Down Expand Up @@ -281,7 +281,6 @@ describe "Keymap", ->
expect(keymap.keystrokeStringForEvent(keydownEvent('left', shiftKey: true))).toBe 'shift-left'
expect(keymap.keystrokeStringForEvent(keydownEvent('Left', shiftKey: true))).toBe 'shift-left'


describe ".bindingsForElement(element)", ->
it "returns the matching bindings for the element", ->
keymap.bindKeys '.command-mode', 'c': 'c'
Expand Down
6 changes: 4 additions & 2 deletions src/app/keymap.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class Keymap
while currentNode.length
bindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
bindingSets.sort (a, b) -> b.specificity - a.specificity
_.defaults(keystrokeMap, set.keystrokeMap) for set in bindingSets
_.defaults(keystrokeMap, set.commandsByKeystrokes) for set in bindingSets
currentNode = currentNode.parent()

keystrokeMap

handleKeyEvent: (event) ->
event.keystrokes = @multiKeystrokeStringForEvent(event)
isMultiKeystroke = @queuedKeystrokes?
@queuedKeystrokes = null
currentNode = $(event.target)
while currentNode.length
Expand All @@ -59,7 +60,8 @@ class Keymap
@queuedKeystrokes = event.keystrokes
return false
currentNode = currentNode.parent()
true

!isMultiKeystroke

triggerCommandEvent: (keyEvent, commandName) ->
commandEvent = $.Event(commandName)
Expand Down

0 comments on commit 171a193

Please sign in to comment.