Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/components/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default class Blocks {
if (replace) {
this.blocks[index].holder.remove();
this.blocks[index].call(BlockToolAPI.REMOVED);
this.blocks[index].destroy();
}

const deleteCount = replace ? 1 : 0;
Expand Down Expand Up @@ -237,6 +238,8 @@ export default class Blocks {
prevBlock.holder.replaceWith(block.holder);

this.blocks[index] = block;

prevBlock.destroy();
}

/**
Expand Down Expand Up @@ -291,6 +294,8 @@ export default class Blocks {

this.blocks[index].call(BlockToolAPI.REMOVED);

this.blocks[index].destroy();

this.blocks.splice(index, 1);
}

Expand All @@ -300,7 +305,10 @@ export default class Blocks {
public removeAll(): void {
this.workingArea.innerHTML = '';

this.blocks.forEach((block) => block.call(BlockToolAPI.REMOVED));
this.blocks.forEach((block) => {
block.call(BlockToolAPI.REMOVED);
block.destroy();
});

this.blocks.length = 0;
}
Expand Down
1 change: 0 additions & 1 deletion src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ export default class BlockManager extends Module {
}

this._blocks.remove(index);
block.destroy();

/**
* Force call of didMutated event on Block removal
Expand Down
49 changes: 49 additions & 0 deletions test/cypress/tests/api/blocks.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,55 @@ describe('api.blocks', () => {
/**
* api.blocks.insert(type, data, config, index, needToFocus, replace, id)
*/
/**
* api.blocks.update() swaps the Block in the collection, so the Tool of the
* replaced Block has to be destroyed — otherwise it leaks.
*/
describe('.update() cleanup', () => {
it('should destroy the Tool of the Block it replaces', () => {
const onDestroy = cy.spy().as('onDestroy');

/**
* Mock of Tool that reports its destruction
*/
class DestroyableTool extends ToolMock {
/**
* Called by the editor when the Block leaves the collection
*/
public destroy(): void {
onDestroy();
}
}

const existingBlock = {
id: 'destroyable-id-1',
type: 'destroyableTool',
data: {
text: 'Some text',
},
};

cy.createEditor({
tools: {
destroyableTool: {
class: DestroyableTool,
},
},
data: {
blocks: [
existingBlock,
],
},
}).then((editor) => {
editor.blocks.update(existingBlock.id, { text: 'Updated text' });

cy.wait(100).then(() => {
cy.get('@onDestroy').should('have.been.calledOnce');
});
});
});
});

describe('.insert()', function () {
it('should preserve block id if it is passed', function () {
cy.createEditor({
Expand Down