【问题标题】:Inserting a new text at given cursor position在给定的光标位置插入新文本
【发布时间】:2014-05-19 08:49:31
【问题描述】:

我正在为我的新语言模式定制代码镜像。作为这种新模式实现的一部分,我正在编写一个新工具栏,用户可以在其中选择一些文本并说插入。此命令应在用户单击工具栏之前将文本插入到用户正在输入的位置。

我找不到任何 API 级别的支持。如果还有其他方法可以帮助我解决这个问题吗?

基本上获取当前光标所在的位置行号和光标当前所在的位置。可能是一个位置对象

用于插入文本的 API,例如 insertText("Text", PositionObject)

【问题讨论】:

    标签: codemirror


    【解决方案1】:

    我是这样做的:

    function insertTextAtCursor(editor, text) {
        var doc = editor.getDoc();
        var cursor = doc.getCursor();
        doc.replaceRange(text, cursor);
    }
    

    【讨论】:

      【解决方案2】:

      replaceSelection (http://codemirror.net/doc/manual.html#replaceSelection) 怎么样?

      doc.replaceSelection(replacement: string, ?select: string) 用给定的字符串替换选择。默认情况下,新选择在插入文本之后结束。可选的 select 参数可用于更改这一点——传递“around”将导致选择新文本,传递“start”会将选择折叠到插入文本的开头。

      【讨论】:

      • 这在 IE9 中可以正常工作吗?我正在使用它 IE9,但是一旦插入文本,我就无法进一步添加任何字符。这是您在开发代码镜像时注意到的吗?
      【解决方案3】:

      在末尾添加新行 -

      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
      }
      

      调用函数

      updateCodeMirror("This is new line");
      

      【讨论】:

      • 如何从@ctrl/ngx-codemirror 库中获取 CodeMirror cm 实例。 git 中不存在文档
      【解决方案4】:

      改进的功能,如果存在选择,则替换文本,如果不存在,则插入当前光标位置

      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);
      
              }
      
          }
      

      【讨论】:

      • 请不要在改进现有答案时添加额外答案。改为编辑第一个
      • 最佳解决方案,处理替换选定文本。
      【解决方案5】:

      您想使用replaceRange 函数。即使名称说“替换”,它也可以用作“插入”,具体取决于参数。从我写这篇文章时的文档来看:

      用给定的替换 from 和 to 之间的文档部分 细绳。 from 和 to 必须是 {line, ch} 对象。可以省略 只需将字符串插入位置 from。当给出原点时,它 将被传递给“改变”事件,它的第一个字母将是 用于确定此更改是否可以与之前的合并 历史事件,以选择起源所描述的方式。

      【讨论】:

        【解决方案6】:

        以高效方式在当前光标位置插入文本的最终功能。 希望能帮助到你。

        function insertStringInTemplate(str)
        {
            var doc = editor_template.getDoc();
            var cursor = doc.getCursor();
        
            var pos = {
                line: cursor.line,
                ch: cursor.ch
            }
        
            doc.replaceRange(str, pos);
        }
        

        【讨论】:

        • 谢谢。效果很好,使用 setCursor 方法有问题
        【解决方案7】:

        该函数用于插入到指定位置,并将光标移动到插入文本的末尾。

          function insertToCodeMirror(text) {
            const doc = codeMirrorInstance.getDoc();
            const cursor = codeMirrorInstance.getCursor();
            doc.replaceRange(text, cursor);
            codeMirrorInstance.focus();
            setTimeout(() => {
              cursor.ch += text.length;
              codeMirrorInstance.setCursor(cursor);
            }, 0);
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-07
          • 1970-01-01
          相关资源
          最近更新 更多