Skip to content

Commit 661d050

Browse files
authored
fix: insert file upload before block if it is closer to the top of the block (#1857)
1 parent 5ca0572 commit 661d050

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

packages/core/src/api/clipboard/fromClipboard/handleFileInsertion.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function insertOrUpdateBlock<
4949
editor: BlockNoteEditor<BSchema, I, S>,
5050
referenceBlock: Block<BSchema, I, S>,
5151
newBlock: PartialBlock<BSchema, I, S>,
52+
placement: "before" | "after" = "after",
5253
) {
5354
let insertedBlockId: string | undefined;
5455

@@ -61,7 +62,7 @@ function insertOrUpdateBlock<
6162
insertedBlockId = editor.insertBlocks(
6263
[newBlock],
6364
referenceBlock,
64-
"after",
65+
placement,
6566
)[0].id;
6667
}
6768

@@ -156,16 +157,26 @@ export async function handleFileInsertion<
156157
};
157158

158159
const pos = editor.prosemirrorView?.posAtCoords(coords);
160+
159161
if (!pos) {
160162
return;
161163
}
162164

163165
insertedBlockId = editor.transact((tr) => {
164166
const posInfo = getNearestBlockPos(tr.doc, pos.pos);
167+
const blockElement = editor.prosemirrorView?.dom.querySelector(
168+
`[data-id="${posInfo.node.attrs.id}"]`,
169+
);
170+
171+
const blockRect = blockElement?.getBoundingClientRect();
172+
165173
return insertOrUpdateBlock(
166174
editor,
167175
editor.getBlock(posInfo.node.attrs.id)!,
168176
fileBlock,
177+
blockRect && (blockRect.top + blockRect.bottom) / 2 > coords.top
178+
? "before"
179+
: "after",
169180
);
170181
});
171182
} else {

0 commit comments

Comments
 (0)