Is it possible to highlight a search pattern when codemirror loads

霸气de小男生 提交于 2019-12-12 10:06:48

问题


Is there a way for codemirror to highlight the code matching a pattern (like if I use the search addon) when the page load? So I could load the page with ?search=my_pattern and pass the pattern to codemirror.

Here's a sample code and a jsfiddle. You can use CTRL+F to use the search addon.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
        <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/codemirror.css" />
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/codemirror.min.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/search.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/searchcursor.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/match-highlighter.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/python.js"></script>
    </head>
    <body>
        <textarea id="myTextArea">print "hello world"</textarea>
        <script>
        var myTextArea = document.getElementById('myTextArea');
        var myCodeMirror = CodeMirror.fromTextArea(myTextArea, {
            'mode': 'python',
            'lineNumbers': true
        });
        </script>
    </body>
</html>

http://jsfiddle.net/ErxMb/


回答1:


I figured out how to do it using overlay.js by looking at the CodeMirror: Overlay Parser Demo.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
        <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/codemirror.css" />
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/codemirror.min.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/search.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/searchcursor.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/overlay.min.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/match-highlighter.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/python.js"></script>
    </head>
    <style type="text/css">
        .cm-highlightSearch {background: yellow;}
    </style>
    <body>
        <textarea id="myTextArea">print "hello world"</textarea>
        <script>
        var keyword = 'hello';

        CodeMirror.defineMode("highlightSearch", function(config, parserConfig) {
          var searchOverlay = {
            token: function(stream, state) {
                if (stream.match(keyword)) {
                    return "highlightSearch";
                }

                while (stream.next() != null && !stream.match(keyword, false)) {}
                return null;
            }
          };
          return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "python"), searchOverlay);
        });

        var myTextArea = document.getElementById('myTextArea');
        var myCodeMirror = CodeMirror.fromTextArea(myTextArea, {
            'mode': 'highlightSearch',
            'lineNumbers': true
        });
        </script>
    </body>
</html>

http://jsfiddle.net/HkjY7/



来源:https://stackoverflow.com/questions/15979135/is-it-possible-to-highlight-a-search-pattern-when-codemirror-loads

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