1 VimState = require '../lib/vim-state'
2 GlobalVimState = require '../lib/global-vim-state'
3 VimMode = require '../lib/vim-mode'
4 StatusBarManager = require '../lib/status-bar-manager'
9 getEditorElement = (callback) ->
13 atom.project.open().then (e) ->
17 element = document.createElement("atom-text-editor")
18 element.setModel(textEditor)
19 element.classList.add('vim-mode')
20 element.vimState = new VimState(element, new StatusBarManager, new GlobalVimState)
22 element.addEventListener "keydown", (e) ->
23 atom.keymaps.handleKeyboardEvent(e)
27 mockPlatform = (editorElement, platform) ->
28 wrapper = document.createElement('div')
29 wrapper.className = platform
30 wrapper.appendChild(editorElement)
32 unmockPlatform = (editorElement) ->
33 editorElement.parentNode.removeChild(editorElement)
35 dispatchKeyboardEvent = (target, eventArgs...) ->
36 e = document.createEvent('KeyboardEvent')
37 e.initKeyboardEvent(eventArgs...)
38 # 0 is the default, and it's valid ASCII, but it's wrong.
39 Object.defineProperty(e, 'keyCode', get: -> undefined) if e.keyCode is 0
40 target.dispatchEvent e
42 dispatchTextEvent = (target, eventArgs...) ->
43 e = document.createEvent('TextEvent')
44 e.initTextEvent(eventArgs...)
45 target.dispatchEvent e
47 keydown = (key, {element, ctrl, shift, alt, meta, raw}={}) ->
48 key = "U+#{key.charCodeAt(0).toString(16)}" unless key is 'escape' or raw?
49 element ||= document.activeElement
56 ctrl, alt, shift, meta
59 canceled = not dispatchKeyboardEvent(element, 'keydown', eventArgs...)
60 dispatchKeyboardEvent(element, 'keypress', eventArgs...)
62 if dispatchTextEvent(element, 'textInput', eventArgs...)
64 dispatchKeyboardEvent(element, 'keyup', eventArgs...)
66 module.exports = {keydown, getEditorElement, mockPlatform, unmockPlatform}