Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset the undo stack in ACE editor

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.

like image 482
Ionică Bizău Avatar asked Jun 22 '15 17:06

Ionică Bizău


2 Answers

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>
like image 147
skbly7 Avatar answered Oct 09 '22 16:10

skbly7


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

like image 42
a user Avatar answered Oct 09 '22 16:10

a user