Add "Open PDF reader in new window" preference (#2868)

When enabled:
- Double-clicking a PDF or choosing "Open PDF" opens a new window
- Shift-double-clicking opens a new tab
- "Open in New Window" locate option becomes "Open in New Tab" and has the
  reverse behavior
This commit is contained in:
Abe Jellinek 2022-11-14 17:12:48 -05:00 committed by GitHub
parent b6591dba5a
commit a7b605f0cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 30 deletions

View file

@ -123,17 +123,17 @@ var Zotero_LocateMenu = new function() {
}); });
function _addViewOption(selectedItems, optionName, optionObject, showIcons) { function _addViewOption(selectedItems, optionName, optionObject, showIcons) {
var menuitem = _createMenuItem(Zotero.getString("locate."+optionName+".label"), var menuitem = _createMenuItem(optionObject.label || Zotero.getString(`locate.${optionName}.label`),
null, null); null, null);
if(showIcons) { if (showIcons) {
menuitem.setAttribute("class", "menuitem-iconic"); menuitem.setAttribute("class", "menuitem-iconic");
menuitem.style.listStyleImage = "url('"+optionObject.icon+"')"; menuitem.style.listStyleImage = `url('${optionObject.icon}')`;
} }
menuitem.setAttribute("zotero-locate", "true"); menuitem.setAttribute("zotero-locate", "true");
menuitem.addEventListener("command", function(event) { menuitem.addEventListener("command", function (event) {
optionObject.handleItems(selectedItems, event); optionObject.handleItems(selectedItems, event);
}, false) }, false);
return menuitem; return menuitem;
} }
@ -344,16 +344,30 @@ var Zotero_LocateMenu = new function() {
* Should appear only when the item is a PDF, or a linked or attached file or web attachment is * Should appear only when the item is a PDF, or a linked or attached file or web attachment is
* a PDF * a PDF
*/ */
function ViewPDF(inNewWindow) { function ViewPDF(alternateWindowBehavior) {
this.icon = "chrome://zotero/skin/treeitem-attachment-pdf.png"; this.icon = "chrome://zotero/skin/treeitem-attachment-pdf.png";
this._mimeTypes = ["application/pdf"]; this._mimeTypes = ["application/pdf"];
// Don't show "Open PDF in New Window" in toolbar Locate menu // Don't show alternate-behavior option ("in New Window" when openReaderInNewWindow is false,
this.hideInToolbar = inNewWindow; // "in New Tab" when it's true) in toolbar Locate menu
this.hideInToolbar = alternateWindowBehavior;
Object.defineProperty(this, 'label', {
get() {
if (alternateWindowBehavior) {
return Zotero.getString(Zotero.Prefs.get('openReaderInNewWindow')
? 'locate.pdfNewTab.label'
: 'locate.pdfNewWindow.label');
}
else {
return Zotero.getString('locate.pdf.label');
}
}
});
this.canHandleItem = async function (item) { this.canHandleItem = async function (item) {
// Don't show "Open PDF in New Window" when using an external PDF viewer // Don't show alternate-behavior option when using an external PDF viewer
if (inNewWindow && Zotero.Prefs.get("fileHandler.pdf")) { if (alternateWindowBehavior && Zotero.Prefs.get("fileHandler.pdf")) {
return false; return false;
} }
return _getFirstAttachmentWithMIMEType(item, this._mimeTypes).then((item) => !!item); return _getFirstAttachmentWithMIMEType(item, this._mimeTypes).then((item) => !!item);
@ -367,7 +381,7 @@ var Zotero_LocateMenu = new function() {
} }
ZoteroPane_Local.viewAttachment(attachments, event, false, ZoteroPane_Local.viewAttachment(attachments, event, false,
{ forceOpenPDFInWindow: inNewWindow }); { forceAlternateWindowBehavior: alternateWindowBehavior });
}); });
var _getFirstAttachmentWithMIMEType = Zotero.Promise.coroutine(function* (item, mimeTypes) { var _getFirstAttachmentWithMIMEType = Zotero.Promise.coroutine(function* (item, mimeTypes) {
@ -384,7 +398,7 @@ var Zotero_LocateMenu = new function() {
} }
ViewOptions.pdf = new ViewPDF(false); ViewOptions.pdf = new ViewPDF(false);
ViewOptions.pdfNewWindow = new ViewPDF(true); ViewOptions.pdfAlternateWindowBehavior = new ViewPDF(true);
/** /**
* "View Online" option * "View Online" option

View file

@ -170,11 +170,13 @@ Zotero_Preferences.General = {
var handler = Zotero.Prefs.get('fileHandler.pdf'); var handler = Zotero.Prefs.get('fileHandler.pdf');
var menulist = document.getElementById('fileHandler-pdf'); var menulist = document.getElementById('fileHandler-pdf');
var customMenuItem = document.getElementById('fileHandler-custom'); var customMenuItem = document.getElementById('fileHandler-custom');
var inNewWindowCheckbox = document.getElementById('open-reader-in-new-window');
// System default // System default
if (handler == 'system') { if (handler == 'system') {
customMenuItem.hidden = true; customMenuItem.hidden = true;
menulist.selectedIndex = 1; menulist.selectedIndex = 1;
inNewWindowCheckbox.disabled = true;
} }
// Custom handler // Custom handler
else if (handler) { else if (handler) {
@ -201,6 +203,7 @@ Zotero_Preferences.General = {
} }
customMenuItem.hidden = false; customMenuItem.hidden = false;
menulist.selectedIndex = 2; menulist.selectedIndex = 2;
inNewWindowCheckbox.disabled = true;
// There's almost certainly a better way to do this... // There's almost certainly a better way to do this...
// but why doesn't the icon just behave by default? // but why doesn't the icon just behave by default?
@ -211,6 +214,7 @@ Zotero_Preferences.General = {
let menuitem = document.getElementById('fileHandler-internal'); let menuitem = document.getElementById('fileHandler-internal');
menulist.selectedIndex = 0; menulist.selectedIndex = 0;
customMenuItem.hidden = true; customMenuItem.hidden = true;
inNewWindowCheckbox.disabled = false;
} }
}, },

View file

@ -55,20 +55,26 @@
oncommand="Zotero_Preferences.General.updateAutoRenameFilesUI()" native="true"/> oncommand="Zotero_Preferences.General.updateAutoRenameFilesUI()" native="true"/>
</vbox> </vbox>
<hbox align="center"> <vbox>
<label value="&zotero.preferences.fileHandler.openPDFsUsing;" control="file-handler-pdf"/> <hbox align="center">
<menulist id="fileHandler-pdf" class="fileHandler-menu" native="true"> <label value="&zotero.preferences.fileHandler.openPDFsUsing;" control="file-handler-pdf"/>
<menupopup> <menulist id="fileHandler-pdf" class="fileHandler-menu" native="true">
<menuitem id="fileHandler-internal" <menupopup>
oncommand="Zotero_Preferences.General.setFileHandler('pdf', '')"/> <menuitem id="fileHandler-internal"
<menuitem label="&zotero.preferences.fileHandler.systemDefault;" oncommand="Zotero_Preferences.General.setFileHandler('pdf', '')"/>
oncommand="Zotero_Preferences.General.setFileHandler('pdf', 'system')"/> <menuitem label="&zotero.preferences.fileHandler.systemDefault;"
<menuitem id="fileHandler-custom"/> oncommand="Zotero_Preferences.General.setFileHandler('pdf', 'system')"/>
<menuitem label="&zotero.preferences.custom;" <menuitem id="fileHandler-custom"/>
oncommand="Zotero_Preferences.General.chooseFileHandler('pdf')"/> <menuitem label="&zotero.preferences.custom;"
</menupopup> oncommand="Zotero_Preferences.General.chooseFileHandler('pdf')"/>
</menulist> </menupopup>
</hbox> </menulist>
</hbox>
<checkbox id="open-reader-in-new-window" class="indented-pref"
label="&zotero.preferences.fileHandler.openReaderInNewWindow;"
preference="extensions.zotero.openReaderInNewWindow"
native="true"/>
</vbox>
</groupbox> </groupbox>
<groupbox> <groupbox>

View file

@ -4446,13 +4446,17 @@ var ZoteroPane = new function()
let pdfHandler = Zotero.Prefs.get("fileHandler.pdf"); let pdfHandler = Zotero.Prefs.get("fileHandler.pdf");
// Zotero PDF reader // Zotero PDF reader
if (!pdfHandler) { if (!pdfHandler) {
let openInWindow = Zotero.Prefs.get('openReaderInNewWindow');
let useAlternateWindowBehavior = event?.shiftKey || extraData?.forceAlternateWindowBehavior;
if (useAlternateWindowBehavior) {
openInWindow = !openInWindow;
}
await Zotero.Reader.open( await Zotero.Reader.open(
itemID, itemID,
extraData && extraData.location, extraData && extraData.location,
{ {
openInWindow: (event && event.shiftKey) openInWindow,
|| (extraData && extraData.forceOpenPDFInWindow), allowDuplicate: openInWindow
allowDuplicate: event && event.shiftKey
} }
); );
return; return;

View file

@ -19,6 +19,7 @@
<!ENTITY zotero.preferences.autoRenameFiles.renameLinked "Rename linked files"> <!ENTITY zotero.preferences.autoRenameFiles.renameLinked "Rename linked files">
<!ENTITY zotero.preferences.fileHandler.openPDFsUsing "Open PDFs using"> <!ENTITY zotero.preferences.fileHandler.openPDFsUsing "Open PDFs using">
<!ENTITY zotero.preferences.fileHandler.systemDefault "System Default"> <!ENTITY zotero.preferences.fileHandler.systemDefault "System Default">
<!ENTITY zotero.preferences.fileHandler.openReaderInNewWindow "Open PDFs in new windows instead of tabs">
<!ENTITY zotero.preferences.miscellaneous "Miscellaneous"> <!ENTITY zotero.preferences.miscellaneous "Miscellaneous">
<!ENTITY zotero.preferences.autoUpdate "Automatically check for updated translators and styles"> <!ENTITY zotero.preferences.autoUpdate "Automatically check for updated translators and styles">

View file

@ -1220,6 +1220,7 @@ createParent.prompt = Enter a DOI, ISBN, PMID, arXiv ID, or ADS Bibcode to i
locate.online.label = View Online locate.online.label = View Online
locate.pdf.label = Open PDF locate.pdf.label = Open PDF
locate.pdfNewWindow.label = Open PDF in New Window locate.pdfNewWindow.label = Open PDF in New Window
locate.pdfNewTab.label = Open PDF in New Tab
locate.snapshot.label = View Snapshot locate.snapshot.label = View Snapshot
locate.file.label = View File locate.file.label = View File
locate.externalViewer.label = Open in External Viewer locate.externalViewer.label = Open in External Viewer

View file

@ -184,8 +184,8 @@ pref("extensions.zotero.purge.tags", false);
// Zotero pane persistent data // Zotero pane persistent data
pref("extensions.zotero.pane.persist", ""); pref("extensions.zotero.pane.persist", "");
// Custom file handlers
pref("extensions.zotero.fileHandler.pdf", ""); pref("extensions.zotero.fileHandler.pdf", "");
pref("extensions.zotero.openReaderInNewWindow", false);
// File/URL opening executable if launch() fails // File/URL opening executable if launch() fails
pref("extensions.zotero.fallbackLauncher.unix", "/usr/bin/xdg-open"); pref("extensions.zotero.fallbackLauncher.unix", "/usr/bin/xdg-open");