@@ -169,24 +169,34 @@ export class BlockNoteEditor {
169
169
* @param reverse Whether the blocks should be traversed in reverse order.
170
170
*/
171
171
public forEachBlock (
172
- callback : ( block : Block ) => void ,
172
+ callback : ( block : Block ) => boolean ,
173
173
reverse : boolean = false
174
174
) : void {
175
- function helper ( blocks : Block [ ] ) {
176
- if ( reverse ) {
177
- for ( const block of blocks . reverse ( ) ) {
178
- helper ( block . children ) ;
179
- callback ( block ) ;
175
+ const blocks = this . topLevelBlocks . slice ( ) ;
176
+
177
+ if ( reverse ) {
178
+ blocks . reverse ( ) ;
179
+ }
180
+
181
+ function traverseBlockArray ( blockArray : Block [ ] ) : boolean {
182
+ for ( const block of blockArray ) {
183
+ if ( callback ( block ) === false ) {
184
+ return false ;
180
185
}
181
- } else {
182
- for ( const block of blocks ) {
183
- callback ( block ) ;
184
- helper ( block . children ) ;
186
+
187
+ const children = reverse
188
+ ? block . children . slice ( ) . reverse ( )
189
+ : block . children ;
190
+
191
+ if ( traverseBlockArray ( children ) === false ) {
192
+ return false ;
185
193
}
186
194
}
195
+
196
+ return true ;
187
197
}
188
198
189
- helper ( this . topLevelBlocks ) ;
199
+ traverseBlockArray ( blocks ) ;
190
200
}
191
201
192
202
/**
@@ -283,15 +293,15 @@ export class BlockNoteEditor {
283
293
* @param blockToUpdate The block that should be updated.
284
294
* @param update A partial block which defines how the existing block should be changed.
285
295
*/
286
- public updateBlock ( blockToUpdate : Block , update : PartialBlock ) {
296
+ public updateBlock ( blockToUpdate : BlockIdentifier , update : PartialBlock ) {
287
297
updateBlock ( blockToUpdate , update , this . _tiptapEditor ) ;
288
298
}
289
299
290
300
/**
291
301
* Removes existing blocks from the editor. Throws an error if any of the blocks could not be found.
292
302
* @param blocksToRemove An array of identifiers for existing blocks that should be removed.
293
303
*/
294
- public removeBlocks ( blocksToRemove : Block [ ] ) {
304
+ public removeBlocks ( blocksToRemove : BlockIdentifier [ ] ) {
295
305
removeBlocks ( blocksToRemove , this . _tiptapEditor ) ;
296
306
}
297
307
@@ -303,7 +313,7 @@ export class BlockNoteEditor {
303
313
* @param blocksToInsert An array of partial blocks to replace the old ones with.
304
314
*/
305
315
public replaceBlocks (
306
- blocksToRemove : Block [ ] ,
316
+ blocksToRemove : BlockIdentifier [ ] ,
307
317
blocksToInsert : PartialBlock [ ]
308
318
) {
309
319
replaceBlocks ( blocksToRemove , blocksToInsert , this . _tiptapEditor ) ;
0 commit comments