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

92 lines
2.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
import { useState, useEffect, forwardRef, useRef } from 'react';
import Box from '@mui/material/Box';
import { IReaderSettings } from '@/typings';
2023-10-28 00:32:02 +02:00
import { SpinnerImage } from '@/components/util/SpinnerImage';
2021-03-18 21:46:24 +03:30
2021-09-09 17:51:22 +04:30
function imageStyle(settings: IReaderSettings): any {
const [dimensions, setDimensions] = useState({
height: window.innerHeight,
width: window.innerWidth,
});
useEffect(() => {
function handleResize() {
setDimensions({
height: window.innerHeight,
width: window.innerWidth,
});
}
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
if (
settings.fitPageToWindow ||
settings.readerType === 'DoubleLTR' ||
settings.readerType === 'DoubleRTL' ||
settings.readerType === 'ContinuesHorizontalLTR' ||
settings.readerType === 'ContinuesHorizontalRTL'
) {
return {
display: 'block',
marginLeft: '7px',
marginRight: '7px',
width: 'auto',
minHeight: '99vh',
height: 'auto',
maxHeight: '99vh',
objectFit: 'contain',
};
}
return {
display: 'block',
marginBottom: settings.readerType === 'ContinuesVertical' ? '15px' : 0,
minWidth: '50vw',
width: dimensions.width < dimensions.height ? '100vw' : '100%',
maxWidth: '100%',
};
}
2021-03-18 21:46:24 +03:30
interface IProps {
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) => {
const { src, index, onImageLoad, settings } = props;
2021-03-23 03:50:55 +04:30
2021-05-28 19:37:26 +04:30
const imgRef = useRef<HTMLImageElement>(null);
2021-03-19 14:52:20 +03:30
const imgStyle = imageStyle(settings);
2021-03-18 21:46:24 +03:30
return (
<Box ref={ref} sx={{ margin: 'auto' }}>
2021-05-28 19:37:26 +04:30
<SpinnerImage
src={src}
onImageLoad={onImageLoad}
2021-05-28 19:37:26 +04:30
alt={`Page #${index}`}
imgRef={imgRef}
spinnerStyle={{
...imgStyle,
height: '100vh',
width: '70vw',
backgroundColor: '#525252',
}}
imgStyle={imgStyle}
2021-05-15 18:26:40 +04:30
/>
</Box>
2021-03-18 21:46:24 +03:30
);
2021-05-18 02:26:45 +04:30
});