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
20 changes: 18 additions & 2 deletions packages/ui/src/Blocks/Blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export class BlocksUI implements EditorjsPlugin {
*/
#api: EditorAPI;

/**
* Controls the lifetime of the DOM listeners attached to the blocks holder
* so they can all be removed at once on destroy()
*/
#listenersController = new AbortController();

/**
* EditorUI constructor method
* @param params - Plugin parameters
Expand Down Expand Up @@ -113,7 +119,7 @@ export class BlocksUI implements EditorjsPlugin {
targetRanges: e.getTargetRanges(),
isCrossInputSelection,
}));
});
}, { signal: this.#listenersController.signal });

blocksHolder.addEventListener('keydown', (e) => {
if (e.code !== 'KeyZ') {
Expand All @@ -135,7 +141,7 @@ export class BlocksUI implements EditorjsPlugin {
this.#api.document.undo();

e.preventDefault();
});
}, { signal: this.#listenersController.signal });

blocksHolder.addEventListener('copy', (e) => {
const payload: CopyUIEventPayload = {
Expand Down Expand Up @@ -230,5 +236,15 @@ export class BlocksUI implements EditorjsPlugin {
public destroy(): void {
this.#blocks.forEach(block => block.remove());
this.#blocks = [];

/**
* Removes the beforeinput/keydown listeners attached to the blocks holder
*/
this.#listenersController.abort();

/**
* Detach the blocks holder itself so it doesn't linger in the DOM
*/
this.#blocksHolder.remove();
}
}
2 changes: 1 addition & 1 deletion packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class EditorjsUI implements EditorjsPlugin {
* Method to destroy the plugin
*/
public destroy(): void {
// Cleanup if needed
this.#editorWrapper.remove();
}

/**
Expand Down