2021-05-17 01:38:59 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* 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-05-24 12:16:05 -07:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2021-11-14 15:51:08 +05:30
|
|
|
import { Box } from '@mui/system';
|
2021-05-17 01:38:59 +04:30
|
|
|
import Page from '../Page';
|
2021-05-15 17:18:57 +04:30
|
|
|
|
2021-05-17 09:57:14 +02:00
|
|
|
export default function VerticalReader(props: IReaderProps) {
|
|
|
|
|
const {
|
2021-11-15 12:53:18 +03:30
|
|
|
pages, settings, setCurPage, curPage, nextChapter, prevChapter,
|
2021-05-17 09:57:14 +02:00
|
|
|
} = props;
|
2021-05-15 17:18:57 +04:30
|
|
|
|
2021-05-24 12:16:05 -07:00
|
|
|
const selfRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const pagesRef = useRef<HTMLDivElement[]>([]);
|
|
|
|
|
|
2021-05-28 19:37:26 +04:30
|
|
|
useEffect(() => {
|
|
|
|
|
pagesRef.current = pagesRef.current.slice(0, pages.length);
|
|
|
|
|
}, [pages.length]);
|
|
|
|
|
|
2021-05-24 12:16:05 -07:00
|
|
|
function nextPage() {
|
|
|
|
|
if (curPage < pages.length - 1) {
|
|
|
|
|
pagesRef.current[curPage + 1]?.scrollIntoView();
|
|
|
|
|
setCurPage((page) => page + 1);
|
|
|
|
|
} else if (settings.loadNextonEnding) {
|
|
|
|
|
nextChapter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prevPage() {
|
|
|
|
|
if (curPage > 0) {
|
|
|
|
|
const rect = pagesRef.current[curPage].getBoundingClientRect();
|
|
|
|
|
if (rect.y < 0 && rect.y + rect.height > 0) {
|
|
|
|
|
pagesRef.current[curPage]?.scrollIntoView();
|
|
|
|
|
} else {
|
|
|
|
|
pagesRef.current[curPage - 1]?.scrollIntoView();
|
|
|
|
|
setCurPage(curPage - 1);
|
|
|
|
|
}
|
2021-05-25 13:14:07 +04:30
|
|
|
} else if (curPage === 0) {
|
|
|
|
|
prevChapter();
|
2021-05-24 12:16:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function keyboardControl(e:KeyboardEvent) {
|
|
|
|
|
switch (e.code) {
|
|
|
|
|
case 'Space':
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
nextPage();
|
|
|
|
|
break;
|
|
|
|
|
case 'ArrowRight':
|
|
|
|
|
nextPage();
|
|
|
|
|
break;
|
|
|
|
|
case 'ArrowLeft':
|
|
|
|
|
prevPage();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clickControl(e:MouseEvent) {
|
|
|
|
|
if (e.clientX > window.innerWidth / 2) {
|
|
|
|
|
nextPage();
|
|
|
|
|
} else {
|
|
|
|
|
prevPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-18 02:26:45 +04:30
|
|
|
|
2021-05-17 09:57:14 +02:00
|
|
|
const handleLoadNextonEnding = () => {
|
|
|
|
|
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
|
2021-05-18 01:10:28 +04:30
|
|
|
nextChapter();
|
2021-05-17 09:57:14 +02:00
|
|
|
}
|
|
|
|
|
};
|
2021-05-24 12:16:05 -07:00
|
|
|
|
2021-05-17 09:57:14 +02:00
|
|
|
useEffect(() => {
|
2021-05-25 16:19:08 +04:30
|
|
|
if (settings.loadNextonEnding) { document.addEventListener('scroll', handleLoadNextonEnding); }
|
2021-05-24 12:16:05 -07:00
|
|
|
document.addEventListener('keydown', keyboardControl, false);
|
|
|
|
|
selfRef.current?.addEventListener('click', clickControl);
|
2021-05-17 09:57:14 +02:00
|
|
|
|
|
|
|
|
return () => {
|
2021-05-24 12:16:05 -07:00
|
|
|
document.removeEventListener('scroll', handleLoadNextonEnding);
|
|
|
|
|
document.removeEventListener('keydown', keyboardControl);
|
|
|
|
|
selfRef.current?.removeEventListener('click', clickControl);
|
2021-05-17 09:57:14 +02:00
|
|
|
};
|
2021-05-24 12:16:05 -07:00
|
|
|
}, [selfRef, curPage]);
|
2021-05-15 17:18:57 +04:30
|
|
|
|
2021-05-18 02:26:45 +04:30
|
|
|
useEffect(() => {
|
2021-11-15 12:53:18 +03:30
|
|
|
// scroll last read page into view after first mount
|
|
|
|
|
pagesRef.current[curPage].scrollIntoView();
|
2021-05-28 19:37:26 +04:30
|
|
|
}, [pagesRef.current.length]);
|
2021-05-18 02:26:45 +04:30
|
|
|
|
2021-05-15 17:18:57 +04:30
|
|
|
return (
|
2021-11-14 15:51:08 +05:30
|
|
|
<Box
|
|
|
|
|
ref={selfRef}
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
margin: '0 auto',
|
|
|
|
|
width: '100%',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2021-05-15 17:18:57 +04:30
|
|
|
{
|
|
|
|
|
pages.map((page) => (
|
|
|
|
|
<Page
|
|
|
|
|
key={page.index}
|
|
|
|
|
index={page.index}
|
|
|
|
|
src={page.src}
|
2021-05-28 05:36:55 -07:00
|
|
|
onImageLoad={() => {}}
|
2021-05-15 17:18:57 +04:30
|
|
|
setCurPage={setCurPage}
|
|
|
|
|
settings={settings}
|
2021-05-24 12:16:05 -07:00
|
|
|
ref={(e:HTMLDivElement) => { pagesRef.current[page.index] = e; }}
|
2021-05-15 17:18:57 +04:30
|
|
|
/>
|
|
|
|
|
))
|
|
|
|
|
}
|
2021-11-14 15:51:08 +05:30
|
|
|
</Box>
|
2021-05-15 17:18:57 +04:30
|
|
|
);
|
|
|
|
|
}
|