Fix duplicate attachment row from attachments-box notify() (#4250)

This commit is contained in:
Abe Jellinek 2024-06-19 02:23:46 -04:00 committed by GitHub
parent fad3e25278
commit 310e741a01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 24 deletions

View file

@ -104,40 +104,24 @@
}
notify(action, type, ids) {
if (!(action === 'add' || action === 'modify' || action === 'refresh' || action === 'delete')) {
return;
}
if (!this._item?.isRegularItem()) return;
this._updateAttachmentIDs().then(() => {
this.updatePreview();
let attachments = Zotero.Items.get((this._attachmentIDs).filter(id => ids.includes(id)));
if (attachments.length === 0 && action !== "delete") {
return;
}
if (action == 'add') {
for (let attachment of attachments) {
this.addRow(attachment);
}
}
// When annotation added to attachment, action=modify
// When annotation deleted from attachment, action=refresh
else if (action == 'modify' || action == 'refresh') {
for (let attachment of attachments) {
let row = this.querySelector(`attachment-row[attachment-id="${attachment.id}"]`);
if (row) {
row.remove();
}
this.addRow(attachment);
}
}
else if (action == 'delete') {
for (let id of ids) {
let row = this.querySelector(`attachment-row[attachment-id="${id}"]`);
if (row) {
row.remove();
this.querySelector(`attachment-row[attachment-id="${id}"]`)
?.remove();
}
if (action !== 'delete') {
let attachments = Zotero.Items.get(this._attachmentIDs.filter(id => ids.includes(id)));
for (let attachment of attachments) {
this.addRow(attachment);
}
}
}
this.updateCount();
});
}

View file

@ -597,6 +597,9 @@ describe("Item pane", function () {
assert.equal(preview.previewType, "pdf");
// 2 rows
assert.equal(attachmentsBox.querySelectorAll("attachment-row").length, 2);
// Simulate an extra 'add' event on the attachment - still 2 rows
attachmentsBox.notify('add', 'item', [attachment2.id]);
assert.equal(attachmentsBox.querySelectorAll("attachment-row").length, 2);
// Created annotations should be update in preview and attachment row
let annotation = await createAnnotation('highlight', attachment2);