1- import { assign } from "lodash" ;
21import * as React from "react" ;
32
43export type DivProps = React . HTMLProps < HTMLImageElement > ;
@@ -19,7 +18,7 @@ export interface ProgressiveImageState {
1918
2019export class ProgressiveImage extends React . Component < ProgressiveImageProps & DivProps & ImageProps , ProgressiveImageState > {
2120
22- private clonedProps : React . HTMLProps < HTMLDivElement | HTMLImageElement > = { } ;
21+ private clonedProps : React . HTMLProps < HTMLDivElement & HTMLImageElement > = { } ;
2322
2423 static defaultProps = {
2524 transitionTime : 500 ,
@@ -30,24 +29,37 @@ export class ProgressiveImage extends React.Component<ProgressiveImageProps & Di
3029 const { src, preview} = this . props ;
3130 this . setState ( { src : "" , blur : 10 } ) ;
3231 this . cloneProps ( ) ;
33- fetch ( preview )
34- . then ( ( ) => this . setState ( { src : preview , blur : 10 } ) )
35- . then ( ( ) => fetch ( src ) )
36- . then ( ( ) => this . setState ( { src, blur : 0 } ) ) ;
32+ this . fetch ( preview )
33+ . then ( previewDataURI => this . setState ( { src : previewDataURI , blur : 10 } ) )
34+ . then ( ( ) => this . fetch ( src ) )
35+ . then ( srcDataURI => this . setState ( { src : srcDataURI , blur : 0 } ) ) ;
3736 }
38-
3937 render ( ) {
4038 const { src, style, background} = this . props ;
4139 return background ?
42- < div style = { assign ( this . getBackgroundStyle ( ) , style ) } { ...this . clonedProps } />
43- :
44- < img src = { src } style = { assign ( this . getStyle ( ) , style ) } { ...this . clonedProps } />
45- ;
40+ < div style = { Object . assign ( this . getBackgroundStyle ( ) , style ) } { ...this . clonedProps } />
41+ :
42+ < img src = { src } style = { Object . assign ( this . getStyle ( ) , style ) } { ...this . clonedProps } />
43+ ;
44+ }
45+
46+ private fetch ( uri : string ) : Promise < string > {
47+ return new Promise ( resolve => {
48+ fetch ( uri )
49+ . then ( response => response . blob ( ) )
50+ . then ( blob => {
51+ const fp = new FileReader ( ) ;
52+ fp . onload = ( ) => {
53+ resolve ( fp . result as string ) ;
54+ } ;
55+ fp . readAsDataURL ( blob ) ;
56+ } ) ;
57+ } ) ;
4658 }
4759
4860 private cloneProps ( ) {
4961 Object . keys ( this . props )
50- . filter ( prop => [ "style" , "src" , "preview" , "background" ] . indexOf ( prop ) === - 1 )
62+ . filter ( prop => [ "style" , "src" , "preview" , "background" , "transitionTime" , "timingFunction" ] . indexOf ( prop ) === - 1 )
5163 . forEach ( prop => this . clonedProps [ prop ] = this . props [ prop ] ) ;
5264 }
5365
@@ -65,6 +77,6 @@ export class ProgressiveImage extends React.Component<ProgressiveImageProps & Di
6577 const style = {
6678 backgroundImage : `url(${ src } )`
6779 } ;
68- return assign ( style , this . getStyle ( ) ) ;
80+ return Object . assign ( style , this . getStyle ( ) ) ;
6981 }
70- }
82+ }
0 commit comments