CodeMirror:如何获取当前光标的段落文本

时间:2016-09-29 00:32:32

标签: javascript jquery textarea codemirror

CodeMirror具有editor.getSelection(),它将获得突出显示的文本。我想知道是否有可能在2个换行符之间获取当前光标的所有文本(适用于linux / mac / windows)

This text is not selected because cursor is in next text block

Start point of selected text ....
Because cursor is here | .....
Rest of text also selected

This text is not selected ...

好的解决了,因为我现在有一个版本正在运行:

$('#test').on('click', function() {
    var cur = editor.getCursor().line;
    var arr = editor.getValue().split("\n");
    var txt = '', start = 0, stop = 0;
    for (i = 0; i < arr.length; i++) {
        t = arr[i].trim();
        len = t.length;
        if (!start) {
            if (!len) txt = '';
            if (i == cur) start = 1;
        }
        if (!stop) {
            if (start && !len) stop = 1;
            else if (len) txt += t + "\n";
        }
    }
    alert(txt);
})

如果您有更好的解决方案,请在下面回答

0 个答案:

没有答案