使用 CodeMirror 搜索功能滚动显示找到的字符串

Using CodeMirror search function scrolling to show the string found

在这个 jsFiddle 中,我有一个包含三行的 CodeMirror 编辑器。我正在尝试通过单击按钮在第三行查找字符串并定位突出显示的结果来实现 search/searchcursor 插件。

单击搜索按钮时,光标应位于 ccc 一词(编辑器视图外的第三行,因此 CodeMirror 应滚动到该位置并使第三行可见)。

我收到错误 Cannot read property 'line' of undefined 即使我添加了插件。任何想法如何使这项工作?

<button type=button onclick="search()">Search</button>
<textarea rows="4" cols="30" id="cm" name="cm">aaa 
bbb 
ccc</textarea>


var editor = CodeMirror.fromTextArea(document.getElementById('cm'), {
    lineNumbers: true
});  

editor.setSize(null,30);

function search() {
  var text = 'ccc';
  var cursor = editor.getSearchCursor(text, editor.getCursor());
  editor.setSelection(cursor.from(), cursor.to());
}

尝试加载相应的 search.js 文件

https://codemirror.net/addon/search/search.js

https://codemirror.net/demo/search.html