Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Jun 24, 2013 @ 17:15
    Warren Buckley
    0

    CodeMirror - Trying to highlight a line

    Hello all,
    I am working on a package that will use the CodeMirror file editor in my package.
    For part of the package it will highlight errors in the file, by marking the lines in red.

    The JS Api for CodeMirror allows this to be done, I am just having a problem getting the existing code editor as a Javascript Object, allowing me to run the functions & methods available on the CodeMirror object.

    Here is my attempt, but without luck:

    <script type="text/javascript">
        function highlightLine(lineNumber) {
                
            //This seems to re-inititalize
            var editor = CodeMirror.fromTextArea(document.getElementById("body_CodeTextBox"), {
                lineNumbers: true,
                matchBrackets: true
            });
    
            //Log the editor object
            console.log(editor);
    
            //Then highlight the line
            var line = editor.getLineHandle(lineNumber);
            console.log(line);
                
            //Set line css class
            editor.addLineClass(line, 'background''line-error');
        }
    </script>

    Any ideas please?

    Thanks,
    Warren

     

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Jun 24, 2013 @ 18:39
    Warren Buckley
    100

    Sorted the problem myself,
    For anyone else interested here is the solution:

    <script type="text/javascript">
        function highlightLine(lineNumber) {
    
            //Line number is zero based index
            var actualLineNumber = lineNumber - 1;
    
            //Select editor loaded in the DOM
            var myEditor = $("#body_EditorSource .CodeMirror");
            console.log(myEditor);
            console.log(myEditor[0].CodeMirror);
    
            var codeMirrorEditor = myEditor[0].CodeMirror;
                
            //Set line css class
            codeMirrorEditor.setLineClass(actualLineNumber, 'background''line-error');
        }
    </script>
  • Prakhar Shukla 1 post 21 karma points
    Jun 16, 2020 @ 03:06
    Prakhar Shukla
    0

    This doesn't work in in new version of codemirror

Please Sign in or register to post replies

Write your reply to:

Draft