-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStory.js
127 lines (108 loc) · 2.15 KB
/
Story.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
var React = require('react-native');
var dataWarehouse = require('./DataWarehouse');
var {
View,
StyleSheet,
Text,
NavigatorIOS,
Image,
Component,
WebView,
ScrollView,
Dimensions,
Animated,
ActivityIndicatorIOS,
} = React;
var WEBVIEW_REF = 'webview';
class Story extends Component{
constructor(props){
super(props);
this.state={
loaded: false
};
}
componentDidMount(){
console.log(this.props.storyId);
//id为文章的id
this._fetchData(this.props.storyId);
}
_fetchData(id){
var url = dataWarehouse.story + id;
fetch(url)
.then(response => response.json())
.then(json => this._handleResponse(json))
.catch(err=>{
this.setState({
loaded: true,
message: 'timeout'
});
});
}
_handleResponse(json){
//handle the html
var body = json.body;
var image = json.image;
var css = json.css[0];
var headImg = '<img src="'+image+'" width="100%" height="200"/>'
var html = body.replace('<div class="img-place-holder"></div>',headImg)+'<link href="'+css+'" rel="stylesheet"/>';
console.log(html);
this.setState({
loaded: true,
message: '',
html: html,
image: image,
});
}
render(){
console.log('render story');
console.log(this.state.loaded);
if(this.state.loaded){
var html = this.state.html;
var image = this.state.image;
return (
<ScrollView>
<WebView
ref={WEBVIEW_REF}
automaticallyAdjustContentInsets={false}
source={{html: html}}
style={styles.webview}
//contentInset={{top:0,bottom:47}}
/>
</ScrollView>
);
} else {
return (
<View>
<ActivityIndicatorIOS
animating={true}
size={'large'} />
</View>
);
}
}
}
var styles = StyleSheet.create({
container:{
flex: 1,
padding: 5,
marginTop: 15,
alignItems: 'center',
justifyContent: 'center'
},
webview: {
height: Dimensions.get('window').height,
},
headerImg: {
//width: 320,
height: 200,
top: 0,
right: 0,
left: 0
},
content: {
flex: 1,
//marginTop: 36,
},
});
module.exports = Story;