1
- import { PartialBlock } from "../../../blocks/defaultBlocks.js" ;
1
+ import { Block , PartialBlock } from "../../../blocks/defaultBlocks.js" ;
2
2
import type { BlockNoteEditor } from "../../../editor/BlockNoteEditor" ;
3
3
import {
4
4
BlockSchema ,
5
5
FileBlockConfig ,
6
6
InlineContentSchema ,
7
7
StyleSchema ,
8
8
} from "../../../schema/index.js" ;
9
- import { getBlockInfo , getNearestBlockPos } from "../../getBlockInfoFromPos.js" ;
9
+ import { getNearestBlockPos } from "../../getBlockInfoFromPos.js" ;
10
10
import { acceptedMIMETypes } from "./acceptedMIMETypes.js" ;
11
11
12
12
function checkFileExtensionsMatch (
@@ -41,6 +41,33 @@ function checkMIMETypesMatch(mimeType1: string, mimeType2: string) {
41
41
return types1 [ 0 ] === types2 [ 0 ] && types1 [ 1 ] === types2 [ 1 ] ;
42
42
}
43
43
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
+
44
71
export async function handleFileInsertion <
45
72
BSchema extends BlockSchema ,
46
73
I extends InlineContentSchema ,
@@ -120,11 +147,8 @@ export async function handleFileInsertion<
120
147
let insertedBlockId : string | undefined = undefined ;
121
148
122
149
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 ) ;
128
152
} else if ( event . type === "drop" ) {
129
153
const coords = {
130
154
left : ( event as DragEvent ) . clientX ,
@@ -141,13 +165,11 @@ export async function handleFileInsertion<
141
165
pos . pos
142
166
) ;
143
167
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
+ ) ;
151
173
} else {
152
174
return ;
153
175
}
0 commit comments