From 894309d06152500c11a938ba62d3d1ad505d9fec Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 2 Sep 2022 01:43:44 +0200 Subject: [PATCH] Add `collectionID` option to EditorInstance.createNoteFromAnnotations() For adding a standalone note to the current collection --- chrome/content/zotero/contextPane.js | 7 ++++++- chrome/content/zotero/xpcom/editorInstance.js | 13 ++++++++++--- chrome/content/zotero/zoteroPane.js | 7 ++++++- test/tests/annotationsTest.js | 8 ++++---- test/tests/itemsTest.js | 4 ++-- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/chrome/content/zotero/contextPane.js b/chrome/content/zotero/contextPane.js index b0bcacf2b1..e79c49fd71 100644 --- a/chrome/content/zotero/contextPane.js +++ b/chrome/content/zotero/contextPane.js @@ -497,7 +497,12 @@ var ZoteroContextPane = new function () { if (!annotations.length) { return; } - var note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, child && attachment.parentID); + var note = await Zotero.EditorInstance.createNoteFromAnnotations( + annotations, + { + parentID: child && attachment.parentID + } + ); _updateAddToNote(); diff --git a/chrome/content/zotero/xpcom/editorInstance.js b/chrome/content/zotero/xpcom/editorInstance.js index 869acbcb73..627018814b 100644 --- a/chrome/content/zotero/xpcom/editorInstance.js +++ b/chrome/content/zotero/xpcom/editorInstance.js @@ -1212,10 +1212,12 @@ class EditorInstance { * Create note from annotations * * @param {Zotero.Item[]} annotations - * @param {Integer} parentID Creates standalone note if not provided + * @param {Object} options + * @param {Integer} options.parentID - Creates standalone note if not provided + * @param {Integer} options.collectionID - Only valid if parentID not provided * @returns {Promise} */ - static async createNoteFromAnnotations(annotations, parentID) { + static async createNoteFromAnnotations(annotations, { parentID, collectionID } = {}) { if (!annotations.length) { throw new Error("No annotations provided"); } @@ -1236,7 +1238,12 @@ class EditorInstance { let note = new Zotero.Item('note'); note.libraryID = annotations[0].libraryID; - note.parentID = parentID; + if (parentID) { + note.parentID = parentID; + } + else if (collectionID) { + note.addToCollection(collectionID); + } await note.saveTx(); let editorInstance = new EditorInstance(); editorInstance._item = note; diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index f4f6ad311e..c84d97ff86 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -4962,7 +4962,12 @@ var ZoteroPane = new function() if (!annotations.length) { return; } - var note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, attachment.parentID); + var note = await Zotero.EditorInstance.createNoteFromAnnotations( + annotations, + { + parentID: attachment.parentID + } + ); if (!skipSelect) { await this.selectItem(note.id); } diff --git a/test/tests/annotationsTest.js b/test/tests/annotationsTest.js index 6070240878..2336529b72 100644 --- a/test/tests/annotationsTest.js +++ b/test/tests/annotationsTest.js @@ -413,7 +413,7 @@ describe("Create a note from annotations from multiple items and attachments", f annotations.push(annotation1); let annotation2 = await createAnnotation('highlight', attachment); annotations.push(annotation2); - let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); + let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations); assert.equal(note.note.split('test.pdf').length - 1, 1); assert.equal(note.note.split(annotation1.annotationText).length - 1, 1); assert.equal(note.note.split(annotation2.annotationText).length - 1, 1); @@ -428,7 +428,7 @@ describe("Create a note from annotations from multiple items and attachments", f annotations.push(annotation1); let annotation2 = await createAnnotation('highlight', attachment2); annotations.push(annotation2); - let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); + let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations); assert.equal(note.note.split('test.pdf').length - 1, 2); assert.equal(note.note.split('>' + item.getField('title') + '<').length - 1, 0); assert.equal(note.note.split(annotation1.annotationText).length - 1, 1); @@ -445,7 +445,7 @@ describe("Create a note from annotations from multiple items and attachments", f annotations.push(annotation1); let annotation2 = await createAnnotation('highlight', attachment2); annotations.push(annotation2); - let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); + let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations); assert.equal(note.note.split('test.pdf').length - 1, 0); assert.equal(note.note.split('>' + item1.getField('title') + '<').length - 1, 1); assert.equal(note.note.split('>' + item2.getField('title') + '<').length - 1, 1); @@ -468,7 +468,7 @@ describe("Create a note from annotations from multiple items and attachments", f annotations.push(annotation3); let annotation4 = await createAnnotation('highlight', attachment3); annotations.push(annotation4); - let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); + let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations); Zotero.debug(note.note); assert.equal(note.note.split('test.pdf').length - 1, 2); assert.equal(note.note.split('>' + item1.getField('title') + '<').length - 1, 1); diff --git a/test/tests/itemsTest.js b/test/tests/itemsTest.js index 4d3bd758a2..01e05b44dc 100644 --- a/test/tests/itemsTest.js +++ b/test/tests/itemsTest.js @@ -608,7 +608,7 @@ describe("Zotero.Items", function () { await item2.saveTx(); let attachment2 = await importPDFAttachment(item2); let annotation2 = await createAnnotation('highlight', attachment2); - let annotation2Note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation2], item2.id); + let annotation2Note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation2], { parentID: item2.id }); assert.include(annotation2Note.getNote(), attachment2.key); @@ -651,7 +651,7 @@ describe("Zotero.Items", function () { for (let filename of attachmentFilenames) { let attachment = await importFileAttachment(filename, { parentID: item2.id }); let annotation = await createAnnotation('highlight', attachment); - let note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation], item2.id); + let note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation], { parentID: item2.id }); attachments2.push(attachment); annotations2.push(annotation); notes2.push(note);