From 2ca16186272057699fcc6863bd22414f73f03fdd Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 18 Sep 2020 02:43:24 -0400 Subject: [PATCH] Tab bar: Change tab with Ctrl-Page-Up/Page-Down --- chrome/content/zotero/components/tabBar.jsx | 29 ++++++++++++++++++++- chrome/content/zotero/tabs.js | 19 +++++++++++++- chrome/content/zotero/zoteroPane.js | 12 +++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/components/tabBar.jsx b/chrome/content/zotero/components/tabBar.jsx index 6591556ae6..f9134be386 100644 --- a/chrome/content/zotero/components/tabBar.jsx +++ b/chrome/content/zotero/components/tabBar.jsx @@ -33,6 +33,33 @@ const TabBar = forwardRef(function (props, ref) { const [selectedIndex, setSelectedIndex] = useState(0); useImperativeHandle(ref, () => ({ + get selectedIndex() { + return selectedIndex; + }, + + selectLeft() { + var newIndex = selectedIndex - 1; + if (newIndex < 0) { + newIndex = tabs.length + newIndex; + } + setSelectedIndex(newIndex); + }, + + selectRight() { + var newIndex = selectedIndex + 1; + if (newIndex >= tabs.length) { + newIndex = newIndex - tabs.length; + } + setSelectedIndex(newIndex); + }, + + select(index) { + if (index > tabs.length - 1) { + throw new Error("Tab index out of bounds"); + } + setSelectedIndex(index); + }, + addTab({ title, type }) { var newTabs = [...tabs]; newTabs.push({ title, type }); @@ -49,7 +76,7 @@ const TabBar = forwardRef(function (props, ref) { return newTab; }); setTabs(newTabs); - } + }, })); useEffect(() => { diff --git a/chrome/content/zotero/tabs.js b/chrome/content/zotero/tabs.js index af07e5fd8d..ac144ab5cd 100644 --- a/chrome/content/zotero/tabs.js +++ b/chrome/content/zotero/tabs.js @@ -44,6 +44,7 @@ var Zotero_Tabs = new function () { type: 'library' } ]; + this._selectedIndex = 0; this.init = function () { ReactDOM.render( @@ -59,6 +60,21 @@ var Zotero_Tabs = new function () { }; + this.selectLeft = function () { + this._tabBarRef.current.selectLeft(); + }; + + + this.selectRight = function () { + this._tabBarRef.current.selectRight(); + }; + + + this.select = function (index) { + this._tabBarRef.current.select(index); + }, + + /** * @return {Element} - The element created in the deck */ @@ -85,7 +101,7 @@ var Zotero_Tabs = new function () { this.rename = function (title, index) { if (index === undefined) { - index = this.deck.selectedIndex; + index = this._selectedIndex; } this._tabs[index].title = title; this._tabBarRef.current.renameTab(title, index); @@ -93,6 +109,7 @@ var Zotero_Tabs = new function () { this._onTabSelected = function (index) { + this._selectedIndex = index; this.deck.selectedIndex = index; }; diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index a92606c63f..eeb24301c8 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -628,6 +628,18 @@ var ZoteroPane = new function() var command = Zotero.Keys.getCommand(event.key); + 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) {