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'
6 [globalVimState, statusBarManager] = []
10 statusBarManager = null
14 getEditorElement = (callback) ->
18 atom.project.open().then (e) ->
22 element = document.createElement("atom-text-editor")
23 element.setModel(textEditor)
24 element.classList.add('vim-mode')
25 statusBarManager ?= new StatusBarManager
26 globalVimState ?= new GlobalVimState
27 element.vimState = new VimState(element, statusBarManager, globalVimState)
29 element.addEventListener "keydown", (e) ->
30 atom.keymaps.handleKeyboardEvent(e)
32 # mock parent element for the text editor
33 document.createElement('html').appendChild(atom.views.getView(textEditor))
37 mockPlatform = (editorElement, platform) ->
38 wrapper = document.createElement('div')
39 wrapper.className = platform
40 wrapper.appendChild(editorElement)
42 unmockPlatform = (editorElement) ->
43 editorElement.parentNode.removeChild(editorElement)
45 dispatchKeyboardEvent = (target, eventArgs...) ->
46 e = document.createEvent('KeyboardEvent')
47 e.initKeyboardEvent(eventArgs...)
48 # 0 is the default, and it's valid ASCII, but it's wrong.
49 Object.defineProperty(e, 'keyCode', get: -> undefined) if e.keyCode is 0
50 target.dispatchEvent e
52 dispatchTextEvent = (target, eventArgs...) ->
53 e = document.createEvent('TextEvent')
54 e.initTextEvent(eventArgs...)
55 target.dispatchEvent e
57 keydown = (key, {element, ctrl, shift, alt, meta, raw}={}) ->
58 key = "U+#{key.charCodeAt(0).toString(16)}" unless key is 'escape' or raw?
59 element ||= document.activeElement
66 ctrl, alt, shift, meta
69 canceled = not dispatchKeyboardEvent(element, 'keydown', eventArgs...)
70 dispatchKeyboardEvent(element, 'keypress', eventArgs...)
72 if dispatchTextEvent(element, 'textInput', eventArgs...)
74 dispatchKeyboardEvent(element, 'keyup', eventArgs...)
76 module.exports = {keydown, getEditorElement, mockPlatform, unmockPlatform}