javascripteditorjs

How to count the number of characters when using editorJs


I am using EditorJs(block styled text editor) to create a note taking app im trying to count the number of characters for every block i tried adding some javascirpt code in the onChange method of the editorJs but didnt work as i expected here is the current code of editorJs configuration:

const editor = new EditorJS({
            holder: "editorjs",
            logLevel: "ERROR",
            data: editorData,
            onReady: () => {
                ejInstance.current = editor;
            },
            onChange: async (api, event) => {
                console.log(event);
                let content = await editor.save();
                setEditorData(content);
                let blockIndex = api.blocks.getCurrentBlockIndex();
                let currentBlock = content.blocks.at(blockIndex - 1).data;
                setCount(currentBlock.text.length);
            },
            autofocus: true,
            tools: { marker: Marker, unerline: Underline },
        });

Solution

  • This was added to the editorJs by the developers you can check this blogpost to see how you can implement this in your project https://codex.so/editorjs-max-length