Implement a custom Editor.js Tool class
nextA Tool is a class that provides a custom Block type (e.g., Text, Image, Header). To create a tool, you must implement a class structure that includes a constructor and a render method.
Required Methods
constructor({data, config, api, block}): Initializes the tool instance.data: Data to be rendered.config: Special configuration parameters.api: Editor.js API methods.block: Block's API methods.
render(): Returns anHTMLElementthat will be placed into the Editor.save(): Processes the tool's element in the DOM and returns the block's data.
Optional Methods
validate(data: BlockToolData): boolean | Promise<boolean>: Validates the tool's data before saving. If it returnsfalse, the data won't be saved.merge(data): Specifies how to merge two blocks of the same type (e.g., onBackspace).
class MyCustomTool {
constructor({data, config, api}) {
this.data = data;
this.api = api;
this.config = config;
}
render() {
const div = document.createElement('DIV');
div.innerHTML = 'Hello World';
return div;
}
save() {
return { text: 'Hello World' };
}
}