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

96 lines
2.8 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/. */
import React, { useRef } from 'react';
2021-10-30 16:10:34 +03:30
import SpinnerImage from 'components/util/SpinnerImage';
2021-09-18 20:59:23 +04:30
import useLocalStorage from 'util/useLocalStorage';
import Box from '@mui/system/Box';
2021-03-18 21:46:24 +03:30
2021-09-09 17:51:22 +04:30
function imageStyle(settings: IReaderSettings): any {
const [dimensions, setDimensions] = React.useState({
height: window.innerHeight,
width: window.innerWidth,
});
React.useEffect(() => {
function handleResize() {
setDimensions({
height: window.innerHeight,
width: window.innerWidth,
});
}
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
if (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',
pointerEvents: 'none',
};
}
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
2021-03-23 03:50:55 +04:30
settings: IReaderSettings
2021-03-18 21:46:24 +03:30
}
2021-05-28 19:37:26 +04:30
const Page = React.forwardRef((props: IProps, ref: any) => {
2021-03-23 03:50:55 +04:30
const {
src, index, onImageLoad, settings,
2021-03-23 03:50:55 +04:30
} = props;
2021-09-18 20:59:23 +04:30
const [useCache] = useLocalStorage<boolean>('useCache', true);
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
2021-09-18 20:59:23 +04:30
src={`${src}?useCache=${useCache}`}
onImageLoad={onImageLoad}
2021-05-28 19:37:26 +04:30
alt={`Page #${index}`}
imgRef={imgRef}
spinnerStyle={{
...imgStyle,
height: '100vh',
width: '70vw',
padding: '50px calc(50% - 20px)',
backgroundColor: '#525252',
marginBottom: 10,
}}
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
});
export default Page;