From 7889cd5d39d9fe4edee0542f6f8203b956b04368 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 21 Mar 2021 14:35:57 -0400 Subject: [PATCH] Fix item.clone() on annotation items --- chrome/content/zotero/xpcom/annotations.js | 5 +++++ chrome/content/zotero/xpcom/data/item.js | 9 ++++++++- test/tests/itemTest.js | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/annotations.js b/chrome/content/zotero/xpcom/annotations.js index e061115cb5..6739ccc3b4 100644 --- a/chrome/content/zotero/xpcom/annotations.js +++ b/chrome/content/zotero/xpcom/annotations.js @@ -31,6 +31,11 @@ Zotero.Annotations = new function () { Zotero.defineProperty(this, 'ANNOTATION_TYPE_NOTE', { value: 2 }); Zotero.defineProperty(this, 'ANNOTATION_TYPE_IMAGE', { value: 3 }); + Zotero.defineProperty(this, 'PROPS', { + value: ['type', 'text', 'comment', 'color', 'sortIndex', 'position'], + writable: false + }); + this.getCacheImagePath = function ({ libraryID, key }) { var file = this._getLibraryCacheDirectory(libraryID); diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 674ae2a4a2..a753dd2a46 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -4481,7 +4481,7 @@ Zotero.Item.prototype.clone = function (libraryID, options = {}) { if (this.isRegularItem()) { newItem.setCreators(this.getCreators()); } - else { + else if (this.isNote() || this.isAttachment()) { newItem.setNote(this.getNote()); if (sameLibrary) { var parent = this.parentKey; @@ -4501,6 +4501,13 @@ Zotero.Item.prototype.clone = function (libraryID, options = {}) { } } } + else if (this.isAnnotation()) { + let props = Zotero.Annotations.PROPS; + for (let prop of props) { + let fullProp = 'annotation' + Zotero.Utilities.capitalize(prop); + newItem[fullProp] = this[fullProp]; + } + } if (!options.skipTags) { newItem.setTags(this.getTags()); diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 7dec374f50..87991ccacf 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1729,6 +1729,19 @@ describe("Zotero.Item", function () { var newItem = item.clone(); assert.isEmpty(Object.keys(newItem.toJSON().relations)); }); + + it("should clone an annotation item", async function () { + var attachment = await importFileAttachment('test.pdf'); + var annotation = await createAnnotation('highlight', attachment); + var newAnnotation = annotation.clone(); + + var fields = Object.keys(annotation.toJSON()) + .filter(field => field.startsWith('annotation')); + assert.isAbove(fields.length, 0); + for (let field of fields) { + assert.equal(annotation[field], newAnnotation[field]); + } + }); }) describe("#moveToLibrary()", function () {