Skip to content

Commit c5a8d13

Browse files
authored
Merge pull request #3 from itenl/dev-1.2.0
Dev 1.2.0
2 parents 37bb2e4 + a536e49 commit c5a8d13

5 files changed

Lines changed: 165 additions & 25 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- [x] 可视化 Response
1616
- [x] Log 等级分类
1717
- [x] 关键字过滤 Log / Network
18-
- [ ] Command 历史记录 (ing...)
18+
- [x] Command 历史记录
1919
- [ ] 导出所有 Log / Network (ing...)
2020

2121
## Install

index.js

Lines changed: 112 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Network, { traceNetwork } from './src/network';
66
import Log, { traceLog } from './src/log';
77
import Info from './src/info';
88
import HocComp from './src/hoc';
9+
import Storage from './src/storage';
10+
import { replaceReg } from './src/tool';
911

1012
const { 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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-vdebug",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "React-Native 调试工具",
55
"main": "index.js",
66
"scripts": {

src/storage.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { AsyncStorage } from 'react-native';
2+
3+
const storage = {
4+
support: function () {
5+
try {
6+
if (AsyncStorage && AsyncStorage.getItem && AsyncStorage.setItem) return true;
7+
return false;
8+
} catch (error) {
9+
return false;
10+
}
11+
},
12+
get: async function (key) {
13+
try {
14+
const value = await AsyncStorage.getItem(key);
15+
if (value !== null) {
16+
const jsonValue = JSON.parse(value);
17+
return jsonValue;
18+
}
19+
return null;
20+
} catch (error) {
21+
return null;
22+
}
23+
},
24+
save: async function (key, value) {
25+
try {
26+
await AsyncStorage.setItem(key, JSON.stringify(value));
27+
} catch (error) {
28+
console.log(error);
29+
}
30+
},
31+
update: function (key, value) {
32+
return StorageUtil.get(key).then(item => {
33+
value = typeof value === 'string' ? value : Object.assign({}, item, value);
34+
return AsyncStorage.setItem(key, JSON.stringify(value));
35+
});
36+
},
37+
delete: async key => {
38+
await AsyncStorage.removeItem(key);
39+
}
40+
};
41+
42+
export default storage;

src/tool.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ function debounce(delay, atBegin, callback) {
4242
return callback === undefined ? throttle(delay, atBegin, false) : throttle(delay, callback, atBegin !== false);
4343
}
4444

45+
function replaceReg(str) {
46+
const regStr = /\\|\$|\(|\)|\*|\+|\.|\[|\]|\?|\^|\{|\}|\|/gi;
47+
return str.replace(regStr, function (input) {
48+
return `\\${input}`;
49+
});
50+
}
51+
4552
module.exports = {
4653
throttle,
47-
debounce
54+
debounce,
55+
replaceReg
4856
};

0 commit comments

Comments
 (0)