fix: don't copy embedded images to its parent note

This commit is contained in:
windingwind 2024-12-13 13:18:13 +01:00
parent 6bfae7376c
commit e5e149e6f2
2 changed files with 18 additions and 3 deletions

View File

@ -114,7 +114,7 @@ async function renderSection(
if (!targetItem) {
continue;
}
count ++;
count++;
const linkParams = {
workspaceUID: (body.closest("bn-workspace") as Workspace)?.dataset.uid,

View File

@ -322,6 +322,10 @@ async function copyEmbeddedImagesFromNote(
) {
await Zotero.DB.executeTransaction(async () => {
for (const fromNote of sourceNotes) {
// Do not copy to itself, otherwise the note may break the DB
if (!fromNote.id || !targetNote.id || fromNote.id === targetNote.id) {
continue;
}
await Zotero.Notes.copyEmbeddedImages(fromNote, targetNote);
}
});
@ -361,17 +365,28 @@ async function copyEmbeddedImagesInHTML(
doc.querySelectorAll(`img[data-attachment-key="${attachment.key}"]`),
) as HTMLImageElement[];
if (nodes.length) {
let copiedAttachment: Zotero.Item;
let copiedAttachment: Zotero.Item | undefined;
await Zotero.DB.executeTransaction(async () => {
Zotero.DB.requireTransaction();
// Do not copy to itself, otherwise the note may break the DB
if (
!attachment.parentID ||
!targetNote.id ||
attachment.parentID === targetNote.id
) {
return;
}
copiedAttachment = await Zotero.Attachments.copyEmbeddedImage({
attachment,
note: targetNote,
});
});
if (!copiedAttachment) {
continue;
}
nodes.forEach(
(node) =>
node?.setAttribute("data-attachment-key", copiedAttachment.key),
node?.setAttribute("data-attachment-key", copiedAttachment!.key),
);
}
}