当我启动 CodeMirror 时
var jsEditor = CodeMirror.fromTextArea(document.getElementById('js'),
{
lineNumbers: true,
mode: 'javascript',
theme: 'material',
height: 'auto',
viewportMargin: 'Infinity'
});
默认为 10 行。我希望它以 2 行开头
最佳答案
您可以在 .CodeMirror
上使用 height: fit-content !important
或 height: auto !important
。您必须使用“!important”来覆盖 codemirror 的样式。
如果你想从 View 中的一定数量的行开始,你可以设置一个变量 minLines
并像这样使用它:
// on initialization
var minLines = 10;
editor.focus();
// Set the cursor at the end of existing content
editor.setCursor(editor.lineCount());
var lineCount = editor.lineCount(); // current number of lines
var n = editor.options.minLines - lineCount; // how many lines we need
var line = editor.getCursor().line;
var ch = editor.getCursor().ch;
for(i = 0; i < n; i++) {
editor.replaceRange("\n", { line });
line++;
}
或者,如果您的意思是实际开始编辑器,您可以在编辑器选项中使用 firstLineNumber
,如下所示:
var jsEditor = CodeMirror.fromTextArea(document.getElementById('js'),
{
lineNumbers: true,
firstLineNumber: 10,
mode: 'javascript',
theme: 'material',
height: 'auto',
viewportMargin: 'Infinity'
});
关于javascript - 如何使 CodeMirror 从一定行数开始,并扩展到用户输入的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56845924/