Don't relate book sections to each other when creating from book

Fixes #2757
This commit is contained in:
Dan Stillman 2022-08-13 05:45:23 -04:00
parent b505c630d3
commit 3dc3359cff
2 changed files with 43 additions and 1 deletions

View file

@ -1820,7 +1820,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++) {
@ -1830,6 +1832,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

View file

@ -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;