Add "Export PDF" options to File menu

Quick implementation, but works on parent items or PDF attachments

Currently always includes annotations, but we'll want that to be an
option
This commit is contained in:
Dan Stillman 2021-03-03 15:30:29 -05:00
parent 2cea1a50f5
commit 49906c3fa0
3 changed files with 96 additions and 4 deletions

View file

@ -143,6 +143,46 @@ const ZoteroStandalone = new function() {
}
catch (e) {}
this.updateMenuItemEnabled('manage-attachments-menu', active);
// TEMP: Quick implementation
try {
let menuitem = document.getElementById('menu_export_files');
let sep = menuitem.nextSibling;
let zp = Zotero.getActiveZoteroPane();
if (zp) {
let numFiles = zp.getSelectedItems().reduce((num, item) => {
if (item.isPDFAttachment()) {
return num + 1;
}
if (item.isRegularItem()) {
return num + item.numPDFAttachments();
}
return num;
}, 0);
if (numFiles) {
menuitem.hidden = false;
sep.hidden = false;
if (numFiles == 1) {
menuitem.label = 'Export PDF…';
}
else {
menuitem.label = 'Export PDFs…';
}
}
else {
menuitem.hidden = true;
sep.hidden = true;
}
}
else {
menuitem.hidden = true;
sep.hidden = true;
}
}
catch (e) {
Zotero.logError(e);
}
};

View file

@ -147,7 +147,10 @@
command="cmd_zotero_newStandaloneNote"/>
<menuitem id="menu_newCollection" class="menu-type-library" label="&zotero.toolbar.newCollection.label;"
command="cmd_zotero_newCollection"/>
<menuitem label="Save As" class="menu-type-reader" oncommand="ZoteroStandalone.onReaderCmd('export')"/>
<!-- TODO: Localize -->
<menuitem id="menu_export_file" class="menu-type-reader"
label="Save As…"
oncommand="ZoteroStandalone.onReaderCmd('export')"/>
<menuitem label="Print" class="menu-type-reader" oncommand="ZoteroStandalone.onReaderCmd('print')"/>
<menuseparator/>
<menuitem id="menu_close" class="menu-type-library" label="&closeCmd.label;" key="key_close"
@ -155,6 +158,10 @@
<menuitem id="menu_close_tab" class="menu-type-reader" label="&closeCmd.label;" key="key_close"
accesskey="&closeCmd.accesskey;" oncommand="Zotero_Tabs.close()"/>
<menuseparator class="menu-type-library"/>
<menuitem id="menu_export_files" class="menu-type-library"
oncommand="ZoteroPane.exportSelectedFiles()"
hidden="true"/>
<menuseparator class="menu-type-library" hidden="true"/>
<menuitem id="menu_import" class="menu-type-library" label="&importCmd.label;"
command="cmd_zotero_import" key="key_import"/>
<menuitem id="menu_importFromClipboard" class="menu-type-library" label="&importFromClipboardCmd.label;"

View file

@ -4758,13 +4758,14 @@ var ZoteroPane = new function()
this.exportPDF = async function (itemID) {
let item = await Zotero.Items.getAsync(itemID);
if (!item || !item.isAttachment()) {
throw new Error('Item ' + itemID + ' is not attachment');
if (!item || !item.isPDFAttachment()) {
throw new Error('Item ' + itemID + ' is not a PDF attachment');
}
let filename = item.attachmentFilename;
var fp = new FilePicker();
fp.init(window, Zotero.getString('styles.editor.save'), fp.modeSave);
// TODO: Localize
fp.init(window, "Export File", fp.modeSave);
fp.appendFilter("PDF", "*.pdf");
fp.defaultString = filename;
@ -4776,6 +4777,50 @@ var ZoteroPane = new function()
};
// TEMP: Quick implementation
this.exportSelectedFiles = async function () {
var items = ZoteroPane.getSelectedItems()
.reduce((arr, item) => {
if (item.isPDFAttachment()) {
return arr.concat([item]);
}
if (item.isRegularItem()) {
return arr.concat(item.getAttachments()
.map(x => Zotero.Items.get(x))
.filter(x => x.isPDFAttachment()));
}
return arr;
}, []);
// Deduplicate, in case parent and child items are both selected
items = [...new Set(items)];
if (!items.length) return;
if (items.length == 1) {
await this.exportPDF(items[0].id);
return;
}
var fp = new FilePicker();
// TODO: Localize
fp.init(window, "Export Files", fp.modeGetFolder);
var rv = await fp.show();
if (rv === fp.returnOK || rv === fp.returnReplace) {
let folder = fp.file;
for (let item of items) {
let outputFile = OS.Path.join(folder, item.attachmentFilename);
if (await OS.File.exists(outputFile)) {
let newNSIFile = Zotero.File.pathToFile(outputFile);
newNSIFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o644);
outputFile = newNSIFile.path;
newNSIFile.remove(null);
}
await Zotero.PDFWorker.export(item.id, outputFile, true);
}
}
};
this.renameSelectedAttachmentsFromParents = Zotero.Promise.coroutine(function* () {
// TEMP: fix