codemirror代码格式化
上效果
代码
此处演⽰纯js版本,⽽切实5.x版本,最新版本6.x变化较⼤,不搞
<!DOCTYPE html>
<html lang="en">
<head>
浏览器json格式化
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="cdn.bootcdn/ajax/libs/codemirror/5.62.2/codemirror.min.css" rel="stylesheet">
<link href="cdn.bootcdn/ajax/libs/codemirror/5.62.2/theme/night.min.css" rel="stylesheet">
</head>
<body>
<textarea id="abc">
{"job_name":"西藏域名监控","metrics_path":"/probe","params":{"module":["http_2xx"]},"file_sd_configs":[{"files":["targets/probes/xizang.json"],"refresh_interval":"6m"}],"relabel_configs":[{"source_labels":["__address__"],"target_label":"__para    </textarea>
<script src="cdn.bootcdn/ajax/libs/codemirror/5.62.2/codemirror.min.js"></script>
<script src="cdn.bootcdn/ajax/libs/codemirror/5.62.2/mode/javascript/javascript.min.js"></script>
<script>
//  格式化插件
commentStart: "/*",
commentEnd: "*/",
newlineAfterToken: function (type, content) {
return /^[;{}]$/.test(content);
}
});
commentStart: "/*",
commentEnd: "*/",
// FIXME semicolons inside of for
newlineAfterToken: function (type, content, textAfter, state) {
if (this.jsonMode) {
return /^[\[,{]$/.test(content) || /^}/.test(textAfter);
} else {
if (content == ";" && state.lexical && pe == ")") return false;
return /^[;{}]$/.test(content) && !/^;/.test(textAfter);
}
}
});
commentStart: "<!--",
commentEnd: "-->",
newlineAfterToken: function (type, content, textAfter) {
return type == "tag" && />$/.test(content) || /^</.test(textAfter);
}
});
// Comment/uncomment the specified range
CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
let cm = this, curMode = CodeMirror.Mode(), cm.getTokenAt(from).state).mode;
cm.operation(function () {
if (isComment) { // Comment range
if (from.line == to.line && from.ch == to.ch) // An empty comment inserted - put cursor inside
cm.setCursor(from.line, from.ch + curModementStart.length);
} else { // Uncomment range
let selText = cm.getRange(from, to);
let startIndex = selText.indexOf(curModementStart);
let endIndex = selText.lastIndexOf(curModementEnd);
if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
// Take string till comment start
selText = selText.substr(0, startIndex)
// From comment start till comment end
+ selText.substring(startIndex + curModementStart.length, endIndex)
/
/ From comment end till string end
+ selText.substr(endIndex + curModementEnd.length);
}
}
});
});
// Applies automatic mode-aware indentation to the specified range
CodeMirror.defineExtension("autoIndentRange", function (from, to) {
let cmInstance = this;
this.operation(function () {
for (let i = from.line; i <= to.line; i++) {
cmInstance.indentLine(i, "smart");
}
});
});
// Applies automatic formatting to the specified range
CodeMirror.defineExtension("autoFormatRange", function (from, to) {
let cm = this;
let outer = cm.getMode(), text = cm.getRange(from, to).split("\n");
let state = pyState(outer, cm.getTokenAt(from).state);
let tabSize = cm.getOption("tabSize");
let out = "", lines = 0, atSol = from.ch == 0;
function newline() {
out += "\n";
atSol = true;
++lines;
}
for (let i = 0; i < text.length; ++i) {
let stream = new CodeMirror.StringStream(text[i], tabSize);
while (!l()) {
let inner = CodeMirror.innerMode(outer, state);
let style = ken(stream, state), cur = stream.current();
stream.start = stream.pos;
if (!atSol || /\S/.test(cur)) {
out += cur;
atSol = false;
}
if (!atSol && wlineAfterToken &&
}
if (!stream.pos && outer.blankLine) outer.blankLine(state);
if (!atSol) newline();
}
cm.operation(function () {
for (let cur = from.line + 1, end = from.line + lines; cur <= end; ++cur)
cm.indentLine(cur, "smart");
cm.setSelection(from, cm.getCursor(false));
});
});
// 使⽤
let editor = CodeMirror.ElementById('abc'), {
lineNumbers: true,
mode: "application/json",
theme: 'night'
});
let totalLines = editor.lineCount();
editor.autoFormatRange({ line: 0, ch: 0 }, { line: totalLines });
</script>
</body>
</html>
react版
参考,写这篇⽂章的时候,有⽤到

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。