@@ -11,18 +11,23 @@ import ReactDOM from 'react-dom';
11
11
import Remarkable from 'remarkable' ;
12
12
// TODO: switch back to the upstream version of react-live
13
13
// once https://github.com/FormidableLabs/react-live/issues/37 is fixed.
14
- import { LiveProvider , LiveEditor } from '@gaearon/react-live' ;
14
+ import { LiveEditor , LiveProvider } from '@gaearon/react-live' ;
15
15
import { colors , media } from 'theme' ;
16
16
import MetaTitle from 'templates/components/MetaTitle' ;
17
17
18
- const compile = code =>
19
- Babel . transform ( code , { presets : [ 'es2015' , 'react' ] } ) . code ; // eslint-disable-line no-undef
18
+ const compileES5 = (
19
+ code , // eslint-disable-next-line no-undef
20
+ ) => Babel . transform ( code , { presets : [ 'es2015' , 'react' ] } ) . code ;
21
+
22
+ // eslint-disable-next-line no-undef
23
+ const compileES6 = code => Babel . transform ( code , { presets : [ 'react' ] } ) . code ;
20
24
21
25
class CodeEditor extends Component {
22
26
constructor ( props , context ) {
23
27
super ( props , context ) ;
24
28
25
29
this . state = this . _updateState ( props . code ) ;
30
+ this . state . showJSX = true ;
26
31
}
27
32
28
33
componentDidMount ( ) {
@@ -38,11 +43,11 @@ class CodeEditor extends Component {
38
43
}
39
44
40
45
render ( ) {
41
- const { children, code } = this . props ;
42
- const { error} = this . state ;
46
+ const { children} = this . props ;
47
+ const { compiledES6 , code , error, showJSX } = this . state ;
43
48
44
49
return (
45
- < LiveProvider code = { code } mountStylesheet = { false } >
50
+ < LiveProvider code = { showJSX ? code : compiledES6 } mountStylesheet = { false } >
46
51
< div
47
52
css = { {
48
53
[ media . greaterThan ( 'xlarge' ) ] : {
@@ -112,7 +117,23 @@ class CodeEditor extends Component {
112
117
background : colors . darker ,
113
118
color : colors . white ,
114
119
} } >
115
- < MetaTitle onDark = { true } > Live JSX Editor</ MetaTitle >
120
+ < MetaTitle onDark = { true } >
121
+ Live JSX Editor
122
+ < label
123
+ css = { {
124
+ fontSize : 14 ,
125
+ float : 'right' ,
126
+ cursor : 'pointer' ,
127
+ } } >
128
+ < input
129
+ checked = { this . state . showJSX }
130
+ onChange = { event =>
131
+ this . setState ( { showJSX : event . target . checked } ) }
132
+ type = "checkbox"
133
+ /> { ' ' }
134
+ JSX?
135
+ </ label >
136
+ </ MetaTitle >
116
137
</ div >
117
138
< div
118
139
css = { {
@@ -265,12 +286,21 @@ class CodeEditor extends Component {
265
286
this . _mountNode = ref ;
266
287
} ;
267
288
268
- _updateState ( code ) {
289
+ _updateState ( code , showJSX = true ) {
269
290
try {
270
- return {
271
- compiled : compile ( code ) ,
291
+ let newState = {
292
+ compiled : compileES5 ( code ) ,
272
293
error : null ,
273
294
} ;
295
+
296
+ if ( showJSX ) {
297
+ newState . code = code ;
298
+ newState . compiledES6 = compileES6 ( code ) ;
299
+ } else {
300
+ newState . compiledES6 = code ;
301
+ }
302
+
303
+ return newState ;
274
304
} catch ( error ) {
275
305
console . error ( error ) ;
276
306
@@ -282,7 +312,7 @@ class CodeEditor extends Component {
282
312
}
283
313
284
314
_onChange = code => {
285
- this . setState ( this . _updateState ( code ) ) ;
315
+ this . setState ( state => this . _updateState ( code , state . showJSX ) ) ;
286
316
} ;
287
317
}
288
318
0 commit comments