forked from jsdf/react-native-htmlview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLView.js
More file actions
143 lines (125 loc) · 3.45 KB
/
HTMLView.js
File metadata and controls
143 lines (125 loc) · 3.45 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, {PureComponent} from 'react';
import htmlToElement from './htmlToElement';
import {Linking, Platform, StyleSheet, View} from 'react-native';
const boldStyle = {fontWeight: 'bold'};
const italicStyle = {fontStyle: 'italic'};
const underlineStyle = {textDecorationLine: 'underline'};
const strikethroughStyle = {textDecorationLine: 'line-through'};
const codeStyle = {fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace'};
const baseStyles = StyleSheet.create({
b: boldStyle,
strong: boldStyle,
i: italicStyle,
em: italicStyle,
u: underlineStyle,
s: strikethroughStyle,
strike: strikethroughStyle,
pre: codeStyle,
code: codeStyle,
a: {
fontWeight: 'bold',
color: '#007AFF',
},
h1: {fontWeight: 'bold', fontSize: 36},
h2: {fontWeight: 'bold', fontSize: 30},
h3: {fontWeight: 'bold', fontSize: 24},
h4: {fontWeight: 'bold', fontSize: 18},
h5: {fontWeight: 'bold', fontSize: 14},
h6: {fontWeight: 'bold', fontSize: 12},
});
const htmlToElementOptKeys = [
'lineBreak',
'paragraphBreak',
'bullet',
'TextComponent',
'textComponentProps',
'NodeComponent',
'nodeComponentProps',
];
class HtmlView extends PureComponent {
constructor() {
super();
this.state = {
element: null,
};
}
componentDidMount() {
this.mounted = true;
this.startHtmlRender(this.props.value);
}
componentDidUpdate(prevProps) {
if (this.props.value !== prevProps.value || this.props.stylesheet !== prevProps.stylesheet || this.props.textComponentProps !== prevProps.textComponentProps || this.props.nodeComponentProps !== prevProps.nodeComponentProps) {
this.startHtmlRender(this.props.value, this.props.stylesheet, this.props.textComponentProps, this.props.nodeComponentProps);
}
}
componentWillUnmount() {
this.mounted = false;
}
startHtmlRender(value, style, textComponentProps, nodeComponentProps) {
const {
addLineBreaks,
onLinkPress,
onLinkLongPress,
stylesheet,
renderNode,
onError,
} = this.props;
if (!value) {
this.setState({element: null});
}
const opts = {
addLineBreaks,
linkHandler: onLinkPress,
linkLongPressHandler: onLinkLongPress,
styles: {...baseStyles, ...stylesheet, ...style},
customRenderer: renderNode,
};
htmlToElementOptKeys.forEach(key => {
if (typeof this.props[key] !== 'undefined') {
opts[key] = this.props[key];
}
});
if (textComponentProps) {
opts.textComponentProps = textComponentProps;
}
if (nodeComponentProps) {
opts.nodeComponentProps = nodeComponentProps;
}
htmlToElement(value, opts, (err, element) => {
if (err) {
onError(err);
}
if (this.mounted) {
this.setState({element});
}
});
}
render() {
const {RootComponent, style} = this.props;
const {element} = this.state;
if (element) {
return (
<RootComponent
{...this.props.rootComponentProps}
style={style}
>
{element}
</RootComponent>
);
}
return (
<RootComponent
{...this.props.rootComponentProps}
style={style}
/>
);
}
}
HtmlView.defaultProps = {
addLineBreaks: true,
onLinkPress: url => Linking.openURL(url),
onLinkLongPress: null,
onError: console.error.bind(console),
RootComponent: element => <View {...element} />, // eslint-disable-line react/display-name
};
export default HtmlView;