Skip to content

Commit 37bb63a

Browse files
fix: parameters and foreachblock (#153)
* Fixed some params to be `BlockIdentifier` instead of `Block` and changed `forEachBlock` callback to return a boolean * change forEachBlock --------- Co-authored-by: yousefed <[email protected]>
1 parent 6925ee1 commit 37bb63a

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

packages/core/src/BlockNoteEditor.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,34 @@ export class BlockNoteEditor {
169169
* @param reverse Whether the blocks should be traversed in reverse order.
170170
*/
171171
public forEachBlock(
172-
callback: (block: Block) => void,
172+
callback: (block: Block) => boolean,
173173
reverse: boolean = false
174174
): 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;
180185
}
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;
185193
}
186194
}
195+
196+
return true;
187197
}
188198

189-
helper(this.topLevelBlocks);
199+
traverseBlockArray(blocks);
190200
}
191201

192202
/**
@@ -283,15 +293,15 @@ export class BlockNoteEditor {
283293
* @param blockToUpdate The block that should be updated.
284294
* @param update A partial block which defines how the existing block should be changed.
285295
*/
286-
public updateBlock(blockToUpdate: Block, update: PartialBlock) {
296+
public updateBlock(blockToUpdate: BlockIdentifier, update: PartialBlock) {
287297
updateBlock(blockToUpdate, update, this._tiptapEditor);
288298
}
289299

290300
/**
291301
* Removes existing blocks from the editor. Throws an error if any of the blocks could not be found.
292302
* @param blocksToRemove An array of identifiers for existing blocks that should be removed.
293303
*/
294-
public removeBlocks(blocksToRemove: Block[]) {
304+
public removeBlocks(blocksToRemove: BlockIdentifier[]) {
295305
removeBlocks(blocksToRemove, this._tiptapEditor);
296306
}
297307

@@ -303,7 +313,7 @@ export class BlockNoteEditor {
303313
* @param blocksToInsert An array of partial blocks to replace the old ones with.
304314
*/
305315
public replaceBlocks(
306-
blocksToRemove: Block[],
316+
blocksToRemove: BlockIdentifier[],
307317
blocksToInsert: PartialBlock[]
308318
) {
309319
replaceBlocks(blocksToRemove, blocksToInsert, this._tiptapEditor);

0 commit comments

Comments
 (0)