Search code examples
javascriptgoogle-chromeelectronundo-redo

What is electrons webContents.undo() doing?


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.


Solution

  • 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.
        }
    }