]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/lib/view-models/vim-normal-mode-input-element.coffee
Adds atom packages
[rbdr/dotfiles] / atom / packages / vim-mode / lib / view-models / vim-normal-mode-input-element.coffee
1 class VimNormalModeInputElement extends HTMLDivElement
2 createdCallback: ->
3 @className = "normal-mode-input"
4
5 initialize: (@viewModel, @mainEditorElement, opts = {}) ->
6 if opts.class?
7 @classList.add(opts.class)
8
9 @editorElement = document.createElement "atom-text-editor"
10 @editorElement.classList.add('editor')
11 @editorElement.getModel().setMini(true)
12 @editorElement.setAttribute('mini', '')
13 @appendChild(@editorElement)
14
15 @singleChar = opts.singleChar
16 @defaultText = opts.defaultText ? ''
17
18 if opts.hidden
19 @classList.add('vim-hidden-normal-mode-input')
20 @mainEditorElement.parentNode.appendChild(this)
21 else
22 @panel = atom.workspace.addBottomPanel(item: this, priority: 100)
23
24 @focus()
25 @handleEvents()
26
27 this
28
29 handleEvents: ->
30 if @singleChar?
31 compositing = false
32 @editorElement.getModel().getBuffer().onDidChange (e) =>
33 @confirm() if e.newText and not compositing
34 @editorElement.addEventListener 'compositionstart', -> compositing = true
35 @editorElement.addEventListener 'compositionend', -> compositing = false
36 else
37 atom.commands.add(@editorElement, 'editor:newline', @confirm.bind(this))
38
39 atom.commands.add(@editorElement, 'core:confirm', @confirm.bind(this))
40 atom.commands.add(@editorElement, 'core:cancel', @cancel.bind(this))
41 atom.commands.add(@editorElement, 'blur', @cancel.bind(this))
42
43 confirm: ->
44 @value = @editorElement.getModel().getText() or @defaultText
45 @viewModel.confirm(this)
46 @removePanel()
47
48 focus: ->
49 @editorElement.focus()
50
51 cancel: (e) ->
52 @viewModel.cancel(this)
53 @removePanel()
54
55 removePanel: ->
56 atom.workspace.getActivePane().activate()
57 if @panel?
58 @panel.destroy()
59 else
60 this.remove()
61
62 module.exports =
63 document.registerElement("vim-normal-mode-input"
64 extends: "div",
65 prototype: VimNormalModeInputElement.prototype
66 )