From 76d8818bed68b0eb39bbcadcf7ba0855bac304b6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 26 Apr 2021 03:47:46 -0400 Subject: [PATCH] Don't count missing annotation values as changed if empty string passed Similar to 0d377a1c4a, this fixes an unnecessary annotation reload and loss of unsaved typing on auto-sync immediately after an annotation is created. --- chrome/content/zotero/xpcom/data/item.js | 8 ++++++++ test/content/support.js | 7 ++++++- test/tests/itemTest.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 06229a8665..a53874267f 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -3671,6 +3671,14 @@ for (let name of ['type', 'text', 'comment', 'color', 'pageLabel', 'sortIndex', set: function (value) { this._requireData('annotation'); + // Normalize values + if (typeof value == 'string') { + value = value.trim().normalize(); + if (value === "") { + value = null; + } + } + if (this._getLatestField(field) === value) { return; } diff --git a/test/content/support.js b/test/content/support.js index 333d38ab59..1d46958247 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -942,7 +942,12 @@ async function createAnnotation(type, parentItem, options = {}) { if (type == 'highlight') { annotation.annotationText = Zotero.Utilities.randomString(); } - annotation.annotationComment = Zotero.Utilities.randomString(); + if (options.comment !== undefined) { + annotation.annotationComment = options.comment; + } + else { + annotation.annotationComment = Zotero.Utilities.randomString(); + } annotation.annotationColor = '#ffd400'; var page = Zotero.Utilities.rand(1, 100); annotation.annotationPageLabel = `${page}`; diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 993b2c0aaf..fcfe0ac614 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1333,6 +1333,20 @@ describe("Zotero.Item", function () { }); }); + describe("#annotationComment", function () { + it("should not mark object without comment as changed if empty string", async function () { + var annotation = await createAnnotation('highlight', attachment, { comment: "" }); + annotation.annotationComment = ""; + assert.isFalse(annotation.hasChanged()); + }); + + it("should clear existing value when empty string is passed", async function () { + var annotation = await createAnnotation('highlight', attachment); + annotation.annotationComment = ""; + assert.isTrue(annotation.hasChanged()); + }); + }); + describe("#saveTx()", function () { it("should save a highlight annotation", async function () { var annotation = new Zotero.Item('annotation');