Cache tabs' icons in data field (#4367)

So that after reload, we can display the correct icon sooner

Fixes: #4342
This commit is contained in:
abaevbog 2024-07-09 21:31:54 -07:00 committed by GitHub
parent 8cb93b35a4
commit 32b92c0205
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -73,7 +73,8 @@ var Zotero_Tabs = new function () {
this._tabs = [{
id: 'zotero-pane',
type: 'library',
title: ''
title: '',
data: {}
}];
this._selectedID = 'zotero-pane';
this._prevSelectedID = null;
@ -93,33 +94,41 @@ var Zotero_Tabs = new function () {
};
this._update = function () {
this._tabBarRef.current.setTabs(this._tabs.map((tab) => {
let icon = null;
// Go through all tabs and try to save their icons to tab.data
for (let tab of this._tabs) {
// If the icon was earlier cached, skip this tab
if (tab.data.icon) continue;
// Find the icon for the library tab
if (tab.id === 'zotero-pane') {
let index = ZoteroPane.collectionsView?.selection?.focused;
if (typeof index !== 'undefined' && ZoteroPane.collectionsView.getRow(index)) {
let iconName = ZoteroPane.collectionsView.getIconName(index);
icon = { isItemType: false, icon: iconName };
tab.data.icon = iconName;
}
}
else if (tab.data?.itemID) {
else {
// Try to fetch the icon for the reader tab
try {
let item = Zotero.Items.get(tab.data.itemID);
icon = { isItemType: true, icon: item.getItemTypeIconName(true) };
tab.data.icon = item.getItemTypeIconName(true);
}
catch (e) {
// item might not yet be loaded, we will get the right icon on the next update
// but until then use a default placeholder
icon = { isItemType: true, icon: null };
tab.data.icon = null;
}
}
}
this._tabBarRef.current.setTabs(this._tabs.map((tab) => {
return {
id: tab.id,
type: tab.type,
title: tab.title,
selected: tab.id == this._selectedID,
...icon,
isItemType: tab.id !== 'zotero-pane',
icon: tab.data?.icon || null
};
}));
// Disable File > Close menuitem if multiple tabs are open
@ -198,6 +207,9 @@ var Zotero_Tabs = new function () {
let tab = tabs[i];
if (tab.type === 'library') {
this.rename('zotero-pane', tab.title);
// At first, library tab is added without the icon data. We set it here once we know what it is
let libraryTab = this._getTab('zotero-pane');
libraryTab.tab.data = tab.data || {};
}
else if (tab.type === 'reader') {
if (Zotero.Items.exists(tab.data.itemID)) {