]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/spec/spec-helper.coffee
Adds atom
[rbdr/dotfiles] / atom / packages / vim-mode / spec / spec-helper.coffee
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'
5
6 beforeEach ->
7 atom.workspace ||= {}
8
9 getEditorElement = (callback) ->
10 textEditor = null
11
12 waitsForPromise ->
13 atom.project.open().then (e) ->
14 textEditor = e
15
16 runs ->
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)
21
22 element.addEventListener "keydown", (e) ->
23 atom.keymaps.handleKeyboardEvent(e)
24
25 callback(element)
26
27 mockPlatform = (editorElement, platform) ->
28 wrapper = document.createElement('div')
29 wrapper.className = platform
30 wrapper.appendChild(editorElement)
31
32 unmockPlatform = (editorElement) ->
33 editorElement.parentNode.removeChild(editorElement)
34
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
41
42 dispatchTextEvent = (target, eventArgs...) ->
43 e = document.createEvent('TextEvent')
44 e.initTextEvent(eventArgs...)
45 target.dispatchEvent e
46
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
50 eventArgs = [
51 true, # bubbles
52 true, # cancelable
53 null, # view
54 key, # key
55 0, # location
56 ctrl, alt, shift, meta
57 ]
58
59 canceled = not dispatchKeyboardEvent(element, 'keydown', eventArgs...)
60 dispatchKeyboardEvent(element, 'keypress', eventArgs...)
61 if not canceled
62 if dispatchTextEvent(element, 'textInput', eventArgs...)
63 element.value += key
64 dispatchKeyboardEvent(element, 'keyup', eventArgs...)
65
66 module.exports = {keydown, getEditorElement, mockPlatform, unmockPlatform}