ACE 编辑器 - 美化 CSS
ACE Editor - Beautify for CSS
我目前正在实施 ace 作为多种编程语言的编辑器。我真的很想实现美化功能,目前我使用这种方法:
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;
}
如您所见,选择器中的所有空格都已删除,这会更改规则的目标。
我的代码有错吗? CSS 在 ace 中是否有替代美化器?
作为后备,我会删除不是理想解决方案的功能。
TL;DR
有没有可以正确美化CSS的王牌plugin/extension?难道我做错了什么?
嗯,我找到了一个不错的 css beautifier,我将其添加到我的解决方案中。
神奇之处在于:
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);
});
}
}
我目前正在实施 ace 作为多种编程语言的编辑器。我真的很想实现美化功能,目前我使用这种方法:
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;
}
如您所见,选择器中的所有空格都已删除,这会更改规则的目标。
我的代码有错吗? CSS 在 ace 中是否有替代美化器?
作为后备,我会删除不是理想解决方案的功能。
TL;DR
有没有可以正确美化CSS的王牌plugin/extension?难道我做错了什么?
嗯,我找到了一个不错的 css beautifier,我将其添加到我的解决方案中。
神奇之处在于:
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);
});
}
}