Repository navigation
Expand file tree
/
Copy pathHtml.js
More file actions
126 lines (99 loc) · 2.48 KB
/
Copy pathHtml.js
File metadata and controls
126 lines (99 loc) · 2.48 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
import React, {Component, PropTypes} from 'react';
import {StyleSheet, Image, Dimensions} from 'react-native';
import _ from 'lodash';
import HtmlRender from 'react-native-html-render';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import CustomImage from './CustomImage';
import {parseImgUrl, link} from '../../utils';
const {width, height} = Dimensions.get('window');
const defaultMaxImageWidth = width - 30 - 20;
const regs = {
http: {
topic: /^https?:\/\/cnodejs\.org\/topic\/\w*/,
user: /^https?:\/\/cnodejs\.org\/user\/\w*/
},
gif: /.*\.gif$/
};
const styles = StyleSheet.create({
defaultImg: {
height: defaultMaxImageWidth,
width: defaultMaxImageWidth,
resizeMode: Image.resizeMode.cover,
borderRadius: 5,
margin: 10
}
});
class Html extends Component {
static propTypes = {
router: PropTypes.object,
imgStyle: PropTypes.object
};
static defaultProps = {
maxImageWidth: defaultMaxImageWidth
};
constructor(props) {
super(props);
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
_onLinkPress(url) {
let router = this.props.router;
if (/^\/user\/\w*/.test(url)) {
let authorName = url.replace(/^\/user\//, '');
router.toUser({
userName: authorName
});
}
if (/^https?:\/\/.*/.test(url)) {
if (regs.http.topic.test(url)) {
let topicId = url.replace(/^https?:\/\/cnodejs\.org\/topic\//, '');
return router.toTopic({
id: topicId
});
}
if (regs.http.user.test(url)) {
let userName = url.replace(/^https?:\/\/cnodejs\.org\/user\//, '');
return router.toUser({
userName: userName
});
}
link(url);
}
if (/^mailto:\w*/.test(url)) {
link(url);
}
}
_renderNode(node, index, parent, type) {
const name = node.name;
const {imgStyle=styles.defaultImg, maxImageWidth} = this.props;
if (node.type == 'block' && type == 'block') {
if (name == 'img') {
const uri = parseImgUrl(node.attribs.src);
if (regs.gif.test(uri)) return null;
const imageId = _.uniqueId('image_');
return (
<CustomImage
key={imageId}
uri={uri}
style={imgStyle}
defaultSize={{
height: 300,
width: defaultMaxImageWidth
}}
maxImageWidth={maxImageWidth}
/>
);
}
}
}
render() {
return (
<HtmlRender
value={this.props.content}
stylesheet={this.props.style}
onLinkPress={this._onLinkPress.bind(this)}
renderNode={this._renderNode.bind(this)}
/>
);
}
}
export default Html;