kommitters / editorjs-undo

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

Undo does not work in document with initial data #151

Closed fedorin-git closed 1 year ago

fedorin-git commented 2 years ago

Description

Undo does not work in document with initial data

Environment

Steps to reproduce:

  1. Open document with initial data
  2. Type something anywere
  3. Press 'CMD+Z'

Current behavior

Expected behavior

Changes should be rolled back in appropriate order or appearance

Implementation code

...
editorRef.current = new EditorJS({
        placeholder: 'Enter some reach text...',
        inlineToolbar: true,
        readOnly,
        data,
        holder: 'editorjs',
        onReady: () => {
          const undo = new Undo({
            editor: editorRef.current,
            config: {
              shortcuts: {
                undo: 'CMD+Z',
                redo: 'CMD+SHIFT+Z'
              }
            }
          });
          if (data) undo.initialize(data);
          return undo;
        },
        tools: EDITOR_JS_TOOLS({
          readOnly,
          policyId: orgPolicyId,
          orgId,
          companyName: orgName,
          blocks: data?.blocks,
          orgEntities,
          ...orgParams
        }),
        tunes: ['blockTune', 'anchorBlockTune']
});
...

Note:

Work well in empty document.

FelipeGuzmanSierra commented 1 year ago

Hello @fedorin-git , I hope you are doing well.

Thanks a lot for raising the issue. I checked, and the error may occur when the EditorJs is instantiated more than once, maybe related to the React hard reload which duplicates the editor each render. It could be your case possibly?

If not, I will appreciate it if you could give us more information about the issue to replicate the same problem you are getting.

Best regards.

karolrojas07 commented 1 year ago

Hi @fedorin-git, I tested this and this is working as you can see in the following gif Undo

When my index.js root.render is like this:

root.render(
  <App />
);

But, If you use StrictMode like this:

root.render(
 <React.StrictMode>
    <App />
  </React.StrictMode>
);

Then, a duplicated error will happen:

UndoError

Here you are more detailed information about StrictMode: https://mariosfakiolas.com/blog/my-react-components-render-twice-and-drive-me-crazy/

Also, we recommend you read the README.md usage section: https://github.com/kommitters/editorjs-undo#usage


Closing this, please let us know if the issue keeps happening to you.