Skip to content

Commit 18a6473

Browse files
author
Ives van Hoorne
committed
Progress
1 parent fa75e6c commit 18a6473

File tree

11 files changed

+272
-35
lines changed

11 files changed

+272
-35
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { configure } from '@storybook/react';
22

3+
const req = require.context('../stories', true);
4+
35
function loadStories() {
4-
require('../stories');
6+
req.keys().forEach(filename => req(filename));
57
}
68

79
configure(loadStories, module);

packages/codesandbox-playground/src/components/CodeEditor/CodeEditor.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @flow
22
import * as React from 'react';
3+
import PropTypes from 'prop-types';
4+
35
import CodeMirror from 'codemirror';
46

57
import 'codemirror/lib/codemirror.css';
@@ -14,11 +16,21 @@ import 'codemirror/addon/fold/foldcode';
1416
import 'codemirror/addon/fold/foldgutter';
1517
import 'codemirror/addon/fold/brace-fold';
1618

19+
import './styles.css';
20+
1721
export default class CodeEditor extends React.Component {
22+
static propTypes = {
23+
code: PropTypes.string.isRequired,
24+
onChangeCode: PropTypes.func,
25+
};
26+
27+
static defaultProps = {
28+
onChangeCode: () => {},
29+
};
30+
1831
getCodeMirror = el => {
19-
console.log('oke');
2032
this.codemirror = new CodeMirror(el, {
21-
value: 'console.log("mama")',
33+
value: this.props.code,
2234
theme: 'oceanic',
2335
keyMap: 'sublime',
2436
indentUnit: 2,
@@ -29,16 +41,22 @@ export default class CodeEditor extends React.Component {
2941
lineNumbers: true,
3042
lineWrapping: false,
3143
styleActiveLine: true,
32-
lint: false,
44+
mode: 'javascript',
3345
});
46+
47+
this.codemirror.on('change', this.handleChange);
48+
};
49+
50+
handleChange = cm => {
51+
this.props.onChangeCode(cm.getValue());
3452
};
3553

3654
render() {
37-
console.log('hey?');
3855
return (
3956
<div
4057
style={{
4158
height: '100%',
59+
width: '100%',
4260
}}
4361
ref={this.getCodeMirror}
4462
/>
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
import React from 'react';
2-
3-
import CodeEditor from './CodeEditor';
4-
5-
type Props = {
6-
id: string,
7-
8-
changeCode: (id: string, code: string) => Object,
9-
saveCode: ?() => void,
10-
};
11-
12-
export default (props: Props) => {
13-
return <CodeEditor {...props} />;
14-
};
1+
export default from './CodeEditor';
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
.CodeMirror {
2+
font-family: Source Code Pro, Menlo, monospace;
3+
line-height: 1.4;
4+
background: #1c2022;
5+
color: #e0e0e0;
6+
height: 100%;
7+
font-weight: 500;
8+
}
9+
div.CodeMirror-selected {
10+
background: #374140;
11+
}
12+
.CodeMirror-line::selection,
13+
.CodeMirror-line > span::selection,
14+
.CodeMirror-line > span > span::selection {
15+
background: #65737e;
16+
}
17+
.CodeMirror-line::-moz-selection,
18+
.CodeMirror-line > span::-moz-selection,
19+
.CodeMirror-line > span > span::-moz-selection {
20+
background: #65737e;
21+
}
22+
.CodeMirror-gutters {
23+
background: #1c2022;
24+
border-right: 0px;
25+
}
26+
.CodeMirror-guttermarker {
27+
color: #ac4142;
28+
}
29+
.CodeMirror-guttermarker-subtle {
30+
color: #505050;
31+
}
32+
.CodeMirror-linenumber {
33+
color: #505050;
34+
}
35+
.CodeMirror-cursor {
36+
border-left: 1px solid #b0b0b0;
37+
}
38+
39+
span.cm-comment {
40+
color: #626466;
41+
}
42+
span.cm-atom {
43+
color: #aa759f;
44+
}
45+
span.cm-number {
46+
color: #aa759f;
47+
}
48+
49+
span.cm-property,
50+
span.cm-attribute {
51+
color: #aa759f;
52+
}
53+
span.cm-keyword {
54+
color: #6caedd;
55+
}
56+
span.cm-string {
57+
color: #99c794;
58+
}
59+
60+
span.cm-variable {
61+
color: #ffd399;
62+
}
63+
span.cm-variable-2 {
64+
color: #6caedd;
65+
}
66+
span.cm-def {
67+
color: #fac863;
68+
}
69+
span.cm-bracket {
70+
color: #e0e0e0;
71+
}
72+
span.cm-tag {
73+
color: #ec5f67;
74+
}
75+
span.cm-link {
76+
color: #aa759f;
77+
}
78+
span.cm-error {
79+
background: #ac4142;
80+
color: #b0b0b0;
81+
}
82+
83+
.CodeMirror-activeline-background {
84+
background: rgba(0, 0, 0, 0.2);
85+
}
86+
.CodeMirror-matchingbracket {
87+
text-decoration: underline;
88+
color: white !important;
89+
}
90+
span.CodeMirror-matchingtag {
91+
background-color: inherit;
92+
}
93+
span.cm-tag.CodeMirror-matchingtag {
94+
text-decoration: underline;
95+
}
96+
span.cm-tag.cm-bracket.CodeMirror-matchingtag {
97+
text-decoration: none;
98+
}
99+
100+
div.cm-line-highlight.CodeMirror-linebackground {
101+
background-color: rgba(0, 0, 0, 0.3);
102+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import { filePropTypes } from '../../utils/prop-types';
3+
4+
import CodeEditor from '../CodeEditor';
5+
import Preview from '../Preview';
6+
7+
export default class Playground extends React.Component {
8+
static propTypes = {
9+
...filePropTypes,
10+
};
11+
static defaultProps = {
12+
entry: '/index.js',
13+
};
14+
15+
state = {
16+
files: this.props.files,
17+
};
18+
19+
handleChange = (code: string) => {
20+
const newFiles = {
21+
...this.state.files,
22+
[this.props.entry]: {
23+
...[this.state.files[this.props.entry]],
24+
code,
25+
},
26+
};
27+
28+
this.setState({
29+
files: newFiles,
30+
});
31+
};
32+
33+
render() {
34+
const code = this.state.files[this.props.entry].code;
35+
36+
return (
37+
<div>
38+
<div
39+
style={{
40+
height: '3rem',
41+
width: '100%',
42+
backgroundColor: '#1C2022',
43+
color: 'white',
44+
}}
45+
>
46+
CodeSandbox
47+
</div>
48+
<div style={{ display: 'flex' }}>
49+
<CodeEditor code={code} onChangeCode={this.handleChange} />
50+
<Preview {...this.props} files={this.state.files} />
51+
</div>
52+
</div>
53+
);
54+
}
55+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default from './Playground';

packages/codesandbox-playground/src/components/Preview/Preview.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
2+
3+
import { filePropTypes } from '../../utils/prop-types';
34

45
export default class Preview extends React.PureComponent {
56
static propTypes = {
6-
files: PropTypes.shape({
7-
path: PropTypes.shape({
8-
code: PropTypes.string.isRequired,
9-
}),
10-
}).isRequired,
11-
dependencies: PropTypes.objectOf(PropTypes.string),
12-
resources: PropTypes.arrayOf(PropTypes.string),
7+
...filePropTypes,
138
};
149

1510
static defaultProps = {
@@ -18,7 +13,6 @@ export default class Preview extends React.PureComponent {
1813
};
1914

2015
setupFrame = el => {
21-
// listen(this.handleMessage);
2216
if (el) {
2317
this.frame = el;
2418
this.frame.onload = () => {
@@ -47,11 +41,11 @@ export default class Preview extends React.PureComponent {
4741
);
4842
};
4943

50-
handleMessage = (data, source) => {
51-
// if (data.type === 'initialized') {
52-
// this.frame = source;
53-
// }
54-
};
44+
componentDidUpdate(prevProps) {
45+
if (this.props.files !== prevProps.files) {
46+
this.sendCode();
47+
}
48+
}
5549

5650
render() {
5751
return (

packages/codesandbox-playground/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { filePropTypes } from './utils/prop-types';
24

35
import CodeEditor from './components/CodeEditor';
46
import Preview from './components/Preview';
57

68
export default class Playground extends React.Component {
9+
static propTypes = {
10+
...filePropTypes,
11+
};
712
static defaultProps = {
813
entry: 'index.js',
914
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import PropTypes from 'prop-types';
2+
3+
export const filePropTypes = {
4+
files: PropTypes.shape({
5+
path: PropTypes.shape({
6+
code: PropTypes.string.isRequired,
7+
}),
8+
}).isRequired,
9+
dependencies: PropTypes.objectOf(PropTypes.string),
10+
resources: PropTypes.arrayOf(PropTypes.string),
11+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react';
2+
3+
import { storiesOf } from '@storybook/react';
4+
5+
import Playground from '../src/components/Playground';
6+
7+
const stories = storiesOf('Playground', module);
8+
9+
stories.add('with one file', () => (
10+
<Playground
11+
files={{
12+
'/index.js': {
13+
code: `document.body.innerHTML = \`<div>$\{require('uuid')()}</div>\``,
14+
},
15+
}}
16+
dependencies={{
17+
uuid: 'latest',
18+
}}
19+
/>
20+
));
21+
22+
stories.add('with multiple files', () => (
23+
<Playground
24+
files={{
25+
'/index.js': {
26+
code: `
27+
import Hello from './Hello.js';
28+
29+
document.body.innerHTML = JSON.stringify(Hello);
30+
`,
31+
},
32+
'/Hello.js': {
33+
code: `export default "Hello from another file!"`,
34+
},
35+
}}
36+
dependencies={{}}
37+
/>
38+
));
39+
40+
stories.add('with errors', () => (
41+
<Playground
42+
files={{
43+
'/index.js': {
44+
code: `
45+
throw new Error("I'm an error!");
46+
`,
47+
},
48+
}}
49+
dependencies={{}}
50+
/>
51+
));

0 commit comments

Comments
 (0)