Skip to content

Commit 626c74b

Browse files
author
Ives van Hoorne
committed
Change of philosophy: we now support other frameworks beside React
1 parent 4d70ce6 commit 626c74b

File tree

19 files changed

+444
-212
lines changed

19 files changed

+444
-212
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"react-deep-force-update": "^2.0.1",
7979
"react-dnd": "^2.1.4",
8080
"react-dnd-html5-backend": "^2.1.2",
81+
"react-docgen": "^2.13.0",
8182
"react-dom": "^15.4.1",
8283
"react-frame-component": "^1.0.2",
8384
"react-icons": "^2.2.2",

src/app/pages/SandboxView/Sandbox/Editor/Content/subviews/CodeEditor/index.js

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
// @flow
22
import React from 'react';
33
import CodeMirror from 'codemirror';
4-
import styled, { injectGlobal, keyframes } from 'styled-components';
5-
6-
import 'codemirror/lib/codemirror.css';
7-
import 'codemirror/addon/dialog/dialog.css';
8-
import 'codemirror/addon/hint/show-hint.css';
9-
import 'codemirror/addon/tern/tern.css';
10-
import 'codemirror/mode/jsx/jsx';
11-
import 'codemirror/keymap/sublime';
12-
import 'codemirror/addon/fold/xml-fold'; // Needed to match JSX
13-
import 'codemirror/addon/edit/matchtags';
14-
import 'codemirror/addon/edit/closebrackets';
15-
import 'codemirror/addon/comment/comment';
16-
import 'codemirror/addon/selection/active-line';
17-
import 'codemirror/addon/fold/foldcode';
18-
import 'codemirror/addon/fold/foldgutter';
19-
import 'codemirror/addon/fold/brace-fold';
4+
import styled from 'styled-components';
5+
6+
import { getCodeMirror } from 'app/utils/codemirror';
7+
208
import 'codemirror/addon/dialog/dialog';
219
import 'codemirror/addon/hint/show-hint';
2210
import 'codemirror/addon/tern/tern';
2311

2412
import type { Preferences } from 'app/store/preferences/reducer';
2513

26-
import theme from '../../../../../../../../common/theme';
2714
import Header from './Header';
2815

2916
const documentCache = {};
@@ -45,15 +32,15 @@ const Container = styled.div`
4532
display: flex;
4633
flex-direction: column;
4734
width: 100%;
48-
height: 100%;
35+
height: calc(100% - 3rem);
4936
overflow: auto;
5037
`;
5138

5239
const CodeContainer = styled.div`
5340
flex: 1 1 auto;
5441
position: relative;
5542
overflow: auto;
56-
height: 100%;
43+
height: calc(100% - 6rem);
5744
`;
5845

5946
const handleError = (cm, currentError, nextError, nextCode, nextId) => {
@@ -75,7 +62,7 @@ const handleError = (cm, currentError, nextError, nextCode, nextId) => {
7562
nextError.line !== 0 &&
7663
nextError.line <= code.split('\n').length
7764
) {
78-
cm.addLineClass(nextError.line, 'background', 'cm-line-error');
65+
cm.addLineClass(nextError.line - 1, 'background', 'cm-line-error');
7966
}
8067
}
8168
};
@@ -119,7 +106,7 @@ export default class CodeEditor extends React.PureComponent {
119106
}
120107
}
121108

