@@ -15,7 +15,6 @@ import {
1515 shareReplay ,
1616 switchMap ,
1717 take ,
18- withLatestFrom ,
1918} from "rxjs" ;
2019import {
2120 __ ,
@@ -35,6 +34,7 @@ import {
3534 without ,
3635 zip ,
3736} from "ramda" ;
37+ import { getVerticalScrollParent } from "./utilites" ;
3838
3939export function computeHeightAboveWindowOf ( el : Element ) : number {
4040 const top = el . getBoundingClientRect ( ) . top ;
@@ -228,10 +228,12 @@ interface PipelineInput {
228228 scrollTo$ : Observable < number | undefined > ;
229229}
230230
231+ export type ScrollAction = [ Element , number ] ;
232+
231233interface PipelineOutput {
232234 buffer$ : Observable < InternalItem [ ] > ;
233235 contentHeight$ : Observable < number > ;
234- windowScrollTo $ : Observable < number > ;
236+ scrollAction $ : Observable < ScrollAction > ;
235237}
236238
237239export function pipeline ( {
@@ -262,20 +264,26 @@ export function pipeline({
262264 // endregion
263265
264266 // region: scroll to a given item by index
265- const windowScrollTo$ = scrollTo$ . pipe (
267+ const scrollAction$ : Observable < ScrollAction > = scrollTo$ . pipe (
266268 filter ( complement ( isNil ) ) ,
267- switchMap ( ( scrollTo ) =>
268- combineLatest ( [ of ( scrollTo ) , resizeMeasurement$ , rootResize$ ] ) . pipe (
269- take ( 1 )
270- )
269+ switchMap < number , Observable < [ number , ResizeMeasurement , Element ] > > (
270+ ( scrollTo ) =>
271+ combineLatest ( [ of ( scrollTo ) , resizeMeasurement$ , rootResize$ ] ) . pipe (
272+ take ( 1 )
273+ )
271274 ) ,
272- map (
273- ( [ scrollTo , { columns, itemHeightWithGap } , rootEl ] ) =>
274- // The offset within the grid
275- Math . floor ( scrollTo / columns ) * itemHeightWithGap +
276- // The offset of grid root to the document
277- ( rootEl . getBoundingClientRect ( ) . top +
278- document . documentElement . scrollTop )
275+ map < [ number , ResizeMeasurement , Element ] , ScrollAction > (
276+ ( [ scrollTo , { columns, itemHeightWithGap } , rootEl ] ) => {
277+ const verticalScrollEl = getVerticalScrollParent ( rootEl ) ;
278+
279+ const scrollTop =
280+ // The offset within the grid container
281+ Math . floor ( ( scrollTo - 1 ) / columns ) * itemHeightWithGap +
282+ // Offset to the offsetParent
283+ ( rootEl instanceof HTMLElement ? rootEl . offsetTop : 0 ) ;
284+
285+ return [ verticalScrollEl , scrollTop ] ;
286+ }
279287 )
280288 ) ;
281289 // endregion
@@ -319,5 +327,5 @@ export function pipeline({
319327 ) . pipe ( scan ( accumulateBuffer , [ ] ) ) ;
320328 // endregion
321329
322- return { buffer$, contentHeight$, windowScrollTo $ } ;
330+ return { buffer$, contentHeight$, scrollAction$ : scrollAction $ } ;
323331}
0 commit comments