简体   繁体   English

重置摩纳哥编辑器状态

[英]Reset Monaco Editor state

I've implemented the Monaco Editor ( https://github.com/Microsoft/monaco-editor ) as a way for the user to insert some JSON. 我已经实现了Monaco编辑器( https://github.com/Microsoft/monaco-editor ),作为用户插入JSON的一种方式。

I enable the editor once the user clicks a "post" button. 用户单击“发布”按钮后,我将启用编辑器。 The problem is, the editor is enabled inside a switch function. 问题是,在开关功能内部启用了编辑器。 So once the button have been clicked once, the editor will like append beneath the first created editor, if the client clicks the same button again. 因此,一旦单击按钮一次,如果客户端再次单击相同的按钮,编辑器将喜欢追加到第一个创建的编辑器下面。 Is there any way to "reset" the editor, so it wont actually append, but just instead either make a new one, or use the one already created? 有什么方法可以“重置”编辑器,因此它实际上不会追加,而只是制作一个新的或使用已经创建的编辑器?

Here is my current code. 这是我当前的代码。

require.config({ paths: { 'vs': '/scripts/monaco-editor/min/vs' } });

switch (id) {
        case 'post':
            $('#httpMethodGet').css('display', 'none');
            $('#httpMethodPost').show();
            require(['vs/editor/editor.main'], function () {
                monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
                    schemas: [{
                        uri: "http://myserver/bar-schema.json",
                        schema: {
                            type: "object",
                            properties: {
                                q1: {
                                    enum: ["x1", "x2"]
                                }
                            }
                        }
                    }]
                });
                var jsonObject = [
                    '{',
                    '    "$schema": "http://myserver/foo-schema.json"',
                    "}"
                ].join('\n');
                window.editor = monaco.editor.create(document.getElementById('codeEditor'), {
                    value: jsonObject,
                    language: 'json',
                    theme: 'vs-dark',
                    scrollBeyondLastLine: false,
                    renderWhitespace: true
                });
            });
            break;

So I want the window.editor = monaco.editor.create(document.getElementById('codeEditor'), {}) to either use the same already created, if there is one created, or make a new one, each time this switch cases is entered, so it doesn't append bellow the one(s) already created. 因此,我希望window.editor = monaco.editor.create(document.getElementById('codeEditor'), {})使用已创建的相同window.editor = monaco.editor.create(document.getElementById('codeEditor'), {})如果已创建),或者每次进行此切换时都使用一个新文件输入了case,因此不会在下面附加已经创建的案例。

You should create the monaco code editor out side your switch function. 您应该在切换功能之外创建monaco代码编辑器。 Inside your function you should change your model only. 在函数内部,您仅应更改模型。

So before the switch() function 所以在switch()函数之前

window.editor = monaco.editor.create(document.getElementById('codeEditor'), {
                    value: '',
                    language: 'json',
                    theme: 'vs-dark',
                    scrollBeyondLastLine: false,
                    renderWhitespace: true
                });

then inside switch() 然后在switch()内部

window.editor.getModel(). setValue(jsonObject);

APIs are avalable here . API在这里可用。

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

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