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

117 lines
4.8 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 { Direction, useTheme } from '@mui/material/styles';
import { Fragment, useMemo } from 'react';
import { BasePager } from '@/modules/reader/components/viewer/pager/BasePager.tsx';
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
import { ReaderPagerProps, ReadingDirection } from '@/modules/reader/types/Reader.types.ts';
import { applyStyles } from '@/modules/core/utils/ApplyStyles.ts';
import { createReaderPage } from '@/modules/reader/utils/ReaderPager.utils.tsx';
import { getNextIndexFromPage, getPage } from '@/modules/reader/utils/ReaderProgressBar.utils.tsx';
2024-11-06 13:00:36 +01:00
const getPagePosition = (
pageType: 'first' | 'second',
themeDirection: Direction,
readingDirection: ReadingDirection,
): 'left' | 'right' => {
const isLtrReadingDirection = readingDirection === ReadingDirection.LTR;
if (pageType === 'first') {
if (themeDirection === 'ltr') {
return isLtrReadingDirection ? 'right' : 'left';
}
return isLtrReadingDirection ? 'left' : 'right';
}
if (themeDirection === 'ltr') {
return isLtrReadingDirection ? 'left' : 'right';
}
return isLtrReadingDirection ? 'right' : 'left';
};
2024-12-07 04:47:28 +01:00
export const ReaderDoublePagedPager = ({
onLoad,
onError,
pageLoadStates,
retryFailedPagesKeyPrefix,
...props
}: ReaderPagerProps) => {
2024-11-06 13:00:36 +01:00
const { currentPageIndex, pages, totalPages } = props;
const { readingDirection } = ReaderService.useSettings();
const { direction: themeDirection } = useTheme();
const currentPage = useMemo(() => getPage(currentPageIndex, pages), [currentPageIndex, pages]);
const isLtrReadingDirection = readingDirection.value === ReadingDirection.LTR;
return (
<BasePager
{...props}
2024-12-03 18:00:46 +01:00
createPage={(page, pagesIndex, shouldLoad, shouldDisplay) => {
2024-11-06 13:00:36 +01:00
const { primary, secondary } = page;
const currentSecondaryPageIndex = getNextIndexFromPage(currentPage);
2024-11-06 13:00:36 +01:00
const hasSecondaryPage = !!secondary;
const isPrimaryPage = currentPage.primary.index === primary.index;
const isSecondaryPage = !!secondary && currentSecondaryPageIndex === secondary.index;
return (
<Fragment key={`${primary.url}_${secondary?.url}`}>
{createReaderPage(
page,
() => onLoad?.(pagesIndex),
2024-12-07 04:47:28 +01:00
() => onError?.(primary.index),
2024-11-06 13:00:36 +01:00
shouldLoad,
2024-12-03 18:00:46 +01:00
shouldDisplay && isPrimaryPage,
2024-11-06 13:00:36 +01:00
currentPage.primary.index,
totalPages,
2024-12-07 04:47:28 +01:00
pageLoadStates[primary.index].error ? retryFailedPagesKeyPrefix : undefined,
2024-11-06 13:00:36 +01:00
hasSecondaryPage
? getPagePosition('first', themeDirection, readingDirection.value)
: undefined,
hasSecondaryPage,
)}
{hasSecondaryPage &&
createReaderPage(
{ ...page, primary: { ...page.secondary! } },
() => onLoad?.(pagesIndex, false),
2024-12-07 04:47:28 +01:00
() => onError?.(secondary.index),
2024-11-06 13:00:36 +01:00
shouldLoad,
2024-12-03 18:00:46 +01:00
shouldDisplay && isSecondaryPage,
2024-11-06 13:00:36 +01:00
currentSecondaryPageIndex,
totalPages,
2024-12-07 04:47:28 +01:00
pageLoadStates[secondary.index].error ? retryFailedPagesKeyPrefix : undefined,
2024-11-06 13:00:36 +01:00
getPagePosition('second', themeDirection, readingDirection.value),
true,
)}
</Fragment>
);
}}
slots={{
stackProps: {
sx: {
margin: 'auto',
flexDirection: 'row',
...applyStyles(themeDirection === 'ltr', {
flexDirection: isLtrReadingDirection ? 'row' : 'row-reverse',
}),
...applyStyles(themeDirection === 'rtl', {
flexDirection: isLtrReadingDirection ? 'row-reverse' : 'row',
}),
flexWrap: 'nowrap',
},
},
}}
/>
);
};