DetailText.js 2.28 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.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,backgroundColor: 'white'}}>
				<WebView style={{width: width-40,height: this.state.realContentHeight,backgroundColor: 'white',marginRight: 20,marginLeft: 20}}
					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,
	},
});