javascript - CodeMirror: How to read editor text before or after cursor position - Stack Overflow

I'm trying to find a way to test if the cursor is preceded by a specific string and then trigger a

I'm trying to find a way to test if the cursor is preceded by a specific string and then trigger an event.

Example of what I'm trying to do: User clicks somewhere inside the editor, a cursorActivity (cursor or editor changed) event is fired, I catch the event and test whether or not the previous 6 characters matches the string 'color:' If so then do I something.

I can't seem to find any kind of method that lets you actually read directly from the editor though, aside from catching the readInput event which is triggered every time a character is typed or a string is pasted. This works to a point but fails when a user moves the cursor with a mouse click.

TL;DR How can I detect when the cursor has moved somewhere immediately after a specific string?

I'm trying to find a way to test if the cursor is preceded by a specific string and then trigger an event.

Example of what I'm trying to do: User clicks somewhere inside the editor, a cursorActivity (cursor or editor changed) event is fired, I catch the event and test whether or not the previous 6 characters matches the string 'color:' If so then do I something.

I can't seem to find any kind of method that lets you actually read directly from the editor though, aside from catching the readInput event which is triggered every time a character is typed or a string is pasted. This works to a point but fails when a user moves the cursor with a mouse click.

TL;DR How can I detect when the cursor has moved somewhere immediately after a specific string?

Share Improve this question edited Sep 17, 2015 at 3:37 Deftwun asked Sep 17, 2015 at 3:23 DeftwunDeftwun 1,23213 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Ok finally found a solution. You can retrieve the actual document using editor.doc which lets you get the cursor line & character position. Then you can retrieve the desired line with editor.doc.getLine(n) and pare your substring

Here's my test case:

<!-- Create a simple CodeMirror instance -->
<link rel="stylesheet" href="lib/codemirror.css">
<script src="lib/codemirror.js"></script>
<textarea id="myTextarea"></textarea>
<script>

  var editor = CodeMirror.fromTextArea(myTextarea, {
    lineNumbers: true
  });

  //Catch cursor change event
  editor.on('cursorActivity',function(e){
    var line = e.doc.getCursor().line,   //Cursor line
        ch = e.doc.getCursor().ch,       //Cursor character
        stringToMatch = "color:",
        n = stringToMatch.length,
        stringToTest = e.doc.getLine(line).substr(Math.max(ch - n,0),n);

    if (stringToTest == stringToMatch) console.log("SUCCESS!!!");
  });

</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744112491a4559013.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信