Start experiment: associate key bindings with CSS selectors

KeyEventHandler holds references to BindingSets. The name "binding set"
is based on the concept of a CSS ruleset. The idea is to choose a key
binding for an event based on what selectors (match / most closely
contain) the event's target DOM node.
This commit is contained in:
Nathan Sobo
2012-01-09 16:53:10 -08:00
committed by Corey Johnson & Nathan Sobo
parent 3a1d167a0f
commit 18e614e88d
4 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
KeyEventHandler = require 'key-event-handler'
$ = require 'jquery'
describe "KeyEventHandler", ->
handler = null
beforeEach ->
handler = new KeyEventHandler
fdescribe "handleKeypress", ->
describe "when there is a mapping in a selector that matches the event's element", ->
fragment = null
deleteCharHandler = null
insertCharHandler = null
beforeEach ->
handler.bindKeys '.command-mode', 'x': 'deleteChar'
handler.bindKeys '.insert-mode', 'x': 'insertChar'
fragment = $('<div class="command-mode">')
deleteCharHandler = jasmine.createSpy 'deleteCharHandler'
insertCharHandler = jasmine.createSpy 'insertCharHandler'
fragment.on 'deleteChar', deleteCharHandler
fragment.on 'insertChar', insertCharHandler
it "only triggers an event based on the key-binding in that selector", ->
handler.handleKeypress(keypressEvent('x', target: fragment[0]))
expect(deleteCharHandler).toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
deleteCharHandler.reset()
fragment.removeClass('command-mode').addClass('insert-mode')
handler.handleKeypress(keypressEvent('x', target: fragment[0]))
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).toHaveBeenCalled()

View File

@@ -15,6 +15,9 @@ window.keydown = (pattern) ->
window.createKeyEvent = (pattern) ->
$.Event "keydown", atom.keyBinder.parseKeyPattern(pattern)
window.keypressEvent = (pattern, properties={}) ->
$.Event "keypress", _.extend(atom.keyBinder.parseKeyPattern(pattern), properties)
window.waitsForPromise = (fn) ->
window.waitsFor (moveOn) ->
fn().done(moveOn)