]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/spec/spec-helper.coffee
Remove vim-snipmate
[rbdr/dotfiles] / atom / packages / ex-mode / spec / spec-helper.coffee
1 ExState = require '../lib/ex-state'
2 GlobalExState = require '../lib/global-ex-state'
3
4 beforeEach ->
5 atom.workspace ||= {}
6
7 activateExMode = ->
8 atom.workspace.open().then ->
9 atom.commands.dispatch(atom.views.getView(atom.workspace), 'ex-mode:open')
10 keydown('escape')
11 atom.workspace.getActivePane().destroyActiveItem()
12
13
14 getEditorElement = (callback) ->
15 textEditor = null
16
17 waitsForPromise ->
18 atom.workspace.open().then (e) ->
19 textEditor = e
20
21 runs ->
22 # element = document.createElement("atom-text-editor")
23 # element.setModel(textEditor)
24 # element.classList.add('vim-mode')
25 # element.exState = new ExState(element, new GlobalExState)
26 #
27 # element.addEventListener "keydown", (e) ->
28 # atom.keymaps.handleKeyboardEvent(e)
29
30 element = atom.views.getView(textEditor)
31
32 callback(element)
33
34 dispatchKeyboardEvent = (target, eventArgs...) ->
35 e = document.createEvent('KeyboardEvent')
36 e.initKeyboardEvent(eventArgs...)
37 # 0 is the default, and it's valid ASCII, but it's wrong.
38 Object.defineProperty(e, 'keyCode', get: -> undefined) if e.keyCode is 0
39 target.dispatchEvent e
40
41 dispatchTextEvent = (target, eventArgs...) ->
42 e = document.createEvent('TextEvent')
43 e.initTextEvent(eventArgs...)
44 target.dispatchEvent e
45
46 keydown = (key, {element, ctrl, shift, alt, meta, raw}={}) ->
47 key = "U+#{key.charCodeAt(0).toString(16)}" unless key is 'escape' or raw?
48 element ||= document.activeElement
49 eventArgs = [
50 true, # bubbles
51 true, # cancelable
52 null, # view
53 key, # key
54 0, # location
55 ctrl, alt, shift, meta
56 ]
57
58 canceled = not dispatchKeyboardEvent(element, 'keydown', eventArgs...)
59 dispatchKeyboardEvent(element, 'keypress', eventArgs...)
60 if not canceled
61 if dispatchTextEvent(element, 'textInput', eventArgs...)
62 element.value += key
63 dispatchKeyboardEvent(element, 'keyup', eventArgs...)
64
65 module.exports = {keydown, getEditorElement, activateExMode}