Revert "Fix page resume in continuous pagers"

This reverts commit 9ab6ec89c7.
This commit is contained in:
schroda
2024-12-31 12:46:25 +01:00
parent 8594a7b178
commit 36ae02d2bd

View File

@@ -6,7 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
import { MutableRefObject, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { MutableRefObject, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { Direction } from '@mui/material/styles'; import { Direction } from '@mui/material/styles';
import { import {
ReaderPageSpreadState, ReaderPageSpreadState,
@@ -33,7 +33,6 @@ import { ReaderStatePages } from '@/modules/reader/types/ReaderProgressBar.types
import { ReaderControls } from '@/modules/reader/services/ReaderControls.ts'; import { ReaderControls } from '@/modules/reader/services/ReaderControls.ts';
import { coerceIn } from '@/lib/HelperFunctions.ts'; import { coerceIn } from '@/lib/HelperFunctions.ts';
import { TReaderTapZoneContext } from '@/modules/reader/types/TapZoneLayout.types.ts'; import { TReaderTapZoneContext } from '@/modules/reader/types/TapZoneLayout.types.ts';
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
export const getInitialReaderPageIndex = ( export const getInitialReaderPageIndex = (
resumeMode: ReaderResumeMode, resumeMode: ReaderResumeMode,
@@ -232,46 +231,37 @@ export const useReaderHandlePageSelection = (
readingDirection: ReadingDirection, readingDirection: ReadingDirection,
scrollElementRef: MutableRefObject<HTMLDivElement | null>, scrollElementRef: MutableRefObject<HTMLDivElement | null>,
) => { ) => {
const pageToScrollTo = useMemo(() => getPage(pageToScrollToIndex ?? 0, pages), [pageToScrollToIndex, pages]); useLayoutEffect(() => {
if (pageToScrollToIndex == null) {
return;
}
const imageRef = imageRefs.current[pageToScrollTo.pagesIndex]; const pageToScrollTo = getPage(pageToScrollToIndex, pages);
const ref = isContinuousReadingModeActive ? imageRef : scrollElementRef;
useResizeObserver( if (isContinuousReadingModeActive) {
ref, const directionOffset =
useCallback( pageToScrollToIndex > currentPageIndex ? DirectionOffset.PREVIOUS : DirectionOffset.NEXT;
(entries, observer) => { const imageRef = imageRefs.current[pageToScrollTo.pagesIndex];
if (pageToScrollToIndex == null) {
return;
}
const element = entries[0].target as HTMLElement; imageRef?.scrollIntoView({
block: 'start',
inline: getScrollIntoViewInlineOption(directionOffset, themeDirection, readingDirection),
});
}
if (isContinuousReadingModeActive) { if (!isContinuousReadingModeActive) {
const directionOffset = scrollElementRef.current?.scrollTo(
pageToScrollToIndex > currentPageIndex ? DirectionOffset.PREVIOUS : DirectionOffset.NEXT; getScrollToXForReadingDirection(scrollElementRef.current, themeDirection, readingDirection),
0,
);
}
element.scrollIntoView({ const newPageIndex = getNextIndexFromPage(pageToScrollTo);
block: 'start', const isLastPage = newPageIndex === totalPages - 1;
inline: getScrollIntoViewInlineOption(directionOffset, themeDirection, readingDirection),
});
}
if (!isContinuousReadingModeActive) { setPageToScrollToIndex(null);
element.scrollTo(getScrollToXForReadingDirection(element, themeDirection, readingDirection), 0); updateCurrentPageIndex(newPageIndex, !isLastPage);
} }, [pageToScrollToIndex]);
const newPageIndex = getNextIndexFromPage(pageToScrollTo);
const isLastPage = newPageIndex === totalPages - 1;
setPageToScrollToIndex(null);
updateCurrentPageIndex(newPageIndex, !isLastPage);
observer.disconnect();
},
[pageToScrollToIndex],
),
);
}; };
export const useReaderHideCursorOnInactivity = (scrollElementRef: MutableRefObject<HTMLDivElement | null>) => { export const useReaderHideCursorOnInactivity = (scrollElementRef: MutableRefObject<HTMLDivElement | null>) => {