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-17 14:22:24 +04:30
|
|
|
import React, { useEffect, useRef } from 'react';
|
2021-12-03 10:36:36 +03:30
|
|
|
import { Box } from '@mui/system';
|
2023-02-05 16:15:20 +01:00
|
|
|
import Page from 'components/reader/Page';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { IReaderProps } from 'typings';
|
2021-05-15 18:17:12 +04:30
|
|
|
|
2021-05-15 23:22:37 +04:30
|
|
|
export default function PagedReader(props: IReaderProps) {
|
2023-02-12 16:12:17 +01:00
|
|
|
const { pages, settings, setCurPage, initialPage, curPage, nextChapter, prevChapter } = props;
|
2021-05-15 18:17:12 +04:30
|
|
|
|
2021-05-24 12:16:05 -07:00
|
|
|
const selfRef = useRef<HTMLDivElement>(null);
|
2021-05-17 14:22:24 +04:30
|
|
|
|
2022-11-24 21:04:39 +01:00
|
|
|
const changePage = (newPage: number) => {
|
|
|
|
|
setCurPage(newPage);
|
|
|
|
|
window.scroll({ top: 0 });
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-15 18:17:12 +04:30
|
|
|
function nextPage() {
|
2021-05-17 09:57:14 +02:00
|
|
|
if (curPage < pages.length - 1) {
|
2022-11-24 21:04:39 +01:00
|
|
|
changePage(curPage + 1);
|
2023-01-06 18:19:45 +01:00
|
|
|
} else if (settings.loadNextOnEnding) {
|
2021-05-18 01:10:28 +04:30
|
|
|
nextChapter();
|
2021-05-17 09:57:14 +02:00
|
|
|
}
|
2021-05-15 18:17:12 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prevPage() {
|
2021-05-28 05:36:55 -07:00
|
|
|
if (curPage > 0) {
|
2022-11-24 21:04:39 +01:00
|
|
|
changePage(curPage - 1);
|
2021-05-28 05:36:55 -07:00
|
|
|
} else {
|
|
|
|
|
prevChapter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goLeft() {
|
|
|
|
|
if (settings.readerType === 'SingleLTR') {
|
|
|
|
|
prevPage();
|
|
|
|
|
} else if (settings.readerType === 'SingleRTL') {
|
|
|
|
|
nextPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goRight() {
|
|
|
|
|
if (settings.readerType === 'SingleLTR') {
|
|
|
|
|
nextPage();
|
|
|
|
|
} else if (settings.readerType === 'SingleRTL') {
|
|
|
|
|
prevPage();
|
|
|
|
|
}
|
2021-05-15 18:17:12 +04:30
|
|
|
}
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
function keyboardControl(e: KeyboardEvent) {
|
2021-05-24 12:16:05 -07:00
|
|
|
switch (e.code) {
|
|
|
|
|
case 'Space':
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
nextPage();
|
|
|
|
|
break;
|
2021-05-15 18:17:12 +04:30
|
|
|
case 'ArrowRight':
|
2021-05-28 05:36:55 -07:00
|
|
|
goRight();
|
2021-05-15 18:17:12 +04:30
|
|
|
break;
|
|
|
|
|
case 'ArrowLeft':
|
2021-05-28 05:36:55 -07:00
|
|
|
goLeft();
|
2021-05-15 18:17:12 +04:30
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
function clickControl(e: MouseEvent) {
|
2021-05-15 18:17:12 +04:30
|
|
|
if (e.clientX > window.innerWidth / 2) {
|
2021-05-28 05:36:55 -07:00
|
|
|
goRight();
|
2021-05-15 18:17:12 +04:30
|
|
|
} else {
|
2021-05-28 05:36:55 -07:00
|
|
|
goLeft();
|
2021-05-15 18:17:12 +04:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-05-24 12:16:05 -07:00
|
|
|
document.addEventListener('keydown', keyboardControl);
|
|
|
|
|
selfRef.current?.addEventListener('click', clickControl);
|
2021-05-15 18:17:12 +04:30
|
|
|
|
|
|
|
|
return () => {
|
2021-05-24 12:16:05 -07:00
|
|
|
document.removeEventListener('keydown', keyboardControl);
|
|
|
|
|
selfRef.current?.removeEventListener('click', clickControl);
|
2021-05-15 18:17:12 +04:30
|
|
|
};
|
2023-04-05 13:37:12 +02:00
|
|
|
}, [selfRef, curPage, settings.readerType, prevChapter, nextChapter]);
|
2021-05-15 18:17:12 +04:30
|
|
|
|
2023-02-12 16:12:17 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
// Delay scrolling to next cycle
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
// scroll last read page into view when initialPage changes
|
|
|
|
|
changePage(initialPage);
|
|
|
|
|
}, 0);
|
|
|
|
|
}, [initialPage]);
|
|
|
|
|
|
2021-05-15 18:17:12 +04:30
|
|
|
return (
|
2021-12-03 10:36:36 +03:30
|
|
|
<Box
|
|
|
|
|
ref={selfRef}
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
margin: '0 auto',
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '100vh',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-02-08 18:19:26 +01:00
|
|
|
<Page key={curPage} index={curPage} onImageLoad={() => {}} src={pages[curPage].src} settings={settings} />
|
2021-12-03 10:36:36 +03:30
|
|
|
</Box>
|
2021-05-15 18:17:12 +04:30
|
|
|
);
|
|
|
|
|
}
|