Files
suwayomi-material-you-webui/src/components/reader/Page.tsx
T

108 lines
3.6 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-03-18 21:46:24 +03:30
* 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/.
*/
2021-03-18 21:46:24 +03:30
2024-09-06 23:43:38 +02:00
import { CSSProperties, forwardRef } from 'react';
2023-06-04 21:08:39 +02:00
import Box from '@mui/material/Box';
2024-01-26 11:51:08 -08:00
import { useTheme } from '@mui/material/styles';
2024-04-14 15:50:12 +02:00
import useMediaQuery from '@mui/material/useMediaQuery';
2024-01-21 13:33:13 -08:00
import { IReaderSettings, ReaderType } from '@/typings';
2023-10-28 00:32:02 +02:00
import { SpinnerImage } from '@/components/util/SpinnerImage';
2024-09-06 23:43:38 +02:00
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
2021-03-18 21:46:24 +03:30
2024-01-26 11:51:08 -08:00
export const isHorizontalReaderType = (readerType: ReaderType): boolean =>
['ContinuesHorizontalLTR', 'ContinuesHorizontalRTL'].includes(readerType);
2024-01-21 13:33:13 -08:00
export function imageStyle(settings: IReaderSettings): CSSProperties {
const isVertical = settings.readerType === 'ContinuesVertical';
2024-01-26 11:51:08 -08:00
const isHorizontal = isHorizontalReaderType(settings.readerType);
2024-09-06 23:43:38 +02:00
2024-09-06 23:50:38 +02:00
const scrollbarHeight = MediaQuery.useGetScrollbarSize('height');
2024-09-06 23:43:38 +02:00
const baseStyling: CSSProperties = {
margin: 0,
width: `${settings.readerWidth}%`,
objectFit: 'contain',
};
const continuesVerticalStyling: CSSProperties = {
marginBottom: '15px',
};
const continuesHorizontalStyling: CSSProperties = {
width: undefined,
minHeight: `calc(100vh - ${scrollbarHeight}px)`,
maxHeight: `calc(100vh - ${scrollbarHeight}px)`,
marginLeft: '7px',
marginRight: '7px',
};
const fitToPageStyling: CSSProperties = {
width: undefined,
height: undefined,
minWidth: settings.scalePage ? 'calc(100vw - (100vw - 100%))' : undefined,
maxWidth: 'calc(100vw - (100vw - 100%))',
minHeight: settings.scalePage ? `calc(100vh - ${scrollbarHeight}px)` : undefined,
maxHeight: `calc(100vh - ${scrollbarHeight}px)`,
};
2021-05-28 05:36:55 -07:00
return {
...baseStyling,
...(isHorizontal ? continuesHorizontalStyling : undefined),
...(isVertical ? continuesVerticalStyling : undefined),
...(settings.fitPageToWindow && !isHorizontal ? fitToPageStyling : undefined),
2021-05-28 05:36:55 -07:00
};
}
2021-03-18 21:46:24 +03:30
interface IProps {
2023-02-06 10:06:33 +01:00
src: string;
index: number;
onImageLoad: () => void;
settings: IReaderSettings;
2021-03-18 21:46:24 +03:30
}
2023-10-28 00:32:02 +02:00
export const Page = forwardRef((props: IProps, ref: any) => {
2023-02-06 10:06:33 +01:00
const { src, index, onImageLoad, settings } = props;
2021-03-23 03:50:55 +04:30
2024-01-26 11:51:08 -08:00
const theme = useTheme();
const isMobileWidth = useMediaQuery(theme.breakpoints.down('md'));
2021-12-01 03:06:06 +03:30
const imgStyle = imageStyle(settings);
2024-01-26 11:51:08 -08:00
const isDoublePageReader = ['DoubleRTL', 'DoubleLTR'].includes(settings.readerType);
2021-12-01 03:06:06 +03:30
2021-03-18 21:46:24 +03:30
return (
2024-01-26 11:51:08 -08:00
<Box
ref={ref}
2024-02-21 02:32:09 +01:00
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
minWidth: isDoublePageReader ? '100%' : undefined,
}}
2024-01-26 11:51:08 -08:00
>
2021-05-28 19:37:26 +04:30
<SpinnerImage
2023-05-18 13:25:19 +02:00
src={src}
2021-05-28 05:36:55 -07:00
onImageLoad={onImageLoad}
2021-05-28 19:37:26 +04:30
alt={`Page #${index}`}
2021-12-01 03:06:06 +03:30
spinnerStyle={{
...imgStyle,
height: '100vh',
// eslint-disable-next-line no-nested-ternary
width: isMobileWidth
? '100vw'
: isHorizontalReaderType(settings.readerType)
? '50vw'
: 'calc(100% * 0.5)',
backgroundColor: 'background.paper',
2021-12-01 03:06:06 +03:30
}}
imgStyle={imgStyle}
2021-05-15 18:26:40 +04:30
/>
2021-12-01 03:06:06 +03:30
</Box>
2021-03-18 21:46:24 +03:30
);
2021-05-18 02:26:45 +04:30
});