kommitters / editorjs-undo

Undo/Redo feature for Editor.js
https://www.npmjs.com/package/editorjs-undo
MIT License
169 stars 51 forks source link

Undo (Ctrl+Z) causes error when empty paragraphs are present, likely when reaching the end of the undo stack #284

Closed Matergi closed 2 weeks ago

Matergi commented 3 weeks ago

Precheck

Description

When performing an undo (Ctrl+Z) action in a project containing empty paragraphs, an error occurs, seemingly when reaching the end of the undo stack. Writing content works as expected, but when undoing multiple actions, the error appears, likely triggered when all changes have been undone and the stack is exhausted. Subsequent undo actions function normally after the initial error.

https://github.com/user-attachments/assets/b5f24f82-3c94-4b99-b977-9acb350892ae

config

const undo = new Undo({ editor: this.editorJSRef });
if (initialData) {
  undo.initialize(initialData);
}

initialData

{
    "time": 1726488299703,
    "blocks": [
        {
            "id": "gQylYzS2g3",
            "data": {
                "text": "Preparazione",
                "level": 2
            },
            "type": "header"
        },
        {
            "id": "OEa17yeViK",
            "data": {
                "text": "ffff"
            },
            "type": "paragraph"
        },
        {
            "id": "tqnPuoDqad",
            "data": {
                "text": "Esecuzione",
                "level": 2
            },
            "type": "header"
        },
        {
            "id": "DXvmBu7NB1",
            "data": {
                "text": ""
            },
            "type": "paragraph"
        },
        {
            "id": "8JhJyLNqig",
            "data": {
                "text": "Varianti",
                "level": 2
            },
            "type": "header"
        },
        {
            "id": "-3r_em4xqt",
            "data": {
                "text": ""
            },
            "type": "paragraph"
        },
        {
            "id": "-PTRcHuD4d",
            "data": {
                "text": "Consigli",
                "level": 2
            },
            "type": "header"
        },
        {
            "id": "-I1jKL30IU",
            "data": {
                "text": ""
            },
            "type": "paragraph"
        },
        {
            "id": "ECVz89TiLC",
            "data": {
                "text": "Materiali",
                "level": 2
            },
            "type": "header"
        }
    ],
    "version": "2.30.3"
}

plugins:

paragraph: {
  class: Paragraph,
  inlineToolbar: true,
  config: {
    placeholder: strings.get('editorjs.start writing here'),
    preserveBlank: true,
  },
},

Environment

Current behavior

(Ctrl+Z) at the end of the stack

Screenshot 2024-09-16 at 14 15 05 Screenshot 2024-09-16 at 14 16 26 Screenshot 2024-09-16 at 14 16 48

Expected behavior

A clear and concise description of what you expected to happen.

Additional context

Add any other context about the problem here.

Matergi commented 3 weeks ago

It seems that if I don't include:

undo.initialize(initialData);

the issue doesn't occur, but in this case, performing Ctrl+Z clears everything

Matergi commented 2 weeks ago

Forgive me, it was my mistake, I was initializing EditorJS like this:

onReady: async () => {
    ...
    this.editorJSRef.blocks.render(data);
    ...
}

but I had also added the data prop in the EditorJS constructor:

new EditorJS({
    ...
    data
    ...
})

By removing this.editorJSRef.blocks.render(data);, everything works fine. Issue closed.