@@ -2,10 +2,11 @@ import React, { Component } from 'react';
22
33import { connect } from 'react-redux' ;
44
5- import { Form , Segment , Button , Icon } from 'semantic-ui-react' ;
5+ import { Form , Grid , Segment , Button , Icon } from 'semantic-ui-react' ;
66
77import classNames from 'classnames' ;
88import Dropzone from 'react-dropzone' ;
9+ import prettyFormat from 'pretty-format' ;
910
1011import EditorContainer from '../containers/EditorContainer' ;
1112
@@ -33,7 +34,10 @@ class CodeContainer extends Component {
3334 super ( props ) ;
3435 this . state = {
3536 selectedTabIndex : TAB_CODE ,
37+ evaluate : false ,
38+ evaluatedResult : '' ,
3639 }
40+ this . capturingConsole = null ;
3741 }
3842
3943 componentWillReceiveProps ( nextProps ) {
@@ -42,6 +46,10 @@ class CodeContainer extends Component {
4246 selectedTabIndex : TAB_RESULTS ,
4347 } )
4448 }
49+
50+ if ( this . state . evaluate ) {
51+ this . evaluate ( nextProps . obfuscatedCode ) ;
52+ }
4553 }
4654
4755 onTabClick ( index ) {
@@ -62,6 +70,64 @@ class CodeContainer extends Component {
6270
6371 }
6472
73+ toggleEvaluate = ( ) => {
74+ const nextEvaluate = ! this . state . evaluate ;
75+
76+ this . setState ( {
77+ evaluate : nextEvaluate ,
78+ } ) ;
79+
80+ if ( nextEvaluate ) {
81+ this . evaluate ( this . props . obfuscatedCode ) ;
82+ }
83+
84+ }
85+
86+ // from https://github.com/babel/babel.github.io/blob/e7d082e4d545a75d7aa29b1df580c86114ab1586/scripts/7.js#L361
87+ evaluate ( code ) {
88+ this . capturingConsole = Object . create ( console ) ;
89+ const capturingConsole = this . capturingConsole ;
90+ let buffer = [ ] ;
91+ var done = false ;
92+
93+ const self = this ;
94+
95+ function flush ( ) {
96+ self . setState ( {
97+ evaluatedResult : buffer . join ( '\n' ) ,
98+ } )
99+ }
100+
101+ function write ( data ) {
102+ buffer . push ( data ) ;
103+ if ( done ) flush ( ) ;
104+ }
105+
106+ function capture ( ) {
107+ const logs = [ ] . map . call ( arguments , ( log ) => {
108+ return prettyFormat ( log ) ;
109+ } ) ;
110+ write ( logs . join ( '\n' ) ) ;
111+ }
112+
113+ [ 'error' , 'log' , 'info' , 'debug' ] . forEach ( function ( key ) {
114+ capturingConsole [ key ] = function ( ) {
115+ Function . prototype . apply . call ( console [ key ] , console , arguments ) ;
116+ capture . apply ( this , arguments ) ;
117+ } ;
118+ } ) ;
119+
120+ try {
121+ new Function ( 'console' , code ) ( capturingConsole ) ;
122+ } catch ( err ) {
123+ buffer . push ( err . message ) ;
124+ }
125+
126+ done = true ;
127+ flush ( ) ;
128+
129+ }
130+
65131 render ( ) {
66132 const tabIndex = this . state . selectedTabIndex ;
67133
@@ -113,21 +179,43 @@ class CodeContainer extends Component {
113179 onFocus = { ( event ) => event . target . select ( ) }
114180 > </ Form . TextArea >
115181 </ Form >
116- < Segment basic >
117- < Button
118- disabled = { ! hasObfuscatedCode }
119- onClick = { onDownloadCodeClick }
120- >
121- < Icon name = 'download' /> Download obfuscated code
122- </ Button >
123- { hasSourceMap &&
124- < Button
125- onClick = { onDownloadSourceMapClick }
126- >
127- < Icon name = 'download' /> Download source map file
128- </ Button >
129- }
130- </ Segment >
182+
183+ < Grid columns = { 2 } relaxed >
184+ < Grid . Column width = { 13 } >
185+ < Segment basic >
186+ < Button
187+ disabled = { ! hasObfuscatedCode }
188+ onClick = { onDownloadCodeClick }
189+ >
190+ < Icon name = 'download' /> Download obfuscated code
191+ </ Button >
192+ { hasSourceMap &&
193+ < Button
194+ onClick = { onDownloadSourceMapClick }
195+ >
196+ < Icon name = 'download' /> Download source map file
197+ </ Button >
198+ }
199+ </ Segment >
200+ </ Grid . Column >
201+
202+ < Grid . Column width = { 3 } >
203+ < Segment basic >
204+ < Form . Checkbox
205+ label = 'Evaluate'
206+ checked = { this . state . evaluate }
207+ onChange = { this . toggleEvaluate } />
208+ </ Segment >
209+ </ Grid . Column >
210+ </ Grid >
211+
212+ { this . state . evaluate &&
213+ < Segment basic >
214+ < div className = "evaluatedCode" >
215+ { this . state . evaluatedResult }
216+ </ div >
217+ </ Segment >
218+ }
131219 </ Pane >
132220
133221 </ div >
0 commit comments