Inserting a new text at given cursor position

后端 未结 7 2197
夕颜
夕颜 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 19:01

    Improved function that, if selection present, replaces the text, if not, inserts in current cursor position

    function insertString(editor,str){
    
            var selection = editor.getSelection();
    
            if(selection.length>0){
                editor.replaceSelection(str);
            }
            else{
    
                var doc = editor.getDoc();
                var cursor = doc.getCursor();
    
                var pos = {
                   line: cursor.line,
                   ch: cursor.ch
                }
    
                doc.replaceRange(str, pos);
    
            }
    
        }
    

提交回复
热议问题