CodeMirror : how to limit height in editor

ぃ、小莉子 提交于 2020-01-04 02:41:09

问题


I am using codemirror to show some code on a webpage. But when I initialize the codemirror editor, the height of the editor is way more than the number of lines in the code. Please see this fiddle or below image to understand what I am saying

Below is the code to create the codemirror editor.
var myCodeArea = document.getElementById("codeArea");
var myCodeMirror = CodeMirror(function(elt) {
  myCodeArea.parentNode.replaceChild(elt, myCodeArea);
}, {value: myCodeArea.innerHTML,
   lineNumbers:true, mode:"javascript"});

I read codemirror doc but not able to figure out which property controls the height.

Please help me with this


回答1:


The height can be set through CSS (by giving the .CodeMirror class a height property), or by calling the editor's setSize method.

If you want the editor height to correspond to the height of it's contents, see this demo.




回答2:


If you see your fiddle, there is an entry in css which defines height.

.CodeMirror {


 /* Set height, width, borders, and global font properties here */
    font-family: monospace;
    height: 300px;
}

You can either override this css or use setSize(width,height) method of codemirror.




回答3:


Might help someone.

<textarea class="form-control" id="exampleTextarea"></textarea>

.

var config, editor;

            config = {                  
                lineNumbers: true,
                mode: "text/javascript",
                lineWrapping: true,
                htmlMode: true,
                matchClosing: true,       
                indentWithTabs: true,
                readOnly: true
            };


            editor = CodeMirror.fromTextArea(document.getElementById("exampleTextarea"), config);
            editor.setSize(900,"100%");


来源:https://stackoverflow.com/questions/28378229/codemirror-how-to-limit-height-in-editor

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