ItemWebView.js 2.93 KB
import React, {Component} from "react";
import {StyleSheet, View} from "react-native";
import {zoomH, zoomW} from "../../utils/getSize";
import MyWebView from "react-native-webview-autoheight";
import RichText from "../../widget/RichText";

//图片资源
const defaultIcon = require('../../img/defaultIcon.png');
/**
 * 文章类型 item
 *
 * 调用者需要传入的参数数据源示例(字段必须要保持一致):{
 *                                isForward: false,//是否是转发
 *                                forwardContent:'转发了',//转发填写的内容,不是转发填'',
 *                                threadDetailContent:'xxxxx',//原帖子的详细内容,富文本样式
 *                          }
 */
export default class ItemWebView extends Component {

    //自定义属性默认值
    static defaultProps = {
        data:{},//数据源
        nav:{},//导航跳转
    };

    // 构造
    constructor(props) {
        super(props);
        console.log(111);

        let forwardContent;
        let threadDetailContent;
        if (this.props.data.forwardContent){
            forwardContent = this.props.data.forwardContent.replace(/\<img/g,'<img width=100% height="auto"');

        }
        if (this.props.data.threadDetailContent) {
            threadDetailContent = this.props.data.threadDetailContent.replace(/\<img/g,'<img width=100% height="auto"')

        }

        this.state = {
            forwardContent:forwardContent?forwardContent:this.props.data.forwardContent,
            threadDetailContent:threadDetailContent?threadDetailContent:this.props.data.threadDetailContent
        }
    }
    render() {
        return (
            <View>
                {/*转发*/}
                {!!this.props.data.isForward && (
                    <View style={{ width: "100%", marginLeft: 15 / zoomW,marginRight:15/zoomW,marginTop:8/zoomH,marginBottom:8/zoomH }}>
                        <RichText
                            item={this.state.forwardContent}
                            nav={this.props.nav}
                            clearNumberOfLine  ={true}
                        />
                    </View>
                )}
                <View
                    style={
                        !!this.props.data.isForward
                            ? styles.forwardingBg
                            : styles.commonBg
                    }
                >
                    {this.state.threadDetailContent && <MyWebView
                        style= {{width:'100%'}}
                        source={{html:this.state.threadDetailContent}}
                        startInLoadingState={true}
                    />}
                </View>

            </View>
        )
    }


}

const styles = StyleSheet.create({
    forwardingBg: {
        marginLeft:15/zoomW,
        marginRight:15/zoomW,
        paddingTop:14/zoomH,
        borderWidth: 1,
        borderColor: "#eeeeee"
    },

    commonBg: {

    },
});