From 23f411bc698d12bf6a448eeb95a41eae953bcc76 Mon Sep 17 00:00:00 2001 From: narpat-ps Date: Sun, 15 Jun 2025 17:50:22 +0000 Subject: [PATCH 1/2] fix(blocks):Error occurred when calling renderFromHTML: Can't find a Block to remove. --- src/components/blocks.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/blocks.ts b/src/components/blocks.ts index 30334ae20..10702726a 100644 --- a/src/components/blocks.ts +++ b/src/components/blocks.ts @@ -287,9 +287,8 @@ export default class Blocks { index = this.length - 1; } - this.blocks[index].holder.remove(); - this.blocks[index].call(BlockToolAPI.REMOVED); + this.blocks[index].holder.remove(); this.blocks.splice(index, 1); } From 69ad9f327aad62f708380318457a3a20b4eb3ef4 Mon Sep 17 00:00:00 2001 From: narpat-ps Date: Sun, 15 Jun 2025 18:15:21 +0000 Subject: [PATCH 2/2] 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/blocks.ts | 3 ++- src/components/modules/api/blocks.ts | 4 ++-- src/components/modules/blockManager.ts | 7 +++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/components/blocks.ts b/src/components/blocks.ts index 10702726a..30334ae20 100644 --- a/src/components/blocks.ts +++ b/src/components/blocks.ts @@ -287,9 +287,10 @@ export default class Blocks { index = this.length - 1; } - this.blocks[index].call(BlockToolAPI.REMOVED); this.blocks[index].holder.remove(); + this.blocks[index].call(BlockToolAPI.REMOVED); + this.blocks.splice(index, 1); } diff --git a/src/components/modules/api/blocks.ts b/src/components/modules/api/blocks.ts index f9297d5d4..9ad22176a 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 48fec0499..be8e1e247 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); });