@@ -6,6 +6,8 @@ import Network, { traceNetwork } from './src/network';
66import Log , { traceLog } from './src/log' ;
77import Info from './src/info' ;
88import HocComp from './src/hoc' ;
9+ import Storage from './src/storage' ;
10+ import { replaceReg } from './src/tool' ;
911
1012const { width, height } = Dimensions . get ( 'window' ) ;
1113
@@ -46,7 +48,10 @@ class VDebug extends PureComponent {
4648 pan : new Animated . ValueXY ( ) ,
4749 scale : new Animated . Value ( 1 ) ,
4850 panelHeight : new Animated . Value ( 0 ) ,
49- panels : this . addPanels ( )
51+ panels : this . addPanels ( ) ,
52+ history : [ ] ,
53+ historyFilter : [ ] ,
54+ showHistory : false
5055 } ;
5156 this . panResponder = PanResponder . create ( {
5257 onStartShouldSetPanResponder : ( ) => true ,
@@ -83,6 +88,14 @@ class VDebug extends PureComponent {
8388
8489 componentDidMount ( ) {
8590 this . state . pan . setValue ( { x : 0 , y : 0 } ) ;
91+ Storage . support ( ) &&
92+ Storage . get ( 'react-native-vdebug@history' ) . then ( res => {
93+ if ( res ) {
94+ this . setState ( {
95+ history : res
96+ } ) ;
97+ }
98+ } ) ;
8699 }
87100
88101 getRef ( index ) {
@@ -153,11 +166,15 @@ class VDebug extends PureComponent {
153166 execCommand ( ) {
154167 if ( ! this . state . commandValue ) return ;
155168 this . evalInContext ( this . state . commandValue , commandContext ) ;
169+ this . syncHistory ( ) ;
156170 Keyboard . dismiss ( ) ;
157171 }
158172
159173 clearCommand ( ) {
160174 this . textInput . clear ( ) ;
175+ this . setState ( {
176+ historyFilter : [ ]
177+ } ) ;
161178 }
162179
163180 scrollToPage ( index , animated = true ) {
@@ -206,25 +223,91 @@ class VDebug extends PureComponent {
206223 ) ;
207224 }
208225
226+ syncHistory ( ) {
227+ if ( ! Storage . support ( ) ) return ;
228+ const res = this . state . history . filter ( f => {
229+ return f == this . state . commandValue ;
230+ } ) ;
231+ if ( res && res . length ) return ;
232+ this . state . history . splice ( 0 , 0 , this . state . commandValue ) ;
233+ this . state . historyFilter . splice ( 0 , 0 , this . state . commandValue ) ;
234+ this . setState (
235+ {
236+ history : this . state . history ,
237+ historyFilter : this . state . historyFilter
238+ } ,
239+ ( ) => {
240+ Storage . save ( 'react-native-vdebug@history' , this . state . history ) ;
241+ this . forceUpdate ( ) ;
242+ }
243+ ) ;
244+ }
245+
246+ onChange ( text ) {
247+ const state = { commandValue : text } ;
248+ if ( text ) {
249+ const res = this . state . history . filter ( f => f . toLowerCase ( ) . match ( replaceReg ( text ) ) ) ;
250+ if ( res && res . length ) state . historyFilter = res ;
251+ } else {
252+ state . historyFilter = [ ] ;
253+ }
254+ this . setState ( state ) ;
255+ }
256+
209257 renderCommandBar ( ) {
210258 return (
211- < KeyboardAvoidingView keyboardVerticalOffset = { Platform . OS == 'android' ? 0 : 300 } contentContainerStyle = { { flex : 1 , flexDirection : 'row' } } behavior = { 'position' } style = { styles . commandBar } >
212- < TextInput
213- ref = { ref => {
214- this . textInput = ref ;
215- } }
216- style = { styles . commandBarInput }
217- placeholderTextColor = { '#000000a1' }
218- placeholder = "Command..."
219- onChangeText = { text => this . setState ( { commandValue : text } ) }
220- value = { this . state . commandValue }
221- />
222- < TouchableOpacity style = { styles . commandBarBtn } onPress = { this . clearCommand . bind ( this ) } >
223- < Text > X</ Text >
224- </ TouchableOpacity >
225- < TouchableOpacity style = { styles . commandBarBtn } onPress = { this . execCommand . bind ( this ) } >
226- < Text > OK</ Text >
227- </ TouchableOpacity >
259+ < KeyboardAvoidingView
260+ keyboardVerticalOffset = { Platform . OS == 'android' ? 0 : 300 }
261+ contentContainerStyle = { { flex : 1 } }
262+ behavior = { 'position' }
263+ style = { {
264+ height : this . state . historyFilter . length ? 120 : 40 ,
265+ borderWidth : StyleSheet . hairlineWidth ,
266+ borderColor : '#d9d9d9'
267+ } }
268+ >
269+ < View style = { [ styles . historyContainer , { height : this . state . historyFilter . length ? 80 : 0 } ] } >
270+ < ScrollView >
271+ { this . state . historyFilter . map ( text => {
272+ return (
273+ < TouchableOpacity
274+ style = { { borderBottomWidth : 1 , borderBottomColor : '#eeeeeea1' } }
275+ onPress = { ( ) => {
276+ if ( text && text . toString ) {
277+ this . setState ( {
278+ commandValue : text . toString ( )
279+ } ) ;
280+ }
281+ } }
282+ >
283+ < Text style = { { lineHeight : 25 } } > { text } </ Text >
284+ </ TouchableOpacity >
285+ ) ;
286+ } ) }
287+ </ ScrollView >
288+ </ View >
289+ < View style = { styles . commandBar } >
290+ < TextInput
291+ ref = { ref => {
292+ this . textInput = ref ;
293+ } }
294+ style = { styles . commandBarInput }
295+ placeholderTextColor = { '#000000a1' }
296+ placeholder = "Command..."
297+ onChangeText = { this . onChange . bind ( this ) }
298+ value = { this . state . commandValue }
299+ onFocus = { ( ) => {
300+ this . setState ( { showHistory : true } ) ;
301+ } }
302+ onSubmitEditing = { this . execCommand . bind ( this ) }
303+ />
304+ < TouchableOpacity style = { styles . commandBarBtn } onPress = { this . clearCommand . bind ( this ) } >
305+ < Text > X</ Text >
306+ </ TouchableOpacity >
307+ < TouchableOpacity style = { styles . commandBarBtn } onPress = { this . execCommand . bind ( this ) } >
308+ < Text > OK</ Text >
309+ </ TouchableOpacity >
310+ </ View >
228311 </ KeyboardAvoidingView >
229312 ) ;
230313 }
@@ -342,12 +425,12 @@ const styles = StyleSheet.create({
342425 } ,
343426 panelBottom : {
344427 width,
345- flex : 0.1 ,
346428 borderWidth : StyleSheet . hairlineWidth ,
347429 borderColor : '#d9d9d9' ,
348430 flexDirection : 'row' ,
349431 alignItems : 'center' ,
350- backgroundColor : '#eee'
432+ backgroundColor : '#eee' ,
433+ height : 40
351434 } ,
352435 panelBottomBtn : {
353436 flex : 1 ,
@@ -386,10 +469,11 @@ const styles = StyleSheet.create({
386469 color : '#fff'
387470 } ,
388471 commandBar : {
389- height : 40 ,
390472 flexDirection : 'row' ,
391473 borderWidth : StyleSheet . hairlineWidth ,
392- borderColor : '#d9d9d9'
474+ borderColor : '#d9d9d9' ,
475+ flexDirection : 'row' ,
476+ height : 40
393477 } ,
394478 commandBarInput : {
395479 flex : 1 ,
@@ -402,6 +486,12 @@ const styles = StyleSheet.create({
402486 alignItems : 'center' ,
403487 justifyContent : 'center' ,
404488 backgroundColor : '#eee'
489+ } ,
490+ historyContainer : {
491+ borderTopWidth : 1 ,
492+ borderTopColor : '#d9d9d9' ,
493+ backgroundColor : '#ffffff' ,
494+ paddingHorizontal : 10
405495 }
406496} ) ;
407497
0 commit comments