From 3772301a6ec9f44f71dc4a8dfe968e3e4c6cd9ae Mon Sep 17 00:00:00 2001 From: adarshsm <24850536+adarshsm@users.noreply.github.com> Date: Fri, 16 Oct 2026 06:26:35 +0530 Subject: [PATCH] fix(xl-multi-column): undo an edge drop in a single step The multi-column drop handler removed the dragged blocks and then created or updated the column list with separate editor calls, so each dispatched its own transaction. When the two edits weren't adjacent, prosemirror-history put them in separate undo groups: the first undo only reverted the column change, leaving the dragged blocks deleted, and a second undo was needed to bring them back. Run the removal and the column list change in one `editor.transact` call so the whole drop is one transaction and one undo step. Closes #3197 --- .../DropCursor/multiColumnHandleDropPlugin.ts | 75 ++++++++++--------- .../src/test/extensions/columnDrop.test.ts | 27 +++++++ 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/packages/xl-multi-column/src/extensions/DropCursor/multiColumnHandleDropPlugin.ts b/packages/xl-multi-column/src/extensions/DropCursor/multiColumnHandleDropPlugin.ts index 5c0fc07dd1..1f786b9a25 100644 --- a/packages/xl-multi-column/src/extensions/DropCursor/multiColumnHandleDropPlugin.ts +++ b/packages/xl-multi-column/src/extensions/DropCursor/multiColumnHandleDropPlugin.ts @@ -195,22 +195,25 @@ export function createMultiColumnHandleDropPlugin( id: UniqueID.options.generateID(), }); - const blocksToRemove = draggedBlocks.filter( - (block) => - editor.getBlock(block.id) && - !blocksAlreadyInColumnList.has(block.id), - ); - if (blocksToRemove.length > 0) { - editor.removeBlocks(blocksToRemove); - } + // A single transaction, so that the drop is undone in one step. + editor.transact(() => { + const blocksToRemove = draggedBlocks.filter( + (block) => + editor.getBlock(block.id) && + !blocksAlreadyInColumnList.has(block.id), + ); + if (blocksToRemove.length > 0) { + editor.removeBlocks(blocksToRemove); + } - if (newChildren.length === 1) { - editor.replaceBlocks([columnList], draggedBlocks); - } else { - editor.updateBlock(columnList, { - children: newChildren, - }); - } + if (newChildren.length === 1) { + editor.replaceBlocks([columnList], draggedBlocks); + } else { + editor.updateBlock(columnList, { + children: newChildren, + }); + } + }); } else { // Create new columnList with blocks as columns const block = nodeToBlock(blockInfo.block.node, view.state.doc); @@ -228,27 +231,29 @@ export function createMultiColumnHandleDropPlugin( ? [draggedBlocks, [block]] : [[block], draggedBlocks]; - const blocksToRemove = draggedBlocks.filter((draggedBlock) => - editor.getBlock(draggedBlock.id), - ); - if (blocksToRemove.length > 0) { - editor.removeBlocks(blocksToRemove); - } + editor.transact(() => { + const blocksToRemove = draggedBlocks.filter((draggedBlock) => + editor.getBlock(draggedBlock.id), + ); + if (blocksToRemove.length > 0) { + editor.removeBlocks(blocksToRemove); + } - editor.replaceBlocks( - [block], - [ - { - type: "columnList", - children: columns.map((children) => { - return { - type: "column", - children, - }; - }), - }, - ], - ); + editor.replaceBlocks( + [block], + [ + { + type: "columnList", + children: columns.map((children) => { + return { + type: "column", + children, + }; + }), + }, + ], + ); + }); } return true; // Prevent default ProseMirror drop behavior diff --git a/packages/xl-multi-column/src/test/extensions/columnDrop.test.ts b/packages/xl-multi-column/src/test/extensions/columnDrop.test.ts index 94c696f512..c8b321c2ab 100644 --- a/packages/xl-multi-column/src/test/extensions/columnDrop.test.ts +++ b/packages/xl-multi-column/src/test/extensions/columnDrop.test.ts @@ -91,6 +91,33 @@ describe("column edge drops", () => { }); }); +// The drop removes the dragged blocks and then creates or updates the column +// list. When the two edits aren't adjacent, they used to land in separate +// undo steps, so a single undo brought back the target but not the dragged +// blocks. +describe("undoing an edge drop", () => { + it.each([ + ["creates a column list", ["paragraph-2"], "paragraph-1"], + ["adds a column", ["paragraph-0"], "column-1"], + ])("restores the document in one step when the drop %s", (_, ids, target) => { + const editor = getEditor(); + const before = editor.document; + // Start a new undo group, so that the drop isn't merged with the + // `beforeEach` setup. + const now = Date.now(); + const dateNow = vi.spyOn(Date, "now").mockReturnValue(now + 1000); + try { + expect(dropOnEdge(ids, target, "left")).toBe(true); + } finally { + dateNow.mockRestore(); + } + expect(editor.document).not.toEqual(before); + + editor.undo(); + expect(editor.document).toEqual(before); + }); +}); + // Dropping blocks onto themselves does nothing, like dropping a block next to // itself. Removing the dragged blocks from where they are used to remove the // drop target with them, and the handler then threw when it updated the