@@ -333,33 +333,27 @@ module.exports = React.createClass({
333
333
if ( excessHeight <= 0 ) {
334
334
return ;
335
335
}
336
- var itemlist = this . refs . itemlist ;
337
- var tiles = itemlist . children ;
336
+ const tiles = this . refs . itemlist . children ;
338
337
339
338
// The scroll token of the first/last tile to be unpaginated
340
339
let markerScrollToken = null ;
341
340
342
- // Subtract clientHeights to simulate the events being unpaginated whilst counting
343
- // the events to be unpaginated.
344
- if ( backwards ) {
345
- // Iterate forwards from start of tiles, subtracting event tile height
346
- let i = 0 ;
347
- while ( i < tiles . length && excessHeight > tiles [ i ] . clientHeight ) {
348
- excessHeight -= tiles [ i ] . clientHeight ;
349
- if ( tiles [ i ] . dataset . scrollToken ) {
350
- markerScrollToken = tiles [ i ] . dataset . scrollToken ;
351
- }
352
- i ++ ;
341
+ // Subtract heights of tiles to simulate the tiles being unpaginated until the
342
+ // excess height is less than the height of the next tile to subtract. This
343
+ // prevents excessHeight becoming negative, which could lead to future
344
+ // pagination.
345
+ //
346
+ // If backwards is true, we unpaginate (remove) tiles from the back (top).
347
+ for ( let i = 0 ; i < tiles . length ; i ++ ) {
348
+ const tile = tiles [ backwards ? tiles . length - 1 - i : i ] ;
349
+ // Subtract height of tile as if it were unpaginated
350
+ excessHeight -= tile . clientHeight ;
351
+ // The tile may not have a scroll token, so guard it
352
+ if ( tile . dataset . scrollToken ) {
353
+ markerScrollToken = tile . dataset . scrollToken ;
353
354
}
354
- } else {
355
- // Iterate backwards from end of tiles, subtracting event tile height
356
- let i = tiles . length - 1 ;
357
- while ( i > 0 && excessHeight > tiles [ i ] . clientHeight ) {
358
- excessHeight -= tiles [ i ] . clientHeight ;
359
- if ( tiles [ i ] . dataset . scrollToken ) {
360
- markerScrollToken = tiles [ i ] . dataset . scrollToken ;
361
- }
362
- i -- ;
355
+ if ( tile . clientHeight > excessHeight ) {
356
+ break ;
363
357
}
364
358
}
365
359
@@ -589,24 +583,34 @@ module.exports = React.createClass({
589
583
var itemlist = this . refs . itemlist ;
590
584
var wrapperRect = ReactDOM . findDOMNode ( this ) . getBoundingClientRect ( ) ;
591
585
var messages = itemlist . children ;
586
+ let newScrollState = null ;
592
587
593
588
for ( var i = messages . length - 1 ; i >= 0 ; -- i ) {
594
589
var node = messages [ i ] ;
595
590
if ( ! node . dataset . scrollToken ) continue ;
596
591
597
592
var boundingRect = node . getBoundingClientRect ( ) ;
598
- if ( boundingRect . bottom < wrapperRect . bottom ) {
599
- this . scrollState = {
600
- stuckAtBottom : false ,
601
- trackedScrollToken : node . dataset . scrollToken ,
602
- pixelOffset : wrapperRect . bottom - boundingRect . bottom ,
603
- } ;
604
- debuglog ( "ScrollPanel: saved scroll state" , this . scrollState ) ;
605
- return ;
593
+ newScrollState = {
594
+ stuckAtBottom : false ,
595
+ trackedScrollToken : node . dataset . scrollToken ,
596
+ pixelOffset : wrapperRect . bottom - boundingRect . bottom ,
597
+ } ;
598
+ // If the bottom of the panel intersects the ClientRect of node, use this node
599
+ // as the scrollToken.
600
+ // If this is false for the entire for-loop, we default to the last node
601
+ // (which is why newScrollState is set on every iteration).
602
+ if ( boundingRect . top < wrapperRect . bottom ) {
603
+ // Use this node as the scrollToken
604
+ break ;
606
605
}
607
606
}
608
-
609
- debuglog ( "ScrollPanel: unable to save scroll state: found no children in the viewport" ) ;
607
+ // This is only false if there were no nodes with `node.dataset.scrollToken` set.
608
+ if ( newScrollState ) {
609
+ this . scrollState = newScrollState ;
610
+ debuglog ( "ScrollPanel: saved scroll state" , this . scrollState ) ;
611
+ } else {
612
+ debuglog ( "ScrollPanel: unable to save scroll state: found no children in the viewport" ) ;
613
+ }
610
614
} ,
611
615
612
616
_restoreSavedScrollState : function ( ) {
0 commit comments