-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature-Request: Restorable undoManager (or even restorable session!) #1452
Comments
👍 for [de]serializing undo manager |
👍 |
1 similar comment
👍 |
Has this ever been implemented? Would like to store the undomanager in localstorage... 🙂 |
Link of interest: https://stackoverflow.com/a/53387989/2470524 |
ace.EditSession.prototype.toJSON = function() {
return {
annotations: this.getAnnotations(),
breakpoints: this.getBreakpoints(),
folds: this.getAllFolds().map(function(fold) {
return fold.range;
}),
history: {
undo: this.getUndoManager().$undoStack,
redo: this.getUndoManager().$redoStack,
rev: this.getUndoManager().$rev,
mark: this.getUndoManager().mark
},
mode: this.getMode().$id,
scrollLeft: this.getScrollLeft(),
scrollTop: this.getScrollTop(),
selection: this.getSelection().toJSON(),
value: this.getValue()
};
}; This lets you persist the session: let session = JSON.stringify(editor.getSession());
localStorage.setItem('session', session); |
That looks nice, how would one restore the information again after the browser has been reloaded? |
Get the serialized session from the storage and deserialize it back into a object: let session = JSON.parse(localStorage.getItem('session')); Then either create a new function createEditSession(session) {
let editSession = new ace.EditSession(session.value);
session.folds.forEach(function(fold) {
editSession.addFold("...", ace.Range.fromPoints(fold.start, fold.end));
});
editSession.setAnnotations(session.annotations);
editSession.setBreakpoints(session.breakpoints);
editSession.setUndoManager(new ace.UndoManager());
editSession.$undoManager.$undoStack = session.history.undo;
editSession.$undoManager.$redoStack = session.history.redo;
editSession.$undoManager.$rev = session.history.rev;
editSession.$undoManager.mark = session.history.mark;
editSession.setMode(session.mode);
editSession.setScrollLeft(session.scrollLeft);
editSession.setScrollTop(session.scrollTop);
editSession.selection.fromJSON(session.selection);
return editSession;
}
let editSession = createEditSession(session);
editor.setSession(editSession); Or you could just modify the existing default session without creating a new session.folds.forEach(function(fold) {
editor.session.addFold("...", ace.Range.fromPoints(fold.start, fold.end));
});
editor.session.setAnnotations(session.annotations);
editor.session.setBreakpoints(session.breakpoints);
editor.session.$undoManager.$undoStack = session.history.undo;
editor.session.$undoManager.$redoStack = session.history.redo;
editor.session.$undoManager.$rev = session.history.rev;
editor.session.$undoManager.mark = session.history.mark;
editor.session.setMode(session.mode);
editor.session.setScrollLeft(session.scrollLeft);
editor.session.setScrollTop(session.scrollTop);
editor.session.selection.fromJSON(session.selection);
editor.setValue(session.value); Edit: I've updated this comment a couple of times. Note that folds are n-depth so needs a recursive function to properly restore. |
Update 1: :-( I could restore the undoStacks and can even undo after page reload, but now I get Update 2: I could fix the above issue by replacing Update 3: Next fix (and sorry for spamming everybody again):
I had to make a few minor changes for @vanillajonathan 's code to work for me. Here is my version of the session restoration: 1.) Store the session and undoManager-data (just as proposed by vanillajonathan):
2.) To restore after page reload (I tried to highlight my changes in bold, so I couldn't insert it as a code block):function createEditSession(session) { session = JSON.parse(localStorage.getItem('session')); |
This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue. |
Hi,
it would be cool, if the state of the current undoManager could be stored and restored later, even after leaving the page.
I once implemented such a persistent undo/redo history for textareas. I used undo/redo stacks with a max. number of say n deltas. On submission of the form I stored both delta-stacks (using session storage) referenced by a unique key for that textarea.
Returning to the page later I could restore the undo/redo stacks from the data in session storage.
I would like to adopt this behavior into the ACE Editor, but I failed saving and restoring the state of the undoManager (using Object.toJSON() and JSON.parse()), because of the following reasons:
Now, it would be cool to have something like session.getUndoManager().toString() and session.getUndoManager.loadFromString(str) or even save/load methods that store the data in some (configurable) place (i.e. sessionStorage, localStorage).
Or even one step further: session.save()->String and session.load(str).
Regards
Chris
The text was updated successfully, but these errors were encountered: