Skip to content

Commit 3f567d9

Browse files
author
Ives van Hoorne
committed
Allow prettifying of css
1 parent 2b94e66 commit 3f567d9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/app/components/sandbox/CodeEditor/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ export default class CodeEditor extends React.PureComponent {
288288
const { id, title, preferences } = this.props;
289289
const code = this.getCode();
290290
const mode = await this.getMode(title);
291-
if (mode === 'jsx') {
291+
if (mode === 'jsx' || mode === 'css') {
292292
try {
293-
const newCode = await prettify(code, preferences.lintEnabled);
293+
const newCode = await prettify(code, mode, preferences.lintEnabled);
294294

295295
if (newCode !== code) {
296296
this.props.changeCode(id, newCode);
@@ -323,7 +323,7 @@ export default class CodeEditor extends React.PureComponent {
323323
addons: addon,
324324
syntax: newMode,
325325
});
326-
}
326+
};
327327

328328
codemirror: typeof CodeMirror;
329329
server: typeof CodeMirror.TernServer;

src/app/utils/codemirror/prettify.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
export default (async function prettify(code, eslintEnabled) {
1+
function getParser(mode) {
2+
if (mode === 'jsx') return 'babylon';
3+
if (mode === 'css') return 'postcss';
4+
5+
return 'babylon';
6+
}
7+
8+
export default (async function prettify(code, mode, eslintEnabled) {
29
const prettier = await System.import('prettier');
310
const prettifiedCode = prettier.format(code, {
411
singleQuote: true,
12+
parser: getParser(mode),
513
trailingComma: 'all',
614
});
715

8-
if (eslintEnabled) {
16+
if (eslintEnabled && mode === 'jsx') {
917
const { fix } = await System.import('app/utils/codemirror/eslint-lint');
1018
try {
1119
const lintedCode = fix(prettifiedCode).output;

0 commit comments

Comments
 (0)