问题
Is there any way to detect a newline in codemirror editor, either when user hits enter or a line of code wraps?
p.s: in the screenshot attached 3 new lines are created by user hitting enter key (228, 229, 230), and one line (between 229 and 300) is created because of line wrapping.
screenshot here: http://s9.postimage.org/gsroinedp/Screen_Shot_2012_11_19_at_11_30_09_PM.png
回答1:
Catching the enter key is built into the api.
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
lineWrapping:'true',
extraKeys:{
Enter: function(){
alert('enter pressed');
}
}
});
I'm not currently aware of any api that allows you to capture a 'wrap' event.
You could get the CodeMirror-scrollbar-inner height, and if it size increases, and it's not an onpaste event you know the line wrapped or enter was pressed :)
来源:https://stackoverflow.com/questions/13469605/detecting-new-line-in-codemirror