From b5f7255da2a5a6342f5ec752d616a0e41a849a03 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 21 Sep 2020 05:48:35 -0400 Subject: [PATCH] Tab bar: Close current tab with Cmd/Ctrl-w And use keydown, not keypress, for moving between tabs --- chrome/content/zotero/components/tabBar.jsx | 11 +++++- chrome/content/zotero/tabs.js | 16 +++++++- chrome/content/zotero/zoteroPane.js | 43 ++++++++++++++------- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/chrome/content/zotero/components/tabBar.jsx b/chrome/content/zotero/components/tabBar.jsx index f9134be386..e47329c502 100644 --- a/chrome/content/zotero/components/tabBar.jsx +++ b/chrome/content/zotero/components/tabBar.jsx @@ -60,14 +60,14 @@ const TabBar = forwardRef(function (props, ref) { setSelectedIndex(index); }, - addTab({ title, type }) { + add({ title, type }) { var newTabs = [...tabs]; newTabs.push({ title, type }); setTabs(newTabs); setSelectedIndex(newTabs.length - 1); }, - renameTab(title, index) { + rename(title, index) { var newTabs = tabs.map((tab, currentIndex) => { let newTab = Object.assign({}, tab); if (index == currentIndex) { @@ -77,6 +77,13 @@ const TabBar = forwardRef(function (props, ref) { }); setTabs(newTabs); }, + + close(index) { + if (index == 0) { + return; + } + removeTab(index); + } })); useEffect(() => { diff --git a/chrome/content/zotero/tabs.js b/chrome/content/zotero/tabs.js index ac144ab5cd..0dea915efd 100644 --- a/chrome/content/zotero/tabs.js +++ b/chrome/content/zotero/tabs.js @@ -33,6 +33,10 @@ import TabBar from 'components/tabBar'; var Zotero_Tabs = new function () { const HTML_NS = 'http://www.w3.org/1999/xhtml'; + Object.defineProperty(this, 'selectedIndex', { + get: () => this._selectedIndex + }); + Object.defineProperty(this, 'deck', { get: () => document.getElementById('tabs-deck') }); @@ -79,7 +83,7 @@ var Zotero_Tabs = new function () { * @return {Element} - The element created in the deck */ this.add = function ({ title, type, url, index }) { - this._tabBarRef.current.addTab({ title, type }); + this._tabBarRef.current.add({ title, type }); var elem; if (url) { @@ -104,7 +108,15 @@ var Zotero_Tabs = new function () { index = this._selectedIndex; } this._tabs[index].title = title; - this._tabBarRef.current.renameTab(title, index); + this._tabBarRef.current.rename(title, index); + }; + + + this.close = function (index) { + if (index === undefined) { + index = this._selectedIndex; + } + this._tabBarRef.current.close(index); }; diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index de21d172fc..6ec39af8d2 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -542,6 +542,35 @@ var ZoteroPane = new function() } if (from == 'zotero-pane') { + // Tab navigation + // TODO: Select across tabs without selecting with Ctrl-Shift, as in Firefox? + let ctrlOnly = event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey; + if (ctrlOnly) { + if (event.key == 'PageUp') { + Zotero_Tabs.selectLeft(); + } + else if (event.key == 'PageDown') { + Zotero_Tabs.selectRight(); + } + event.preventDefault(); + return; + } + + // Close current tab + if (event.key == 'w') { + let close = Zotero.isMac + ? (event.metaKey && !event.shiftKey && !event.ctrlKey && !event.altKey) + : (event.ctrlKey && !event.shiftKey && !event.altKey); + if (close) { + if (Zotero_Tabs.selectedIndex > 0) { + Zotero_Tabs.close(); + event.preventDefault(); + event.stopPropagation(); + } + return; + } + } + // Highlight collections containing selected items // // We use Control (17) on Windows because Alt triggers the menubar; @@ -628,20 +657,6 @@ var ZoteroPane = new function() var command = Zotero.Keys.getCommand(event.key); - // Tab navigation - // TODO: Select across tabs without selecting with Ctrl-Shift, as in Firefox? - let ctrlOnly = event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey; - if (ctrlOnly) { - if (event.key == 'PageUp') { - Zotero_Tabs.selectLeft(); - } - else if (event.key == 'PageDown') { - Zotero_Tabs.selectRight(); - } - event.preventDefault(); - return; - } - if (from == 'zotero-collections-tree') { if ((event.keyCode == event.DOM_VK_BACK_SPACE && Zotero.isMac) || event.keyCode == event.DOM_VK_DELETE) {