@@ -15,7 +15,6 @@ import {
15
15
shareReplay ,
16
16
switchMap ,
17
17
take ,
18
- withLatestFrom ,
19
18
} from "rxjs" ;
20
19
import {
21
20
__ ,
@@ -35,6 +34,7 @@ import {
35
34
without ,
36
35
zip ,
37
36
} from "ramda" ;
37
+ import { getVerticalScrollParent } from "./utilites" ;
38
38
39
39
export function computeHeightAboveWindowOf ( el : Element ) : number {
40
40
const top = el . getBoundingClientRect ( ) . top ;
@@ -228,10 +228,12 @@ interface PipelineInput {
228
228
scrollTo$ : Observable < number | undefined > ;
229
229
}
230
230
231
+ export type ScrollAction = [ Element , number ] ;
232
+
231
233
interface PipelineOutput {
232
234
buffer$ : Observable < InternalItem [ ] > ;
233
235
contentHeight$ : Observable < number > ;
234
- windowScrollTo $ : Observable < number > ;
236
+ scrollAction $ : Observable < ScrollAction > ;
235
237
}
236
238
237
239
export function pipeline ( {
@@ -262,20 +264,26 @@ export function pipeline({
262
264
// endregion
263
265
264
266
// region: scroll to a given item by index
265
- const windowScrollTo$ = scrollTo$ . pipe (
267
+ const scrollAction$ : Observable < ScrollAction > = scrollTo$ . pipe (
266
268
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
+ )
271
274
) ,
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
+ }
279
287
)
280
288
) ;
281
289
// endregion
@@ -319,5 +327,5 @@ export function pipeline({
319
327
) . pipe ( scan ( accumulateBuffer , [ ] ) ) ;
320
328
// endregion
321
329
322
- return { buffer$, contentHeight$, windowScrollTo $ } ;
330
+ return { buffer$, contentHeight$, scrollAction$ : scrollAction $ } ;
323
331
}
0 commit comments