codemirror:搜索并突出显示多词而不显示对话框 [英] codemirror: search and highlight multipule words without dialog

查看:62
本文介绍了codemirror:搜索并突出显示多词而不显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标
我正在使用 codemirror 作为编辑。我想


  1. 搜索并突出显示多个字符串

  2. 我希望能够迭代每个数字找到匹配项并打印其行号。

  3. 我想以编程方式进行操作,并且不希望使用示例 https://codemirror.net/demo/search.html

  1. Search and highlight multiple strings
  2. I want to be able to iterate each number of matches found and print its line number.
  3. I want to do it programatically and do no want to use dialog as in example https://codemirror.net/demo/search.html

问题:


  1. 在while循环中,仅选择了最后一个匹配项,清除了前一个匹配项,但是我也希望它呈黄色突出显示,例如 https://codemirror.net/demo/search.html

  1. during while loop only last match is selected, previous ones are cleared , but i also want it hightlighted yellow like https://codemirror.net/demo/search.html

JSFIDDLE: https://jsfiddle.net/bababalcksheep/p7xg1utn/30/

代码:

$(document).ready(function() {
  //
  var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    mode: "text/html",
    lineNumbers: true,
  });
  //
  function search(val) {
    var cursor = editor.getSearchCursor(val);
    while (cursor.findNext()) {
      editor.setSelection(cursor.from(), cursor.to());
        console.log('found at line ', cursor.pos.from.line + 1);
    }
  }
  //
  $('#search').click(function(event) {
    event.preventDefault();
    search(/^alpha|^beta/);
  });

  //
});


推荐答案

调用 setSelection 一次只能突出显示一个连续的子字符串。相反,您可以使用 markText 方法,并传入 cursor.from() cursor.to()获取要突出显示的位置:

Calling setSelection can only highlight one continuous substring at a time. Instead, you can use the markText method for this, passing in cursor.from() and cursor.to() to get the locations you want to highlight:

$(document).ready(function() {
  var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    mode: "text/html",
    lineNumbers: true,
  });
  function search(val) {
    var cursor = editor.getSearchCursor(val);
    while (cursor.findNext()) {
        editor.markText(
          cursor.from(),
          cursor.to(),
          { className: 'highlight' }
        );
    }
  }
  //
  $('#search').click(function(event) {
    event.preventDefault();
    search(/^alpha|^beta/);
  });
});

.CodeMirror {
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  height: 200px;
}
.highlight {
  color: orange;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/addon/search/searchcursor.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.css">

<div class="container">
  <p><strong>Objective:</strong></p>
  <p>Find/search and highlight both words <strong>alpha</strong> and <strong>beta</strong> in codemirror editor</p>
  <button id="search" type="button" class="btn btn-primary">Search and highlight</button>
  <br><br>
  <textarea id="code" name="code" rows="8">Text line
alpha 1
Text line
Text line
alpha 2
Text line
Text line
beta 1
Text line</textarea>

这篇关于codemirror:搜索并突出显示多词而不显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