@@ -5,18 +5,20 @@ import React, {
5
5
Dimensions ,
6
6
PropTypes
7
7
} from 'react-native' ;
8
+ import _ from 'lodash' ;
8
9
import HtmlRender from 'react-native-html-render' ;
9
- import { parseImgUrl , link } from '../../utils' ;
10
+ import { parseImgUrl , link } from '../../utils' ;
10
11
11
12
12
- const { width, height } = Dimensions . get ( 'window' ) ;
13
-
13
+ const { width, height} = Dimensions . get ( 'window' ) ;
14
+ const defaultMaxImageWidth = width - 30 - 20 ;
14
15
15
16
const regs = {
16
17
http : {
17
18
topic : / ^ h t t p s ? : \/ \/ c n o d e j s \. o r g \/ t o p i c \/ \w * / ,
18
19
user : / ^ h t t p s ? : \/ \/ c n o d e j s \. o r g \/ u s e r \/ \w * /
19
- }
20
+ } ,
21
+ gif : / .* \. g i f $ /
20
22
} ;
21
23
22
24
@@ -27,6 +29,16 @@ class Html extends Component {
27
29
} ;
28
30
29
31
32
+ static defaultProps = {
33
+ maxImageWidth : defaultMaxImageWidth
34
+ } ;
35
+
36
+ constructor ( props ) {
37
+ super ( props ) ;
38
+ this . _images = { } ;
39
+ }
40
+
41
+
30
42
_onLinkPress ( url ) {
31
43
let router = this . props . router ;
32
44
@@ -64,6 +76,23 @@ class Html extends Component {
64
76
}
65
77
66
78
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
+
67
96
_renderNode ( node , index , parent , type ) {
68
97
const name = node . name ;
69
98
const imgStyle = this . props . imgStyle || styles . img ;
@@ -72,13 +101,16 @@ class Html extends Component {
72
101
if ( node . type == 'block' && type == 'block' ) {
73
102
if ( name == 'img' ) {
74
103
const uri = parseImgUrl ( node . attribs . src ) ;
75
- if ( / .* \. g i f $ / . test ( uri ) ) return null ;
104
+ if ( regs . gif . test ( uri ) ) return null ;
105
+ const imageId = _ . uniqueId ( 'image_' ) ;
76
106
return (
77
107
< Image
78
- key = { index }
108
+ key = { imageId }
109
+ ref = { view => this . _images [ imageId ] = view }
79
110
source = { { uri :uri } }
80
- style = { imgStyle } >
81
- </ Image >
111
+ style = { imgStyle }
112
+ onLoadEnd = { this . _onImageLoadEnd . bind ( this , uri , imageId ) }
113
+ />
82
114
)
83
115
}
84
116
}
@@ -100,9 +132,11 @@ class Html extends Component {
100
132
101
133
const styles = StyleSheet . create ( {
102
134
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
106
140
}
107
141
} ) ;
108
142
0 commit comments