From a4b539234f706ed3b587c4c4a2e750efd20884dd Mon Sep 17 00:00:00 2001 From: Nick the Sick Date: Fri, 18 Jul 2025 15:06:38 +0200 Subject: [PATCH] fix: insert file upload before block if it is closer to the top of the block --- .../clipboard/fromClipboard/handleFileInsertion.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/clipboard/fromClipboard/handleFileInsertion.ts b/packages/core/src/api/clipboard/fromClipboard/handleFileInsertion.ts index 588763e9a9..d164dcb762 100644 --- a/packages/core/src/api/clipboard/fromClipboard/handleFileInsertion.ts +++ b/packages/core/src/api/clipboard/fromClipboard/handleFileInsertion.ts @@ -49,6 +49,7 @@ function insertOrUpdateBlock< editor: BlockNoteEditor, referenceBlock: Block, newBlock: PartialBlock, + placement: "before" | "after" = "after", ) { let insertedBlockId: string | undefined; @@ -61,7 +62,7 @@ function insertOrUpdateBlock< insertedBlockId = editor.insertBlocks( [newBlock], referenceBlock, - "after", + placement, )[0].id; } @@ -156,16 +157,26 @@ export async function handleFileInsertion< }; const pos = editor.prosemirrorView?.posAtCoords(coords); + if (!pos) { return; } insertedBlockId = editor.transact((tr) => { const posInfo = getNearestBlockPos(tr.doc, pos.pos); + const blockElement = editor.prosemirrorView?.dom.querySelector( + `[data-id="${posInfo.node.attrs.id}"]`, + ); + + const blockRect = blockElement?.getBoundingClientRect(); + return insertOrUpdateBlock( editor, editor.getBlock(posInfo.node.attrs.id)!, fileBlock, + blockRect && (blockRect.top + blockRect.bottom) / 2 > coords.top + ? "before" + : "after", ); }); } else {