aboutsummaryrefslogtreecommitdiff
path: root/atom/packages/vim-mode/lib/view-models/vim-normal-mode-input-element.coffee
blob: 23717531611e4c7ac1f2b29ee23d5df93f42307b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class VimNormalModeInputElement extends HTMLDivElement
  createdCallback: ->
    @className = "normal-mode-input"

  initialize: (@viewModel, @mainEditorElement, opts = {}) ->
    if opts.class?
      @classList.add(opts.class)

    @editorElement = document.createElement "atom-text-editor"
    @editorElement.classList.add('editor')
    @editorElement.getModel().setMini(true)
    @editorElement.setAttribute('mini', '')
    @appendChild(@editorElement)

    @singleChar = opts.singleChar
    @defaultText = opts.defaultText ? ''

    if opts.hidden
      @classList.add('vim-hidden-normal-mode-input')
      @mainEditorElement.parentNode.appendChild(this)
    else
      @panel = atom.workspace.addBottomPanel(item: this, priority: 100)

    @focus()
    @handleEvents()

    this

  handleEvents: ->
    if @singleChar?
      compositing = false
      @editorElement.getModel().getBuffer().onDidChange (e) =>
        @confirm() if e.newText and not compositing
      @editorElement.addEventListener 'compositionstart', -> compositing = true
      @editorElement.addEventListener 'compositionend', -> compositing = false
    else
      atom.commands.add(@editorElement, 'editor:newline', @confirm.bind(this))

    atom.commands.add(@editorElement, 'core:confirm', @confirm.bind(this))
    atom.commands.add(@editorElement, 'core:cancel', @cancel.bind(this))
    atom.commands.add(@editorElement, 'blur', @cancel.bind(this))

  confirm: ->
    @value = @editorElement.getModel().getText() or @defaultText
    @viewModel.confirm(this)
    @removePanel()

  focus: ->
    @editorElement.focus()

  cancel: (e) ->
    @viewModel.cancel(this)
    @removePanel()

  removePanel: ->
    atom.workspace.getActivePane().activate()
    if @panel?
      @panel.destroy()
    else
      this.remove()

module.exports =
document.registerElement("vim-normal-mode-input"
  extends: "div",
  prototype: VimNormalModeInputElement.prototype
)