From df7d3a7883100a66c8db352aa615003706b5df98 Mon Sep 17 00:00:00 2001 From: narpat-ps Date: Sat, 6 Sep 2025 18:50:08 +0530 Subject: [PATCH] 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 --- src/components/modules/api/blocks.ts | 4 ++-- src/components/modules/blockManager.ts | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/components/modules/api/blocks.ts b/src/components/modules/api/blocks.ts index f9297d5d..9ad22176 100644 --- a/src/components/modules/api/blocks.ts +++ b/src/components/modules/api/blocks.ts @@ -224,8 +224,8 @@ export default class BlocksAPI extends Module { * @param {string} data - HTML string to render * @returns {Promise} */ - public renderFromHTML(data: string): Promise { - this.Editor.BlockManager.clear(); + public async renderFromHTML(data: string): Promise { + await this.Editor.BlockManager.clear(); return this.Editor.Paste.processText(data, true); } diff --git a/src/components/modules/blockManager.ts b/src/components/modules/blockManager.ts index 48fec049..be8e1e24 100644 --- a/src/components/modules/blockManager.ts +++ b/src/components/modules/blockManager.ts @@ -533,8 +533,8 @@ export default class BlockManager extends Module { throw new Error('Can\'t find a Block to remove'); } - block.destroy(); this._blocks.remove(index); + block.destroy(); /** * Force call of didMutated event on Block removal @@ -894,7 +894,10 @@ export default class BlockManager extends Module { public async clear(needToAddDefaultBlock = false): Promise { const queue = new PromiseQueue(); - this.blocks.forEach((block) => { + // Create a copy of the blocks array to avoid issues with array modification during iteration + const blocksToRemove = [...this.blocks]; + + blocksToRemove.forEach((block) => { queue.add(async () => { await this.removeBlock(block, false); });