Skip to content

Commit b4aefb7

Browse files
committed
added palyground stuffs
1 parent aa3d37d commit b4aefb7

File tree

3 files changed

+112
-15
lines changed

3 files changed

+112
-15
lines changed
Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,105 @@
11
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import PropTypes from 'prop-types';
4+
import { transform } from 'babel-standalone';
25

36
import styles from './styles.scss';
47

5-
const Preview = ({ code }) => (
6-
<div className={styles.preview}>
7-
<div className="header">
8-
Preview
9-
</div>
10-
{code}
11-
</div>
12-
);
8+
class Preview extends React.Component {
9+
constructor(props) {
10+
super(props);
1311

14-
export default Preview;
12+
this.state = {
13+
error: null
14+
}
15+
this.setTimeout = this.setTimeout.bind(this);
16+
this.compileCode = this.compileCode.bind(this);
17+
this.buildScope = this.buildScope.bind(this);
18+
this.executeCode = this.executeCode.bind(this);
19+
}
20+
21+
componentDidMount() {
22+
this.executeCode();
23+
}
24+
25+
componentDidUpdate (prevProps) {
26+
clearTimeout(this.timeoutID);
27+
if (this.props.code !== prevProps.code) {
28+
this.executeCode();
29+
}
30+
}
31+
32+
setTimeout () {
33+
clearTimeout(this.timeoutID);
34+
this.timeoutID = setTimeout(...arguments);
35+
}
36+
37+
compileCode () {
38+
const code = `
39+
(function (${Object.keys(this.props.scope).join(', ')}, mountNode) {
40+
${this.props.code}
41+
});`;
42+
43+
return transform(code, {
44+
presets: ['es2015', 'stage-0', 'react']
45+
}).code;
46+
}
47+
48+
buildScope (mountNode) {
49+
return Object.keys(this.props.scope).map(key => this.props.scope[key]).concat(mountNode);
50+
}
51+
52+
executeCode () {
53+
const mountNode = this.refs.mount;
54+
const scope = this.buildScope(mountNode);
55+
56+
try {
57+
ReactDOM.unmountComponentAtNode(mountNode);
58+
} catch (e) {
59+
console.log(e);
60+
}
61+
try {
62+
63+
const x = eval(this.compileCode())(...scope);
64+
ReactDOM.render(x, mountNode);
65+
if (this.state.error) {
66+
this.setState({ error: null });
67+
}
68+
} catch (err) {
69+
console.log(err);
70+
this.setTimeout(() => {
71+
this.setState({ error: err.toString() });
72+
}, 500);
73+
}
74+
}
75+
76+
render () {
77+
return (
78+
<div className={styles.preview}>
79+
<div ref="mount" />
80+
</div>
81+
);
82+
}
83+
84+
// render() {
85+
// return (
86+
// <div className={styles.preview}>
87+
// <div className="header">
88+
// Preview
89+
// </div>
90+
// {code}
91+
// </div>
92+
// );
93+
// }
94+
}
95+
96+
Preview.propTypes = {
97+
code: PropTypes.string.isRequired,
98+
scope: PropTypes.object,
99+
};
100+
101+
Preview.defaultProps = {
102+
scope: { React },
103+
};
104+
105+
export default Preview;

docs/client/components/PlaygroundPage/PlaygroundWithPreview/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import Playground from './Playground';
44
import styles from './styles.scss';
55

66
const defaultCode = `
7-
const Example = (
8-
<div>
9-
Example Component
10-
</div>
11-
);
7+
class Example extends React.Component {
8+
render() {
9+
return (
10+
<div>
11+
Example
12+
</div>
13+
)
14+
}
15+
}
16+
return <Example />;
1217
`;
1318

1419
class PlaygroundWithPreview extends React.Component {
@@ -33,7 +38,7 @@ class PlaygroundWithPreview extends React.Component {
3338
code={defaultCode}
3439
/>
3540
<Preview
36-
41+
code={defaultCode}
3742
/>
3843
</div>
3944
{activeComponent}

docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"lodash": "^4.17.10",
2020
"node-sass": "^4.9.0",
2121
"postcss-loader": "^2.1.5",
22+
"prop-types": "^15.6.2",
2223
"react": "^16.4.1",
2324
"react-dom": "^16.4.1",
2425
"react-flexbox-grid": "^2.1.2",

0 commit comments

Comments
 (0)