我正在为我的项目使用codemirror和emmet。交互完成(CTRL + E)后,emmet缩写的扩展在codemirror编辑器中非常有用。我想更进一步,并通过在JavaScript代码中扩展缩写来创建代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="codemirror/lib/codemirror.css">
<link rel="stylesheet" type="text/css" href="codemirror/lib/codemirror.css">
<script type="text/javascript" src="codemirror/lib/codemirror.js"></script>
<script type="text/javascript" src="codemirror/lib/codemirror.js"></script>
<script type="text/javascript" src="codemirror/mode/xml/xml.js"></script>
<script type="text/javascript" src="codemirror/mode/javascript/javascript.js"></script>
<script type="text/javascript" src="codemirror/mode/css/css.js"></script>
<script type="text/javascript" src="codemirror/mode/htmlmixed/htmlmixed.js"></script>
<script type="text/javascript" src="resource/emmet.js"></script>
</head>
<body>
<div id="editorContainer"></div>
<script type="text/javascript">
var Test = {};
Test.editor = null;
Test.init = function() {
Test.editor = CodeMirror(document.getElementById('editorContainer'), {
autofocus: true,
lineNumbers: true,
mode: "text/html",
profile: 'xhtml', /* define Emmet output profile */
});
emmetCodeMirror(Test.editor);
Test.ol();
}
Test.ol = function() {
Test.editor.getDoc().replaceSelection('ol>li*3', 'end');
Test.editor.focus();
emmetCodeMirror.emmet.run('expand_abbreviation', Test.editor);
};
Test.init();
</script>
</body>
</html>
通过调用Test.ol()
文本ol>li*3
被插入,但以下执行
emmetCodeMirror.emmet.run('expand_abbreviation', Test.editor);
导致 TypeError: editor.getProfileName is not a function emmet.js:41541
有人可以告诉我我在做什么错吗?
您可以在此处下载该项目。
非常感谢!
您正在将CodeMirror
编辑器实例传递给,emmet.run()
而不是EmmetEditor
。无论如何,最终的解决方案是简单地执行注册的Emmet命令,例如
Test.editor.execCommand('emmet.expand_abbreviation')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句