简体   繁体   English

获取/恢复Monaco编辑器Undo&Redo堆栈

[英]Get/Restore Monaco editor Undo&Redo stack

I want to create a system to store the Undo&Redo stack of the Monaco editor. 我想创建一个系统来存储Monaco编辑器的Undo&Redo stack

Why? 为什么? : I have a Monaco instance where I do several changes. :我有一个Monaco实例,我做了几处修改。 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 ? 问题 :如何获取和恢复Undo&Redo stack


UPDATE: When I dispose the Monaco editor instance, the JavaScript environment can be completely destroyed. 更新:当我处理Monaco编辑器实例时,JavaScript环境可以完全销毁。 It is integrated in a C# environment that is capable to communicate with JS . 它集成在一个能够与JS通信的C#环境中。 My goal is to store the Monaco Editor model in the C# or serialize it. 我的目标是将Monaco Editor model存储在C#或序列化它。

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. viewState可用于恢复编辑器的光标位置。

Here's the unofficial way: 这是非官方的方式:

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

In your case you can then run JSON.stringify on past and future . 在您的情况下,您可以在pastfuture运行JSON.stringify Then when you recreate the editor you just do 然后,当您重新创建编辑器时,您就可以了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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