forked from styleguidist/react-styleguidist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreview.js
153 lines (128 loc) · 3.3 KB
/
Preview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import noop from 'lodash/noop';
import { transform } from 'buble';
import PlaygroundError from 'rsg-components/PlaygroundError';
import Wrapper from 'rsg-components/Wrapper';
/* eslint-disable react/no-multi-comp */
const compileCode = (code, config) => transform(code, config).code;
// Wrap everything in a React component to leverage the state management of this component
class PreviewComponent extends Component {
static propTypes = {
component: PropTypes.func.isRequired,
};
constructor() {
super();
this.state = {};
this.setState = this.setState.bind(this);
this.setInitialState = this.setInitialState.bind(this);
}
// Synchronously set initial state, so it will be ready before first render
// Ignore all consequent calls
setInitialState(initialState) {
Object.assign(this.state, initialState);
this.setInitialState = noop;
}
render() {
return this.props.component(this.state, this.setState, this.setInitialState);
}
}
export default class Preview extends Component {
static propTypes = {
code: PropTypes.string.isRequired,
evalInContext: PropTypes.func.isRequired,
};
static contextTypes = {
config: PropTypes.object.isRequired,
};
state = {
error: null,
};
componentDidMount() {
console.clear(); // eslint-disable-line no-console
this.executeCode();
}
shouldComponentUpdate(nextProps, nextState) {
return this.state.error !== nextState.error || this.props.code !== nextProps.code;
}
componentDidUpdate(prevProps) {
if (this.props.code !== prevProps.code) {
this.executeCode();
}
}
componentWillUnmount() {
this.unmountPreview();
}
unmountPreview() {
if (this.mountNode) {
ReactDOM.unmountComponentAtNode(this.mountNode);
}
}
executeCode() {
this.setState({
error: null,
});
const { code } = this.props;
if (!code) {
return;
}
const compiledCode = this.compileCode(code);
if (!compiledCode) {
return;
}
const exampleComponent = this.evalInContext(compiledCode);
const wrappedComponent = (
<Wrapper>
<PreviewComponent component={exampleComponent} />
</Wrapper>
);
window.requestAnimationFrame(() => {
this.unmountPreview();
try {
ReactDOM.render(wrappedComponent, this.mountNode);
} catch (err) {
this.handleError(err);
}
});
}
compileCode(code) {
try {
return compileCode(code, this.context.config.compilerConfig);
} catch (err) {
this.handleError(err);
}
return false;
}
evalInContext(compiledCode) {
// 1. Use setter/with to call our callback function when user write `initialState = {...}`
// 2. Wrap code in JSON.stringify/eval to catch the component and return it
const exampleComponentCode = `
var stateWrapper = {
set initialState(value) {
__setInitialState(value)
},
}
with (stateWrapper) {
return eval(${JSON.stringify(compiledCode)})
}
`;
return this.props.evalInContext(exampleComponentCode);
}
handleError(err) {
this.unmountPreview();
this.setState({
error: err.toString(),
});
console.error(err); // eslint-disable-line no-console
}
render() {
const { error } = this.state;
return (
<div>
<div ref={ref => (this.mountNode = ref)} />
{error && <PlaygroundError message={error} />}
</div>
);
}
}