Please note: The functionality this fork introduced is now present in the original project - please use the original editor.js.
This is a fork of the Editor.js project from the codex-team (https://editorjs.io) which contains the ability to assign a custom metadata object to the blocks within the editor.
Note: This code may not be actively maintained but is available for anyone who has these requirements.
Editor.js Version: 2.17
The project creates an additional metadata item assigned to all blocks within the editor, to which a unique block ID is assigned along with a blank string called data.
You can modify the metadata object by editing the file src/types-internal/block-data and changing the below to return the format you require;
export interface MetaDataBlock {
id?: string;
data?: string;
}
The code that creates the initial metadata object is located in file /mixin.ts and should be modified to meet your needs, the code below simply creates a unique ID for the block within the page and a blank data string.
import {MetaDataBlock} from './types-internal/block-data';
let counter = 0;
export default {
createMeta() {
let id = 'block-' + counter;
console.log ( 'Creating Meta: ' + id );
const data = '';
const existId = () => {
if ( document.getElementById(id)) {
return true; } else { return false; }
};
do {
counter++;
id = 'block-' + counter;
} while (existId());
return {id, data: ''};
},
};
If the code meets your requirements you can use it directly by saving the file dist/editor.js in place of the file provided by the official project. Everything else remains the same as the official project including plugin compatibility.
Thanks to @nmannitz for their original modification adding an ID to the block.