@@ -5,18 +5,20 @@ import React, {
55 Dimensions ,
66 PropTypes
77} from 'react-native' ;
8+ import _ from 'lodash' ;
89import 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
1516const regs = {
1617 http : {
1718 topic : / ^ h t t p s ? : \/ \/ c n o d e j s \. o r g \/ t o p i c \/ \w * / ,
1819 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 $ /
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 ( / .* \. g i f $ / . 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
101133const 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