Skip to content

Commit d0bd886

Browse files
author
Ives van Hoorne
committed
Fix color parsing
1 parent e1dd699 commit d0bd886

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

packages/app/src/app/components/CodeEditor/Monaco/define-theme.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import Color from 'color';
2+
13
const sanitizeColor = color => {
2-
if (color === 'white') {
3-
return '#ffffff';
4+
if (!color) {
5+
return color;
6+
}
7+
8+
if (/#......$/.test(color) || /#........$/.test(color)) {
9+
return color;
410
}
511

6-
return color;
12+
try {
13+
return new Color(color).hexString();
14+
} catch (e) {
15+
return '#FF0000';
16+
}
717
};
818

919
const colorsAllowed = ({ foreground, background }) => {
@@ -22,7 +32,7 @@ const getTheme = theme => {
2232
const settings = {
2333
foreground: sanitizeColor(token.settings.foreground),
2434
background: sanitizeColor(token.settings.background),
25-
fontStyle: sanitizeColor(token.settings.fontStyle),
35+
fontStyle: token.settings.fontStyle,
2636
};
2737

2838
const scope =
@@ -72,14 +82,24 @@ const defineTheme = (monaco, theme) => {
7282
if (theme) {
7383
const transformedTheme = getTheme(theme);
7484

75-
monaco.editor.defineTheme('CodeSandbox', {
76-
base: getBase(transformedTheme.type),
77-
inherit: true,
78-
colors: transformedTheme.colors,
79-
rules: transformedTheme.rules,
80-
});
81-
82-
monaco.editor.setTheme('CodeSandbox');
85+
try {
86+
monaco.editor.defineTheme('CodeSandbox', {
87+
base: getBase(transformedTheme.type),
88+
inherit: true,
89+
colors: transformedTheme.colors,
90+
rules: transformedTheme.rules,
91+
});
92+
93+
monaco.editor.setTheme('CodeSandbox');
94+
} catch (e) {
95+
console.error(e);
96+
if (window.showNotification) {
97+
window.showNotification(
98+
`Problem initializing template in editor: ${e.message}`,
99+
'error'
100+
);
101+
}
102+
}
83103
}
84104
};
85105

0 commit comments

Comments
 (0)