Files
suwayomi-material-you-webui/src/components/reader/pager/PagedPager.tsx

113 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
* 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 React, { useEffect, useRef } from 'react';
2021-12-03 10:36:36 +03:30
import { Box } from '@mui/system';
import Page from '../Page';
2021-05-15 18:17:12 +04:30
2021-05-15 23:22:37 +04:30
export default function PagedReader(props: IReaderProps) {
2021-05-15 18:17:12 +04:30
const {
pages, settings, setCurPage, curPage, nextChapter, prevChapter,
2021-05-15 18:17:12 +04:30
} = props;
const selfRef = useRef<HTMLDivElement>(null);
const changePage = (newPage: number) => {
setCurPage(newPage);
window.scroll({ top: 0 });
};
2021-05-15 18:17:12 +04:30
function nextPage() {
if (curPage < pages.length - 1) {
changePage(curPage + 1);
} else if (settings.loadNextonEnding) {
nextChapter();
}
2021-05-15 18:17:12 +04:30
}
function prevPage() {
if (curPage > 0) {
changePage(curPage - 1);
} 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
}
function keyboardControl(e:KeyboardEvent) {
switch (e.code) {
case 'Space':
e.preventDefault();
nextPage();
break;
2021-05-15 18:17:12 +04:30
case 'ArrowRight':
goRight();
2021-05-15 18:17:12 +04:30
break;
case 'ArrowLeft':
goLeft();
2021-05-15 18:17:12 +04:30
break;
default:
break;
}
}
function clickControl(e:MouseEvent) {
if (e.clientX > window.innerWidth / 2) {
goRight();
2021-05-15 18:17:12 +04:30
} else {
goLeft();
2021-05-15 18:17:12 +04:30
}
}
useEffect(() => {
document.addEventListener('keydown', keyboardControl);
selfRef.current?.addEventListener('click', clickControl);
2021-05-15 18:17:12 +04:30
return () => {
document.removeEventListener('keydown', keyboardControl);
selfRef.current?.removeEventListener('click', clickControl);
2021-05-15 18:17:12 +04:30
};
}, [selfRef, curPage, settings.readerType]);
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',
}}
>
2021-05-15 18:17:12 +04:30
<Page
key={curPage}
index={curPage}
onImageLoad={() => {}}
2021-05-15 18:17:12 +04:30
src={pages[curPage].src}
settings={settings}
/>
2021-12-03 10:36:36 +03:30
</Box>
2021-05-15 18:17:12 +04:30
);
}