Skip to content

Commit 30a2db6

Browse files
author
Ives van Hoorne
committed
Put VSCode theme in CodeMirror
1 parent 35c29e3 commit 30a2db6

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

packages/app/src/app/components/CodeEditor/CodeMirror/elements.js

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled, { keyframes } from 'styled-components';
1+
import styled, { css, keyframes } from 'styled-components';
22
import theme from 'common/theme';
33

44
const fadeInAnimation = keyframes`
@@ -21,6 +21,33 @@ export const Container = styled.div`
2121
overflow: auto;
2222
`;
2323

24+
const getTokenColor = (scope, defaultStyles) => ({ theme: givenTheme }) => {
25+
if (
26+
!givenTheme ||
27+
!givenTheme.vscodeTheme ||
28+
givenTheme.vscodeTheme.isCodeSandbox
29+
) {
30+
return defaultStyles;
31+
}
32+
33+
const foundScope = givenTheme.vscodeTheme.tokenColors.find(
34+
token => token && token.scope && token.scope.indexOf(scope) === 0
35+
);
36+
37+
if (foundScope && foundScope.settings) {
38+
return css`
39+
${foundScope.settings.foreground &&
40+
`color: ${foundScope.settings.foreground};`}
41+
${foundScope.settings.background &&
42+
`background: ${foundScope.settings.background};`}
43+
${foundScope.settings.fontStyle &&
44+
`font-style: ${foundScope.settings.fontStyle};`}
45+
`;
46+
}
47+
48+
return '';
49+
};
50+
2451
export const CodeContainer = styled.div`
2552
position: relative;
2653
overflow: auto;
@@ -31,60 +58,75 @@ export const CodeContainer = styled.div`
3158
font-family: ${props =>
3259
fontFamilies(props.fontFamily, 'Menlo', 'Source Code Pro', 'monospace')};
3360
line-height: ${props => props.lineHeight};
34-
background: ${theme.background2()};
35-
color: #e0e0e0;
61+
background: ${props =>
62+
props.theme['editor.background'] || theme.background2()};
63+
color: ${props =>
64+
props.theme['editor.foreground'] || props.theme.foreground || '#e0e0e0'};
3665
height: 100%;
3766
font-weight: 500;
67+
68+
/* For retina screens we will not do subpixel anti-aliasing. That looks uglier. */
69+
@media (-webkit-min-device-pixel-ratio: 1.5) {
70+
-webkit-font-smoothing: auto;
71+
}
3872
}
3973
div.CodeMirror-selected {
40-
background: #374140;
74+
background: ${props => props.theme['selection.background'] || '#65737e'};
4175
}
4276
.CodeMirror-line::selection,
4377
.CodeMirror-line > span::selection,
4478
.CodeMirror-line > span > span::selection {
45-
background: #65737e;
79+
background: ${props => props.theme['selection.background'] || '#65737e'};
4680
}
4781
.CodeMirror-line::-moz-selection,
4882
.CodeMirror-line > span::-moz-selection,
4983
.CodeMirror-line > span > span::-moz-selection {
50-
background: #65737e;
84+
background: ${props => props.theme['selection.background'] || '#65737e'};
5185
}
5286
.CodeMirror-gutters {
53-
background: ${theme.background2()};
87+
background: ${props =>
88+
props.theme['editorGutter.background'] || theme.background2()};
5489
border-right: 0px;
5590
}
5691
.CodeMirror-guttermarker {
5792
color: #ac4142;
5893
}
5994
.CodeMirror-guttermarker-subtle {
60-
color: #505050;
95+
color: ${props => props.theme['editorLineNumber.foreground'] || '#505050'};
6196
}
6297
.CodeMirror-linenumber {
63-
color: #505050;
98+
color: ${props => props.theme['editorLineNumber.foreground'] || '#505050'};
6499
}
65100
.CodeMirror-cursor {
66-
border-left: 1px solid #b0b0b0;
101+
border-left: 1px solid
102+
${props => props.theme['editorCursor.foreground'] || '#b0b0b0'};
67103
}
68104
69105
span.cm-comment {
70-
color: #626466;
106+
${getTokenColor('comment', 'color: #626466')};
71107
}
108+
72109
span.cm-atom {
73-
color: #aa759f;
110+
${getTokenColor('constant.numeric', 'color: #aa759f')};
74111
}
112+
75113
span.cm-number {
76-
color: #aa759f;
114+
${getTokenColor('constant.numeric', 'color: #aa759f')};
115+
}
116+
117+
span.cm-property {
118+
${getTokenColor('variable.instance', 'color: #aa759f')};
77119
}
78120
79-
span.cm-property,
80121
span.cm-attribute {
81-
color: #aa759f;
122+
${getTokenColor('entity.other.attribute-name', 'color: #aa759f')};
82123
}
124+
83125
span.cm-keyword {
84-
color: ${theme.secondary()};
126+
${getTokenColor('keyword', `color: ${theme.secondary()}`)};
85127
}
86128
span.cm-string {
87-
color: #99c794;
129+
${getTokenColor('string', 'color: #99c794')};
88130
}
89131
90132
span.cm-variable {
@@ -94,13 +136,13 @@ export const CodeContainer = styled.div`
94136
color: ${theme.secondary()};
95137
}
96138
span.cm-def {
97-
color: #fac863;
139+
${getTokenColor('variable.other.object.property', 'color: #fac863;')};
98140
}
99141
span.cm-bracket {
100-
color: #e0e0e0;
142+
${getTokenColor('meta.brace.round.js', 'color: #e0e0e0')};
101143
}
102144
span.cm-tag {
103-
color: #ec5f67;
145+
${getTokenColor('entity.name.tag', 'color: #ec5f67')};
104146
}
105147
span.cm-link {
106148
color: #aa759f;
@@ -110,9 +152,6 @@ export const CodeContainer = styled.div`
110152
color: #b0b0b0;
111153
}
112154
113-
.CodeMirror-activeline-background {
114-
background: rgba(0, 0, 0, 0.2);
115-
}
116155
.CodeMirror-matchingbracket {
117156
text-decoration: underline;
118157
color: white !important;
@@ -132,7 +171,13 @@ export const CodeContainer = styled.div`
132171
background-color: #561011;
133172
}
134173
174+
.CodeMirror-activeline-background {
175+
background: ${props =>
176+
props.theme['editor.lineHighlightBackground'] || 'rgba(0, 0, 0, 0.3)'};
177+
}
178+
135179
div.cm-line-highlight.CodeMirror-linebackground {
136-
background-color: rgba(0, 0, 0, 0.3);
180+
background: ${props =>
181+
props.theme['editor.lineHighlightBackground'] || 'rgba(0, 0, 0, 0.3)'};
137182
}
138183
`;

packages/app/src/app/components/CodeEditor/CodeMirror/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as React from 'react';
44
import CodeMirror from 'codemirror';
5+
import { withTheme } from 'styled-components';
56

67
import type { ModuleError, Module } from 'common/types';
78
import { getCodeMirror } from 'app/utils/codemirror';
@@ -54,10 +55,18 @@ class CodemirrorEditor extends React.Component<Props, State> implements Editor {
5455
}
5556

5657
shouldComponentUpdate(nextProps: Props) {
57-
return (
58+
if (
5859
this.props.width !== nextProps.width ||
5960
this.props.height !== nextProps.height
60-
);
61+
) {
62+
return true;
63+
}
64+
65+
if (this.props.theme.vscodeTheme !== nextProps.theme.vscodeTheme) {
66+
return true;
67+
}
68+
69+
return false;
6170
}
6271

6372
componentWillUnmount() {
@@ -450,4 +459,4 @@ class CodemirrorEditor extends React.Component<Props, State> implements Editor {
450459
}
451460
}
452461

453-
export default CodemirrorEditor;
462+
export default withTheme(CodemirrorEditor);

packages/common/themes/codesandbox.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "vscode://schemas/color-theme",
3+
"isCodeSandbox": true,
34
"type": "dark",
45
"colors": {
56
"editor.background": "#1C2022",

0 commit comments

Comments
 (0)