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

101 lines
2.9 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 { makeStyles } from '@material-ui/core/styles';
import { CSSProperties } from '@material-ui/core/styles/withStyles';
2021-05-28 19:37:26 +04:30
import React, { useEffect, useRef } from 'react';
import SpinnerImage from 'components/SpinnerImage';
2021-03-18 21:46:24 +03:30
function imageStyle(settings: IReaderSettings): CSSProperties {
2021-05-28 11:57:31 -07:00
if (settings.readerType === 'DoubleLTR' || settings.readerType === 'DoubleRTL' || settings.readerType === 'ContinuesHorizontal') {
return {
display: 'block',
marginBottom: 0,
width: 'auto',
minHeight: '99vh',
height: 'auto',
maxHeight: '99vh',
objectFit: 'contain',
};
}
return {
display: 'block',
marginBottom: settings.readerType === 'ContinuesVertical' ? '15px' : 0,
minWidth: '50vw',
width: '100%',
maxWidth: '100%',
};
}
2021-03-23 03:50:55 +04:30
const useStyles = (settings: IReaderSettings) => makeStyles({
2021-03-18 21:46:24 +03:30
loading: {
margin: '100px auto',
height: '100vh',
2021-05-15 17:18:57 +04:30
width: '100vw',
2021-03-18 21:46:24 +03:30
},
loadingImage: {
height: '100vh',
2021-05-15 17:18:57 +04:30
width: '70vw',
padding: '50px calc(50% - 20px)',
2021-03-18 21:46:24 +03:30
backgroundColor: '#525252',
marginBottom: 10,
},
image: imageStyle(settings),
2021-03-18 21:46:24 +03:30
});
interface IProps {
src: string
index: number
onImageLoad: () => void
2021-03-19 14:52:20 +03:30
setCurPage: React.Dispatch<React.SetStateAction<number>>
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, setCurPage, settings,
2021-03-23 03:50:55 +04:30
} = props;
const classes = useStyles(settings)();
2021-05-28 19:37:26 +04:30
const imgRef = useRef<HTMLImageElement>(null);
2021-03-19 14:52:20 +03:30
const handleScroll = () => {
2021-05-28 19:37:26 +04:30
if (imgRef.current) {
const rect = imgRef.current.getBoundingClientRect();
2021-03-19 14:52:20 +03:30
if (rect.y < 0 && rect.y + rect.height > 0) {
setCurPage(index);
}
}
};
useEffect(() => {
2021-05-18 02:26:45 +04:30
if (settings.readerType === 'Webtoon' || settings.readerType === 'ContinuesVertical') {
window.addEventListener('scroll', handleScroll);
2021-03-19 14:52:20 +03:30
2021-05-18 02:26:45 +04:30
return () => {
window.removeEventListener('scroll', handleScroll);
};
} return () => {};
2021-03-19 14:52:20 +03:30
}, [handleScroll]);
2021-03-18 21:46:24 +03:30
return (
2021-05-18 02:26:45 +04:30
<div ref={ref} style={{ margin: '0 auto' }}>
2021-05-28 19:37:26 +04:30
<SpinnerImage
2021-05-15 18:26:40 +04:30
src={src}
onImageLoad={onImageLoad}
2021-05-28 19:37:26 +04:30
alt={`Page #${index}`}
imgRef={imgRef}
spinnerClassName={`${classes.image} ${classes.loadingImage}`}
imgClassName={classes.image}
2021-05-15 18:26:40 +04:30
/>
2021-03-18 21:46:24 +03:30
</div>
);
2021-05-18 02:26:45 +04:30
});
export default Page;