Reset the undo stack in ACE editor

7k views Asked by At

I want to reset the undo stack in an ACE editor. The behavior should be:

  1. I do some changes in editor.
  2. Call some magic function to reset the undo stack
  3. When trying to undo, this will not be possible because the undo stack was reset.

I guess it has to do with the UndoManager from ACE, but I have no idea how can I use it in the following example.

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/markdown");

setTimeout(function() { 
  editor.setValue("And now how can I reset the\nundo stack,so pressing\nCTRL+Z (or Command + Z) will *NOT*\ngo back to previous value?", -1);
}, 3000);
#editor {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-size: 25px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/ace.js"></script>
<div id="editor">This value will be changed in 3 seconds.</div>

I have looked into editor and editor.session prototypes to find some helper function, but without success.

2

There are 2 answers

6
skbly7 On BEST ANSWER

Yes, UndoManager is the class which maintains all the history. The solution is to initialize the session with a blank/newly created class.

Check out the snippet.

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/markdown");

setTimeout(function() {
  editor.setValue("And now how can I reset the\nundo stack,so pressing\nCTRL+Z (or Command + Z) will *NOT*\ngo back to previous value?", -1);
  editor.getSession().setUndoManager(new ace.UndoManager())
}, 3000);
#editor {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-size: 25px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/ace.js"></script>
<div id="editor">This value will be changed in 3 seconds.</div>

5
a user On

use editor.session.setValue() or call editor.session.getUndoManager().reset(); see https://github.com/ajaxorg/ace/blob/v1.1.9/lib/ace/edit_session.js#L279