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

120 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/. */
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 {
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: '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 handleVerticalScroll = () => {
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);
}
}
};
const handleHorizontalScroll = () => {
if (imgRef.current) {
const rect = imgRef.current.getBoundingClientRect();
if (rect.left <= window.innerWidth / 2 && rect.right > window.innerWidth / 2) {
setCurPage(index);
}
}
};
2021-03-19 14:52:20 +03:30
useEffect(() => {
switch (settings.readerType) {
case 'Webtoon':
case 'ContinuesVertical':
window.addEventListener('scroll', handleVerticalScroll);
return () => window.removeEventListener('scroll', handleVerticalScroll);
case 'ContinuesHorizontalLTR':
case 'ContinuesHorizontalRTL':
window.addEventListener('scroll', handleHorizontalScroll);
return () => window.removeEventListener('scroll', handleHorizontalScroll);
default:
return () => {};
}
}, [handleVerticalScroll]);
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;