From 69543120b538005a2000bf4514f0c6bbe1747bcf Mon Sep 17 00:00:00 2001 From: Marcel Gerber Date: Wed, 14 Jun 2017 00:28:10 +0200 Subject: [PATCH] Add ProjectManager.getSelectedFileTreeItem --- src/document/DocumentCommandHandlers.js | 4 +--- src/project/ProjectManager.js | 30 ++++++++++++++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/document/DocumentCommandHandlers.js b/src/document/DocumentCommandHandlers.js index 25d27858d3a..ca217e83e91 100644 --- a/src/document/DocumentCommandHandlers.js +++ b/src/document/DocumentCommandHandlers.js @@ -626,10 +626,8 @@ define(function (require, exports, module) { // If a file is currently selected in the tree, put it next to it. // If a directory is currently selected in the tree, put it in it. // If an Untitled document is selected or nothing is selected in the tree, put it at the root of the project. - // (Note: 'selected' may be an item that's selected in the workingset and not the tree; but in that case - // ProjectManager.createNewItem() ignores the baseDir we give it and falls back to the project root on its own) var baseDirEntry, - selected = ProjectManager.getSelectedItem(false); + selected = ProjectManager.getSelectedFileTreeItem(); if ((!selected) || (selected instanceof InMemoryFile)) { selected = ProjectManager.getProjectRoot(); } diff --git a/src/project/ProjectManager.js b/src/project/ProjectManager.js index 53cfe162d66..137e5666480 100644 --- a/src/project/ProjectManager.js +++ b/src/project/ProjectManager.js @@ -382,26 +382,33 @@ define(function (require, exports, module) { * Singleton actionCreator that is used for dispatching changes to the ProjectModel. */ var actionCreator = new ActionCreator(model); + + /** + * Returns the File or Directory corresponding to the item selected in the file tree; or null if no + * file tree item is selected. + * MAY NOT be identical to the current Document - a folder may be selected in the file tree. + * @return {?(File|Directory)} + */ + function getSelectedFileTreeItem() { + // Prefer file tree context, else use file tree selection + var selectedEntry = model.getContext(); + if (!selectedEntry) { + selectedEntry = model.getSelected(); + } + return selectedEntry; + } /** * Returns the File or Directory corresponding to the item selected in the sidebar panel, whether in * the file tree OR in the working set; or null if no item is selected anywhere in the sidebar. * May NOT be identical to the current Document - a folder may be selected in the sidebar, or the sidebar may not * have the current document visible in the tree & working set. - * @param {boolean=} includeWorkingSet If true, fall back to the working set item that is selected if there's - * no selected file tree item (default behaviour) * @return {?(File|Directory)} */ - function getSelectedItem(includeWorkingSet) { - if (includeWorkingSet === undefined) { - includeWorkingSet = true; - } - // Prefer file tree context, then selection, else use working set - var selectedEntry = model.getContext(); + function getSelectedItem() { + // Prefer file tree, else use working set + var selectedEntry = getSelectedFileTreeItem(); if (!selectedEntry) { - selectedEntry = model.getSelected(); - } - if (!selectedEntry && includeWorkingSet) { selectedEntry = MainViewManager.getCurrentlyViewedFile(); } return selectedEntry; @@ -1401,6 +1408,7 @@ define(function (require, exports, module) { exports.makeProjectRelativeIfPossible = makeProjectRelativeIfPossible; exports.shouldShow = ProjectModel.shouldShow; exports.openProject = openProject; + exports.getSelectedFileTreeItem = getSelectedFileTreeItem; exports.getSelectedItem = getSelectedItem; exports.getContext = getContext; exports.getInitialProjectPath = getInitialProjectPath;