From a5fb64f295369db8594badbc541f2d27f70c165e Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Thu, 2 Feb 2023 22:35:58 +0000 Subject: [PATCH] Add a hidden pref for tab title with support for Creator-Year-Title option (#2985) --- chrome/content/zotero/xpcom/reader.js | 57 +++++++++++++++------------ defaults/preferences/zotero.js | 3 ++ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/chrome/content/zotero/xpcom/reader.js b/chrome/content/zotero/xpcom/reader.js index 929f6b69f4..5661965ff2 100644 --- a/chrome/content/zotero/xpcom/reader.js +++ b/chrome/content/zotero/xpcom/reader.js @@ -105,7 +105,7 @@ class ReaderInstance { this.state = state; this._itemID = item.id; // Set `ReaderTab` title as fast as possible - this.updateTitle(); + await this.updateTitle(); let path = await item.getFilePathAsync(); // Check file size, otherwise we get uncatchable error: // JavaScript error: resource://gre/modules/osfile/osfile_native.jsm, line 60: RangeError: invalid array length @@ -151,10 +151,11 @@ class ReaderInstance { } }, [buf]); // Set title once again, because `ReaderWindow` isn't loaded the first time - this.updateTitle(); + await this.updateTitle(); this._prefObserverIDs = [ - Zotero.Prefs.registerObserver('fontSize', this._handleFontSizeChange) + Zotero.Prefs.registerObserver('fontSize', this._handleFontSizePrefChange), + Zotero.Prefs.registerObserver('tabs.title', this._handleTabTitlePrefChange) ]; return true; @@ -170,32 +171,34 @@ class ReaderInstance { return this._itemID; } - updateTitle() { + async updateTitle() { let item = Zotero.Items.get(this._itemID); - let title = item.getDisplayTitle(); + let readerTitle = item.getDisplayTitle(); let parentItem = item.parentItem; if (parentItem) { - let parts = []; - let displayTitle = parentItem.getDisplayTitle(); - if (displayTitle) { - parts.push(displayTitle); + let attachment = await parentItem.getBestAttachment(); + if (attachment && attachment.id === this._itemID) { + let parts = []; + let type = Zotero.Prefs.get('tabs.title'); + let creator = parentItem.getField('firstCreator'); + let year = parentItem.getField('year'); + let title = parentItem.getDisplayTitle(); + // If creator is missing fall back to titleCreatorYear + if (type === 'creatorYearTitle' && creator) { + parts = [creator, year, title]; + } + else if (type === 'title') { + parts = [title]; + } + // If type is titleCreatorYear, or is missing, or another type falls back + else { + parts = [title, creator, year]; + } + readerTitle = parts.filter(x => x).join(' - '); } - - let firstCreator = parentItem.getField('firstCreator'); - if (firstCreator) { - parts.push(firstCreator); - } - - let year = parentItem.getField('year'); - if (year) { - parts.push(year); - } - - title = parts.join(' - '); } - - this._title = title; - this._setTitleValue(title); + this._title = readerTitle; + this._setTitleValue(readerTitle); } async setAnnotations(items) { @@ -598,10 +601,14 @@ class ReaderInstance { || item.parentItem && item.parentItem.deleted; } - _handleFontSizeChange = () => { + _handleFontSizePrefChange = () => { this._postMessage({ action: 'setFontSize', fontSize: Zotero.Prefs.get('fontSize') }); }; + _handleTabTitlePrefChange = async () => { + await this.updateTitle(); + }; + _dataURLtoBlob(dataurl) { let parts = dataurl.split(','); let mime = parts[0].match(/:(.*?);/)[1]; diff --git a/defaults/preferences/zotero.js b/defaults/preferences/zotero.js index e4b8e86cb1..76b097b474 100644 --- a/defaults/preferences/zotero.js +++ b/defaults/preferences/zotero.js @@ -211,3 +211,6 @@ pref("extensions.zotero.annotations.noteTemplates.note", "

{{citation}} {{comm // Scaffold pref("extensions.zotero.scaffold.eslint.enabled", true); + +// Tabs +pref("extensions.zotero.tabs.title", "titleCreatorYear");