DetailText.js 3.22 KB
'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.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) || 0; // turn NaN to 0
        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('<a 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)}}
					scrollEnabled={false}
                    javaScriptEnabled={true}
                    decelerationRate="normal"
                    domStorageEnabled={true}
                    onNavigationStateChange={this.handleNavigationChange}
                    onShouldStartLoadWithRequest={hasLink?this.shouldStartLoadWithRequest:null}
				>
				</WebView>
			</View>
		);
		return null;
    }
};


let {width, height} = Dimensions.get('window');

let styles = StyleSheet.create({
	webview_style: {
		backgroundColor: 'red',
		height: 100,
	},
});