user414873 user414873 - 29 days ago 22
Javascript Question

CodeMirror: Catching Enter Key prevents line breaks

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.

Answer

Add a line break at the end of onNewLine function. This should work

 function onNewLine(e){
    console.log(e);
    editor.replaceSelection("\n" ,"end");
  }
Comments