Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools destroy called when the editor is destroyed #1264

Merged
2 changes: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- `Improvements` - The `initialBlock` property of Editor config is deprecated. Use the `defaultBlock` instead. [#993](https://github.com/codex-team/editor.js/issues/993)
- `Fix` - Fixed the issue of toolbar not disappearing on entering input in Chinese, Hindi and some other languages. [#1196](https://github.com/codex-team/editor.js/issues/1196)
- `Improvements` - BlockAPI `call()` method now returns the result of calling method, thus allowing it to expose arbitrary data as needed [#1205](https://github.com/codex-team/editor.js/pull/1205)
- `Fix` - Tool's `destroy` method is not invoked when `editor.destroy()` is called. [#1047](https://github.com/codex-team/editor.js/issues/1047)
- `Improvements` - Tool's `reset` static method added to the API to clean up any data added by Tool on initialization
- `Improvements` - Unuseful log about missed i18n section has been removed [#1269](https://github.com/codex-team/editor.js/issues/1269)

### 2.18
Expand Down
30 changes: 30 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ There are few options available by Editor.js.
| `inlineToolbar` | _Boolean/Array_ | `false` | Pass `true` to enable the Inline Toolbar with all Tools, or pass an array with specified Tools list |
| `config` | _Object_ | `null` | User's configuration for Plugin.

## Tool prepare and reset

If you need to prepare some data for Tool (eg. load external script, create HTML nodes in the document, etc) you can use static prepare method.

It accepts tools config passed on Editor's initialization as an argument:

```javascript
class Tool {
static prepare(config) {
loadScript();
insertNodes();
...
}
}
```

On Editor destroy you can use an opposite method `reset` to clean up all prepared data:

```javascript
class Tool {
static reset() {
cleanUpScripts();
deleteNodes();
...
}
}
```

Both methods might be async.

## Paste handling

Editor.js handles paste on Blocks and provides API for Tools to process the pasted data.
Expand Down
2 changes: 1 addition & 1 deletion example/tools/image
Submodule image updated 5 files
+18 −0 README.md
+2 −2 dist/bundle.js
+1 −1 package.json
+2 −0 src/index.js
+15 −5 src/tunes.js
12 changes: 12 additions & 0 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,18 @@ export default class BlockManager extends Module {
this.Editor.UI.checkEmptiness();
}

/**
* Cleans up all the block tools' resources
* This is called when editor is destroyed
*/
public async destroy(): Promise<void> {
await Promise.all(this.blocks.map((block) => {
if (_.isFunction(block.tool.destroy)) {
return block.tool.destroy();
}
}));
}

/**
* Bind Events
*
Expand Down
11 changes: 11 additions & 0 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ export default class Tools extends Module {
};
}

/**
* Calls each Tool reset method to clean up anything set by Tool
*/
public destroy(): void {
Object.values(this.available).forEach(async tool => {
if (_.isFunction(tool.reset)) {
await tool.reset();
}
});
}

/**
* Binds prepare function of plugins with user or default config
*
Expand Down
2 changes: 1 addition & 1 deletion src/components/tools/paragraph
Submodule paragraph updated 1 files
+1 −1 README.md
6 changes: 6 additions & 0 deletions types/tools/block-tool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export interface BlockTool extends BaseTool {
*/
onPaste?(event: PasteEvent): void;

/**
* Cleanup resources used by your tool here
* Called when the editor is destroyed
*/
destroy?(): void;

/**
* Lifecycle hooks
*/
Expand Down
5 changes: 5 additions & 0 deletions types/tools/tool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ export interface BaseToolConstructable {
* @param data
*/
prepare?(data: {toolName: string, config: ToolConfig}): void | Promise<void>;

/**
* Tool`s reset method to clean up anything set by prepare. Can be async
*/
reset?(): void | Promise<void>;
}