简体   繁体   English

防止CodeMirror json-lint禁止空输入

[英]Prevent CodeMirror json-lint from linting empty input

I'm putting together a quick CodeMirror input for JSON and to make sure the user doesn't mess things up, I'm using the json-lint addon. 我正在使用JSON的CodeMirror快速输入,并确保用户不会弄乱事情,我使用的是json-lint插件。 My issue is that immediately on render the empty CodeMirror input is displaying a lint error. 我的问题是,在渲染时,空CodeMirror输入立即显示棉绒错误。 I understand that an empty input doesn't constitute valid JSON, but I'd rather it only runs when input has been made. 我知道空输入并不构成有效的JSON,但我希望它仅在输入后才运行。

I'm using the addon/lint/json-lint.js add-on, which in turn is using the jsonlint package. 我正在使用addon/lint/json-lint.js附加组件,而后者又使用了jsonlint包。

JS JS

var jsonConfig = {
    mode:              'application/json',
    lineNumbers:       true,
    lint:              true,
    autoCloseBrackets: true,
    gutters:           ['CodeMirror-lint-markers']
};

$('.json textarea').each(function (pos, el) {
    CodeMirror.fromTextArea(el, jsonConfig);
});

Example empty input results: 空输入结果示例: 在此处输入图片说明

Lint message: 皮棉消息: 在此处输入图片说明

I can't see anything in the docs to disable linting for empty inputs. 我在文档中看不到任何禁用空输入的lint的功能。 Am I missing something simple? 我是否缺少简单的东西?

Hope this helps: 希望这可以帮助:

    var editor_json = CodeMirror.fromTextArea(document.getElementById("textAreaId"), {
       lineNumbers: true,
       mode: "application/json",
       gutters: ["CodeMirror-lint-markers"],
       lint: true,
       theme: '<yourThemeName>'
    });

    //on load check if the textarea is empty and disable validation
    editor_json.setOption("lint", editor_json.getValue().trim() );

    //once changes happen, if the textarea gets filled up again re-enable the validation
    editor_json.on("change", function(cm, change) {
        editor_json.setOption("lint", editor_json.getValue().trim() );
    });

    ///sometimes you need to refresh the CodeMirror instance to fix alignment problems or some other glitch
    setTimeout(function(){
       editor_json.refresh();
    },0);

So in short: 简而言之:

editor_json.setOption("lint", editor_json.getValue().trim() );

Which is translated to: 转换为:

< yourCodeMirrorInstance >.setOption('< yourOption >',< true/false or any required value >) <yourCodeMirrorInstance> .setOption('<yourOption>',<true / false或任何必需值>)

Hope this helps some one. 希望这可以帮助一些人。

you could set a default value to the textarea like this 您可以像这样为textarea设置默认值

{\n\t\n}

you would have something like this 你会有这样的事情

{

}

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

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