Skip to content

Commit 81a2bee

Browse files
Wojciech KrysiakWojciech Krysiak
authored andcommitted
feat: change react preview editor
1 parent 830a2ef commit 81a2bee

File tree

8 files changed

+279
-54
lines changed

8 files changed

+279
-54
lines changed

fixtures/components/component.jsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,18 @@ const Bold = styled.div`
1010

1111
/**
1212
* Some documented component
13-
*
13+
*
1414
* @component
1515
* @example <caption>Default example</caption>
1616
* const text = 'Meva'
1717
* return (
18-
* <Documented2>
19-
* <Documented text={text} />
20-
* </Documented2>
18+
* <Documented text={text} />
2119
* )
22-
*
20+
*
2321
* @example <caption>Ala ma kota</caption>
2422
* const text = 'some example text 2'
2523
* return (
26-
* <Documented2>
27-
* <Documented text={text} header={'sime'} />
28-
* </Documented2>
24+
* <Documented text={text} header={'sime'} />
2925
* )
3026
*/
3127
const Documented = (props) => {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@
6464
"dependencies": {
6565
"brace": "^0.11.1",
6666
"marked": "^1.1.1",
67+
"prism-react-renderer": "^1.1.1",
6768
"react-ace": "^6.5.0",
6869
"react-docgen": "^5.3.0",
6970
"react-frame-component": "^4.1.1",
71+
"react-live": "^2.2.2",
72+
"react-simple-code-editor": "^0.11.0",
7073
"underscore": "^1.9.1",
7174
"vue-docgen-api": "^3.22.0",
7275
"vue2-ace-editor": "^0.0.13"

src/components/editor.jsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { Fragment, useState } from 'react'
2+
3+
import Editor from 'react-simple-code-editor'
4+
import Highlight, { defaultProps } from 'prism-react-renderer'
5+
import theme from 'prism-react-renderer/themes/nightOwlLight'
6+
7+
const styles = {
8+
root: {
9+
boxSizing: 'border-box',
10+
fontFamily: '"Dank Mono", "Fira Code", monospace',
11+
...theme.plain,
12+
},
13+
}
14+
15+
const EditorExample = (props) => {
16+
const { value, onChange } = props
17+
18+
const highlight = code => (
19+
<Highlight {...defaultProps} theme={theme} code={code} language="jsx">
20+
{({ className, style, tokens, getLineProps, getTokenProps }) => (
21+
<Fragment>
22+
{tokens.map((line, i) => (
23+
<div {...getLineProps({ line, key: i })}>
24+
{line.map((token, key) => <span {...getTokenProps({ token, key })} />)}
25+
</div>
26+
))}
27+
</Fragment>
28+
)}
29+
</Highlight>
30+
)
31+
32+
return (
33+
<Editor
34+
value={value}
35+
onValueChange={onChange}
36+
highlight={highlight}
37+
padding={10}
38+
style={styles.root}
39+
/>
40+
)
41+
}
42+
export default EditorExample

src/components/react-wrapper.jsx

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import 'brace/mode/jsx'
88
import 'brace/theme/monokai'
99
import ComponentRenderer from './component-renderer'
1010

11+
import Editor from './editor'
12+
1113
window.component = null
1214

1315
class Wrapper extends React.Component {
1416
constructor(props) {
1517
super(props)
1618
window.component = window.component || {}
17-
this.iframeRef= React.createRef()
19+
this.iframeRef = React.createRef()
1820
this.handleChange = this.handleChange.bind(this)
19-
this.toggleEditor = this.toggleEditor.bind(this)
2021
let { example } = props
2122
example = example || 'return (<div>Example</div>)'
2223
this.state = {
2324
example,
2425
height: 200,
25-
showEditor: false,
2626
}
2727
this.executeScript(example)
2828
}
@@ -31,9 +31,9 @@ class Wrapper extends React.Component {
3131
const { uniqId } = this.props
3232
const script = document.createElement('script')
3333
const self = this
34-
script.onload = script.onerror = function() {
34+
script.onload = script.onerror = function () {
3535
this.remove()
36-
self.setState(state =>({
36+
self.setState(state => ({
3737
...state,
3838
component: window.component[uniqId] || '',
3939
}))
@@ -48,11 +48,11 @@ class Wrapper extends React.Component {
4848
})()`
4949
try {
5050
const src = Babel.transform(wrapper, { presets: ['react', 'es2015'] }).code
51-
script.src = 'data:text/plain;base64,' + btoa(src)
51+
script.src = `data:text/plain;base64,${btoa(src)}`
5252
} catch (error) {
5353
console.log(error)
5454
}
55-
55+
5656
document.body.appendChild(script)
5757
}
5858

@@ -88,27 +88,19 @@ class Wrapper extends React.Component {
8888
this.computeHeight()
8989
}, 1000)
9090
}
91-
91+
9292
componentWillUnmount() {
9393
clearInterval(this.heightInterval)
9494
}
9595

96-
toggleEditor(event) {
97-
event.preventDefault()
98-
this.setState(state => ({
99-
...state,
100-
showEditor: !state.showEditor,
101-
}))
102-
}
103-
104-
render () {
105-
const { component, height, showEditor } = this.state
96+
render() {
97+
const { component, height } = this.state
10698
return (
10799
<div>
108100
<Frame
109101
className="component-wrapper"
110102
ref={this.iframeRef}
111-
style={{width: '100%', height }}
103+
style={{ width: '100%', height }}
112104
onLoad={this.computeHeight()}
113105
>
114106
<link type="text/css" rel="stylesheet" href="./build/entry.css" />
@@ -122,29 +114,14 @@ class Wrapper extends React.Component {
122114
}
123115
</FrameContextConsumer>
124116
</Frame>
125-
<div className="bd__button">
126-
<a href="#" onClick={this.toggleEditor}>Modify Example Code</a>
117+
<div className="field editor-preview">
118+
<Editor value={this.state.example} onChange={code => this.handleChange(code)} />
127119
</div>
128-
{showEditor ? (
129-
<div className="field">
130-
<AceEditor
131-
style={{width: '100%', height: '200px', marginBottom: '20px'}}
132-
value={this.state.example}
133-
mode="jsx"
134-
theme="monokai"
135-
onChange={(code) => this.handleChange(code)}
136-
name="editor-div"
137-
editorProps={{ $useSoftTabs: true }}
138-
/>
139-
</div>
140-
) : ''}
141120
</div>
142121
)
143122
}
144123
}
145124

146-
export default (props) => {
147-
return (
148-
<Wrapper {...props} />
149-
)
150-
}
125+
export default props => (
126+
<Wrapper {...props} />
127+
)

styles/app.sass

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@import 'base/landing'
1111

1212
@import 'components/top-navbar'
13+
@import 'components/preview'
1314
@import 'components/sidebar'
1415
@import 'components/side-nav'
1516
@import 'components/footer'

styles/components/preview.sass

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.editor-preview
2+
border: 1px solid $border;
3+
position: relative;
4+
margin: 30px 0;
5+
6+
&::before
7+
content: "Edit code";
8+
position: absolute;
9+
right: 0;
10+
top: -25px;

styles/components/tag.sass

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
.tag
2-
text-transform: uppercase
2+
text-transform: uppercase
3+
4+
body pre .tag
5+
text-transform: none
6+
align-items: normal
7+
background-color: inherit
8+
border-radius: 0
9+
color: inherit
10+
display: inline
11+
font-size: 15px
12+
height: auto
13+
justify-content: normal
14+
line-height: 22.5px
15+
padding-left: 0
16+
padding-right: 0
17+
white-space: pre-wrap

0 commit comments

Comments
 (0)