Get / Restore Monaco Undo & Redo stack editor

I want to create a system for storing the Undo&Redo stack Monaco editor.

Why? : I have a Monaco instance where I make a few changes. Then I have to delete this instance and open a new one. Here I want to restore the same state of the stack that I had in the previous instance.

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


UPDATE: When I delete an instance of the Monaco editor, the JavaScript environment can be completely destroyed. It is integrated into the C# environment, capable of communicating with JS . My goal is to save the Monaco Editor model in C# or serialize it.

+5
source share
2 answers

All this is connected with the model.

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

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 could help was to hook the events in Monaco.

 monaco.editor.onWillDisposeModel(saveModel) 

viewState can be used to resume the cursor position in the editor.

+2
source

Here's an unofficial way:

 const {past, future} = editor.getModel()._commandManager; 

In your case, you can run JSON.stringify on past and future . Then, when you recreate the editor, you just do

 const cm = editor.getModel()._commandManager; cm.past = JSON.parse(past); cm.future = JSON.parse(future); 
0
source

Source: https://habr.com/ru/post/1274635/


All Articles