From 0d12396bf377826fc020c4d1e994751c2a512ed1 Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Fri, 17 Mar 2017 18:09:06 -0700 Subject: [PATCH] Add `saveFocusedPaneItem()` and call it in "core:save" command Also, do the same for "core:save-as" and `saveFocusedPaneItemAs()`. This behavior change means that pane items in docks (#13977) will be savable too. --- spec/workspace-spec.js | 32 ++++++++++++++++++++++ src/register-default-commands.coffee | 4 +-- src/workspace.js | 41 ++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/spec/workspace-spec.js b/spec/workspace-spec.js index d2f922b57..8dbc59b40 100644 --- a/spec/workspace-spec.js +++ b/spec/workspace-spec.js @@ -1872,6 +1872,38 @@ i = /test/; #FIXME\ }) }) + describe('::saveFocusedPaneItem', () => { + let editor, workspaceElement + + beforeEach(() => { + workspaceElement = atom.views.getView(atom.workspace) + document.body.appendChild(workspaceElement) + waitsForPromise(() => atom.workspace.open('a').then(o => { editor = o })) + }) + + afterEach(() => { + workspaceElement.remove() + }) + + it("calls the focused item's save method", () => { + spyOn(editor, 'save') + editor.getElement().focus() + atom.workspace.saveFocusedPaneItem() + expect(editor.save).toHaveBeenCalled() + }) + + it("doesn't save the active editor if it's not focused", () => { + spyOn(editor, 'save') + const input = document.createElement('input') + document.body.appendChild(input) + input.focus() + expect(document.activeElement).toBe(input) + atom.workspace.saveFocusedPaneItem() + expect(editor.save).not.toHaveBeenCalled() + input.remove() + }) + }) + describe('::saveActivePaneItem()', () => { let editor = null beforeEach(() => diff --git a/src/register-default-commands.coffee b/src/register-default-commands.coffee index 3b3849f12..833f21ac0 100644 --- a/src/register-default-commands.coffee +++ b/src/register-default-commands.coffee @@ -77,8 +77,8 @@ module.exports = ({commandRegistry, commandInstaller, config, notificationManage 'window:toggle-auto-indent': -> config.set("editor.autoIndent", not config.get("editor.autoIndent")) 'pane:reopen-closed-item': -> @getModel().reopenItem() 'core:close': -> @getModel().closeActivePaneItemOrEmptyPaneOrWindow() - 'core:save': -> @getModel().saveActivePaneItem() - 'core:save-as': -> @getModel().saveActivePaneItemAs() + 'core:save': -> @getModel().saveFocusedPaneItem() + 'core:save-as': -> @getModel().saveFocusedPaneItemAs() }, false ) diff --git a/src/workspace.js b/src/workspace.js index 1916c24c6..36a398193 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -10,6 +10,7 @@ const DefaultDirectorySearcher = require('./default-directory-searcher') const Model = require('./model') const TextEditor = require('./text-editor') const PaneContainer = require('./pane-container') +const Pane = require('./pane') const Panel = require('./panel') const PanelContainer = require('./panel-container') const Task = require('./task') @@ -863,7 +864,7 @@ module.exports = class Workspace extends Model { // {::saveActivePaneItemAs} # will be called instead. This method does nothing // if the active item does not implement a `.save` method. saveActivePaneItem () { - return this.getActivePane().saveActiveItem() + this.getActivePane().saveActiveItem() } // Prompt the user for a path and save the active pane item to it. @@ -872,7 +873,43 @@ module.exports = class Workspace extends Model { // `.saveAs` on the item with the selected path. This method does nothing if // the active item does not implement a `.saveAs` method. saveActivePaneItemAs () { - return this.getActivePane().saveActiveItemAs() + this.getActivePane().saveActiveItemAs() + } + + getFocusedPane () { + let el = document.activeElement + while (el != null) { + if (typeof el.getModel === 'function') { + const model = el.getModel() + if (model instanceof Pane) return model + } + el = el.parentElement + } + } + + // Save the currently focused pane item. + // + // If the focused pane item currently has a URI according to the item's + // `.getURI` method, calls `.save` on the item. Otherwise + // {::saveFocusedPaneItemAs} will be called instead. This method does nothing + // if the focused item does not implement a `.save` method. + saveFocusedPaneItem () { + const pane = this.getFocusedPane() + if (pane) { + pane.saveActiveItem() + } + } + + // Prompt the user for a path and save the focused pane item to it. + // + // Opens a native dialog where the user selects a path on disk, then calls + // `.saveAs` on the item with the selected path. This method does nothing if + // the focused item does not implement a `.saveAs` method. + saveFocusedPaneItemAs () { + const pane = this.getFocusedPane() + if (pane) { + pane.saveActiveItemAs() + } } // Destroy (close) the active pane item.