3

I used the extraKeys-option of CodeMirror 3.12 to detect when the user starts a new line:

extraKeys: {
    "Enter": onNewLine
}

onNewLine() does nothing but a console.log(). Now CodeMirror ignores that key. You can't start a new line anymore. Is there a way to hook up additional functionality on a new-line-event without interfering CodeMirror internals? I just want to analyze the text of the recently closed line.

4

2 回答 2

4

在 onNewLine 函数的末尾添加换行符。这应该工作

 function onNewLine(e){
    console.log(e);
    editor.replaceSelection("\n" ,"end");
  }
于 2013-04-21T12:02:06.013 回答
2

我发现返回 CodeMirror.Pass 也可以:

function onNewLine(e) {
    console.log("Enter key was pressed!");
    return CodeMirror.Pass;
}

从文档中:

密钥处理程序函数可能返回 CodeMirror.Pass 以指示它已决定不处理密钥,并且应该轮到其他处理程序(或默认行为)。

即使处理程序确实执行了操作,这似乎也有效。在我的例子中,当用户按下回车键时,我使用 editor.indentLine 函数来缩进当前行。

于 2016-11-04T14:31:39.917 回答