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
This commit is contained in:
narpat-ps 2025-09-06 18:50:08 +05:30 committed by GitHub
commit df7d3a7883
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 4 deletions

View file

@ -224,8 +224,8 @@ export default class BlocksAPI extends Module {
* @param {string} data - HTML string to render
* @returns {Promise<void>}
*/
public renderFromHTML(data: string): Promise<void> {
this.Editor.BlockManager.clear();
public async renderFromHTML(data: string): Promise<void> {
await this.Editor.BlockManager.clear();
return this.Editor.Paste.processText(data, true);
}

View file

@ -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<void> {
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);
});