kommitters / editorjs-undo

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

Undo event handler to trigger undo or redo without keyboard event. #212

Closed cptiwari20 closed 10 months ago

cptiwari20 commented 1 year ago

Feature

I want to handle the Undo and Redo using a custom button inside or outside the editors,

Describe the solution you'd like

If there will be an event handler for undo library that would be very helpful.

If you have any existing solution for this, please tell me, I am not able to find it so far.

cptiwari20 commented 11 months ago

temporary solution I found,

const handleUndo = (e, isUndo = true) => {
    try {
      e.preventDefault();
      const event = new KeyboardEvent("keydown", {
        // bubbles: true,
        cancelable: true,
        key: "z",
        metaKey: /(Mac)/i.test(navigator.platform) ? true : false,
        ctrlKey: /(Mac)/i.test(navigator.platform) ? false : true,
        shiftKey: !isUndo,
      });
      document.getElementById("editorjs").dispatchEvent(event);
    } catch (error) {
      console.error("Error while Undo/Redo ", error);
    }
  };
MarioRodriguezS commented 10 months ago

@cptiwari20 Thank you for reporting the issue. To utilize the 'Undo' and 'Redo' functions outside of the plugin, you can assign a variable to the 'Undo' instance within the onReady() callback. Then, you can employ this variable anywhere within your code. For a practical example, please refer to the CodeSandbox demonstration provided.

Code Sandbox example

Please let us know if this solution proves helpful to you.