From d546caca4c3272562016999915fb8fc86bb4e1fd Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 13 Aug 2022 05:45:23 -0400 Subject: [PATCH] Don't relate book sections to each other when creating from book Fixes #2757 --- chrome/content/zotero/zoteroPane.js | 16 +++++++++++++++- test/tests/zoteroPaneTest.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 56142093be..301d2b6c2a 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -1813,7 +1813,9 @@ var ZoteroPane = new function() let original = this.getSelectedItems()[0]; let duplicate = await this.duplicateSelectedItem(); if (!duplicate) return null; - + + // TODO: Move this logic to duplicateSelectedItem() with a `targetItemType` flag to avoid + // extra saves? if (duplicate.itemType == 'book') { duplicate.setType(Zotero.ItemTypes.getID('bookSection')); for (let i = 0; i < duplicate.numCreators(); i++) { @@ -1823,6 +1825,18 @@ var ZoteroPane = new function() } duplicate.setCreator(i, creator); } + // Remove related-item relations to other book sections of this book + for (let relItemKey of [...duplicate.relatedItems]) { + let relItem = await Zotero.Items.getByLibraryAndKeyAsync( + duplicate.libraryID, relItemKey + ); + if (relItem.itemType == 'bookSection' + && relItem.getField('bookTitle') == original.getField('title')) { + duplicate.removeRelatedItem(relItem); + relItem.removeRelatedItem(duplicate); + await relItem.saveTx(); + } + } } else { duplicate.setField('title', false); // So bookTitle becomes title diff --git a/test/tests/zoteroPaneTest.js b/test/tests/zoteroPaneTest.js index 9102b811d9..0c40a754c9 100644 --- a/test/tests/zoteroPaneTest.js +++ b/test/tests/zoteroPaneTest.js @@ -442,6 +442,34 @@ describe("ZoteroPane", function() { }); + describe("#duplicateAndConvertSelectedItem()", function () { + describe("book to book section", function () { + it("should not add relations to other book sections for the same book", async function () { + await selectLibrary(win); + var bookItem = await createDataObject('item', { itemType: 'book', title: "Book Title" }); + + // Relate book to another book section with a different title + var otherBookSection = createUnsavedDataObject('item', { itemType: 'bookSection', setTitle: true }) + otherBookSection.setField('bookTitle', "Another Book Title"); + await otherBookSection.saveTx(); + bookItem.addRelatedItem(otherBookSection); + await bookItem.saveTx(); + otherBookSection.addRelatedItem(bookItem); + await otherBookSection.saveTx(); + + await zp.selectItem(bookItem.id); + var bookSectionItem1 = await zp.duplicateAndConvertSelectedItem(); + await zp.selectItem(bookItem.id); + var bookSectionItem2 = await zp.duplicateAndConvertSelectedItem(); + + // Book sections should only be related to parent + assert.sameMembers(bookSectionItem1.relatedItems, [bookItem.key, otherBookSection.key]); + assert.sameMembers(bookSectionItem2.relatedItems, [bookItem.key, otherBookSection.key]); + }); + }); + }); + + describe("#deleteSelectedItems()", function () { const DELETE_KEY_CODE = 46;