/**
risk:['demo'] //自定义关键词数组
eachLine //codeMirror遍历行
cmOptions: { codeMirror options
lineNumbers: true,
matchBrackets: true,
mode: this.scriptType,
tabSize: 4,
line: true,
styleSelectedText: true,
},
content:'document.getElementById("demo").innerHTML=Date();'
**/
this.editor = CodeMirror.fromTextArea(this.$refs.mycode, this.cmOptions);
this.editor.setValue(this.content);
this.editor.eachLine((line) => {
const risk = this.risk.includes(line.text);
if (risk) {
const getLine = this.editor.lineInfo(line).line;// 获取行号
const getStart = line.text.indexOf() + 1; // 获取起始位置
const getEnd = getStart + line.text.length; // 获取结尾位置
this.editor.markText({ line: getLine, ch: getStart }, { line: getLine, ch: getEnd }, { className: 'styled-background' });
}
});
CodeMirror自定义关键词添加class
最新推荐文章于 2024-04-11 17:57:55 发布