From bb79e10851e0b5a532beaaa5fc58b9d2a4bd0044 Mon Sep 17 00:00:00 2001 From: abaevbog Date: Fri, 2 May 2025 22:34:06 -0700 Subject: [PATCH] Fix unintentional title transfer between attachments on item switch (#5245) Also added similar test for item details table Fixes #5244 --- .../content/zotero/elements/attachmentBox.js | 6 ++++ test/tests/itemPaneTest.js | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/chrome/content/zotero/elements/attachmentBox.js b/chrome/content/zotero/elements/attachmentBox.js index 6c04806178..765a86c4df 100644 --- a/chrome/content/zotero/elements/attachmentBox.js +++ b/chrome/content/zotero/elements/attachmentBox.js @@ -221,6 +221,12 @@ if (!(val instanceof Zotero.Item)) { throw new Error("'item' must be a Zotero.Item"); } + // Blur editable-text of old attachment. Otherwise, _handleTitleBlur() would fire after + // the new item is set but before updateInfo() sets the title field to have the new + // item's value, causing the old item's title to be set on the new item. + if (this._item && val.id != this._item.id && document.activeElement.closest("editable-text")) { + document.activeElement.blur(); + } if (val.isAttachment()) { this._item = val; this.hidden = false; diff --git a/test/tests/itemPaneTest.js b/test/tests/itemPaneTest.js index ce2595fdf7..f5f00b3b3f 100644 --- a/test/tests/itemPaneTest.js +++ b/test/tests/itemPaneTest.js @@ -609,6 +609,25 @@ describe("Item pane", function () { let fieldMode = newCreatorRow.querySelector("editable-text").getAttribute("fieldMode"); assert.equal(fieldMode, "0"); }); + + it("should save updated title when switching between items", async function () { + let itemOne = new Zotero.Item('book'); + let itemTwo = new Zotero.Item('book'); + itemOne.setField('title', 'Title_one'); + await itemOne.saveTx(); + await itemTwo.saveTx(); + await ZoteroPane.selectItem(itemOne.id); + + let itemDetails = ZoteroPane.itemPane._itemDetails; + let infoBox = itemDetails.getPane("info"); + + let titleField = infoBox.querySelector("#itembox-field-value-title"); + titleField.focus(); + titleField.value = "Updated title"; + await ZoteroPane.selectItem(itemTwo.id); + await waitForNotifierEvent('modify', 'item'); + assert.equal(itemOne.getDisplayTitle(), "Updated title"); + }); }); describe("Libraries and collections pane", function () { @@ -1643,6 +1662,21 @@ describe("Item pane", function () { attachmentBox._discardPreviewTimeout = currentDiscardTimeout; }); + + it("should not transfer focused title while switching between items", async function () { + let item = new Zotero.Item('book'); + let attachmentOne = await importFileAttachment('test.pdf', { title: 'PDF_one', parentItemID: item.id }); + let attachmentTwo = await importFileAttachment('test.pdf', { title: 'PDF_two', parentItemID: item.id }); + await ZoteroPane.selectItem(attachmentOne.id); + + let itemDetails = ZoteroPane.itemPane._itemDetails; + let attachmentBox = itemDetails.getPane(paneID); + + attachmentBox.querySelector("#title").focus(); + await ZoteroPane.selectItem(attachmentTwo.id); + await waitForNotifierEvent('modify', 'item'); + assert.equal(attachmentTwo.getDisplayTitle(), "PDF_two"); + }); });