From 9e08e4e3a091d9dae8c92664c756dcdb6a27baed Mon Sep 17 00:00:00 2001 From: Aleksey Hoffman <61761672+aleksey-hoffman@users.noreply.github.com> Date: Sat, 11 Sep 2021 18:29:28 +0300 Subject: [PATCH] feature: add shortcut 'Ctrl + W': close current tab / window --- src/shortcuts.js | 17 +++++++++++++++++ src/store.js | 35 ++++++++++++++++++++++++++++++++--- src/utils/notifications.js | 8 ++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/shortcuts.js b/src/shortcuts.js index 8ae924ab..2f8f4988 100644 --- a/src/shortcuts.js +++ b/src/shortcuts.js @@ -137,6 +137,23 @@ export default { shortcut: 'Ctrl + T', description: 'New tab in current workspace' }, + closeCurrentTab: { + isGlobal: false, + isReadOnly: false, + conditions: { + dirItemIsSelected: true, + dialogIsOpened: false + }, + routes: ['navigator'], + icon: 'mdi-tab-remove', + iconSize: '20px', + action: { + name: 'CLOSE_CURRENT_TAB', + options: 'selected' + }, + shortcut: 'Ctrl + W', + description: 'Close current tab / window' + }, scrollTop: { isGlobal: false, isReadOnly: false, diff --git a/src/store.js b/src/store.js index 5d379cb9..abb70f05 100644 --- a/src/store.js +++ b/src/store.js @@ -2755,15 +2755,44 @@ export default new Vuex.Store({ tabs = [] dispatch('SET_TABS', tabs) }, - CLOSE_TAB ({ state, commit, dispatch, getters }, tab) { - let tabs = [...getters.selectedWorkspace.tabs] + CLOSE_CURRENT_TAB (store) { + let tabs = [...store.getters.selectedWorkspace.tabs] + const currentDirTab = tabs.find(item => item.path === store.state.navigatorView.currentDir.path) + if (currentDirTab) { + store.dispatch('CLOSE_TAB', currentDirTab) + } + if (tabs.length === 0) { + eventHub.$emit('notification', { + action: 'update-by-type', + type: 'current-workspace-has-no-tabs', + timeout: 3000, + type: '', + closeButton: true, + title: 'Current workspace has no tabs' + }) + } + }, + CLOSE_TAB (store, tab) { + if (!tab?.path) {return} + let tabs = [...store.getters.selectedWorkspace.tabs] const tabIndex = tabs.findIndex(item => item.path === tab.path) const tabExists = tabIndex !== -1 // Remove tab if (tabExists) { tabs.splice(tabIndex, 1) + store.dispatch('SET_TABS', tabs) + new Notification({ + name: 'tabRemoved', + format: { + tabPath: tab.path + } + }) + // Switch tab + const currentDirTabIndex = tabs.findIndex(item => item.path === store.state.navigatorView.currentDir.path) + if (currentDirTabIndex === -1 && tabs.length > 0) { + store.dispatch('SWITCH_TAB', tabs.length) + } } - dispatch('SET_TABS', tabs) }, ADD_TAB ({ state, commit, dispatch, getters }) { let item = state.navigatorView.selectedDirItems.getLast() diff --git a/src/utils/notifications.js b/src/utils/notifications.js index 050aa011..a176a47b 100644 --- a/src/utils/notifications.js +++ b/src/utils/notifications.js @@ -97,6 +97,14 @@ class Notification { title: 'Rename undone', message: '' }, + tabRemoved: { + action: 'update-by-type', + type: 'tab-removed', + timeout: 3000, + closeButton: true, + title: 'Removed tab from current workspace', + message: `{{tabPath}}` + }, } this.data = this.notifications[params.name]