重置摩纳哥编辑状态

时间:2016-09-01 07:26:46

标签: javascript monaco-editor

我已经实现了Monaco编辑器(https://github.com/Microsoft/monaco-editor),以便用户插入一些JSON。

我在用户点击“发布”按钮后启用编辑器。问题是,编辑器在开关功能内启用。因此,一旦单击按钮一次,编辑器将在第一个创建的编辑器下面附加,如果客户端再次单击相同的按钮。有没有办法“重置”编辑器,所以它实际上不会附加,而是要么换一个新的,还是使用已创建的那个?

这是我目前的代码。

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;

所以我希望window.editor = monaco.editor.create(document.getElementById('codeEditor'), {})使用已创建的相同内容,如果有一个已创建,或者每次输入此切换案例时都会创建一个新的,因此它不会附加到下面( s)已经创建。

1 个答案:

答案 0 :(得分:1)

您应该在开关功能旁边创建monaco代码编辑器。 在你的功能中你应该只改变你的模型。

所以在switch()函数之前

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

然后在switch()

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

API可用here

相关问题