Tab bar: Change tab with Ctrl-Page-Up/Page-Down

This commit is contained in:
Dan Stillman 2020-09-18 02:43:24 -04:00
parent a47d7e4d9c
commit 2ca1618627
3 changed files with 58 additions and 2 deletions

View file

@ -33,6 +33,33 @@ const TabBar = forwardRef(function (props, ref) {
const [selectedIndex, setSelectedIndex] = useState(0); const [selectedIndex, setSelectedIndex] = useState(0);
useImperativeHandle(ref, () => ({ 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 }) { addTab({ title, type }) {
var newTabs = [...tabs]; var newTabs = [...tabs];
newTabs.push({ title, type }); newTabs.push({ title, type });
@ -49,7 +76,7 @@ const TabBar = forwardRef(function (props, ref) {
return newTab; return newTab;
}); });
setTabs(newTabs); setTabs(newTabs);
} },
})); }));
useEffect(() => { useEffect(() => {

View file

@ -44,6 +44,7 @@ var Zotero_Tabs = new function () {
type: 'library' type: 'library'
} }
]; ];
this._selectedIndex = 0;
this.init = function () { this.init = function () {
ReactDOM.render( 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 * @return {Element} - The element created in the deck
*/ */
@ -85,7 +101,7 @@ var Zotero_Tabs = new function () {
this.rename = function (title, index) { this.rename = function (title, index) {
if (index === undefined) { if (index === undefined) {
index = this.deck.selectedIndex; index = this._selectedIndex;
} }
this._tabs[index].title = title; this._tabs[index].title = title;
this._tabBarRef.current.renameTab(title, index); this._tabBarRef.current.renameTab(title, index);
@ -93,6 +109,7 @@ var Zotero_Tabs = new function () {
this._onTabSelected = function (index) { this._onTabSelected = function (index) {
this._selectedIndex = index;
this.deck.selectedIndex = index; this.deck.selectedIndex = index;
}; };

View file

@ -628,6 +628,18 @@ var ZoteroPane = new function()
var command = Zotero.Keys.getCommand(event.key); 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 (from == 'zotero-collections-tree') {
if ((event.keyCode == event.DOM_VK_BACK_SPACE && Zotero.isMac) || if ((event.keyCode == event.DOM_VK_BACK_SPACE && Zotero.isMac) ||
event.keyCode == event.DOM_VK_DELETE) { event.keyCode == event.DOM_VK_DELETE) {