@@ -123,7 +123,7 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
123123 public onWindowResize ( ) {
124124 super . onWindowResize ( ) ;
125125 this . updateSize ( ) ;
126- this . updateImageURLs ( ) ;
126+ this . updateImageURLs ( this . currentURLs , this . imgEl , this . sourceWebp , this . sourceJpeg ) ;
127127 }
128128
129129 public wantsDarkerScrim ( ) {
@@ -140,7 +140,7 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
140140 if ( ! this . wasShown ) {
141141 this . wasShown = true ;
142142 this . updateSize ( ) ;
143- this . updateImageURLs ( ) ;
143+ this . updateImageURLs ( this . currentURLs , this . imgEl , this . sourceWebp , this . sourceJpeg ) ;
144144 }
145145 }
146146
@@ -249,7 +249,6 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
249249 this . updateSize ( ) ;
250250 } ) ;
251251 this . imgEl . addEventListener ( "error" , ( ev ) => {
252- console . log ( this . imgEl . currentSrc ) ;
253252 if ( this . imgEl . currentSrc . indexOf ( "/system/downloadExternalMedia" ) != - 1 ) {
254253 this . failed = true ;
255254 if ( ! this . loading )
@@ -287,22 +286,51 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
287286 this . imgWrap . appendChild ( this . errorOverlay ) ;
288287 }
289288
290- private updateImageURLs ( ) {
291- var size = this . currentURLs [ 0 ] , x2size = this . currentURLs [ 0 ] ;
292- for ( var i = 0 ; i < this . currentURLs . length ; i ++ ) {
293- size = this . currentURLs [ i ] ;
294- x2size = i < this . currentURLs . length - 1 ? this . currentURLs [ i + 1 ] : null ;
295- if ( size . width >= this . imgW && size . height >= this . imgH )
289+ private updateImageURLs ( urls :PhotoViewerSizedImageURLs [ ] , imgEl :HTMLImageElement , sourceWebp :HTMLSourceElement , sourceJpeg :HTMLSourceElement ) {
290+ var viewportW = window . innerWidth ;
291+ var viewportH = window . innerHeight ;
292+ var biggest = urls [ urls . length - 1 ] ;
293+ var phW = biggest . width ;
294+ var phH = biggest . height ;
295+
296+ var w = viewportW - 2 - 120 - 34 - 50 ;
297+ var h = viewportH - 31 - 28 - 72 ;
298+ if ( w > 1280 ) {
299+ w = 1280 ;
300+ } else if ( w > 807 && w > 907 ) {
301+ w = 807 ;
302+ } else if ( w < 604 ) {
303+ w = 604 ;
304+ }
305+ if ( h < 453 ) {
306+ h = 453 ;
307+ }
308+
309+ var c = phW > w ? ( w / phW ) : 1 ;
310+ if ( phH * c > h ) {
311+ c = h / phH ;
312+ }
313+ w = Math . max ( 604 , Math . floor ( phW * c ) ) ;
314+ h = Math . max ( 453 , Math . floor ( phH * c ) ) ;
315+
316+ imgEl . style . width = w + "px" ;
317+ imgEl . style . height = h + "px" ;
318+
319+ var size = urls [ 0 ] , x2size = urls [ 0 ] ;
320+ for ( var i = 0 ; i < urls . length ; i ++ ) {
321+ size = urls [ i ] ;
322+ x2size = i < urls . length - 1 ? urls [ i + 1 ] : null ;
323+ if ( size . width >= w && size . height >= h )
296324 break ;
297325 }
298- this . imgEl . src = size . jpeg ;
326+ imgEl . src = size . jpeg ;
299327 if ( x2size ) {
300328 var multiplier = x2size . width / size . width ; // Might not actually be 2x. Will cause misalignment on retina displays if you still put "2x" there.
301- this . sourceWebp . srcset = `${ size . webp } , ${ x2size . webp } ${ multiplier } x` ;
302- this . sourceJpeg . srcset = `${ size . jpeg } , ${ x2size . jpeg } ${ multiplier } x` ;
329+ sourceWebp . srcset = `${ size . webp } , ${ x2size . webp } ${ multiplier } x` ;
330+ sourceJpeg . srcset = `${ size . jpeg } , ${ x2size . jpeg } ${ multiplier } x` ;
303331 } else {
304- this . sourceWebp . srcset = size . webp ;
305- this . sourceJpeg . srcset = size . jpeg ;
332+ sourceWebp . srcset = size . webp ;
333+ sourceJpeg . srcset = size . jpeg ;
306334 }
307335 }
308336
@@ -368,7 +396,7 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
368396 this . photos [ this . currentIndex ] . urls = urls ;
369397 this . currentURLs = urls ;
370398 this . updateSize ( ) ;
371- this . updateImageURLs ( ) ;
399+ this . updateImageURLs ( this . currentURLs , this . imgEl , this . sourceWebp , this . sourceJpeg ) ;
372400 } , ( err ) => {
373401 new MessageBox ( lang ( "error" ) , err || lang ( "network_error" ) , lang ( "close" ) ) . show ( ) ;
374402 buttons . show ( ) ;
@@ -468,6 +496,7 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
468496 this . photos [ i + offset ] = r . photos [ i ] ;
469497 }
470498 this . updateBottomPart ( ) ;
499+ this . preloadAdjacentPhotos ( ) ;
471500 this . loading = false ;
472501 this . maybeLoadMorePhotos ( ) ;
473502 if ( this . failed ) {
@@ -502,10 +531,38 @@ class DesktopPhotoViewer extends BaseMediaViewerLayer{
502531 this . currentURLs = this . photos [ index ] . urls ;
503532 this . updateImage ( ) ;
504533 this . updateSize ( ) ;
505- this . updateImageURLs ( ) ;
534+ this . updateImageURLs ( this . currentURLs , this . imgEl , this . sourceWebp , this . sourceJpeg ) ;
506535 this . updateTitle ( ) ;
507536 this . updateBottomPart ( ) ;
508537 this . maybeLoadMorePhotos ( ) ;
538+ this . preloadAdjacentPhotos ( ) ;
539+ }
540+
541+ private preloadAdjacentPhotos ( ) {
542+ if ( this . total == 1 )
543+ return ;
544+ var next = this . photos [ ( this . currentIndex + 1 ) % this . total ] ;
545+ if ( next ) {
546+ var sourceWebp , sourceJpeg , imgEl ;
547+ var pictureEl = ce ( "picture" , { } , [
548+ sourceWebp = ce ( "source" , { type : "image/webp" } ) ,
549+ sourceJpeg = ce ( "source" , { type : "image/jpeg" } ) ,
550+ imgEl = ce ( "img" )
551+ ] ) ;
552+ this . updateImageURLs ( next . urls , imgEl , sourceWebp , sourceJpeg ) ;
553+ }
554+ if ( this . total == 2 )
555+ return ;
556+ var prev = this . photos [ this . currentIndex == 0 ? this . total - 1 : this . currentIndex - 1 ] ;
557+ if ( prev ) {
558+ var sourceWebp , sourceJpeg , imgEl ;
559+ var pictureEl = ce ( "picture" , { } , [
560+ sourceWebp = ce ( "source" , { type : "image/webp" } ) ,
561+ sourceJpeg = ce ( "source" , { type : "image/jpeg" } ) ,
562+ imgEl = ce ( "img" )
563+ ] ) ;
564+ this . updateImageURLs ( prev . urls , imgEl , sourceWebp , sourceJpeg ) ;
565+ }
509566 }
510567
511568 public showNext ( ) {
0 commit comments