Skip to content

Commit ae2678f

Browse files
lbogdanCompuIves
authored andcommitted
Theme fixes & added 2 new themes (codesandbox#972)
Theme fixes: - ~~use [strip-json-comments](https://www.npmjs.com/package/strip-json-comments) to... well, strip JSON comments 😃~~ - also strip comments when loading theme from an URL Themes added: - Solarized Light - Cobalt 2
1 parent 7ac1106 commit ae2678f

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

packages/app/src/app/pages/Sandbox/Editor/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ class ContentSplit extends React.Component {
4343
const newThemeName = this.props.store.preferences.settings.editorTheme;
4444
const customVSCodeTheme = this.props.store.preferences.settings
4545
.customVSCodeTheme;
46-
const theme = await getVSCodeTheme(newThemeName, customVSCodeTheme);
4746

48-
this.setState({ theme, editorTheme: newThemeName, customVSCodeTheme });
47+
try {
48+
const theme = await getVSCodeTheme(newThemeName, customVSCodeTheme);
49+
this.setState({ theme, editorTheme: newThemeName, customVSCodeTheme });
50+
} catch (e) {
51+
console.error(e);
52+
}
4953
};
5054

5155
render() {

packages/app/src/app/pages/Sandbox/Editor/utils/get-vscode-theme.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const vsDark = {
3030
[editorSelectionHighlight]: '#ADD6FF26',
3131
};
3232

33+
// parses theme, uncommenting commented colors
34+
// and using json5 to strip comments
35+
function parseTheme(theme) {
36+
return JSON.parse(theme.replace('/^s*//"', '"'));
37+
}
38+
3339
function fetchTheme(foundTheme) {
3440
if (!foundTheme) {
3541
return codesandbox;
@@ -43,13 +49,31 @@ function fetchTheme(foundTheme) {
4349
return foundTheme.get();
4450
}
4551

46-
return window.fetch(foundTheme.url).then(x => x.json());
52+
return window
53+
.fetch(foundTheme.url)
54+
.then(x => x.text())
55+
.then(text => {
56+
let theme;
57+
try {
58+
theme = parseTheme(text);
59+
} catch (e) {
60+
console.error(e);
61+
62+
if (window.showNotification) {
63+
window.showNotification(
64+
'We had trouble loading the theme, error: \n' + e.message,
65+
'error'
66+
);
67+
}
68+
}
69+
return theme;
70+
});
4771
}
4872

4973
const findTheme = async (themeName, customTheme) => {
5074
if (customTheme) {
5175
try {
52-
return JSON.parse(customTheme.replace(/^\s*\/\/"/gm, ''));
76+
return parseTheme(customTheme);
5377
} catch (e) {
5478
console.error(e);
5579

packages/common/themes/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export default [
3030
id: 'highContrast',
3131
get: () => import('./high-contrast'),
3232
},
33+
{
34+
name: 'Cobalt 2',
35+
id: 'cobalt2',
36+
url:
37+
'https://cdn.rawgit.com/wesbos/cobalt2-vscode/master/theme/cobalt2.json',
38+
},
3339
{
3440
name: 'VSCode Light',
3541
id: 'vscodeLight',
@@ -41,4 +47,10 @@ export default [
4147
url:
4248
'https://cdn.rawgit.com/akamud/vscode-theme-onelight/master/themes/OneLight.json',
4349
},
50+
{
51+
name: 'Solarized Light',
52+
id: 'solarizedLight',
53+
url:
54+
'https://raw.githubusercontent.com/Microsoft/vscode/d2b6bbb46fbdf535e2c96b3e00ac56ac1d427a69/extensions/theme-solarized-light/themes/solarized-light-color-theme.json',
55+
},
4456
];

0 commit comments

Comments
 (0)