soroush ghorashi
soroush ghorashi

Reputation: 31

Detecting new line in codemirror

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

Upvotes: 1

Views: 1997

Answers (1)

gkiely
gkiely

Reputation: 3007

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 :)

Upvotes: 3

Related Questions