Skip to content

Commit ba2223a

Browse files
narpat-psVolgaIgor
authored andcommitted
resolve "Can't find a Block to remove" error in renderFromHTML (codex-team#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 codex-team#2518
1 parent aac8469 commit ba2223a

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
@@ -535,8 +535,8 @@ export default class BlockManager extends Module {
535535
throw new Error('Can\'t find a Block to remove');
536536
}
537537

538-
block.destroy();
539538
this._blocks.remove(index);
539+
block.destroy();
540540

541541
/**
542542
* Force call of didMutated event on Block removal
@@ -898,7 +898,10 @@ export default class BlockManager extends Module {
898898
public async clear(needToAddDefaultBlock = false): Promise<void> {
899899
const queue = new PromiseQueue();
900900

901-
this.blocks.forEach((block) => {
901+
// Create a copy of the blocks array to avoid issues with array modification during iteration
902+
const blocksToRemove = [...this.blocks];
903+
904+
blocksToRemove.forEach((block) => {
902905
queue.add(async () => {
903906
await this.removeBlock(block, false);
904907
});

0 commit comments

Comments
 (0)