1- import { PartialBlock } from "../../../blocks/defaultBlocks.js" ;
1+ import { Block , PartialBlock } from "../../../blocks/defaultBlocks.js" ;
22import type { BlockNoteEditor } from "../../../editor/BlockNoteEditor" ;
33import {
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" ;
1010import { acceptedMIMETypes } from "./acceptedMIMETypes.js" ;
1111
1212function 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+
4471export 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