Skip to content

Commit c1f8ac2

Browse files
feat: File insertion QoL (#1459)
* Made file insertion update the target block if it's empty instead of inserting new one * Cleaned up code
1 parent 33884be commit c1f8ac2

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

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

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { PartialBlock } from "../../../blocks/defaultBlocks.js";
1+
import { Block, PartialBlock } from "../../../blocks/defaultBlocks.js";
22
import type { BlockNoteEditor } from "../../../editor/BlockNoteEditor";
33
import {
44
BlockSchema,
55
FileBlockConfig,
66
InlineContentSchema,
77
StyleSchema,
88
} from "../../../schema/index.js";
9-
import { getBlockInfo, getNearestBlockPos } from "../../getBlockInfoFromPos.js";
9+
import { getNearestBlockPos } from "../../getBlockInfoFromPos.js";
1010
import { acceptedMIMETypes } from "./acceptedMIMETypes.js";
1111

1212
function checkFileExtensionsMatch(
@@ -41,6 +41,33 @@ function checkMIMETypesMatch(mimeType1: string, mimeType2: string) {
4141
return types1[0] === types2[0] && types1[1] === types2[1];
4242
}
4343

44+
function insertOrUpdateBlock<
45+
BSchema extends BlockSchema,
46+
I extends InlineContentSchema,
47+
S extends StyleSchema
48+
>(
49+
editor: BlockNoteEditor<BSchema, I, S>,
50+
referenceBlock: Block<BSchema, I, S>,
51+
newBlock: PartialBlock<BSchema, I, S>
52+
) {
53+
let insertedBlockId: string | undefined;
54+
55+
if (
56+
Array.isArray(referenceBlock.content) &&
57+
referenceBlock.content.length === 0
58+
) {
59+
insertedBlockId = editor.updateBlock(referenceBlock, newBlock).id;
60+
} else {
61+
insertedBlockId = editor.insertBlocks(
62+
[newBlock],
63+
referenceBlock,
64+
"after"
65+
)[0].id;
66+
}
67+
68+
return insertedBlockId;
69+
}
70+
4471
export async function handleFileInsertion<
4572
BSchema extends BlockSchema,
4673
I extends InlineContentSchema,
@@ -120,11 +147,8 @@ export async function handleFileInsertion<
120147
let insertedBlockId: string | undefined = undefined;
121148

122149
if (event.type === "paste") {
123-
insertedBlockId = editor.insertBlocks(
124-
[fileBlock],
125-
editor.getTextCursorPosition().block,
126-
"after"
127-
)[0].id;
150+
const currentBlock = editor.getTextCursorPosition().block;
151+
insertedBlockId = insertOrUpdateBlock(editor, currentBlock, fileBlock);
128152
} else if (event.type === "drop") {
129153
const coords = {
130154
left: (event as DragEvent).clientX,
@@ -141,13 +165,11 @@ export async function handleFileInsertion<
141165
pos.pos
142166
);
143167

144-
const blockInfo = getBlockInfo(posInfo);
145-
146-
insertedBlockId = editor.insertBlocks(
147-
[fileBlock],
148-
blockInfo.bnBlock.node.attrs.id,
149-
"after"
150-
)[0].id;
168+
insertedBlockId = insertOrUpdateBlock(
169+
editor,
170+
editor.getBlock(posInfo.node.attrs.id)!,
171+
fileBlock
172+
);
151173
} else {
152174
return;
153175
}

0 commit comments

Comments
 (0)