Monaco 编辑器默认 json uri 模式

Rom*_*om1 6 json monaco-editor angular ngx-monaco-editor

我正在使用摩纳哥编辑器来编辑 JSON,我想设置自定义诊断选项。我正在尝试https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-configure-json-defaults

// Configures two JSON schemas, with references.

var jsonCode = [
    '{',
    '    "p1": "v3",',
    '    "p2": false',
    "}"
].join('\n');
var modelUri = monaco.Uri.parse("a://b/foo.json"); // a made up unique URI for our model
var model = monaco.editor.createModel(jsonCode, "json", modelUri);

// configure the JSON language support with schemas and schema associations
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
    validate: true,
    schemas: [{
        uri: "http://myserver/foo-schema.json", // id of the first schema
        fileMatch: [modelUri.toString()], // associate with our model
        schema: {
            type: "object",
            properties: {
                p1: {
                    enum: ["v1", "v2"]
                },
                p2: {
                    $ref: "http://myserver/bar-schema.json" // reference the second schema
                }
            }
        }
    }, {
        uri: "http://myserver/bar-schema.json", // id of the second schema
        schema: {
            type: "object",
            properties: {
                q1: {
                    enum: ["x1", "x2"]
                }
            }
        }
    }]
});

monaco.editor.create(document.getElementById("container"), {
    model: model
});
Run Code Online (Sandbox Code Playgroud)

从哪里来uri: "http://myserver/foo-schema.json"?我只想使用默认的 JSON 模式。不是我自己的。

像这样设置 uri 有效: uri: "http://localhost:4200/assets/monaco-editor/min/vs/language/json/jsonMode.js",

但是有没有一种干净的方法来设置这个值?也许 JSON 的 uri 值在某处可用?我搜索了 monaco.languages.json.jsonDefaults 但没有找到任何内容。

mwa*_*wag 0

"http://myserver/foo-schema.json"是一个任意值——你可以将其设置为你想要的任何值。仅当您还使用enableSchemaRequest时才重要- 在这种情况下它应该指向您想要从中获取模式的位置 - 但您没有这样做,所以这并不重要。事实上,如果我正确理解您的意图,与此 URI 相关的所有内容都与您尝试执行的操作无关。

当你说“我只想使用默认的 JSON 模式,而不是我自己的”时,我想你的意思是你只是想确保它是有效的 JSON,对吧?因为,不存在“默认 JSON 模式”这样的东西——根据定义,它是由您定义的——但是存在JSON的正式定义(另一方面,JSON 模式假设您已经从有效的 JSON 开始,然后允许您定义(有效)JSON 必须符合的模式)。

假设您只想确保它是有效的 JSON(但您不关心 json 是否符合某些自定义架构),您只需将语言设置为“json”即可,并且您的代码可以简单如下:

var myBadJSONText = '{this is not : "JSON"}'

monaco.editor.create(document.getElementById('container'), {
    language: 'json',
    value: myBadJSONText
});
Run Code Online (Sandbox Code Playgroud)

在摩纳哥游乐场跑步可以让你: