How do I format HTML code in Code Mirror when the

2019-04-09 21:43发布

问题:

I am using the Code Mirror plugin to edit HTML source of a page. The HTML code is fetched from database and set as value in Code Mirror. But, after setting the value it is displayed in the same format in which it was saved in the database. How can I display it in proper format? Thanks in advance.

回答1:

There is a function called autoFormatRange

editor.autoFormatRange(range.from, range.to);

This fragment from the CodeMirror Group might be what you need:

function autoFormat() {
    var totalLines = editor.lineCount();
    var totalChars = editor.getTextArea().value.length;
    editor.autoFormatRange({line:0, ch:0}, {line:totalLines, ch:totalChars});
}

http://codemirror.net/2/demo/formatting.html



标签: codemirror