From 73f0066943b78bda9a4b8f76b93a9a4a5423960c Mon Sep 17 00:00:00 2001 From: inokawa <48897392+inokawa@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:11:57 +0900 Subject: [PATCH] Refactor startOffset in WindowVirtualizer --- src/core/scroller.ts | 24 +++++++++++------------- src/core/store.ts | 11 +++++++++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/core/scroller.ts b/src/core/scroller.ts index db395f6dd..81add978d 100644 --- a/src/core/scroller.ts +++ b/src/core/scroller.ts @@ -13,6 +13,7 @@ import { ACTION_MANUAL_SCROLL, SCROLL_IDLE, ACTION_BEFORE_MANUAL_SMOOTH_SCROLL, + ACTION_START_OFFSET_CHANGE, } from "./store"; import { ScrollToIndexOpts } from "./types"; import { debounce, timeout, clamp, microtask } from "./utils"; @@ -42,7 +43,8 @@ const createScrollObserver = ( value: number, shift: boolean, isMomentumScrolling: boolean - ) => void + ) => void, + getStartOffset?: () => number ) => { const now = Date.now; @@ -73,7 +75,11 @@ const createScrollObserver = ( stillMomentumScrolling = true; } + if (getStartOffset) { + store._update(ACTION_START_OFFSET_CHANGE, getStartOffset()); + } store._update(ACTION_SCROLL, getScrollOffset()); + onScrollEnd(); }; @@ -353,8 +359,6 @@ export const createWindowScroller = ( return { _observe(container) { - let prevStartOffset = 0; - const scrollOffsetKey = isHorizontal ? "scrollX" : "scrollY"; const document = getCurrentDocument(container); @@ -392,24 +396,18 @@ export const createWindowScroller = ( store, window, isHorizontal, - () => - normalizeOffset(window[scrollOffsetKey], isHorizontal) - - (prevStartOffset = calcOffsetToViewport( - container, - documentBody, - isHorizontal - )), + () => normalizeOffset(window[scrollOffsetKey], isHorizontal), (jump, shift) => { // TODO support case two window scrollers exist in the same view if (shift) { window.scroll({ - [isHorizontal ? "left" : "top"]: - store._getScrollOffset() + prevStartOffset + jump, + [isHorizontal ? "left" : "top"]: store._getScrollOffset() + jump, }); } else { window.scrollBy(isHorizontal ? jump : 0, isHorizontal ? 0 : jump); } - } + }, + () => calcOffsetToViewport(container, documentBody, isHorizontal) ); }, _dispose() { diff --git a/src/core/store.ts b/src/core/store.ts index f2dee32ce..b3a940299 100644 --- a/src/core/store.ts +++ b/src/core/store.ts @@ -53,9 +53,11 @@ export const ACTION_SCROLL = 4; /** @internal */ export const ACTION_SCROLL_END = 5; /** @internal */ -export const ACTION_MANUAL_SCROLL = 6; +export const ACTION_START_OFFSET_CHANGE = 6; /** @internal */ -export const ACTION_BEFORE_MANUAL_SMOOTH_SCROLL = 7; +export const ACTION_MANUAL_SCROLL = 7; +/** @internal */ +export const ACTION_BEFORE_MANUAL_SMOOTH_SCROLL = 8; type Actions = | [type: typeof ACTION_ITEM_RESIZE, entries: ItemResize[]] @@ -66,6 +68,7 @@ type Actions = ] | [type: typeof ACTION_SCROLL, offset: number] | [type: typeof ACTION_SCROLL_END, dummy?: void] + | [type: typeof ACTION_START_OFFSET_CHANGE, offset: number] | [type: typeof ACTION_MANUAL_SCROLL, dummy?: void] | [type: typeof ACTION_BEFORE_MANUAL_SMOOTH_SCROLL, offset: number]; @@ -439,6 +442,10 @@ export const createVirtualStore = ( _frozenRange = null; break; } + case ACTION_START_OFFSET_CHANGE: { + startSpacerSize = payload; + break; + } case ACTION_MANUAL_SCROLL: { _scrollMode = SCROLL_BY_MANUAL_SCROLL; break;