Tab bar: Close current tab with Cmd/Ctrl-w

And use keydown, not keypress, for moving between tabs
This commit is contained in:
Dan Stillman 2020-09-21 05:48:35 -04:00
parent 9afd509704
commit b5f7255da2
3 changed files with 52 additions and 18 deletions

View file

@ -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(() => {

View file

@ -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);
};

View file

@ -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) {