Files
suwayomi-material-you-webui/src/modules/reader/components/viewer/ReaderViewer.tsx

491 lines
21 KiB
TypeScript
Raw Normal View History

2024-11-06 13:00:36 +01:00
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import {
ForwardedRef,
forwardRef,
2024-12-22 00:41:56 +01:00
memo,
useCallback,
2024-11-06 13:00:36 +01:00
useEffect,
useImperativeHandle,
useLayoutEffect,
useMemo,
useRef,
useState,
2024-11-06 13:00:36 +01:00
} from 'react';
import Stack from '@mui/material/Stack';
import { useTheme } from '@mui/material/styles';
2025-01-28 02:16:00 +01:00
import { useLocation } from 'react-router-dom';
2024-11-06 13:00:36 +01:00
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
2024-12-21 03:46:55 +01:00
import {
IReaderSettings,
PageInViewportType,
2025-01-28 02:16:00 +01:00
ReaderOpenChapterLocationState,
ReaderResumeMode,
ReaderStateChapters,
ReadingDirection,
2024-12-21 03:46:55 +01:00
ReadingMode,
TReaderScrollbarContext,
} from '@/modules/reader/types/Reader.types.ts';
2024-11-06 13:00:36 +01:00
import { userReaderStatePagesContext } from '@/modules/reader/contexts/state/ReaderStatePagesContext.tsx';
import { useReaderScrollbarContext } from '@/modules/reader/contexts/ReaderScrollbarContext.tsx';
import { MediaQuery } from '@/modules/core/utils/MediaQuery.tsx';
import { ReaderControls } from '@/modules/reader/services/ReaderControls.ts';
import {
isContinuousReadingMode,
isContinuousVerticalReadingMode,
shouldApplyReaderWidth,
} from '@/modules/reader/utils/ReaderSettings.utils.tsx';
import { useMouseDragScroll } from '@/modules/core/hooks/useMouseDragScroll.tsx';
2024-12-11 21:04:44 +01:00
import { useReaderOverlayContext } from '@/modules/reader/contexts/ReaderOverlayContext.tsx';
import { applyStyles } from '@/modules/core/utils/ApplyStyles.ts';
2024-12-21 03:46:55 +01:00
import { TReaderOverlayContext } from '@/modules/reader/types/ReaderOverlay.types.ts';
import { ReaderStatePages } from '@/modules/reader/types/ReaderProgressBar.types.ts';
import { withPropsFrom } from '@/modules/core/hoc/withPropsFrom.tsx';
2024-12-27 00:30:19 +01:00
import { useReaderAutoScrollContext } from '@/modules/reader/contexts/ReaderAutoScrollContext.tsx';
import { TReaderTapZoneContext } from '@/modules/reader/types/TapZoneLayout.types.ts';
import { useReaderTapZoneContext } from '@/modules/reader/contexts/ReaderTapZoneContext.tsx';
2025-01-18 22:07:59 +01:00
import { useReaderAutoScroll } from '@/modules/reader/hooks/useReaderAutoScroll.ts';
import { useReaderHideOverlayOnUserScroll } from '@/modules/reader/hooks/useReaderHideOverlayOnUserScroll.ts';
import { useReaderHorizontalModeInvertXYScrolling } from '@/modules/reader/hooks/useReaderHorizontalModeInvertXYScrolling.ts';
import { useReaderHideCursorOnInactivity } from '@/modules/reader/hooks/useReaderHideCursorOnInactivity.ts';
import { useReaderScrollToStartOnPageChange } from '@/modules/reader/hooks/useReaderScrollToStartOnPageChange.ts';
import { useReaderHandlePageSelection } from '@/modules/reader/hooks/useReaderHandlePageSelection.ts';
2025-01-28 02:16:00 +01:00
import { useReaderStateChaptersContext } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
import { ReaderChapterViewer } from '@/modules/reader/components/viewer/ReaderChapterViewer.tsx';
import {
getPreviousNextChapterVisibility,
2025-01-28 02:16:00 +01:00
getReaderChapterViewerCurrentPageIndex,
getReaderChapterViewResumeMode,
} from '@/modules/reader/utils/Reader.utils.ts';
import { coerceIn, noOp } from '@/lib/HelperFunctions.ts';
import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
import { NavbarContextType } from '@/modules/navigation-bar/NavigationBar.types.ts';
import { useReaderPreserveScrollPosition } from '@/modules/reader/hooks/useReaderPreserveScrollPosition.ts';
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
import { ChapterIdInfo } from '@/modules/chapter/services/Chapters';
2024-11-06 13:00:36 +01:00
const READING_MODE_TO_IN_VIEWPORT_TYPE: Record<ReadingMode, PageInViewportType> = {
[ReadingMode.SINGLE_PAGE]: PageInViewportType.X,
[ReadingMode.DOUBLE_PAGE]: PageInViewportType.X,
[ReadingMode.CONTINUOUS_VERTICAL]: PageInViewportType.Y,
[ReadingMode.CONTINUOUS_HORIZONTAL]: PageInViewportType.X,
2024-12-16 16:26:32 +01:00
[ReadingMode.WEBTOON]: PageInViewportType.Y,
2024-11-06 13:00:36 +01:00
};
2024-12-21 03:46:55 +01:00
const BaseReaderViewer = forwardRef(
(
{
currentPageIndex,
pageToScrollToIndex,
setPageToScrollToIndex,
2024-12-21 03:46:55 +01:00
pages,
totalPages,
2025-01-28 02:16:00 +01:00
setPages,
2024-12-21 03:46:55 +01:00
setPageLoadStates,
2025-01-28 02:16:00 +01:00
setTotalPages,
setCurrentPageIndex,
2024-12-21 03:46:55 +01:00
transitionPageMode,
retryFailedPagesKeyPrefix,
2025-01-28 02:16:00 +01:00
setTransitionPageMode,
2024-12-21 03:46:55 +01:00
readingMode,
readingDirection,
readerWidth,
pageScaleMode,
2025-01-28 02:16:00 +01:00
shouldOffsetDoubleSpreads,
imagePreLoadAmount,
pageGap,
customFilter,
shouldStretchPage,
readerNavBarWidth,
2024-12-21 03:46:55 +01:00
setScrollbarXSize,
setScrollbarYSize,
isVisible: isOverlayVisible,
setIsVisible: setIsOverlayVisible,
updateCurrentPageIndex,
showPreview,
setShowPreview,
2025-01-28 02:16:00 +01:00
initialChapter,
currentChapter,
chapters,
visibleChapters,
setReaderStateChapters,
isCurrentChapterReady,
2024-12-21 03:46:55 +01:00
}: Pick<
ReaderStatePages,
| 'currentPageIndex'
| 'pageToScrollToIndex'
| 'setPageToScrollToIndex'
2024-12-21 03:46:55 +01:00
| 'pages'
| 'totalPages'
2025-01-28 02:16:00 +01:00
| 'setPages'
2024-12-21 03:46:55 +01:00
| 'setPageLoadStates'
2025-01-28 02:16:00 +01:00
| 'setTotalPages'
| 'setCurrentPageIndex'
2024-12-21 03:46:55 +01:00
| 'transitionPageMode'
| 'retryFailedPagesKeyPrefix'
2025-01-28 02:16:00 +01:00
| 'setTransitionPageMode'
2024-12-21 03:46:55 +01:00
> &
Pick<
IReaderSettings,
| 'readingMode'
| 'readingDirection'
| 'readerWidth'
| 'pageScaleMode'
| 'shouldOffsetDoubleSpreads'
| 'imagePreLoadAmount'
| 'pageGap'
| 'customFilter'
| 'shouldStretchPage'
> &
2024-12-21 03:46:55 +01:00
Pick<TReaderScrollbarContext, 'setScrollbarXSize' | 'setScrollbarYSize'> &
Pick<NavbarContextType, 'readerNavBarWidth'> &
Pick<TReaderOverlayContext, 'isVisible' | 'setIsVisible'> &
2025-01-28 02:16:00 +01:00
Pick<
ReaderStateChapters,
| 'initialChapter'
| 'currentChapter'
| 'chapters'
| 'visibleChapters'
| 'setReaderStateChapters'
| 'isCurrentChapterReady'
> &
TReaderTapZoneContext & {
2024-12-21 03:46:55 +01:00
updateCurrentPageIndex: ReturnType<typeof ReaderControls.useUpdateCurrentPageIndex>;
},
ref: ForwardedRef<HTMLDivElement | null>,
) => {
const { direction: themeDirection } = useTheme();
const { resumeMode = ReaderResumeMode.START } = useLocation<ReaderOpenChapterLocationState>().state ?? {
2025-01-28 02:16:00 +01:00
resumeMode: ReaderResumeMode.START,
};
2024-12-21 03:46:55 +01:00
const scrollElementRef = useRef<HTMLDivElement | null>(null);
useImperativeHandle(ref, () => scrollElementRef.current!);
2025-01-28 02:16:00 +01:00
const isContinuousVerticalReadingModeActive = isContinuousVerticalReadingMode(readingMode);
2024-12-21 03:46:55 +01:00
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode);
const isDragging = useMouseDragScroll(scrollElementRef);
2024-12-21 03:46:55 +01:00
2024-12-27 00:30:19 +01:00
const automaticScrolling = useReaderAutoScrollContext();
useEffect(() => automaticScrolling.setScrollRef(scrollElementRef), []);
2024-12-21 03:46:55 +01:00
const scrollbarXSize = MediaQuery.useGetScrollbarSize('width', scrollElementRef.current);
const scrollbarYSize = MediaQuery.useGetScrollbarSize('height', scrollElementRef.current);
useLayoutEffect(() => {
setScrollbarXSize(scrollbarXSize);
setScrollbarYSize(scrollbarYSize);
}, [scrollbarXSize, scrollbarYSize]);
const handleClick = ReaderControls.useHandleClick(scrollElementRef.current);
const imageRefs = useRef<(HTMLElement | null)[]>(pages.map(() => null));
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
const [{ minChapterViewWidth, minChapterViewHeight, minChapterSizeSourceChapterId }, setChapterViewerSize] =
useState({
minChapterViewWidth: 0,
minChapterViewHeight: 0,
minChapterSizeSourceChapterId: -1,
});
const [, setTriggerReRender] = useState({});
2024-12-21 03:46:55 +01:00
const inViewportType = READING_MODE_TO_IN_VIEWPORT_TYPE[readingMode];
const isLtrReadingDirection = readingDirection === ReadingDirection.LTR;
2025-01-28 02:16:00 +01:00
const initialChapterIndex = useMemo(
() => chapters.findIndex((chapter) => chapter.id === initialChapter?.id),
[chapters, initialChapter?.id],
2025-01-28 02:16:00 +01:00
);
const chaptersToRender = useMemo(
() =>
2025-01-28 02:16:00 +01:00
chapters.slice(
Math.max(0, initialChapterIndex - visibleChapters.trailing),
Math.min(chapters.length, initialChapterIndex + visibleChapters.leading + 1),
),
[chapters, initialChapterIndex, visibleChapters.trailing, visibleChapters.leading],
2024-12-22 00:42:33 +01:00
);
2025-01-28 02:16:00 +01:00
const currentChapterIndex = useMemo(
() => chaptersToRender.findIndex((chapter) => chapter.id === currentChapter?.id),
[currentChapter, chaptersToRender],
);
2025-01-28 02:16:00 +01:00
const onChapterViewSizeChange = useCallback(
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
(width: number, height: number, chapterId: ChapterIdInfo['id']) => {
if (!isContinuousReadingModeActive) {
return;
}
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
const isSameChapterId = chapterId === minChapterSizeSourceChapterId;
if (isContinuousVerticalReadingModeActive) {
if (!isSameChapterId && minChapterViewWidth >= width) {
return;
}
setChapterViewerSize({
minChapterViewWidth: width,
minChapterViewHeight: 0,
minChapterSizeSourceChapterId: chapterId,
});
return;
}
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
if (isSameChapterId || minChapterViewHeight < height) {
setChapterViewerSize({
minChapterViewWidth: 0,
minChapterViewHeight: height,
minChapterSizeSourceChapterId: chapterId,
});
}
},
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
[
isContinuousReadingModeActive,
isContinuousVerticalReadingModeActive,
minChapterViewWidth,
minChapterViewHeight,
minChapterSizeSourceChapterId,
],
);
useReaderHandlePageSelection(
pageToScrollToIndex,
currentPageIndex,
pages,
totalPages,
setPageToScrollToIndex,
updateCurrentPageIndex,
isContinuousReadingModeActive,
imageRefs,
themeDirection,
readingDirection,
);
useReaderScrollToStartOnPageChange(
currentPageIndex,
isContinuousReadingModeActive,
themeDirection,
readingDirection,
scrollElementRef,
);
useReaderHideCursorOnInactivity(scrollElementRef);
useReaderHorizontalModeInvertXYScrolling(readingMode, readingDirection, scrollElementRef);
useReaderHideOverlayOnUserScroll(
isOverlayVisible,
setIsOverlayVisible,
showPreview,
setShowPreview,
scrollElementRef,
);
useReaderAutoScroll(isOverlayVisible, automaticScrolling);
useReaderPreserveScrollPosition(
scrollElementRef,
currentPageIndex,
readingMode,
isContinuousReadingModeActive,
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
readerNavBarWidth,
setPageToScrollToIndex,
pageScaleMode,
);
2024-12-27 00:30:19 +01:00
useLayoutEffect(() => {
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
setChapterViewerSize({
minChapterViewWidth: 0,
minChapterViewHeight: 0,
minChapterSizeSourceChapterId: -1,
});
setTriggerReRender({});
}, [readingMode]);
2025-01-28 02:16:00 +01:00
if (!initialChapter || !currentChapter) {
throw new Error('ReaderViewer: illegal state - initialChapter and currentChapter should not be undefined');
}
2024-12-21 03:46:55 +01:00
return (
<Stack
ref={scrollElementRef}
sx={{
width: '100%',
height: '100%',
overflow: 'auto',
flexWrap: 'nowrap',
...applyStyles(
2025-01-28 02:16:00 +01:00
isContinuousVerticalReadingModeActive && shouldApplyReaderWidth(readerWidth, pageScaleMode),
{ alignItems: 'center' },
),
2025-01-28 02:16:00 +01:00
...applyStyles(!isContinuousVerticalReadingModeActive, {
...applyStyles(themeDirection === 'ltr', {
flexDirection: isLtrReadingDirection ? 'row' : 'row-reverse',
}),
...applyStyles(themeDirection === 'rtl', {
flexDirection: isLtrReadingDirection ? 'row-reverse' : 'row',
}),
}),
2024-11-06 13:00:36 +01:00
}}
2024-12-21 03:46:55 +01:00
onClick={(e) => !isDragging && handleClick(e)}
onScroll={() =>
ReaderControls.updateCurrentPageOnScroll(
imageRefs,
totalPages - 1,
updateCurrentPageIndex,
inViewportType,
readingDirection,
)
}
>
2025-01-28 02:16:00 +01:00
{chaptersToRender.map((_, index) => {
// chapters are sorted by latest to oldest, thus, loop over it in reversed order
const chapterIndex = Math.max(0, chaptersToRender.length - index - 1);
const chapter = chaptersToRender[chapterIndex];
const previousChapter =
chaptersToRender[chapterIndex + 1] ??
chapters[initialChapterIndex + visibleChapters.leading + 1];
const nextChapter =
chaptersToRender[chapterIndex - 1] ??
chapters[initialChapterIndex - visibleChapters.trailing - 1];
const isInitialChapter = chapter.id === initialChapter.id;
const isCurrentChapter = chapter.id === currentChapter.id;
const isPreviousChapter = chapter.id === chaptersToRender[currentChapterIndex + 1]?.id;
const isNextChapter = chapter.id === chaptersToRender[currentChapterIndex - 1]?.id;
const isLeadingChapter = initialChapter.sourceOrder > chapter.sourceOrder;
const isTrailingChapter = initialChapter.sourceOrder < chapter.sourceOrder;
const isLastLeadingChapter = visibleChapters.lastLeadingChapterSourceOrder === chapter.sourceOrder;
const isLastTrailingChapter =
visibleChapters.lastTrailingChapterSourceOrder === chapter.sourceOrder;
const isPreloadMode =
(isLastLeadingChapter && visibleChapters.isLeadingChapterPreloadMode) ||
(isLastTrailingChapter && visibleChapters.isTrailingChapterPreloadMode);
const previousNextChapterVisibility = getPreviousNextChapterVisibility(
chapterIndex,
chaptersToRender,
visibleChapters,
);
2025-01-28 02:16:00 +01:00
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
const isChapterSizeSourceChapter = chapter.id === minChapterSizeSourceChapterId;
2025-01-28 02:16:00 +01:00
return (
<ReaderChapterViewer
key={chapter.id}
chapterId={chapter.id}
previousChapterId={previousChapter?.id}
nextChapterId={nextChapter?.id}
isPreviousChapterVisible={previousNextChapterVisibility.previous}
isNextChapterVisible={previousNextChapterVisibility.next}
lastPageRead={coerceIn(chapter.lastPageRead, 0, chapter.pageCount - 1)}
2025-01-28 02:16:00 +01:00
currentPageIndex={getReaderChapterViewerCurrentPageIndex(
currentPageIndex,
chapter,
currentChapter,
isCurrentChapter,
isCurrentChapterReady,
isLeadingChapter,
isTrailingChapter,
visibleChapters,
)}
isInitialChapter={isInitialChapter}
isCurrentChapter={isCurrentChapter}
isPreviousChapter={isPreviousChapter}
isNextChapter={isNextChapter}
isLeadingChapter={isLeadingChapter}
isTrailingChapter={isTrailingChapter}
isPreloadMode={isPreloadMode}
2025-01-28 02:16:00 +01:00
imageRefs={imageRefs}
setPages={setPages}
setPageLoadStates={setPageLoadStates}
setTotalPages={setTotalPages}
setCurrentPageIndex={setCurrentPageIndex}
setPageToScrollToIndex={setPageToScrollToIndex}
transitionPageMode={transitionPageMode}
retryFailedPagesKeyPrefix={retryFailedPagesKeyPrefix}
readingMode={readingMode}
readerWidth={readerWidth}
pageScaleMode={pageScaleMode}
shouldOffsetDoubleSpreads={shouldOffsetDoubleSpreads}
readingDirection={readingDirection}
updateCurrentPageIndex={isCurrentChapter ? updateCurrentPageIndex : noOp}
scrollIntoView={isCurrentChapter && visibleChapters.scrollIntoView}
resumeMode={getReaderChapterViewResumeMode(
isCurrentChapter,
isInitialChapter,
isLeadingChapter,
isTrailingChapter,
visibleChapters.resumeMode,
resumeMode,
)}
setReaderStateChapters={setReaderStateChapters}
setTransitionPageMode={setTransitionPageMode}
pageGap={pageGap}
imagePreLoadAmount={imagePreLoadAmount}
customFilter={customFilter}
shouldStretchPage={shouldStretchPage}
scrollbarXSize={scrollbarXSize}
scrollbarYSize={scrollbarYSize}
readerNavBarWidth={readerNavBarWidth}
onSizeChange={onChapterViewSizeChange}
Reader preserve current page on window resize (#905) * Reader: Reset bounds on window size change 19f2d993 introduced a `minWidth` to make sure sizes are consistent over chapter changes (or when images load later), but when the window itself changes, this makes the reader incorrectly sized * Reader: Update size on navbar width change This happens mostly when the bar is pinned; same effect as previous commit * Reader: Attempt to set the page on resize It's not perfect, but not changing position at all seems impossible due to how resizes are handled by the browser (since we adjust our content to the window size) * Simplify and abstract reader size reset Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * fix: Reader page full width was not properly calculated for static navbar Only relevant for Fit Content, this did not properly consider the navbar when the user clamps the content to a custom width * Refactor: Put reader min sizes to reader itself instead of chapter This allows some logic simplification. The `ResizeObserver` can needs to be on an element without restrictions; previously, the observer would not see the chapter elements get smaller, since the `min-width` prevented it from shrinking. By applying to the outer element, we still preserve the centering (since `margin: auto` is also applied here), but allow the chapter itself to be smaller. * Reader: Only scroll in continuous reading modes * Reader: clean up size tracking Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Fixup previous commit stupidness * Reader: move `minWidth`/`minHeight` back to chapter wrapper * Apply suggestions from code review Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Also revert `fullWidth` calculation * Simplify `chapterViewerSize` Co-authored-by: schroda <50052685+schroda@users.noreply.github.com> * Move size logic entirely to hook --------- Co-authored-by: schroda <50052685+schroda@users.noreply.github.com>
2025-03-24 23:45:06 +01:00
minWidth={isChapterSizeSourceChapter ? 0 : minChapterViewWidth}
minHeight={isChapterSizeSourceChapter ? 0 : minChapterViewHeight}
scrollElement={scrollElementRef.current}
2025-01-28 02:16:00 +01:00
/>
);
})}
2024-12-21 03:46:55 +01:00
</Stack>
);
},
);
export const ReaderViewer = withPropsFrom(
2024-12-22 00:41:56 +01:00
memo(BaseReaderViewer),
2024-12-21 03:46:55 +01:00
[
userReaderStatePagesContext,
ReaderService.useSettingsWithoutDefaultFlag,
useReaderScrollbarContext,
useReaderOverlayContext,
() => ({ updateCurrentPageIndex: ReaderControls.useUpdateCurrentPageIndex() }),
useReaderTapZoneContext,
2025-01-28 02:16:00 +01:00
useReaderStateChaptersContext,
useNavBarContext,
2024-12-21 03:46:55 +01:00
],
[
'currentPageIndex',
'pageToScrollToIndex',
'setPageToScrollToIndex',
2024-12-21 03:46:55 +01:00
'pages',
'totalPages',
2025-01-28 02:16:00 +01:00
'setPages',
2024-12-21 03:46:55 +01:00
'setPageLoadStates',
2025-01-28 02:16:00 +01:00
'setTotalPages',
'setCurrentPageIndex',
2024-12-21 03:46:55 +01:00
'retryFailedPagesKeyPrefix',
2025-01-28 02:16:00 +01:00
'setTransitionPageMode',
2024-12-21 03:46:55 +01:00
'readingMode',
'readingDirection',
'readerWidth',
'pageScaleMode',
2025-01-28 02:16:00 +01:00
'shouldOffsetDoubleSpreads',
'imagePreLoadAmount',
'pageGap',
'customFilter',
'shouldStretchPage',
'readerNavBarWidth',
2025-01-28 02:16:00 +01:00
'transitionPageMode',
2024-12-21 03:46:55 +01:00
'setScrollbarXSize',
'setScrollbarYSize',
'isVisible',
'setIsVisible',
'updateCurrentPageIndex',
'showPreview',
'setShowPreview',
2025-01-28 02:16:00 +01:00
'initialChapter',
'currentChapter',
'chapters',
'visibleChapters',
'setReaderStateChapters',
'isCurrentChapterReady',
2024-12-21 03:46:55 +01:00
],
);