Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get/Restore Monaco editor Undo&Redo stack

I want to create a system to store the Undo&Redo stack of the Monaco editor.

Why?: I have a Monaco instance where I do several changes. Then I have to dispose that instance and open a new one. Here, I want to restore the same stack state that I had in the previous instance.

Question: How can I get and restore the Undo&Redo stack?


UPDATE: When I dispose the Monaco editor instance, the JavaScript environment can be completely destroyed. It is integrated in a C# environment that is capable to communicate with JS. My goal is to store the Monaco Editor model in the C# or serialize it.

like image 286
Hélder Gonçalves Avatar asked Jan 11 '18 15:01

Hélder Gonçalves


1 Answers

It all has to do with the Model.

If you restore the same model you will have the Undo&Redo stacks

See Example

var model = editorInstance.getModel();
var viewState = editorInstance.saveViewState();

//Destroy your instance for whatever reason
editorInstance.dispose();

//When you create the new instance load the model that you saved
var newInstance = monaco.editor.create(elem, options);
newInstance.setModel(model);
newInstance.restoreViewState(viewState);

Something that might help would be to tie into the monaco event hook

monaco.editor.onWillDisposeModel(saveModel)

The viewState can be used to resume the cursor position of the editor.

like image 81
joverall22 Avatar answered Oct 10 '22 08:10

joverall22