我正在尝试编写自己的CodeMirror模式,如此处所述.
我的目标是改变特定关键字的颜色.例如,任何"aaa"单词都需要为红色,任何"bbb"单词都需要为蓝色.任何其他单词都需要具有默认颜色.
这是我不成功的尝试(见jsfiddle).如何使这项工作?
HTML:
<textarea rows="4" cols="30" id="cm" name="cm">aaa bbb ccc</textarea>
Run Code Online (Sandbox Code Playgroud)
CSS:
.style1 { color: red; }
.style2 { color: blue; }
Run Code Online (Sandbox Code Playgroud)
使用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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2733 次 |
| 最近记录: |