aboutsummaryrefslogtreecommitdiff
path: root/atom/packages/vim-mode/lib/view-models
diff options
context:
space:
mode:
Diffstat (limited to 'atom/packages/vim-mode/lib/view-models')
-rw-r--r--atom/packages/vim-mode/lib/view-models/search-view-model.coffee42
-rw-r--r--atom/packages/vim-mode/lib/view-models/view-model.coffee24
-rw-r--r--atom/packages/vim-mode/lib/view-models/vim-command-mode-input-element.coffee64
3 files changed, 130 insertions, 0 deletions
diff --git a/atom/packages/vim-mode/lib/view-models/search-view-model.coffee b/atom/packages/vim-mode/lib/view-models/search-view-model.coffee
new file mode 100644
index 0000000..8787210
--- /dev/null
+++ b/atom/packages/vim-mode/lib/view-models/search-view-model.coffee
@@ -0,0 +1,42 @@
+{ViewModel} = require './view-model'
+
+module.exports =
+class SearchViewModel extends ViewModel
+ constructor: (@searchMotion) ->
+ super(@searchMotion, class: 'search')
+ @historyIndex = -1
+
+ atom.commands.add(@view.editorElement, 'core:move-up', @increaseHistorySearch)
+ atom.commands.add(@view.editorElement, 'core:move-down', @decreaseHistorySearch)
+
+ restoreHistory: (index) ->
+ @view.editorElement.getModel().setText(@history(index))
+
+ history: (index) ->
+ @vimState.getSearchHistoryItem(index)
+
+ increaseHistorySearch: =>
+ if @history(@historyIndex + 1)?
+ @historyIndex += 1
+ @restoreHistory(@historyIndex)
+
+ decreaseHistorySearch: =>
+ if @historyIndex <= 0
+ # get us back to a clean slate
+ @historyIndex = -1
+ @view.editorElement.getModel().setText('')
+ else
+ @historyIndex -= 1
+ @restoreHistory(@historyIndex)
+
+ confirm: (view) =>
+ repeatChar = if @searchMotion.initiallyReversed then '?' else '/'
+ if @view.value is '' or @view.value is repeatChar
+ lastSearch = @history(0)
+ if lastSearch?
+ @view.value = lastSearch
+ else
+ @view.value = ''
+ atom.beep()
+ super(view)
+ @vimState.pushSearchHistory(@view.value)
diff --git a/atom/packages/vim-mode/lib/view-models/view-model.coffee b/atom/packages/vim-mode/lib/view-models/view-model.coffee
new file mode 100644
index 0000000..9cc63e1
--- /dev/null
+++ b/atom/packages/vim-mode/lib/view-models/view-model.coffee
@@ -0,0 +1,24 @@
+VimCommandModeInputElement = require './vim-command-mode-input-element'
+
+class ViewModel
+ constructor: (@operation, opts={}) ->
+ {@editor, @vimState} = @operation
+ @view = new VimCommandModeInputElement().initialize(this, opts)
+ @editor.commandModeInputView = @view
+ @vimState.onDidFailToCompose => @view.remove()
+
+ confirm: (view) ->
+ @vimState.pushOperations(new Input(@view.value))
+
+ cancel: (view) ->
+ if @vimState.isOperatorPending()
+ @vimState.pushOperations(new Input(''))
+
+class Input
+ constructor: (@characters) ->
+ isComplete: -> true
+ isRecordable: -> true
+
+module.exports = {
+ ViewModel, Input
+}
diff --git a/atom/packages/vim-mode/lib/view-models/vim-command-mode-input-element.coffee b/atom/packages/vim-mode/lib/view-models/vim-command-mode-input-element.coffee
new file mode 100644
index 0000000..4fd0c52
--- /dev/null
+++ b/atom/packages/vim-mode/lib/view-models/vim-command-mode-input-element.coffee
@@ -0,0 +1,64 @@
+class VimCommandModeInputElement extends HTMLDivElement
+ createdCallback: ->
+ @className = "command-mode-input"
+
+ @editorContainer = document.createElement("div")
+ @editorContainer.className = "editor-container"
+
+ @appendChild(@editorContainer)
+
+ initialize: (@viewModel, opts = {}) ->
+ if opts.class?
+ @editorContainer.classList.add(opts.class)
+
+ if opts.hidden
+ @editorContainer.style.height = "0px"
+
+ @editorElement = document.createElement "atom-text-editor"
+ @editorElement.classList.add('editor')
+ @editorElement.getModel().setMini(true)
+ @editorElement.setAttribute('mini', '')
+ @editorContainer.appendChild(@editorElement)
+
+ @singleChar = opts.singleChar
+ @defaultText = opts.defaultText ? ''
+
+ @panel = atom.workspace.addBottomPanel(item: this, priority: 100)
+
+ @focus()
+ @handleEvents()
+
+ this
+
+ handleEvents: ->
+ if @singleChar?
+ @editorElement.getModel().getBuffer().onDidChange (e) =>
+ @confirm() if e.newText
+ 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()
+ @panel.destroy()
+
+module.exports =
+document.registerElement("vim-command-mode-input"
+ extends: "div",
+ prototype: VimCommandModeInputElement.prototype
+)