Skip to content

Commit df7d3a7

Browse files
authored
resolve "Can't find a Block to remove" error in renderFromHTML (#2941)
* fix(blocks):Error occurred when calling renderFromHTML: Can't find a Block to remove. * fix: resolve "Can't find a Block to remove" error in renderFromHTML - Make renderFromHTML async and await BlockManager.clear() to prevent race condition - Change removeBlock order: remove from array before destroy to prevent index invalidation - Fix clear() method to copy blocks array before iteration to avoid modification during loop Fixes issue where renderFromHTML would fail with "Can't find a Block to remove" error due to concurrent block removal operations and array modification during iteration. Resolves #2518
1 parent 628f218 commit df7d3a7

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/components/modules/api/blocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ export default class BlocksAPI extends Module {
224224
* @param {string} data - HTML string to render
225225
* @returns {Promise<void>}
226226
*/
227-
public renderFromHTML(data: string): Promise<void> {
228-
this.Editor.BlockManager.clear();
227+
public async renderFromHTML(data: string): Promise<void> {
228+
await this.Editor.BlockManager.clear();
229229

230230
return this.Editor.Paste.processText(data, true);
231231
}

src/components/modules/blockManager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ export default class BlockManager extends Module {
533533
throw new Error('Can\'t find a Block to remove');
534534
}
535535

536-
block.destroy();
537536
this._blocks.remove(index);
537+
block.destroy();
538538

539539
/**
540540
* Force call of didMutated event on Block removal
@@ -894,7 +894,10 @@ export default class BlockManager extends Module {
894894
public async clear(needToAddDefaultBlock = false): Promise<void> {
895895
const queue = new PromiseQueue();
896896

897-
this.blocks.forEach((block) => {
897+
// Create a copy of the blocks array to avoid issues with array modification during iteration
898+
const blocksToRemove = [...this.blocks];
899+
900+
blocksToRemove.forEach((block) => {
898901
queue.add(async () => {
899902
await this.removeBlock(block, false);
900903
});

0 commit comments

Comments
 (0)