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

112 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 CircularProgress from '@material-ui/core/CircularProgress';
import { makeStyles } from '@material-ui/core/styles';
2021-03-19 14:52:20 +03:30
import React, { useEffect, useRef, useState } from 'react';
2021-03-18 21:46:24 +03:30
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,
},
2021-03-28 04:03:46 +04:30
image: {
display: 'block',
2021-05-15 23:22:37 +04:30
marginBottom: settings.readerType === 'ContinuesVertical' ? '15px' : 0,
2021-05-15 18:17:12 +04:30
minWidth: '50vw',
width: '100%',
maxWidth: '100%',
2021-03-28 04:03:46 +04:30
},
2021-03-18 21:46:24 +03:30
});
interface IProps {
src: string
index: number
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
}
function LazyImage(props: IProps) {
2021-03-23 03:50:55 +04:30
const {
src, index, setCurPage, settings,
} = props;
const classes = useStyles(settings)();
2021-03-18 21:46:24 +03:30
const [imageSrc, setImagsrc] = useState<string>('');
2021-03-19 14:52:20 +03:30
const ref = useRef<HTMLImageElement>(null);
const handleScroll = () => {
if (ref.current) {
const rect = ref.current.getBoundingClientRect();
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
useEffect(() => {
const img = new Image();
img.src = src;
img.onload = () => setImagsrc(src);
2021-03-23 03:50:55 +04:30
}, [src]);
2021-03-18 21:46:24 +03:30
if (imageSrc.length === 0) {
return (
2021-05-15 17:18:57 +04:30
<div className={`${classes.image} ${classes.loadingImage}`}>
2021-03-18 21:46:24 +03:30
<CircularProgress thickness={5} />
</div>
);
}
return (
2021-03-19 14:52:20 +03:30
<img
2021-03-28 04:03:46 +04:30
className={classes.image}
2021-03-19 14:52:20 +03:30
ref={ref}
src={imageSrc}
alt={`Page #${index}`}
/>
2021-03-18 21:46:24 +03:30
);
}
2021-05-18 02:26:45 +04:30
const Page = React.forwardRef((props: IProps, ref: any) => {
2021-03-23 03:50:55 +04:30
const {
src, index, setCurPage, settings,
} = props;
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-15 18:26:40 +04:30
<LazyImage
src={src}
index={index}
setCurPage={setCurPage}
settings={settings}
/>
2021-03-18 21:46:24 +03:30
</div>
);
2021-05-18 02:26:45 +04:30
});
export default Page;