Search code examples
javascriptcodemirror

How to perform undo and redo operation on code mirror


There is one icon for undo and one for redo. I want to perform redo and undo operations in codemirror when those icon are clicked


Solution

  • It is very simple to undo and redo in codemirror. All you need to do is create a new editor.

    var myCodeMirror = CodeMirror(document.body, {
      value: "function myScript(){return 100;}\n",
      mode:  "javascript"
    });
    

    Then you can call myCodeMirror.undo() and myCodeMirror.redo(). I made a simple electron codeEditor with codemirror a while ago it might help you to have a look over what I did https://github.com/davidmather/electronCodeEditor/blob/master/index.html .

    You can find most of the commands on the codeMirror GitHub here https://github.com/codemirror/CodeMirror/blob/master/src/edit/commands.js