【问题标题】:codemirror - get the current word under the cursorcodemirror - 获取光标下的当前单词
【发布时间】:2014-10-26 17:50:06
【问题描述】:

有没有办法获取光标下的当前文本? 我不是指整行的文本,而只是一个单词.. 例如,如果光标实际上是在单词“orange”的中间-->ora|nge, 我需要得到“橙色”这个词。

非常感谢任何解决此问题的方法,在此先感谢

【问题讨论】:

    标签: codemirror text-cursor


    【解决方案1】:

    感谢 Marijn 的帮助,findWordAt 正是我想要的:

    editor.on('cursorActivity', function() {
        var A1 = editor.getCursor().line;
        var A2 = editor.getCursor().ch;
    
        var B1 = editor.findWordAt({line: A1, ch: A2}).anchor.ch;
        var B2 = editor.findWordAt({line: A1, ch: A2}).head.ch;
    
        console.log(editor.getRange({line: A1,ch: B1}, {line: A1,ch: B2}));
    });
    

    【讨论】:

    • var word = editor.findWordAt(editor.getCursor()); editor.getRange(word.anchor, word.head);var word = editor.findWordAt(editor.getCursor()); editor.getRange(word.anchor, word.head); 是一种奇怪冗长的说法
    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多