Skip to content

Commit 69ad9f3

Browse files
committed
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 23f411b commit 69ad9f3

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/components/blocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,10 @@ export default class Blocks {
287287
index = this.length - 1;
288288
}
289289

290-
this.blocks[index].call(BlockToolAPI.REMOVED);
291290
this.blocks[index].holder.remove();
292291

292+
this.blocks[index].call(BlockToolAPI.REMOVED);
293+
293294
this.blocks.splice(index, 1);
294295
}
295296

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)