Add a hidden pref for tab title with support for Creator-Year-Title option (#2985)

This commit is contained in:
Martynas Bagdonas 2023-02-02 22:35:58 +00:00 committed by GitHub
parent 87fbf3c7fb
commit a5fb64f295
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 25 deletions

View file

@ -105,7 +105,7 @@ class ReaderInstance {
this.state = state; this.state = state;
this._itemID = item.id; this._itemID = item.id;
// Set `ReaderTab` title as fast as possible // Set `ReaderTab` title as fast as possible
this.updateTitle(); await this.updateTitle();
let path = await item.getFilePathAsync(); let path = await item.getFilePathAsync();
// Check file size, otherwise we get uncatchable error: // Check file size, otherwise we get uncatchable error:
// JavaScript error: resource://gre/modules/osfile/osfile_native.jsm, line 60: RangeError: invalid array length // JavaScript error: resource://gre/modules/osfile/osfile_native.jsm, line 60: RangeError: invalid array length
@ -151,10 +151,11 @@ class ReaderInstance {
} }
}, [buf]); }, [buf]);
// Set title once again, because `ReaderWindow` isn't loaded the first time // Set title once again, because `ReaderWindow` isn't loaded the first time
this.updateTitle(); await this.updateTitle();
this._prefObserverIDs = [ this._prefObserverIDs = [
Zotero.Prefs.registerObserver('fontSize', this._handleFontSizeChange) Zotero.Prefs.registerObserver('fontSize', this._handleFontSizePrefChange),
Zotero.Prefs.registerObserver('tabs.title', this._handleTabTitlePrefChange)
]; ];
return true; return true;
@ -170,32 +171,34 @@ class ReaderInstance {
return this._itemID; return this._itemID;
} }
updateTitle() { async updateTitle() {
let item = Zotero.Items.get(this._itemID); let item = Zotero.Items.get(this._itemID);
let title = item.getDisplayTitle(); let readerTitle = item.getDisplayTitle();
let parentItem = item.parentItem; let parentItem = item.parentItem;
if (parentItem) { if (parentItem) {
let attachment = await parentItem.getBestAttachment();
if (attachment && attachment.id === this._itemID) {
let parts = []; let parts = [];
let displayTitle = parentItem.getDisplayTitle(); let type = Zotero.Prefs.get('tabs.title');
if (displayTitle) { let creator = parentItem.getField('firstCreator');
parts.push(displayTitle);
}
let firstCreator = parentItem.getField('firstCreator');
if (firstCreator) {
parts.push(firstCreator);
}
let year = parentItem.getField('year'); let year = parentItem.getField('year');
if (year) { let title = parentItem.getDisplayTitle();
parts.push(year); // If creator is missing fall back to titleCreatorYear
if (type === 'creatorYearTitle' && creator) {
parts = [creator, year, title];
} }
else if (type === 'title') {
title = parts.join(' - '); parts = [title];
} }
// If type is titleCreatorYear, or is missing, or another type falls back
this._title = title; else {
this._setTitleValue(title); parts = [title, creator, year];
}
readerTitle = parts.filter(x => x).join(' - ');
}
}
this._title = readerTitle;
this._setTitleValue(readerTitle);
} }
async setAnnotations(items) { async setAnnotations(items) {
@ -598,10 +601,14 @@ class ReaderInstance {
|| item.parentItem && item.parentItem.deleted; || item.parentItem && item.parentItem.deleted;
} }
_handleFontSizeChange = () => { _handleFontSizePrefChange = () => {
this._postMessage({ action: 'setFontSize', fontSize: Zotero.Prefs.get('fontSize') }); this._postMessage({ action: 'setFontSize', fontSize: Zotero.Prefs.get('fontSize') });
}; };
_handleTabTitlePrefChange = async () => {
await this.updateTitle();
};
_dataURLtoBlob(dataurl) { _dataURLtoBlob(dataurl) {
let parts = dataurl.split(','); let parts = dataurl.split(',');
let mime = parts[0].match(/:(.*?);/)[1]; let mime = parts[0].match(/:(.*?);/)[1];

View file

@ -211,3 +211,6 @@ pref("extensions.zotero.annotations.noteTemplates.note", "<p>{{citation}} {{comm
// Scaffold // Scaffold
pref("extensions.zotero.scaffold.eslint.enabled", true); pref("extensions.zotero.scaffold.eslint.enabled", true);
// Tabs
pref("extensions.zotero.tabs.title", "titleCreatorYear");