editor.js
editor.js copied to clipboard
[Bug] editor.render with "blocks":[] breaks the editor
(Not sure if exact duplicate of https://github.com/codex-team/editor.js/pull/1741 )
Steps to reproduce:
- call editor.render with this json: {"time":1655483036373,"blocks":[],"version":"2.24.3"}
- Observe editor gives errors in console, and it is now impossible to add blocks or type into the editor.
Expected behavior: Editor operates as normal, with either no blocks present or with one default block.
Workaround: If you make a blank document and save it, it returns "blocks":[]. However you cannot load this to get back your blank document. I've had to manually change blank documents to have instead: "blocks":[{"id":"dvSFGiwGIy","type":"paragraph","data":{"text":""}}] With at least one block, the file loads correctly.
Screenshots: I get three different errors when clicking around on the broken editor:
Editor.js 2.24.3 There is no block at index -1 8 editor.js:1:3853
Uncaught TypeError: this.Editor.BlockManager.lastBlock is undefined
Uncaught TypeError: s is undefined value editor.js:1 value editor.js:1 value editor.js:1 on editor.js:1 value editor.js:1 value editor.js:1 t editor.js:1 l editor.js:1 _invoke editor.js:1 e editor.js:1 n editor.js:1 s editor.js:1 exports editor.js:1 exports editor.js:1 value editor.js:1 t editor.js:1 l editor.js:1 _invoke editor.js:1 e editor.js:1 n editor.js:1 s editor.js:1 exports editor.js:1 exports editor.js:1 value editor.js:1 t editor.js:1 l editor.js:1 _invoke editor.js:1 e editor.js:1 n editor.js:1 s editor.js:1 exports editor.js:1 exports editor.js:1 promise callback*t/</< editor.js:1 t editor.js:1 l editor.js:1 _invoke editor.js:1 e editor.js:1 n editor.js:1 s editor.js:1 exports editor.js:1 exports editor.js:1 value editor.js:1 t editor.js:1 l editor.js:1 _invoke editor.js:1 e editor.js:1 n editor.js:1 s editor.js:1 promise callback*n editor.js:1 s editor.js:1 promise callback*n editor.js:1 s editor.js:1 exports editor.js:1 exports editor.js:1 promise callback*t editor.js:1 t editor.js:1 <anonymous> book.js:1 <anonymous> book.js:1 <anonymous> book.js:1
Device, Browser, OS:
Device, Browser, OS: Desktop/Firefox/Windows 10
Editor.js version: 2.24.3
Plugins you use with their versions: [email protected] [email protected] [email protected] [email protected]
Ive run into this issue too and get Uncaught TypeError: Cannot read properties of undefined (reading 'holder')
when I later try to re-add a block after deleting them all.
You can solve this by always having a block inside, like not saving when the list is empty. Though, I think this is rather an ugly workaround and ideally, editorjs should just let you not delete the block if it is the only block in the array.
this is a real problem for me.. is there any way to check if the editor is empty? i found that editor.clear() works but editor.render(
this is a real problem for me.. is there any way to check if the editor is empty? i found that editor.clear() works but editor.render() breaks so if i could check if its empty and call a clear before safing that would fix it
Since the devs dont seem to care to address one of the most basic functionalities of an editor I resolved it by making sure the editor is never empty. Initialize it with a block saying „enter content here“ or something and dont save when the editor is empty.
Instead of blocks: []
, use blocks: [{ type: "paragraph", data: {} }]
Instead of
blocks: []
, useblocks: [{ type: "paragraph", data: {} }]
It helped me. Now, when I save blocks to database, I'm checking for empty array blocks and if it is empty, adds empty paragraph. Looks weird, but works. Thanks.
This will fixed by a simple update:
const lastBlock = this.Editor.BlockManager.getBlockByIndex(-1);
let isClickedBottom;
if (!lastBlock) {
isClickedBottom = true;
} else {
const lastBlockBottomCoord = $.offset(lastBlock.holder).bottom;
const clickedCoord = event.pageY;
isClickedBottom = event.target instanceof Element &&
event.target.isEqualNode(this.nodes.redactor) &&
/**
* If there is cross block selection started, target will be equal to redactor so we need additional check
*/
!BlockSelection.anyBlockSelected &&
/**
* Prevent caret jumping (to last block) when clicking between blocks
*/
lastBlockBottomCoord < clickedCoord;
}
if (!BlockManager.lastBlock || !BlockManager.lastBlock.tool.isDefault || !BlockManager.lastBlock.isEmpty) {

Adding empty paragraph sometimes was causing issues.
Just added an empty space (nbsp;) in my case.
{
type: "paragraph",
data: {
text: " ",
},
}