Detecting new line in codemirror

故事扮演 提交于 2020-01-14 03:39:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!