Authored by 孙凯

add WebView 高度自适应 review by hongmo

... ... @@ -14,17 +14,66 @@ const {
} = 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,
};
}
shouldComponentUpdate(nextProps){
if (Immutable.is(nextProps.resource, this.props.resource)) {
return false;
} else {
return true;
}
handleNavigationChange(navState) {
let heightT = parseInt(navState.title, 10) || 0; // turn NaN to 0
this.setState({
realContentHeight: heightT,
});
}
... ... @@ -33,11 +82,14 @@ export default class DetailText extends React.Component {
let template_name = resource.get('data');
let text = template_name.get('text');
text = '<html><body>' + text + '</body></html>';
return (
<View style={{flex:1}}>
<WebView style={styles.webview_style}
source= {{html: text}}
<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>
... ...