Reputation: 113365
I want to reset the undo stack in an ACE editor. The behavior should be:
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.
Upvotes: 21
Views: 7259
Reputation: 1162
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>
Upvotes: 27
Reputation: 24104
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
Upvotes: 10