diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 9f4f80611..3c543b537 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -106,3 +106,47 @@ describe "Editor", -> expect(fs.exists(selectedFilePath)).toBeFalsy() + describe "when a keydown event is handled by the ace editor", -> + returnValue = null + handler = null + event = null + + beforeEach -> + event = keydownEvent 'x' + spyOn(event, 'stopPropagation') + + describe "when no key event handler has been assigned", -> + beforeEach -> + expect(editor.keyEventHandler).toBeNull() + + it "handles the event without crashing", -> + editor.aceEditor.onCommandKey event, 0, event.which + + describe "when a key event handler has been assigned", -> + beforeEach -> + handler = { + handleKeyEvent: jasmine.createSpy('handleKeyEvent').andCallFake -> + returnValue + } + 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() + diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index aafd81c5a..5bac41794 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -12,6 +12,7 @@ class Editor extends Template viewProperties: aceEditor: null buffer: null + keyEventHandler: null initialize: () -> @aceSessions = {} @@ -34,6 +35,13 @@ class Editor extends Template @aceEditor = ace.edit this[0] @aceEditor.setTheme(require "ace/theme/twilight") + @aceEditor.setKeyboardHandler + handleKeyboard: (data, hashId, keyString, keyCode, e) => + if @keyEventHandler?.handleKeyEvent(e) + {command: {exec: ->}} + else + null + getAceSession: -> @aceEditor.getSession()