DetailText.js
3.54 KB
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
128
129
130
131
132
133
134
135
136
137
138
'use strict';
import React from 'react';
import Immutable, {Map} from 'immutable';
import {
AppRegistry,
StyleSheet,
View,
Dimensions,
TouchableOpacity,
LayoutAnimation,
Platform,
} from 'react-native';
import { WebView } from 'react-native-webview';
const BODY_TAG_PATTERN = /\<\/ *body\>/;
var script = `
;(function() {
var wrapper = document.createElement("div");
wrapper.id = "height-wrapper";
while (document.body.firstChild) {
wrapper.appendChild(document.body.firstChild);
}
document.body.appendChild(wrapper);
var i = 0;
function updateHeight() {
document.title = wrapper.clientHeight;
window.location.hash = ++i;
}
updateHeight();
window.addEventListener("load", function() {
updateHeight();
setTimeout(updateHeight, 1000);
});
window.addEventListener("resize", updateHeight);
}());
`;
const style = `
<style>
body, html, #height-wrapper {
margin: 0;
padding: 0;
}
#height-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
}
</style>
<script>
${script}
</script>
`;
const codeInject = (html) => html.replace(BODY_TAG_PATTERN, style + "</body>");
export default class DetailText extends React.Component {
constructor(props) {
super(props);
this.handleNavigationChange = this.handleNavigationChange.bind(this);
this.shouldStartLoadWithRequest = this.shouldStartLoadWithRequest.bind(this);
this.state = {
realContentHeight : 0,
};
}
shouldComponentUpdate(nextProps,nextState){
if (Immutable.is(nextProps.resource, this.props.resource) && nextState.realContentHeight==this.state.realContentHeight) {
return false;
} else {
return true;
}
}
handleNavigationChange(navState) {
let heightT = parseInt(navState.title, 10) || this.state.realContentHeight; // turn NaN to 0
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
this.setState({
realContentHeight: heightT,
});
}
shouldStartLoadWithRequest(event) {
if (event.navigationType == 'click') {
this.props.onPressLink && this.props.onPressLink(event.url);
return false;
}
return true;
}
render() {
let {resource} = this.props;
let template_name = resource.get('data');
let text = template_name.get('text');
text = '<html><style type="text/css">img {max-width: 100%;}</style><body>' + text + '</body></html>'
let hasLink = false;
if (text.indexOf('href=') >= 0) {
hasLink = true;
}
return (
<View style={{width: width, height: this.state.realContentHeight, backgroundColor: 'white'}}>
<WebView style={{
width: width - 40,
height: this.state.realContentHeight,
backgroundColor: 'white',
marginRight: 20,
marginLeft: 20
}}
source={{html: codeInject(text), baseUrl: ''}}
scrollEnabled={false}
javaScriptEnabled={true}
decelerationRate="normal"
domStorageEnabled={true}
onNavigationStateChange={this.handleNavigationChange}
onShouldStartLoadWithRequest={hasLink ? this.shouldStartLoadWithRequest : null}
>
</WebView>
</View>
);
}
};
let {width, height} = Dimensions.get('window');
let styles = StyleSheet.create({
});