本文介绍了Codemirror-最小行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人确实有最小行数的解决方案-在中?

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

min-height对我有用,但请勿在高度上插入空行。

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 ?

推荐答案

删除最小高度:300px; 并以新行作为初始值初始化编辑器:

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
});

当前,似乎没有CodeMirror的 value 选项对版本2.21有影响。通过初始化后使用 setValue()可以轻松地绕过此操作:

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

注意:
确保不要设置 autoClearEmptyLines:true ,因为它将冲突并取消插入的空行。

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

这篇关于Codemirror-最小行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:27