11import React , { PureComponent } from 'react' ;
22import { ScrollView , View , Text , TouchableOpacity , PanResponder , Animated , Dimensions , StyleSheet , TextInput , Keyboard , NativeModules , Platform , KeyboardAvoidingView } from 'react-native' ;
33import event from './src/event' ;
4- import Network from './src/network' ;
5- import Log from './src/console ' ;
4+ import Network , { traceNetwork } from './src/network' ;
5+ import Log , { traceLog } from './src/log ' ;
66import Info from './src/info' ;
77const { width, height } = Dimensions . get ( 'window' ) ;
88
9- let externalContext = { } ;
9+ let commandContext = global ;
1010
11- export const setExternalContext = context => {
12- externalContext = context || { } ;
11+ export const setExternalContext = externalContext => {
12+ if ( externalContext ) commandContext = externalContext ;
1313} ;
1414
15- function evalInContext ( js , context ) {
16- return function ( str ) {
17- let result = '' ;
18- try {
19- // eslint-disable-next-line no-eval
20- result = eval ( str ) ;
21- } catch ( err ) {
22- result = 'Invalid input' ;
23- }
24- return event . trigger ( 'addLog' , result ) ;
25- } . call ( context , `with(this) { ${ js } } ` ) ;
26- }
15+ // Log/network trace when Element is not initialized.
16+ export const initTrace = ( ) => {
17+ traceLog ( ) ;
18+ traceNetwork ( ) ;
19+ } ;
2720
2821class VDebug extends PureComponent {
2922 constructor ( props ) {
3023 super ( props ) ;
24+ initTrace ( ) ;
3125 this . state = {
3226 commandValue : '' ,
3327 showPanel : false ,
@@ -37,11 +31,11 @@ class VDebug extends PureComponent {
3731 panels : [
3832 {
3933 title : 'Log' ,
40- component : Log
34+ component : < Log />
4135 } ,
4236 {
4337 title : 'Network' ,
44- component : Network
38+ component : < Network />
4539 } ,
4640 {
4741 title : 'Info' ,
@@ -105,16 +99,28 @@ class VDebug extends PureComponent {
10599 NativeModules ?. DevMenu ?. reload ( ) ;
106100 }
107101
108- execCommand = ( ) => {
102+ evalInContext ( js , context ) {
103+ return function ( str ) {
104+ let result = '' ;
105+ try {
106+ // eslint-disable-next-line no-eval
107+ result = eval ( str ) ;
108+ } catch ( err ) {
109+ result = 'Invalid input' ;
110+ }
111+ return event . trigger ( 'addLog' , result ) ;
112+ } . call ( context , `with(this) { ${ js } } ` ) ;
113+ }
114+
115+ execCommand ( ) {
109116 if ( ! this . state . commandValue ) return ;
110- const context = externalContext ;
111- evalInContext ( this . state . commandValue , context ) ;
117+ this . evalInContext ( this . state . commandValue , commandContext ) ;
112118 Keyboard . dismiss ( ) ;
113- } ;
119+ }
114120
115- clearCommand = ( ) => {
121+ clearCommand ( ) {
116122 this . textInput . clear ( ) ;
117- } ;
123+ }
118124
119125 scrollToPage ( index , animated = true ) {
120126 this . scrollToCard ( index , animated ) ;
@@ -156,14 +162,14 @@ class VDebug extends PureComponent {
156162 } }
157163 style = { styles . commandBarInput }
158164 placeholderTextColor = { '#000000a1' }
159- placeholder = "command ..."
165+ placeholder = "Command ..."
160166 onChangeText = { text => this . setState ( { commandValue : text } ) }
161167 value = { this . state . commandValue }
162168 />
163- < TouchableOpacity style = { styles . commandBarBtn } onPress = { ( ) => this . clearCommand ( ) } >
169+ < TouchableOpacity style = { styles . commandBarBtn } onPress = { this . clearCommand . bind ( this ) } >
164170 < Text > X</ Text >
165171 </ TouchableOpacity >
166- < TouchableOpacity style = { styles . commandBarBtn } onPress = { ( ) => this . execCommand ( ) } >
172+ < TouchableOpacity style = { styles . commandBarBtn } onPress = { this . execCommand . bind ( this ) } >
167173 < Text > OK</ Text >
168174 </ TouchableOpacity >
169175 </ View >
@@ -189,7 +195,7 @@ class VDebug extends PureComponent {
189195 }
190196
191197 onScrollAnimationEnd ( { nativeEvent } ) {
192- const currentPageIndex = Math . floor ( nativeEvent . contentOffset . x / width ) ;
198+ const currentPageIndex = Math . floor ( nativeEvent . contentOffset . x / Math . floor ( width ) ) ;
193199 currentPageIndex != this . state . currentPageIndex &&
194200 this . setState ( {
195201 currentPageIndex : currentPageIndex
0 commit comments