From 16650b4ddf98b86afc960fdc4c26b1cce73eb3d3 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Wed, 17 Feb 2021 12:01:45 +0200 Subject: [PATCH] Improve note creation from annotations --- chrome/content/zotero/xpcom/editorInstance.js | 23 ++++++++++--------- chrome/content/zotero/zoteroPane.js | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/chrome/content/zotero/xpcom/editorInstance.js b/chrome/content/zotero/xpcom/editorInstance.js index dd90e0e9ca..ce9975c843 100644 --- a/chrome/content/zotero/xpcom/editorInstance.js +++ b/chrome/content/zotero/xpcom/editorInstance.js @@ -926,27 +926,27 @@ class EditorInstance { wrappedJSObject: io }); } - - static canCreateNoteFromAnnotations(item) { - return item.parentID && item.isAttachment() && item.attachmentContentType === 'application/pdf'; - } - static async createNoteFromAnnotations(attachmentItem) { - if (!this.canCreateNoteFromAnnotations(attachmentItem)) { - return; - } - let annotations = attachmentItem.getAnnotations(); + /** + * Create note from annotations + * + * @param {Zotero.Item[]} annotations + * @param {Integer} parentID Creates standalone note if not provided + * @returns {Promise} + */ + static async createNoteFromAnnotations(annotations, parentID) { if (!annotations.length) { return; } let note = new Zotero.Item('note'); - note.libraryID = attachmentItem.libraryID; - note.parentID = attachmentItem.parentID; + note.libraryID = annotations[0].libraryID; + note.parentID = parentID; await note.saveTx(); let editorInstance = new EditorInstance(); editorInstance._item = note; let jsonAnnotations = []; for (let annotation of annotations) { + let attachmentItem = Zotero.Items.get(annotation.parentID); let jsonAnnotation = await Zotero.Annotations.toJSON(annotation); jsonAnnotation.attachmentItemID = attachmentItem.id; jsonAnnotations.push(jsonAnnotation); @@ -955,6 +955,7 @@ class EditorInstance { html += await editorInstance._digestAnnotations(jsonAnnotations); note.setNote(html); await note.saveTx(); + return note; } } diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 7588b03e1e..168ace5ceb 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -2975,7 +2975,7 @@ var ZoteroPane = new function() } - if (Zotero.EditorInstance.canCreateNoteFromAnnotations(item)) { + if (item.isPDFAttachment()) { show.push(m.createNoteFromAnnotations); } } @@ -4687,7 +4687,7 @@ var ZoteroPane = new function() } let item = this.getSelectedItems()[0]; - Zotero.EditorInstance.createNoteFromAnnotations(item); + Zotero.EditorInstance.createNoteFromAnnotations(item.getAnnotations(), item.parentID); }; this.createEmptyParent = async function (item) {