Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 6da899e

Browse files
author
soliury
committed
make image more suitable
1 parent e605351 commit 6da899e

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/components/CommentHtml.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React, {
77
import Html from './base/Html';
88

99

10-
const { height, width } = Dimensions.get('window');
10+
const {height, width} = Dimensions.get('window');
1111

1212

1313
class CommentHtml extends Component {
@@ -24,6 +24,7 @@ class CommentHtml extends Component {
2424
content={this.props.content}
2525
style={this.styles}
2626
router={this.props.router}
27+
maxImageWidth={width - 80 - 20}
2728
/>
2829
)
2930
}
@@ -172,9 +173,10 @@ const styles = StyleSheet.create({
172173
borderLeftWidth: 3
173174
},
174175
img: {
175-
width: width - 80,
176-
height: width - 80,
177-
resizeMode: Image.resizeMode.contain
176+
width: width - 80 - 20,
177+
height: width - 80 - 20,
178+
resizeMode: Image.resizeMode.contain,
179+
margin: 10
178180
}
179181
});
180182

src/components/base/Html.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import React, {
55
Dimensions,
66
PropTypes
77
} from 'react-native';
8+
import _ from 'lodash';
89
import HtmlRender from 'react-native-html-render';
9-
import { parseImgUrl, link } from '../../utils';
10+
import {parseImgUrl, link} from '../../utils';
1011

1112

12-
const { width, height } = Dimensions.get('window');
13-
13+
const {width, height} = Dimensions.get('window');
14+
const defaultMaxImageWidth = width - 30 - 20;
1415

1516
const regs = {
1617
http: {
1718
topic: /^https?:\/\/cnodejs\.org\/topic\/\w*/,
1819
user: /^https?:\/\/cnodejs\.org\/user\/\w*/
19-
}
20+
},
21+
gif: /.*\.gif$/
2022
};
2123

2224

@@ -27,6 +29,16 @@ class Html extends Component {
2729
};
2830

2931

32+
static defaultProps = {
33+
maxImageWidth: defaultMaxImageWidth
34+
};
35+
36+
constructor(props) {
37+
super(props);
38+
this._images = {};
39+
}
40+
41+
3042
_onLinkPress(url) {
3143
let router = this.props.router;
3244

@@ -64,6 +76,23 @@ class Html extends Component {
6476
}
6577

6678

79+
_onImageLoadEnd(uri, imageId) {
80+
const {maxImageWidth} = this.props;
81+
Image.getSize(uri, (w, h)=> {
82+
if (w >= maxImageWidth) {
83+
h = (maxImageWidth / w) * h;
84+
w = maxImageWidth;
85+
}
86+
this._images[imageId] && this._images[imageId].setNativeProps({
87+
style: {
88+
width: w,
89+
height: h
90+
}
91+
});
92+
});
93+
}
94+
95+
6796
_renderNode(node, index, parent, type) {
6897
const name = node.name;
6998
const imgStyle = this.props.imgStyle || styles.img;
@@ -72,13 +101,16 @@ class Html extends Component {
72101
if (node.type == 'block' && type == 'block') {
73102
if (name == 'img') {
74103
const uri = parseImgUrl(node.attribs.src);
75-
if (/.*\.gif$/.test(uri)) return null;
104+
if (regs.gif.test(uri)) return null;
105+
const imageId = _.uniqueId('image_');
76106
return (
77107
<Image
78-
key={index}
108+
key={imageId}
109+
ref={view=>this._images[imageId]=view}
79110
source={{uri:uri}}
80-
style={imgStyle}>
81-
</Image>
111+
style={imgStyle}
112+
onLoadEnd={this._onImageLoadEnd.bind(this, uri, imageId)}
113+
/>
82114
)
83115
}
84116
}
@@ -100,9 +132,11 @@ class Html extends Component {
100132

101133
const styles = StyleSheet.create({
102134
img: {
103-
width: width - 30,
104-
height: width - 30,
105-
resizeMode: Image.resizeMode.contain
135+
width: defaultMaxImageWidth,
136+
height: defaultMaxImageWidth,
137+
resizeMode: Image.resizeMode.cover,
138+
borderRadius: 5,
139+
margin: 10
106140
}
107141
});
108142

0 commit comments

Comments
 (0)