122-
updateCodeMirrorCode(code) {
109+
updateCodeMirrorCode(code: string) {
123110
const pos = this.codemirror.getCursor();
124111
this.codemirror.setValue(code);
125112
this.codemirror.setCursor(pos);
@@ -139,20 +126,7 @@ export default class CodeEditor extends React.PureComponent {
139126

140127
documentCache[id] = new CodeMirror.Doc(code || '', 'jsx');
141128

142-
this.codemirror = new CodeMirror(el, {
143-
mode: 'jsx',
144-
theme: 'oceanic',
145-
keyMap: 'sublime',
146-
indentUnit: 2,
147-
autoCloseBrackets: true,
148-
matchTags: { bothTags: true },
149-
foldGutter: true,
150-
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
151-
value: documentCache[id],
152-
lineNumbers: true,
153-
lineWrapping: true,
154-
styleActiveLine: true,
155-
});
129+
this.codemirror = getCodeMirror(el, documentCache[id]);
156130

157131
this.codemirror.on('change', this.handleChange);
158132

@@ -281,54 +255,3 @@ export default class CodeEditor extends React.PureComponent {
281255
);
282256
}
283257
}
284-
285-
const fadeInAnimation = keyframes`
286-
0% { background-color: #374140; }
287-
100% { background-color: #561011; }
288-
`;
289-
290-
injectGlobal`
291-
.cm-s-oceanic.CodeMirror {
292-
font-family: 'Source Code Pro', monospace;
293-
background: ${theme.background2()};
294-
color: #e0e0e0;
295-
height: 100%;
296-
font-size: 14px;
297-
font-weight: 500;
298-
}
299-
.cm-s-oceanic div.CodeMirror-selected { background: #343D46; }
300-
.cm-s-oceanic .CodeMirror-line::selection, .cm-s-oceanic .CodeMirror-line > span::selection, .cm-s-oceanic .CodeMirror-line > span > span::selection { background: #65737E; }
301-
.cm-s-oceanic .CodeMirror-line::-moz-selection, .cm-s-oceanic .CodeMirror-line > span::-moz-selection, .cm-s-oceanic .CodeMirror-line > span > span::-moz-selection { background: #65737E; }
302-
.cm-s-oceanic .CodeMirror-gutters {
303-
background: ${theme.background2()};
304-
border-right: 0px;
305-
}
306-
.cm-s-oceanic .CodeMirror-guttermarker { color: #ac4142; }
307-
.cm-s-oceanic .CodeMirror-guttermarker-subtle { color: #505050; }
308-
.cm-s-oceanic .CodeMirror-linenumber { color: #505050; }
309-
.cm-s-oceanic .CodeMirror-cursor { border-left: 1px solid #b0b0b0; }
310-
311-
.cm-s-oceanic span.cm-comment { color: #8f5536; }
312-
.cm-s-oceanic span.cm-atom { color: #aa759f; }
313-
.cm-s-oceanic span.cm-number { color: #aa759f; }
314-
315-
.cm-s-oceanic span.cm-property, .cm-s-oceanic span.cm-attribute { color: #D8DEE9; }
316-
.cm-s-oceanic span.cm-keyword { color: #C594C5; }
317-
.cm-s-oceanic span.cm-string { color: #99C794; }
318-
319-
.cm-s-oceanic span.cm-variable { color: #FAC863; }
320-
.cm-s-oceanic span.cm-variable-2 { color: #6a9fb5; }
321-
.cm-s-oceanic span.cm-def { color: #FAC863; }
322-
.cm-s-oceanic span.cm-bracket { color: #e0e0e0; }
323-
.cm-s-oceanic span.cm-tag { color: #EC5f67; }
324-
.cm-s-oceanic span.cm-link { color: #aa759f; }
325-
.cm-s-oceanic span.cm-error { background: #ac4142; color: #b0b0b0; }
326-
327-
.cm-s-oceanic .CodeMirror-activeline-background { background: #374140; }
328-
.cm-s-oceanic .CodeMirror-matchingbracket { text-decoration: underline; color: white !important; }
329-
.cm-s-oceanic span.CodeMirror-matchingtag { background-color: inherit; }
330-
.cm-s-oceanic span.cm-tag.CodeMirror-matchingtag { text-decoration: underline; }
331-
.cm-s-oceanic span.cm-tag.cm-bracket.CodeMirror-matchingtag { text-decoration: none; }
332-
333-
.cm-s-oceanic div.cm-line-error.CodeMirror-linebackground { animation: ${fadeInAnimation} 0.3s; background-color: #561011; }
334-
`;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
import CodeMirror from 'codemirror';
4+
5+
import { getCodeMirror } from 'app/utils/codemirror';
6+
7+
const Container = styled.div`
8+
text-align: left;
9+
color: white;
10+
11+
.cm-s-oceanic.CodeMirror {
12+
padding: 0.5rem;
13+
margin: 1rem 0;
14+
background: rgba(0, 0, 0, 0.7);
15+
border: 1px solid rgba(0, 0, 0, 0.4);
16+
border-radius: 2px;
17+
font-size: 15px;
18+
}
19+
.cm-s-oceanic div.CodeMirror-selected {
20+
background: ${props => props.readOnly ? 'inherit' : 'rgba(255, 255, 255, 0.1)'};
21+
}
22+
.cm-s-oceanic .CodeMirror-activeline-background {
23+
background: ${props => props.readOnly ? 'inherit' : 'rgba(255, 255, 255, 0.1)'};
24+
}
25+
`;
26+
27+
type Props = {
28+
name: string,
29+
onChange: (code: string) => void,
30+
readOnly: ?boolean,
31+
};
32+
33+
export default class Editor extends React.PureComponent {
34+
props: Props;
35+
36+
setUpCodeMirror = el => {
37+
const { name, readOnly } = this.props;
38+
const doc = new CodeMirror.Doc(
39+
`ReactDOM.render(<${name || 'Component'} />, document.body);`,
40+
'jsx'
41+
);
42+
this.codemirror = getCodeMirror(el, doc);
43+
this.codemirror.setOption('lineNumbers', false);
44+
this.codemirror.setOption('readOnly', readOnly);
45+
46+
this.codemirror.on('change', this.handleChange);
47+
};
48+
49+
handleChange = (cm: any, change: any) => {
50+
if (change.origin !== 'setValue' && this.props.onChange) {
51+
this.props.onChange(cm.getValue());
52+
}
53+
};
54+
55+
render() {
56+
const { readOnly } = this.props;
57+
return (
58+
<Container readOnly={readOnly}>
59+
<div ref={this.setUpCodeMirror} />
60+
</Container>
61+
);
62+
}
63+
}

src/app/pages/SandboxView/Sandbox/Editor/Content/subviews/Preview/ErrorMessage.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)