editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

[Bug] editor.render with "blocks":[] breaks the editor

Open wgprojects opened this issue 2 years ago • 3 comments

(Not sure if exact duplicate of https://github.com/codex-team/editor.js/pull/1741 )

Steps to reproduce:

  1. call editor.render with this json: {"time":1655483036373,"blocks":[],"version":"2.24.3"}
  2. 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]

wgprojects avatar Jun 17 '22 17:06 wgprojects

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.

dafrina avatar Jun 22 '22 14:06 dafrina

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

elYanuki avatar Sep 14 '22 09:09 elYanuki

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.

dafrina avatar Sep 14 '22 12:09 dafrina

Instead of blocks: [], use blocks: [{ type: "paragraph", data: {} }]

AlexRMU avatar Oct 29 '22 15:10 AlexRMU

Instead of blocks: [], use blocks: [{ 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.

Hydrock avatar Dec 04 '22 05:12 Hydrock

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) {
CleanShot 2023-03-31 at 15 01 39@2x

medzhidov avatar Mar 31 '23 09:03 medzhidov

Adding empty paragraph sometimes was causing issues.

Just added an empty space (nbsp;) in my case.

{
  type: "paragraph",
  data: {
    text: "&nbsp;",
  },
}

gauravsaluja2006 avatar Aug 24 '23 18:08 gauravsaluja2006