Javascript 如何在ng2 codemirror中定义自定义模式?
我想定义扩展一种可用模式的自定义模式(例如javascript)?我尝试使用下面的代码,但没有效果Javascript 如何在ng2 codemirror中定义自定义模式?,javascript,angular,typescript,codemirror,codemirror-modes,Javascript,Angular,Typescript,Codemirror,Codemirror Modes,我想定义扩展一种可用模式的自定义模式(例如javascript)?我尝试使用下面的代码,但没有效果 CodeMirror.defineMode("foo", function (config, parserConfig) { var fooOverlay = { token: function (stream, state) { var ch; if (stream.match("{{"))
CodeMirror.defineMode("foo", function (config, parserConfig) {
var fooOverlay = {
token: function (stream, state) {
var ch;
if (stream.match("{{")) {
while ((ch = stream.next()) != null)
if (ch == "}" && stream.next() == "}") {
stream.eat("}");
return "foo";
}
}
while (stream.next() != null && !stream.match("{{", false)) { }
return null;
}
};
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "javascript"), fooOverlay);
});
this.config = {
lineNumbers: true,
mode: 'foo'
};
更新:
CodeMirror.defineSimpleMode("foo", {
start = [
{ regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string" },
{ regex: /'(?:[^\\]|\\.)*?(?:'|$)/, token: "string" },
{
regex: /(?:if|foreach|in|while)\b/,
token: "keyword"
},
{ regex: /true|false|null|undefined/, token: "atom" },
];
});
你能弄明白吗?你解决了吗(对不起,伙计们,久违了。最后,我按照上面更新部分所述的方法解决了这个问题。