Codemirror-最小行数

问题描述 投票:3回答:3

有人对Codemirror中的最小行号有解决方案吗?

最小高度对我有用,但请勿在高度上插入空行。

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 */
}

也许有一个简单的解决方案为此插入空行?

javascript css codemirror
3个回答
6
投票

删除min-height: 300px;,并以新行作为起始值初始化编辑器:

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()轻松地绕开它:

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

editor.setValue(startingValue);

note:确保不要设置autoClearEmptyLines: true,因为它会发生冲突并取消插入的空行。


2
投票

所以我得到了解决方案。出于任何原因,编辑器均无法从配置选项中获取value,因此我将其设置为该值。 @Eliran谢谢,我使用您的方法来设置值。

editor.setValue(startingValue);

DEMO

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


0
投票
editor.setValue( value + "\n".repeat(10) )
© www.soinside.com 2019 - 2024. All rights reserved.