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
89 lines (74 loc) · 1.78 KB
/
HTMLView.js
File metadata and controls
89 lines (74 loc) · 1.78 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
var React = require('react')
var ReactNative = require('react-native')
var htmlToElement = require('./htmlToElement')
var {
Linking,
StyleSheet,
Text,
} = ReactNative
var HTMLView = React.createClass({
propTypes: {
value: React.PropTypes.string,
stylesheet: React.PropTypes.object,
onLinkPress: React.PropTypes.func,
onError: React.PropTypes.func,
renderNode: React.PropTypes.func,
},
getDefaultProps() {
return {
onLinkPress: url => Linking.openURL(url),
onError: console.error.bind(console),
}
},
getInitialState() {
return {
element: null,
}
},
componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this.startHtmlRender(nextProps.value)
}
},
componentDidMount() {
this.mounted = true
this.startHtmlRender(this.props.value)
},
componentWillUnmount() {
this.mounted = false
},
startHtmlRender(value) {
if (!value) return this.setState({element: null})
var opts = {
linkHandler: this.props.onLinkPress,
styles: Object.assign({}, baseStyles, this.props.stylesheet),
customRenderer: this.props.renderNode,
}
htmlToElement(value, opts, (err, element) => {
if (err) return this.props.onError(err)
if (this.mounted) this.setState({element})
})
},
render() {
if (this.state.element) {
return <Text children={this.state.element} />
}
return <Text />
},
})
var boldStyle = {fontWeight: '500'}
var italicStyle = {fontStyle: 'italic'}
var codeStyle = {fontFamily: 'Menlo'}
var baseStyles = StyleSheet.create({
b: boldStyle,
strong: boldStyle,
i: italicStyle,
em: italicStyle,
pre: codeStyle,
code: codeStyle,
a: {
fontWeight: '500',
color: '#007AFF',
},
})
module.exports = HTMLView