forked from sonyboy232/better-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-wrapper.jsx
150 lines (136 loc) · 3.84 KB
/
react-wrapper.jsx
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
import React from 'react'
import brace from 'brace'
import AceEditor from 'react-ace'
import Frame, { FrameContextConsumer } from 'react-frame-component'
import 'brace/mode/jsx'
import 'brace/theme/monokai'
import ComponentRenderer from './component-renderer'
window.component = null
class Wrapper extends React.Component {
constructor(props) {
super(props)
window.component = window.component || {}
this.iframeRef= React.createRef()
this.handleChange = this.handleChange.bind(this)
this.toggleEditor = this.toggleEditor.bind(this)
let { example } = props
example = example || 'return (<div>Example</div>)'
this.state = {
example,
height: 200,
showEditor: false,
}
this.executeScript(example)
}
executeScript(source) {
const { uniqId } = this.props
const script = document.createElement('script')
const self = this
script.onload = script.onerror = function() {
this.remove()
self.setState(state =>({
...state,
component: window.component[uniqId] || '',
}))
}
const wrapper = `window.component['${uniqId}'] = (() => {
${Object.keys(reactComponents).map(k => `const ${k} = reactComponents['${k}'];`).join('\n')}
try {
${source}
} catch (error) {
console.log(error)
}
})()`
try {
const src = Babel.transform(wrapper, { presets: ['react', 'es2015'] }).code
script.src = 'data:text/plain;base64,' + btoa(src)
} catch (error) {
console.log(error)
}
document.body.appendChild(script)
}
handleChange(code) {
this.executeScript(code)
this.setState(state => ({
...state,
example: code,
}))
}
computeHeight() {
const { height } = this.state
const padding = 5 // buffer for any unstyled margins
if (
this.iframeRef.current
&& this.iframeRef.current.node.contentDocument
&& this.iframeRef.current.node.contentDocument.body.offsetHeight !== 0
&& this.iframeRef.current.node.contentDocument.body.offsetHeight !== (height - padding)
) {
this.setState({
height: this.iframeRef.current.node.contentDocument.body.offsetHeight + padding,
})
}
}
componentDidUpdate() {
this.computeHeight()
}
componentDidMount() {
this.heightInterval = setInterval(() => {
this.computeHeight()
}, 1000)
}
componentWillUnmount() {
clearInterval(this.heightInterval)
}
toggleEditor(event) {
event.preventDefault()
this.setState(state => ({
...state,
showEditor: !state.showEditor,
}))
}
render () {
const { component, height, showEditor } = this.state
return (
<div>
<Frame
className="component-wrapper"
ref={this.iframeRef}
style={{width: '100%', height }}
onLoad={this.computeHeight()}
>
<link type="text/css" rel="stylesheet" href="./build/entry.css" />
<FrameContextConsumer>
{
frameContext => (
<ComponentRenderer frameContext={frameContext}>
{component}
</ComponentRenderer>
)
}
</FrameContextConsumer>
</Frame>
<div className="bd__button">
<a href="#" onClick={this.toggleEditor}>Modify Example Code</a>
</div>
{showEditor ? (
<div className="field">
<AceEditor
style={{width: '100%', height: '200px', marginBottom: '20px'}}
value={this.state.example}
mode="jsx"
theme="monokai"
onChange={(code) => this.handleChange(code)}
name="editor-div"
editorProps={{ $useSoftTabs: true }}
/>
</div>
) : ''}
</div>
)
}
}
export default (props) => {
return (
<Wrapper {...props} />
)
}