diff --git a/src/editor/attachments/drag_and_drop.js b/src/editor/attachments/drag_and_drop.js index 275585a28..7d2c23cc1 100644 --- a/src/editor/attachments/drag_and_drop.js +++ b/src/editor/attachments/drag_and_drop.js @@ -277,15 +277,18 @@ export class AttachmentDragAndDrop { if (!targetNode || !$isActionTextAttachmentNode(targetNode)) return if (draggedNode.is(targetNode)) return + // Resolve the gallery before detaching: the drop target matches any + // previewable attachment, but galleries only accept images, so removing + // first deleted PDFs and videos dropped onto another attachment. + const gallery = $findOrCreateGalleryForImage(targetNode) + if (!gallery) return + draggedNode.remove() - const gallery = $findOrCreateGalleryForImage(targetNode) - if (gallery) { - if (position === "before") { - targetNode.insertBefore(draggedNode) - } else { - targetNode.insertAfter(draggedNode) - } + if (position === "before") { + targetNode.insertBefore(draggedNode) + } else { + targetNode.insertAfter(draggedNode) } } diff --git a/test/browser/tests/attachments/attachment_drag_and_drop.test.js b/test/browser/tests/attachments/attachment_drag_and_drop.test.js index d2eadc44f..7c12f347f 100644 --- a/test/browser/tests/attachments/attachment_drag_and_drop.test.js +++ b/test/browser/tests/attachments/attachment_drag_and_drop.test.js @@ -146,6 +146,22 @@ test.describe("Attachment Drag and Drop", () => { await assertGalleryWithImages(editor, 3) }) + + test("drag a previewable non-image onto another keeps both", async ({ page, editor }) => { + await editor.uploadFile("test/fixtures/files/dummy.pdf") + await editor.send("Enter") + await editor.uploadFile("test/fixtures/files/dummy.pdf") + await waitForUploadsComplete(page, editor) + await expect(page.locator("figure.attachment")).toHaveCount(2) + + await simulateDragByIndex(page, 1, 0, "onto") + await editor.flush() + + // Galleries only take images, so this drop cannot be honoured. It should + // leave the document alone rather than swallow the dragged attachment. + await assertNoGallery(page) + await expect(page.locator("figure.attachment")).toHaveCount(2) + }) }) test.describe("Within-gallery reorder", () => {