Multiple modes Codemirror

空扰寡人 提交于 2019-12-12 12:52:12

问题


I want my TextArea to be able to support multiple CodeMirror modes. For now I want it to support json and xml. Is this possible? Also, is it possible to automatically detect whether the user placed json or xml in the area?

Thanks.


回答1:


CodeMirror actually has an example very close to what you are looking for here.

Here is a more specific example that does what you want.

  1. Create a CodeMirror instance.
  2. When the content changes we determine if we should switch modes.

The logic I put in for determining what mode you are in is very simplistic and can be refactored to support as robust a check as you deem appropriate for either mode. (Regex is good for complex checking if you want to get fancy...that is the only reason I used it even in my simple example) Currently, my example code just checks for any content where the first non-space character is "<" thus indicating xml mode. When deciding to switch back it just checks that the first non-space character is not "<" and that it is not blank (in case the user just deleted everything to start over with more xml).

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Code Mirror Example</title>
<script src="lib/codemirror.js"></script>
<link rel="stylesheet" href="lib/codemirror.css">
<script src="mode/javascript/javascript.js"></script>
<script src="mode/xml/xml.js"></script>
<style type="text/css">.CodeMirror{border:1px solid black;}</style>
</head>
<body>
    <div><span>Mode: </span><span id="modeType">JSON</span></div>
    <textarea class='codeEditor'></textarea>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    function determineCodeMirrorType(cm)
    {
        if (cm.getMode().name == 'javascript')
        {
            checkAndSwitchToXML(cm, cm.getValue());
        }
        else if (cm.getMode().name == 'xml')
        {
            checkAndSwitchToJSON(cm, cm.getValue());
        }
    }

    function checkAndSwitchToXML(cm, val)
    {
        if (/^\s*</.test(val))
        {
            cm.setOption("mode", "xml");
            $('#modeType').html("XML");
        }
    }
    function checkAndSwitchToJSON(cm, val)
    {
        if (!/^\s*</.test(val) && val.match(/\S/))
        {
            cm.setOption("mode", "javascript");
            $('#modeType').html("JSON");
        }
    }

    function buildCMInstance(mode, value)
    {
        var cm = CodeMirror.fromTextArea($('.codeEditor')[0], {
            mode:mode,
            value:value,
            lineNumbers:true,
            onChange:function(cmInstance){
                determineCodeMirrorType(cmInstance);
            }
        });
        return cm;
    }
    $(document).ready(function(){
        //mode changing demo:  "http://codemirror.net/demo/changemode.html";
        var cm = buildCMInstance("javascript");
    });
    </script>
</body>
</html>


来源:https://stackoverflow.com/questions/11547506/multiple-modes-codemirror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!