Inserting a new text at given cursor position

后端 未结 7 2142
夕颜
夕颜 2021-01-01 18:35

I am working on customizing the codemirror for my new language mode. As part of this new mode implementation, I am writing a new tool bar where user can select some text and

7条回答
  •  迷失自我
    2021-01-01 18:44

    To add the new line at the end -

    function updateCodeMirror(data){
        var cm = $('.CodeMirror')[0].CodeMirror;
        var doc = cm.getDoc();
        var cursor = doc.getCursor(); // gets the line number in the cursor position
        var line = doc.getLine(cursor.line); // get the line contents
        var pos = { // create a new object to avoid mutation of the original selection
            line: cursor.line,
            ch: line.length - 1 // set the character position to the end of the line
        }
        doc.replaceRange('\n'+data+'\n', pos); // adds a new line
    }
    

    Call function

    updateCodeMirror("This is new line");
    

提交回复
热议问题