Codemirror - minimum lines number

淺唱寂寞╮ 提交于 2019-12-12 14:19:50

问题


Anbody does have an solution for a min lines number - in Codemirror?

min-height worked for me but do not insert empty lines for the height.

JS

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    gutter: true,
    lineWrapping: true
});

CSS

.CodeMirror-scroll {
  overflow: auto;
  height: auto; overflow: visible;
  position: relative;
  outline: none;
  min-height: 300px; /* the minimum height */
}

Maybe there is a simple solution to insert empty lines for that ?


回答1:


remove the min-height: 300px; and initialize the editor with new lines as the starting value:

var minLines = 3;
var startingValue = '';
for (var i = 0; i < minLines; i++) {
    startingValue += '\n';
}

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    gutter: true,
    lineWrapping: true,
    value: startingValue
});

currently, CodeMirror's value option does not seem to have an affect for up to version 2.21. this can be easily bypassed by using setValue() after initialization:

///...
// initialize as before, omitting the value option

editor.setValue(startingValue);

note: make sure not to set autoClearEmptyLines: true as it will clash and cancel out the inserted empty lines.




回答2:


so guys I got the solution. on any reason the editor does not get the value from the configuration options so i set the value after it. @Eliran thanks, i used your method to set the value.

editor.setValue(startingValue);

DEMO

http://jsfiddle.net/vujLv/1/




回答3:


editor.setValue( value + "\n".repeat(10) )


来源:https://stackoverflow.com/questions/10380759/codemirror-minimum-lines-number

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