在 Codemirror/emmet 中用 javascript 展开缩写
expand abbreviation with javascript in Codemirror/emmet
我正在为我的项目使用 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')
我正在为我的项目使用 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')