Center "ReaderChapterViewers"
The viewers can have different sizes which caused theme to not be properly centered. E.g. for the "vertical mode" in case "chapter 1" is greater in width than "chapter 2", the pages of "chapter 2" were not correctly centered due to the "viewers" having different widths.
This commit is contained in:
@@ -46,6 +46,7 @@ import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder
|
|||||||
import { getErrorMessage, noOp } from '@/lib/HelperFunctions.ts';
|
import { getErrorMessage, noOp } from '@/lib/HelperFunctions.ts';
|
||||||
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
||||||
import { ReaderInfiniteScrollUpdateChapter } from '@/modules/reader/components/viewer/ReaderInfiniteScrollUpdateChapter.tsx';
|
import { ReaderInfiniteScrollUpdateChapter } from '@/modules/reader/components/viewer/ReaderInfiniteScrollUpdateChapter.tsx';
|
||||||
|
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
|
||||||
|
|
||||||
const BaseReaderChapterViewer = ({
|
const BaseReaderChapterViewer = ({
|
||||||
currentPageIndex,
|
currentPageIndex,
|
||||||
@@ -86,6 +87,9 @@ const BaseReaderChapterViewer = ({
|
|||||||
scrollbarXSize,
|
scrollbarXSize,
|
||||||
scrollbarYSize,
|
scrollbarYSize,
|
||||||
readerNavBarWidth,
|
readerNavBarWidth,
|
||||||
|
onSizeChange,
|
||||||
|
minWidth,
|
||||||
|
minHeight,
|
||||||
}: Pick<
|
}: Pick<
|
||||||
ReaderStatePages,
|
ReaderStatePages,
|
||||||
| 'currentPageIndex'
|
| 'currentPageIndex'
|
||||||
@@ -120,6 +124,9 @@ const BaseReaderChapterViewer = ({
|
|||||||
imageRefs: MutableRefObject<(HTMLElement | null)[]>;
|
imageRefs: MutableRefObject<(HTMLElement | null)[]>;
|
||||||
scrollIntoView: boolean;
|
scrollIntoView: boolean;
|
||||||
resumeMode: ReaderResumeMode;
|
resumeMode: ReaderResumeMode;
|
||||||
|
onSizeChange: (width: number, height: number) => void;
|
||||||
|
minWidth: number;
|
||||||
|
minHeight: number;
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { direction: themeDirection } = useTheme();
|
const { direction: themeDirection } = useTheme();
|
||||||
@@ -139,6 +146,7 @@ const BaseReaderChapterViewer = ({
|
|||||||
pageLoadStates.map(({ url }) => ({ url, isSpread: false })),
|
pageLoadStates.map(({ url }) => ({ url, isSpread: false })),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
const isCurrentChapterRef = useRef(isCurrentChapter);
|
const isCurrentChapterRef = useRef(isCurrentChapter);
|
||||||
const imageRefs = useRef<(HTMLElement | null)[]>(pages.map(() => null));
|
const imageRefs = useRef<(HTMLElement | null)[]>(pages.map(() => null));
|
||||||
|
|
||||||
@@ -220,6 +228,17 @@ const BaseReaderChapterViewer = ({
|
|||||||
doFetchPages();
|
doFetchPages();
|
||||||
}, [chapterId]);
|
}, [chapterId]);
|
||||||
|
|
||||||
|
useResizeObserver(
|
||||||
|
ref,
|
||||||
|
useCallback(
|
||||||
|
(entries) => {
|
||||||
|
const { clientWidth, clientHeight } = entries[0].target;
|
||||||
|
onSizeChange(clientWidth, clientHeight);
|
||||||
|
},
|
||||||
|
[onSizeChange, ref.current],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
const updatePageState = <T,>(
|
const updatePageState = <T,>(
|
||||||
value: T,
|
value: T,
|
||||||
setLocalState: (value: T) => void,
|
setLocalState: (value: T) => void,
|
||||||
@@ -343,9 +362,16 @@ const BaseReaderChapterViewer = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack
|
<Stack
|
||||||
|
ref={ref}
|
||||||
sx={{
|
sx={{
|
||||||
width: 'fit-content',
|
width: 'fit-content',
|
||||||
height: 'fit-content',
|
height: 'fit-content',
|
||||||
|
...applyStyles(readingMode === ReadingMode.CONTINUOUS_HORIZONTAL, {
|
||||||
|
minHeight,
|
||||||
|
}),
|
||||||
|
...applyStyles(isContinuousVerticalReadingMode(readingMode), {
|
||||||
|
minWidth,
|
||||||
|
}),
|
||||||
margin: 'auto',
|
margin: 'auto',
|
||||||
flexWrap: 'nowrap',
|
flexWrap: 'nowrap',
|
||||||
...applyStyles(shouldHideChapter, {
|
...applyStyles(shouldHideChapter, {
|
||||||
|
|||||||
@@ -10,11 +10,13 @@ import {
|
|||||||
ForwardedRef,
|
ForwardedRef,
|
||||||
forwardRef,
|
forwardRef,
|
||||||
memo,
|
memo,
|
||||||
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
useImperativeHandle,
|
useImperativeHandle,
|
||||||
useLayoutEffect,
|
useLayoutEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
|
useState,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import { useTheme } from '@mui/material/styles';
|
import { useTheme } from '@mui/material/styles';
|
||||||
@@ -180,6 +182,10 @@ const BaseReaderViewer = forwardRef(
|
|||||||
const handleClick = ReaderControls.useHandleClick(scrollElementRef.current);
|
const handleClick = ReaderControls.useHandleClick(scrollElementRef.current);
|
||||||
|
|
||||||
const imageRefs = useRef<(HTMLElement | null)[]>(pages.map(() => null));
|
const imageRefs = useRef<(HTMLElement | null)[]>(pages.map(() => null));
|
||||||
|
const chapterViewerSize = useRef({ minChapterViewWidth: 0, minChapterViewHeight: 0 });
|
||||||
|
const { minChapterViewWidth, minChapterViewHeight } = chapterViewerSize.current;
|
||||||
|
|
||||||
|
const [, setTriggerReRender] = useState({});
|
||||||
|
|
||||||
const inViewportType = READING_MODE_TO_IN_VIEWPORT_TYPE[readingMode];
|
const inViewportType = READING_MODE_TO_IN_VIEWPORT_TYPE[readingMode];
|
||||||
const isLtrReadingDirection = readingDirection === ReadingDirection.LTR;
|
const isLtrReadingDirection = readingDirection === ReadingDirection.LTR;
|
||||||
@@ -200,6 +206,25 @@ const BaseReaderViewer = forwardRef(
|
|||||||
[currentChapter, chaptersToRender],
|
[currentChapter, chaptersToRender],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const onChapterViewSizeChange = useCallback(
|
||||||
|
(width: number, height: number) => {
|
||||||
|
if (!isContinuousReadingModeActive) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isContinuousVerticalReadingModeActive && minChapterViewWidth < width) {
|
||||||
|
chapterViewerSize.current.minChapterViewWidth = width;
|
||||||
|
setTriggerReRender({});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minChapterViewHeight < height) {
|
||||||
|
chapterViewerSize.current.minChapterViewHeight = height;
|
||||||
|
setTriggerReRender({});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[isContinuousReadingModeActive, isContinuousVerticalReadingModeActive],
|
||||||
|
);
|
||||||
|
|
||||||
useReaderHandlePageSelection(
|
useReaderHandlePageSelection(
|
||||||
pageToScrollToIndex,
|
pageToScrollToIndex,
|
||||||
currentPageIndex,
|
currentPageIndex,
|
||||||
@@ -342,6 +367,9 @@ const BaseReaderViewer = forwardRef(
|
|||||||
scrollbarXSize={scrollbarXSize}
|
scrollbarXSize={scrollbarXSize}
|
||||||
scrollbarYSize={scrollbarYSize}
|
scrollbarYSize={scrollbarYSize}
|
||||||
readerNavBarWidth={readerNavBarWidth}
|
readerNavBarWidth={readerNavBarWidth}
|
||||||
|
onSizeChange={onChapterViewSizeChange}
|
||||||
|
minWidth={minChapterViewWidth}
|
||||||
|
minHeight={minChapterViewHeight}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
Reference in New Issue
Block a user