DetailText.js
2.2 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
'use strict';
import React from 'react';
import ReactNative from 'react-native';
import Immutable, {Map} from 'immutable';
const {
AppRegistry,
StyleSheet,
View,
Dimensions,
TouchableOpacity,
WebView,
} = ReactNative;
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.state = {
realContentHeight : 10,
};
}
handleNavigationChange(navState) {
let heightT = parseInt(navState.title, 10) || 0; // turn NaN to 0
this.setState({
realContentHeight: heightT,
});
}
render() {
let {resource} = this.props;
let template_name = resource.get('data');
let text = template_name.get('text');
text = '<html><body>' + text + '</body></html>';
return (
<View style={{width: width,height: this.state.realContentHeight}}>
<WebView style={{width: width,height: this.state.realContentHeight}}
source= {{html: codeInject(text)}}
scrollEnabled={false}
onNavigationStateChange={this.handleNavigationChange}
>
</WebView>
</View>
);
return null;
}
};
let {width, height} = Dimensions.get('window');
let styles = StyleSheet.create({
webview_style: {
backgroundColor: 'red',
height: 100,
},
});