简体   繁体   中英

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.

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.

Here's the unofficial way:

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

In your case you can then 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM