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

99 lines
3.2 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
2023-08-14 21:43:06 +02:00
import { useState, useEffect, forwardRef, useRef } 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';
import { useMediaQuery } from '@mui/material';
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';
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
2024-01-26 11:51:08 -08:00
export function imageStyle(settings: IReaderSettings): any {
2023-08-14 21:43:06 +02:00
const [dimensions, setDimensions] = useState({
2021-10-17 03:15:14 -04:00
height: window.innerHeight,
width: window.innerWidth,
});
2023-08-14 21:43:06 +02:00
useEffect(() => {
2021-10-17 03:15:14 -04:00
function handleResize() {
setDimensions({
height: window.innerHeight,
width: window.innerWidth,
});
}
2023-02-06 10:06:33 +01:00
2021-10-17 03:15:14 -04:00
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
2024-01-26 11:51:08 -08:00
const isHorizontal = isHorizontalReaderType(settings.readerType);
if (settings.fitPageToWindow || isHorizontal) {
2021-05-28 05:36:55 -07:00
return {
2024-01-26 11:51:08 -08:00
marginLeft: isHorizontal ? '7px' : 0,
marginRight: isHorizontal ? '7px' : 0,
2021-05-28 05:36:55 -07:00
width: 'auto',
maxWidth: settings.fitPageToWindow ? 'calc(100vw - (100vw - 100%))' : undefined,
2021-05-28 05:36:55 -07:00
height: 'auto',
2024-01-26 11:51:08 -08:00
maxHeight: '100vh',
2021-05-28 05:36:55 -07:00
objectFit: 'contain',
};
}
return {
marginBottom: settings.readerType === 'ContinuesVertical' ? '15px' : 0,
2024-01-21 13:33:13 -08:00
minWidth: '10vw',
width: dimensions.width < dimensions.height ? '100vw' : `${settings.readerWidth}%`,
2021-05-28 05:36:55 -07:00
maxWidth: '100%',
2024-01-26 11:51:08 -08:00
objectFit: 'contain',
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-05-28 19:37:26 +04:30
const imgRef = useRef<HTMLImageElement>(null);
2021-03-19 14:52:20 +03:30
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}
sx={{ display: 'flex', justifyContent: 'center', minWidth: isDoublePageReader ? '100%' : undefined }}
>
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}`}
imgRef={imgRef}
2021-12-01 03:06:06 +03:30
spinnerStyle={{
...imgStyle,
height: '100vh',
2024-01-26 11:51:08 -08:00
width: isMobileWidth ? '100vw' : 'calc(100% * 0.5)',
2021-12-01 03:06:06 +03:30
backgroundColor: '#525252',
}}
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
});