获取和恢复monaco editor的undo/redo栈

Posted 微个日光日

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取和恢复monaco editor的undo/redo栈相关的知识,希望对你有一定的参考价值。

场景:在使用monaco editor的时候,有时需要销毁之前的实例,创建新的实例。但是这样,新的editor就不能使用之前实例的redo和undo的信息栈。

解决办法:可以在销毁之前的实例之前,将editor的model和viewState保存至全局,然后新创建的实例就可以重新使用它们了。核心代码如下:

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);

缺点:就是将viewState全部存在了内存中(monaco editor有它自己的数据结构),而不能将其序列化使用。

以上是关于获取和恢复monaco editor的undo/redo栈的主要内容,如果未能解决你的问题,请参考以下文章

editor.IStandaloneCodeEditor'不可分配给'monaco.editor.IStandaloneCodeEditor'类型的参数

monaco-editor 常用方法与事件

Angular 5 ngx-monaco-editor 在生产中失败

解决 Monaco Editor 在 macOS 系统上光标错位

在 umijs 项目中使用 Monaco Editor 的配置

在vue项目中使用monaco-editor