admin管理员组文章数量:1325761
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.
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.
Share Improve this question asked Apr 21, 2013 at 8:34 user414873user414873 7838 silver badges20 bronze badges 1- Did you try other key events to make sure it is just this event that is not working? Checkout this post, maybe this helps you stackoverflow./questions/5902683/… – YvesR Commented Apr 21, 2013 at 11:20
2 Answers
Reset to default 5Add a line break at the end of onNewLine function. This should work
function onNewLine(e){
console.log(e);
editor.replaceSelection("\n" ,"end");
}
I found that returning CodeMirror.Pass also works:
function onNewLine(e) {
console.log("Enter key was pressed!");
return CodeMirror.Pass;
}
From the documentation:
A key handler function may return CodeMirror.Pass to indicate that it has decided not to handle the key, and other handlers (or the default behavior) should be given a turn.
This seems to work even if the handler does perform an action. In my case I was using the editor.indentLine function to indent the current line when the user pressed the enter key.
本文标签: javascriptCodeMirror Catching Enter Key prevents line breaksStack Overflow
版权声明:本文标题:javascript - CodeMirror: Catching Enter Key prevents line breaks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191525a2430303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论