Fix bug: Don't call handleKeyEvent with undefined event

Ace triggers key handlers for both keydown (onCommandKey) and input
(onTextInput) events. Input events don't pass the event, which was
blowing up the keymap.
This commit is contained in:
Nathan Sobo
2012-01-10 19:16:46 -08:00
committed by Corey Johnson & Nathan Sobo
parent 66707fc423
commit 783e9446e9
2 changed files with 47 additions and 33 deletions

View File

@@ -111,47 +111,62 @@ describe "Editor", ->
expect(fs.exists(selectedFilePath)).toBeFalsy()
describe "when a keydown event is handled by the ace editor", ->
returnValue = null
describe "key event handling", ->
handler = null
event = null
returnValue = null
beforeEach ->
event = keydownEvent 'x'
spyOn(event, 'stopPropagation')
handler =
handleKeyEvent: jasmine.createSpy('handleKeyEvent').andCallFake ->
returnValue
describe "when onCommandKey is called on the aceEditor (triggered by a keydown event on the textarea)", ->
event = null
describe "when no key event handler has been assigned", ->
beforeEach ->
expect(editor.keyEventHandler).toBeNull()
event = keydownEvent 'x'
spyOn(event, 'stopPropagation')
it "handles the event without crashing", ->
editor.aceEditor.onCommandKey event, 0, event.which
describe "when no key event handler has been assigned", ->
beforeEach ->
expect(editor.keyEventHandler).toBeNull()
describe "when a key event handler has been assigned", ->
beforeEach ->
handler = {
handleKeyEvent: jasmine.createSpy('handleKeyEvent').andCallFake ->
returnValue
}
it "handles the event without crashing", ->
editor.aceEditor.onCommandKey event, 0, event.which
describe "when a key event handler has been assigned", ->
beforeEach ->
editor.keyEventHandler = handler
it "asks the key event handler to handle the event", ->
editor.aceEditor.onCommandKey event, 0, event.which
expect(handler.handleKeyEvent).toHaveBeenCalled()
describe "if the atom key event handler returns false, indicating that it did not handle the event", ->
beforeEach ->
returnValue = false
it "does not stop the propagation of the event, allowing Ace to handle it as normal", ->
editor.aceEditor.onCommandKey event, 0, event.which
expect(event.stopPropagation).not.toHaveBeenCalled()
describe "if the atom key event handler returns true, indicating that it handled the event", ->
beforeEach ->
returnValue = true
it "stops propagation of the event, so Ace does not attempt to handle it", ->
editor.aceEditor.onCommandKey event, 0, event.which
expect(event.stopPropagation).toHaveBeenCalled()
describe "when onTextInput is called on the aceEditor (triggered by an input event)", ->
it "does not call handleKeyEvent on the key event handler, because there is no event", ->
editor.keyEventHandler = handler
it "asks the key event handler to handle the event", ->
editor.aceEditor.onCommandKey event, 0, event.which
expect(handler.handleKeyEvent).toHaveBeenCalled()
editor.aceEditor.onTextInput("x", false)
expect(handler.handleKeyEvent).not.toHaveBeenCalled()
describe "if the atom key event handler returns false, indicating that it did not handle the event", ->
beforeEach ->
returnValue = false
it "does not stop the propagation of the event, allowing Ace to handle it as normal", ->
editor.aceEditor.onCommandKey event, 0, event.which
expect(event.stopPropagation).not.toHaveBeenCalled()
describe "if the atom key event handler returns true, indicating that it handled the event", ->
beforeEach ->
returnValue = true
it "stops propagation of the event, so Ace does not attempt to handle it", ->
editor.aceEditor.onCommandKey event, 0, event.which
expect(event.stopPropagation).toHaveBeenCalled()

View File

@@ -35,10 +35,9 @@ class Editor extends Template
buildAceEditor: ->
@aceEditor = ace.edit this[0]
@aceEditor.setTheme(require "ace/theme/twilight")
@aceEditor.setKeyboardHandler
handleKeyboard: (data, hashId, keyString, keyCode, e) =>
if @keyEventHandler?.handleKeyEvent(e)
handleKeyboard: (data, hashId, keyString, keyCode, event) =>
if event and @keyEventHandler?.handleKeyEvent(event)
{command: {exec: ->}}
else
null