From 3f75ca3a30839535c97d6d83ee8cfde583d43cd5 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Sun, 24 May 2015 11:49:29 +0200 Subject: [PATCH] comply with new Atom API --- lib/ex-command-mode-input-element.coffee | 64 ++++++++++++++++++++++++ lib/ex-command-mode-input-view.coffee | 59 ---------------------- lib/ex-view-model.coffee | 8 +-- lib/ex.coffee | 6 +-- lib/view-model.coffee | 4 +- package.json | 2 +- 6 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 lib/ex-command-mode-input-element.coffee delete mode 100644 lib/ex-command-mode-input-view.coffee diff --git a/lib/ex-command-mode-input-element.coffee b/lib/ex-command-mode-input-element.coffee new file mode 100644 index 0000000..91e0eb2 --- /dev/null +++ b/lib/ex-command-mode-input-element.coffee @@ -0,0 +1,64 @@ +class ExCommandModeInputElement 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("ex-command-mode-input" + extends: "div", + prototype: ExCommandModeInputElement.prototype +) diff --git a/lib/ex-command-mode-input-view.coffee b/lib/ex-command-mode-input-view.coffee deleted file mode 100644 index deb9f37..0000000 --- a/lib/ex-command-mode-input-view.coffee +++ /dev/null @@ -1,59 +0,0 @@ -{View} = require 'space-pen' -{TextEditorView} = require 'atom-space-pen-views' - -module.exports = -class ExCommandModeInputView extends View - @content: -> - @div class: 'command-mode-input', => - @div class: 'editor-container', outlet: 'editorContainer', => - @subview 'editor', new TextEditorView(mini: true) - - initialize: (@viewModel, opts = {})-> - if opts.class? - @editorContainer.addClass opts.class - - if opts.hidden - @editorContainer.addClass 'hidden-input' - - @singleChar = opts.singleChar - @defaultText = opts.defaultText ? '' - - @panel = atom.workspace.addBottomPanel(item: this, priority: 100) - - @focus() - @handleEvents() - - handleEvents: -> - if @singleChar? - @editor.find('input').on 'textInput', @autosubmit - @editor.on 'core:confirm', @confirm - @editor.on 'core:cancel', @cancel - @editor.on 'blur', @cancel - - stopHandlingEvents: -> - if @singleChar? - @editor.find('input').off 'textInput', @autosubmit - @editor.off 'core:confirm', @confirm - @editor.off 'core:cancel', @cancel - @editor.off 'blur', @cancel - - autosubmit: (event) => - @editor.setText(event.originalEvent.data) - @confirm() - - confirm: => - @value = @editor.getText() or @defaultText - @viewModel.confirm(@) - @remove() - - focus: => - @editorContainer.find('.editor').focus() - - cancel: (e) => - @viewModel.cancel(@) - @remove() - - remove: => - @stopHandlingEvents() - atom.workspace.getActivePane().activate() - @panel.destroy() diff --git a/lib/ex-view-model.coffee b/lib/ex-view-model.coffee index 4f9675d..0c99be9 100644 --- a/lib/ex-view-model.coffee +++ b/lib/ex-view-model.coffee @@ -6,11 +6,11 @@ class ExViewModel extends ViewModel super(@exCommand, class: 'command') @historyIndex = -1 - @view.editor.on('core:move-up', @increaseHistoryEx) - @view.editor.on('core:move-down', @decreaseHistoryEx) + atom.commands.add(@view.editorElement, 'core:move-up', @increaseHistoryEx) + atom.commands.add(@view.editorElement, 'core:move-down', @decreaseHistoryEx) restoreHistory: (index) -> - @view.editor.setText(@history(index).value) + @view.editorElement.getModel().setText(@history(index).value) history: (index) -> @exState.getExHistoryItem(index) @@ -24,7 +24,7 @@ class ExViewModel extends ViewModel if @historyIndex <= 0 # get us back to a clean slate @historyIndex = -1 - @view.editor.setText('') + @view.editorElement.getModel().setText('') else @historyIndex -= 1 @restoreHistory(@historyIndex) diff --git a/lib/ex.coffee b/lib/ex.coffee index 4a9a60d..c159534 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -97,7 +97,7 @@ class Ex filePath = filePath.trim() if filePath.indexOf(' ') isnt -1 throw new CommandError('Only one file name allowed') - buffer = atom.workspace.getActiveEditor().buffer + buffer = atom.workspace.getActiveTextEditor().buffer filePath = buffer.getPath() if filePath is '' buffer.setPath(getFullPath(filePath)) buffer.load() @@ -105,7 +105,7 @@ class Ex e: (args...) => @edit(args...) enew: -> - buffer = atom.workspace.getActiveEditor().buffer + buffer = atom.workspace.getActiveTextEditor().buffer buffer.setPath(undefined) buffer.load() @@ -114,7 +114,7 @@ class Ex deferred = Promise.defer() pane = atom.workspace.getActivePane() - editor = atom.workspace.getActiveEditor() + editor = atom.workspace.getActiveTextEditor() if atom.workspace.getActiveTextEditor().getPath() isnt undefined if filePath.length > 0 editorPath = editor.getPath() diff --git a/lib/view-model.coffee b/lib/view-model.coffee index 7568aac..5e761b1 100644 --- a/lib/view-model.coffee +++ b/lib/view-model.coffee @@ -1,10 +1,10 @@ -ExCommandModeInputView = require './ex-command-mode-input-view' +ExCommandModeInputElement = require './ex-command-mode-input-element' class ViewModel constructor: (@command, opts={}) -> {@editor, @exState} = @command - @view = new ExCommandModeInputView(@, opts) + @view = new ExCommandModeInputElement().initialize(@, opts) @editor.commandModeInputView = @view @exState.onDidFailToExecute => @view.remove() diff --git a/package.json b/package.json index fcbd553..74fee9f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "repository": "https://github.com/lloeki/ex-mode", "license": "MIT", "engines": { - "atom": ">=0.174.0 <2.0.0" + "atom": ">=0.200.0 <2.0.0" }, "dependencies": { "underscore-plus": "1.x",