diff --git a/chrome/content/zotero/elements/contextPane.js b/chrome/content/zotero/elements/contextPane.js index 6b8ee474d8..553e600294 100644 --- a/chrome/content/zotero/elements/contextPane.js +++ b/chrome/content/zotero/elements/contextPane.js @@ -90,7 +90,7 @@ this._handleTabClose(action, type, ids, extraData); return; } - if (type == 'tab' && action == 'select') { + if (type == 'tab' && ["select", "load"].includes(action)) { this._handleTabSelect(action, type, ids, extraData); } } @@ -158,23 +158,24 @@ }); } - _handleTabSelect(action, type, ids) { + async _handleTabSelect(action, type, ids, extraData) { // TEMP: move these variables to ZoteroContextPane let _contextPaneSplitter = ZoteroContextPane.splitter; let _contextPane = document.getElementById('zotero-context-pane'); + let tabID = ids[0]; + let tabType = extraData[tabID].type; // It seems that changing `hidden` or `collapsed` values might // be related with significant slow down when there are too many // DOM nodes (i.e. 10k notes) - if (Zotero_Tabs.selectedType == 'library') { + if (tabType == 'library') { _contextPaneSplitter.setAttribute('hidden', true); _contextPane.setAttribute('collapsed', true); ZoteroContextPane.showLoadingMessage(false); this._sidenav.hidden = true; } - else if (Zotero_Tabs.selectedType.includes('reader')) { - let reader = Zotero.Reader.getByTabID(Zotero_Tabs.selectedID); - this._handleReaderReady(reader); - + else if (tabType == 'reader') { + this._handleReaderReady(tabID); + this._setupNotesContext(tabID); _contextPaneSplitter.setAttribute('hidden', false); _contextPane.setAttribute('collapsed', !(_contextPaneSplitter.getAttribute('state') != 'collapsed')); @@ -187,11 +188,26 @@ this._sidenav.hidden = false; } - this._selectItemContext(ids[0]); + this._selectItemContext(tabID); ZoteroContextPane.update(); } - async _handleReaderReady(reader) { + async _setupNotesContext(tabID) { + let { tab } = Zotero_Tabs._getTab(tabID); + if (!tab || !tab.data.itemID) return; + let attachment = await Zotero.Items.getAsync(tab.data.itemID); + if (attachment) { + this._selectNotesContext(attachment.libraryID); + let notesContext = this._getNotesContext(attachment.libraryID); + notesContext.updateNotesListFromCache(); + } + let currentNoteContext = this._getCurrentNotesContext(); + // Always switch to the current selected tab, since the selection might have changed + currentNoteContext.switchToTab(Zotero_Tabs.selectedID); + } + + async _handleReaderReady(tabID) { + let reader = Zotero.Reader.getByTabID(tabID); if (!reader) { return; } @@ -209,16 +225,6 @@ }); } } - - let attachment = await Zotero.Items.getAsync(reader.itemID); - if (attachment) { - this._selectNotesContext(attachment.libraryID); - let notesContext = this._getNotesContext(attachment.libraryID); - notesContext.updateNotesListFromCache(); - } - - let currentNoteContext = this._getCurrentNotesContext(); - currentNoteContext.switchToTab(reader.tabID); } _getCurrentNotesContext() { @@ -270,7 +276,7 @@ } } - async _addItemContext(tabID, itemID, tabType = "") { + async _addItemContext(tabID, itemID, _tabType = "") { let { libraryID } = Zotero.Items.getLibraryAndKeyFromID(itemID); let library = Zotero.Libraries.get(libraryID); await library.waitForDataLoad('item'); diff --git a/chrome/content/zotero/standalone/standalone.js b/chrome/content/zotero/standalone/standalone.js index 008ea491a0..089de41e2e 100644 --- a/chrome/content/zotero/standalone/standalone.js +++ b/chrome/content/zotero/standalone/standalone.js @@ -57,7 +57,7 @@ const ZoteroStandalone = new function() { this._notifierID = Zotero.Notifier.registerObserver( { notify: async (action, type, ids, extraData) => { - if (action == 'select') { + if (['select', 'load'].includes(action)) { // Reader doesn't have tabID yet setTimeout(async () => { // Item and other things might not be loaded yet when reopening tabs diff --git a/chrome/content/zotero/tabs.js b/chrome/content/zotero/tabs.js index 3c67762627..bd6a570701 100644 --- a/chrome/content/zotero/tabs.js +++ b/chrome/content/zotero/tabs.js @@ -456,6 +456,7 @@ var Zotero_Tabs = new function () { selectedTab.lastFocusedElement = document.activeElement; } if (tab.type === 'reader-unloaded') { + tab.type = "reader-loading"; // Make sure the loading message is displayed first. // Then, open reader and hide the loading message once it has loaded. ZoteroContextPane.showLoadingMessage(true); @@ -518,8 +519,10 @@ var Zotero_Tabs = new function () { // Mark a tab as loaded this.markAsLoaded = function (id) { let { tab } = this._getTab(id); - if (!tab) return; + if (!tab || tab.type == "reader") return; + let prevType = tab.type; tab.type = "reader"; + Zotero.Notifier.trigger("load", "tab", [id], { [id]: Object.assign({}, tab, { prevType }) }, true); }; this.unloadUnusedTabs = function () { @@ -569,7 +572,7 @@ var Zotero_Tabs = new function () { setTimeout(() => { reader.focus(); }); - } + }; /** * Moves focus to a tab in the specified direction. diff --git a/chrome/content/zotero/xpcom/reader.js b/chrome/content/zotero/xpcom/reader.js index 9dcd96410b..4e4293dcab 100644 --- a/chrome/content/zotero/xpcom/reader.js +++ b/chrome/content/zotero/xpcom/reader.js @@ -497,7 +497,7 @@ class ReaderInstance { } }, onToggleContextPane: () => { - Zotero.debug('toggle context pane') + Zotero.debug('toggle context pane'); let win = Zotero.getMainWindow(); win.ZoteroContextPane.togglePane(); }, @@ -1800,8 +1800,7 @@ class Reader { let { libraryID } = Zotero.Items.getLibraryAndKeyFromID(itemID); let library = Zotero.Libraries.get(libraryID); let win = Zotero.getMainWindow(); - // Change tab's type from "unloaded-reader" to "reader" - win.Zotero_Tabs.markAsLoaded(tabID); + await library.waitForDataLoad('item'); let item = Zotero.Items.get(itemID); @@ -1885,6 +1884,8 @@ class Reader { } }); this._readers.push(reader); + // Change tab's type from "reader-unloaded" to "reader" after reader loaded + win.Zotero_Tabs.markAsLoaded(tabID); } if (!openInBackground