From f96697e60867c8cf0eddf1bd6e954dcd1a832b7e Mon Sep 17 00:00:00 2001 From: Robert Molina Date: Fri, 8 Aug 2025 23:05:59 -0400 Subject: [PATCH 1/2] fix(nodeUtil): moved blocks now included in the editor changes --- packages/core/src/api/nodeUtil.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/core/src/api/nodeUtil.ts b/packages/core/src/api/nodeUtil.ts index f0d8ce54e7..22cbd4e417 100644 --- a/packages/core/src/api/nodeUtil.ts +++ b/packages/core/src/api/nodeUtil.ts @@ -184,6 +184,7 @@ function collectAllBlocks< { block: Block; parentId: string | undefined; + index: number; } > { const blocks: Record< @@ -191,15 +192,17 @@ function collectAllBlocks< { block: Block; parentId: string | undefined; + index: number; } > = {}; const pmSchema = getPmSchema(doc); - doc.descendants((node, pos) => { + doc.descendants((node, pos, _, index) => { if (isNodeBlock(node)) { const parentId = getParentBlockId(doc, pos); blocks[node.attrs.id] = { block: nodeToBlock(node, pmSchema), parentId, + index, }; } return true; @@ -264,8 +267,9 @@ export function getBlocksChangedByTransaction< const prev = prevBlocks[id]; const next = nextBlocks[id]; const isParentDifferent = prev.parentId !== next.parentId; + const isIndexDifferent = prev.index !== next.index; - if (isParentDifferent) { + if (isParentDifferent || isIndexDifferent) { changes.push({ type: "move", block: next.block, From 80ce2a55b3346ca555d2537ea53d9967f66b339d Mon Sep 17 00:00:00 2001 From: Robert Molina Date: Sun, 10 Aug 2025 00:58:12 -0400 Subject: [PATCH 2/2] fix(nodeUtil): only firing event for dragged block --- packages/core/src/api/nodeUtil.ts | 6 ++++-- .../src/extensions/SideMenu/SideMenuPlugin.ts | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/core/src/api/nodeUtil.ts b/packages/core/src/api/nodeUtil.ts index 22cbd4e417..2488c9175c 100644 --- a/packages/core/src/api/nodeUtil.ts +++ b/packages/core/src/api/nodeUtil.ts @@ -260,6 +260,8 @@ export function getBlocksChangedByTransaction< }); }); + const draggedBlockId = transaction.getMeta("draggedBlockId"); + // Handle updated, moved, indented, outdented blocks Object.keys(nextBlocks) .filter((id) => id in prevBlocks) @@ -267,9 +269,9 @@ export function getBlocksChangedByTransaction< const prev = prevBlocks[id]; const next = nextBlocks[id]; const isParentDifferent = prev.parentId !== next.parentId; - const isIndexDifferent = prev.index !== next.index; + const isDraggedBlock = prev.index !== next.index && draggedBlockId === id; - if (isParentDifferent || isIndexDifferent) { + if (isParentDifferent || isDraggedBlock) { changes.push({ type: "move", block: next.block, diff --git a/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts b/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts index f99e36c380..e51a8aad65 100644 --- a/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts +++ b/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts @@ -20,6 +20,7 @@ import { import { initializeESMDependencies } from "../../util/esmDependencies.js"; import { getDraggableBlockFromElement } from "../getDraggableBlockFromElement.js"; import { dragStart, unsetDragImage } from "./dragging.js"; +import { nodeToBlock } from "../../api/nodeConversions/nodeToBlock.js"; export type SideMenuState< BSchema extends BlockSchema, @@ -695,6 +696,8 @@ export class SideMenuProsemirrorPlugin< public view: SideMenuView | undefined; + private draggedBlockId: string | undefined; + constructor(private readonly editor: BlockNoteEditor) { super(); this.addProsemirrorPlugin( @@ -706,6 +709,23 @@ export class SideMenuProsemirrorPlugin< }); return this.view; }, + props: { + handleDrop: (_view, _event, slice) => { + const draggedBlock = nodeToBlock( + slice.content.child(0), + editor.pmSchema, + ); + this.draggedBlockId = draggedBlock.id; + return false; + }, + }, + filterTransaction: (tr) => { + if (tr.getMeta("uiEvent") === "drop" && this.draggedBlockId) { + tr.setMeta("draggedBlockId", this.draggedBlockId); + this.draggedBlockId = undefined; + } + return true; + }, }), ); }