我目前正在将王牌实现为几种编程语言的编辑器。我真的想实现一个美化功能,目前我使用了以下方法:
var beautify = ace.require("ace/ext/beautify"); // get reference to extension
var editor = ace.edit("editor"); // get reference to editor
beautify.beautify(editor.session);遗憾的是,这不能正确地格式化CSS。示例:
.class1 .subClass { color: red; }通过所描述的代码美化此更改为
.class1.subClass{
color:red;
}如您所见,选择器中的所有空格都被移除,这将更改规则的目标。
我的代码错了吗?在ace中是否有用于CSS的另一种增白剂?
作为退步,我将删除功能,这并不是理想的解决方案。
TL;博士
是否有一个能正确美化CSS的ace插件/扩展?我做错了什么吗?
发布于 2016-06-16 08:37:53
我找到了一个很好的css增白剂,我把它添加到了我的解决方案中。
这是魔法:
container.querySelector('.editor_beautify').addEventListener('click', function () {
var currentMode = editor.session.$modeId.substr(editor.session.$modeId.lastIndexOf('/') + 1);
switch (currentMode) {
case modes.css:
util.formatCss(editor, configuration.scriptBase);
break;
default:
util.ensureScript(configuration.scriptBase + 'ext-beautify.js', function () {
var beautify = ace.require("ace/ext/beautify").beautify(editor.session);
});
}
});formatCss: function (editorAce, scriptBase) {
var unformatted = editorAce.getValue();
if (unformatted.trim().length > 0) {
util.ensureScript(scriptBase + 'cssbeautify.js', function () {
editorAce.getSession().setUseWrapMode(false);
var formatted = cssbeautify(unformatted, {
indent: ' ',
openbrace: 'end-of-line',
autosemicolon: true
});
editorAce.setValue(formatted);
editorAce.getSession().setUseWrapMode(true);
});
}
}https://stackoverflow.com/questions/37831801
复制相似问题