【发布时间】:2015-04-19 19:30:46
【问题描述】:
我正在尝试编写自己的 CodeMirror 模式,如 here 所述。
我的目标是更改特定关键字的颜色。例如,任何“aaa”字都需要是红色的,任何“bbb”字都需要是蓝色的。任何其他单词都需要具有默认颜色。
这是我失败的尝试 (see jsfiddle)。如何使这项工作?
HTML:
<textarea rows="4" cols="30" id="cm" name="cm">aaa bbb ccc</textarea>
CSS:
.style1 { color: red; }
.style2 { color: blue; }
Javascript:
CodeMirror.defineMode("mymode", function() {
return {
token: function(stream,state) {
if (stream.match("aaa") ) {
console.log("aaa found");
while ((ch = stream.next()) != null)
if (ch == " " && stream.next() == " ") break;
return "style1";
}
else if (stream.match("bbb") ) {
console.log("bbb found");
while ((ch = stream.next()) != null)
if (ch == " " && stream.next() == " ") break;
return "style2";
}
else
return null;
}
};
});
var editor = CodeMirror.fromTextArea(document.getElementById('cm'), {
mode: "mymode",
lineNumbers: true
});
【问题讨论】:
标签: codemirror