Skip to content

Commit fa75e6c

Browse files
author
Ives van Hoorne
committed
Progress
1 parent 9e0bd9b commit fa75e6c

File tree

5 files changed

+431
-111
lines changed

5 files changed

+431
-111
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
import '@storybook/addon-knobs/register';
12
import '@storybook/addon-actions/register';
23
import '@storybook/addon-links/register';
4+
import 'storybook-addon-jsx/register';

packages/codesandbox-playground/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
"license": "MIT",
1111
"private": false,
1212
"devDependencies": {
13-
"@storybook/addon-actions": "^3.2.17",
14-
"@storybook/addon-links": "^3.2.17",
15-
"@storybook/react": "^3.2.17",
13+
"@storybook/addon-actions": "=3.2.18",
14+
"@storybook/addon-knobs": "=3.2.18",
15+
"@storybook/addon-links": "=3.2.18",
16+
"@storybook/react": "=3.2.18",
17+
"storybook-addon-jsx": "^5.1.0",
1618
"typescript": "^2.6.2"
1719
},
1820
"peerDependencies": {
@@ -23,6 +25,7 @@
2325
"build-storybook": "build-storybook"
2426
},
2527
"dependencies": {
26-
"codemirror": "^5.32.0"
28+
"codemirror": "^5.32.0",
29+
"prop-types": "^15.6.0"
2730
}
2831
}

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,63 @@
11
import React from 'react';
2-
import { listen, dispatch } from 'codesandbox-api';
2+
import PropTypes from 'prop-types';
33

44
export default class Preview extends React.PureComponent {
5+
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),
13+
};
14+
15+
static defaultProps = {
16+
dependencies: {},
17+
resources: [],
18+
};
19+
520
setupFrame = el => {
6-
listen(this.handleMessage);
21+
// listen(this.handleMessage);
22+
if (el) {
23+
this.frame = el;
24+
this.frame.onload = () => {
25+
this.sendCode();
26+
};
27+
}
728
};
829

930
sendCode = () => {
10-
const module = {
11-
code: 'console.log(require("react")); console.log(require("react-dom"))',
12-
path: 'index.js',
13-
};
14-
this.frame.postMessage(
31+
const modules = Object.keys(this.props.files).map(path => ({
32+
...this.props.files[path],
33+
path,
34+
}));
35+
36+
this.frame.contentWindow.postMessage(
1537
{
1638
type: 'compile',
1739
codesandbox: true,
18-
modules: [module],
19-
path: 'index.js',
20-
externalResources: [],
21-
dependencies: { react: '15.5.4', 'react-dom': '15.5.4' },
40+
version: 2,
41+
modules,
42+
entry: '/index.js',
43+
externalResources: this.props.resources,
44+
dependencies: this.props.dependencies,
2245
},
2346
'*'
2447
);
2548
};
2649

2750
handleMessage = (data, source) => {
28-
if (data.type === 'initialized') {
29-
this.frame = source;
30-
this.sendCode();
31-
}
51+
// if (data.type === 'initialized') {
52+
// this.frame = source;
53+
// }
3254
};
3355

3456
render() {
3557
return (
3658
<iframe
37-
style={{ width: '100%', height: '100%' }}
38-
src="http://localhost:3001"
59+
style={{ width: '100%', height: '1000px', border: 'none' }}
60+
src="https://sandbox.codesandbox.io"
3961
ref={this.setupFrame}
4062
/>
4163
);
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
import React from 'react';
22

33
import { storiesOf } from '@storybook/react';
4-
import { action } from '@storybook/addon-actions';
5-
import { linkTo } from '@storybook/addon-links';
64

7-
import { Button, Welcome } from '@storybook/react/demo';
5+
import Preview from '../src/components/Preview';
86

9-
import Playground from '../src';
7+
const stories = storiesOf('Preview', module);
108

11-
storiesOf('Welcome', module).add('to Storybook', () => (
12-
<Welcome showApp={linkTo('Button')} />
9+
stories.add('with one file', () => (
10+
<Preview
11+
files={{
12+
'/index.js': {
13+
code: `document.body.innerHTML = \`<div>$\{require('uuid')()}</div>\``,
14+
},
15+
}}
16+
dependencies={{
17+
uuid: 'latest',
18+
}}
19+
/>
1320
));
1421

15-
storiesOf('Button', module)
16-
.add('with text', () => (
17-
<Button onClick={action('clicked')}>Hello Button</Button>
18-
))
19-
.add('with some emoji', () => (
20-
<Button onClick={action('clicked')}>😀 😎 👍 💯</Button>
21-
));
22+
stories.add('with multiple files', () => (
23+
<Preview
24+
files={{
25+
'/index.js': {
26+
code: `
27+
import Hello from './Hello.js';
2228
23-
storiesOf('Playground', module).add('with text', () => <Playground />);
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+
uuid: 'latest',
38+
}}
39+
/>
40+
));

0 commit comments

Comments
 (0)