3

I want to build an Electron-based App that uses undo-redo functionality. Now it is possible to put undo/redo entries in the menu and according roles already exist in electron. role: undo+ role: redo. As seen here

So what I already found out is probably happening is the that webContents.undo() will be called if I click on the 'undo'-MenuItem.

My question now is, what exactly happens when this undo is called? Can I somehow register a listener and handle the undo myself? Or would it be best practice not to use the 'undo'-role but define a custom menu entry and handle everything myself?

Maybe there is some specification about how the electron chrome browser handles those undo events and where a can catch this and define my own action that should happen on undo/redo.

patsimm
  • 476
  • 6
  • 19

1 Answers1

2

I think you should define a custom menu entry and handle it yourself.

I don't think you can register a listener and handle it there since there is no event in the webContents documentation.

The menu item would be something like this:

{
    label:       'Undo',
    accelerator: 'CmdOrCtrl+Z',
    click:       function (menuItem, focusedWin) {
        // Undo.
        focusedWin.webContents.undo();

        // Run some custom code.
    }
}
Joshua
  • 5,032
  • 2
  • 29
  • 45
  • 2
    I am still interested in how the undo event is passed to the textarea/contenteditable that handles it. Or is it at all? I'm kind of confused why there is a role but it has no use... – patsimm Nov 28 '17 at 12:28
  • I'm not sure how its passed to the textarea/contenteditable but what sort of code do you need to run on undo? – Joshua Nov 28 '17 at 13:04
  • 1
    So I the real problem is, I have one contenteditable which should use a undo/redo implemented by myself. The other elements could use the standard browser implementation. But all elements including the contenteditable should listen to the keyboard shortcut (this is not a problem) but also to a click on the MenuItem. There the problem starts. I feel like either I can use the standard implementation everywhere, or implement undo/redo for everything myself. Theres no way to do a mix... – patsimm Nov 29 '17 at 09:50
  • Hmmm... I think it's going to be tricky but here's my advice: I think you could listen to the `onChange` event but that wouldn't work if the user is supposed to type in it. If the user is supposed to type in it then you could always listen to the `Ctrl+Z` keyboard shortcut in the page's javascript and if the textarea is focused then do some custom stuff but if not then send a message to the main process to do a undo. – Joshua Nov 29 '17 at 12:38