2021-05-28 05:36:55 -07:00
|
|
|
/*
|
|
|
|
|
* 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-11-15 17:21:31 +03:30
|
|
|
import makeStyles from '@mui/styles/makeStyles';
|
2021-05-28 05:36:55 -07:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
import Page from '../Page';
|
|
|
|
|
import DoublePage from '../DoublePage';
|
|
|
|
|
|
2021-11-15 17:21:31 +03:30
|
|
|
const useStyles = (settings: IReaderSettings) => makeStyles({
|
|
|
|
|
preload: {
|
|
|
|
|
display: 'none',
|
|
|
|
|
},
|
|
|
|
|
reader: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: (settings.readerType === 'DoubleLTR') ? 'row' : 'row-reverse',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
margin: '0 auto',
|
|
|
|
|
width: 'auto',
|
|
|
|
|
height: 'auto',
|
|
|
|
|
overflowX: 'scroll',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-28 05:36:55 -07:00
|
|
|
export default function DoublePagedPager(props: IReaderProps) {
|
|
|
|
|
const {
|
|
|
|
|
pages, settings, setCurPage, curPage, nextChapter, prevChapter,
|
|
|
|
|
} = props;
|
|
|
|
|
|
2021-11-15 17:21:31 +03:30
|
|
|
const classes = useStyles(settings)();
|
|
|
|
|
|
2021-05-28 05:36:55 -07:00
|
|
|
const selfRef = useRef<HTMLDivElement>(null);
|
2021-05-28 11:55:04 -07:00
|
|
|
const pagesRef = useRef<HTMLImageElement[]>([]);
|
2021-05-28 05:36:55 -07:00
|
|
|
|
|
|
|
|
const pagesDisplayed = useRef<number>(0);
|
|
|
|
|
const pageLoaded = useRef<boolean[]>(Array(pages.length).fill(false));
|
|
|
|
|
|
2021-05-28 11:55:04 -07:00
|
|
|
function setPagesToDisplay() {
|
|
|
|
|
pagesDisplayed.current = 0;
|
|
|
|
|
if (curPage < pages.length && pagesRef.current[curPage]) {
|
|
|
|
|
if (pageLoaded.current[curPage]) {
|
|
|
|
|
pagesDisplayed.current = 1;
|
|
|
|
|
const imgElem = pagesRef.current[curPage];
|
|
|
|
|
const aspectRatio = imgElem.height / imgElem.width;
|
|
|
|
|
if (aspectRatio < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (curPage + 1 < pages.length && pagesRef.current[curPage + 1]) {
|
|
|
|
|
if (pageLoaded.current[curPage + 1]) {
|
|
|
|
|
const imgElem = pagesRef.current[curPage + 1];
|
|
|
|
|
const aspectRatio = imgElem.height / imgElem.width;
|
|
|
|
|
if (aspectRatio < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pagesDisplayed.current = 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function displayPages() {
|
|
|
|
|
if (pagesDisplayed.current === 2) {
|
|
|
|
|
ReactDOM.render(
|
|
|
|
|
<DoublePage
|
|
|
|
|
key={curPage}
|
|
|
|
|
index={curPage}
|
|
|
|
|
image1src={pages[curPage].src}
|
|
|
|
|
image2src={pages[curPage + 1].src}
|
|
|
|
|
settings={settings}
|
|
|
|
|
/>,
|
|
|
|
|
document.getElementById('display'),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
ReactDOM.render(
|
|
|
|
|
<Page
|
|
|
|
|
key={curPage}
|
|
|
|
|
index={curPage}
|
|
|
|
|
src={(pagesDisplayed.current === 1) ? pages[curPage].src : ''}
|
|
|
|
|
onImageLoad={() => {}}
|
|
|
|
|
setCurPage={setCurPage}
|
|
|
|
|
settings={settings}
|
|
|
|
|
/>,
|
|
|
|
|
document.getElementById('display'),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 05:36:55 -07:00
|
|
|
function pagesToGoBack() {
|
|
|
|
|
for (let i = 1; i <= 2; i++) {
|
|
|
|
|
if (curPage - i > 0 && pagesRef.current[curPage - i]) {
|
2021-05-28 11:55:04 -07:00
|
|
|
if (pageLoaded.current[curPage - i]) {
|
|
|
|
|
const imgElem = pagesRef.current[curPage - i];
|
2021-05-28 05:36:55 -07:00
|
|
|
const aspectRatio = imgElem.height / imgElem.width;
|
|
|
|
|
if (aspectRatio < 1) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function nextPage() {
|
|
|
|
|
if (curPage < pages.length - 1) {
|
|
|
|
|
const nextCurPage = curPage + pagesDisplayed.current;
|
|
|
|
|
setCurPage((nextCurPage >= pages.length) ? pages.length - 1 : nextCurPage);
|
|
|
|
|
} else if (settings.loadNextonEnding) {
|
|
|
|
|
nextChapter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prevPage() {
|
|
|
|
|
if (curPage > 0) {
|
|
|
|
|
const nextCurPage = curPage - pagesToGoBack();
|
|
|
|
|
setCurPage((nextCurPage < 0) ? 0 : nextCurPage);
|
|
|
|
|
} else {
|
|
|
|
|
prevChapter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goLeft() {
|
|
|
|
|
if (settings.readerType === 'DoubleLTR') {
|
|
|
|
|
prevPage();
|
|
|
|
|
} else {
|
|
|
|
|
nextPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goRight() {
|
|
|
|
|
if (settings.readerType === 'DoubleLTR') {
|
|
|
|
|
nextPage();
|
|
|
|
|
} else {
|
|
|
|
|
prevPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function keyboardControl(e:KeyboardEvent) {
|
|
|
|
|
switch (e.code) {
|
|
|
|
|
case 'Space':
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
nextPage();
|
|
|
|
|
break;
|
|
|
|
|
case 'ArrowRight':
|
|
|
|
|
goRight();
|
|
|
|
|
break;
|
|
|
|
|
case 'ArrowLeft':
|
|
|
|
|
goLeft();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clickControl(e:MouseEvent) {
|
|
|
|
|
if (e.clientX > window.innerWidth / 2) {
|
|
|
|
|
goRight();
|
|
|
|
|
} else {
|
|
|
|
|
goLeft();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleImageLoad(index: number) {
|
|
|
|
|
return () => {
|
|
|
|
|
pageLoaded.current[index] = true;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const retryDisplay = setInterval(() => {
|
|
|
|
|
const isLastPage = (curPage === pages.length - 1);
|
|
|
|
|
if ((!isLastPage && pageLoaded.current[curPage] && pageLoaded.current[curPage + 1])
|
|
|
|
|
|| pageLoaded.current[curPage]) {
|
|
|
|
|
setPagesToDisplay();
|
2021-05-28 11:55:04 -07:00
|
|
|
displayPages();
|
2021-05-28 05:36:55 -07:00
|
|
|
clearInterval(retryDisplay);
|
|
|
|
|
}
|
|
|
|
|
}, 50);
|
|
|
|
|
|
|
|
|
|
document.addEventListener('keydown', keyboardControl);
|
|
|
|
|
selfRef.current?.addEventListener('click', clickControl);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
clearInterval(retryDisplay);
|
|
|
|
|
document.removeEventListener('keydown', keyboardControl);
|
|
|
|
|
selfRef.current?.removeEventListener('click', clickControl);
|
|
|
|
|
};
|
|
|
|
|
}, [selfRef, curPage, settings.readerType]);
|
|
|
|
|
|
|
|
|
|
return (
|
2021-11-15 17:21:31 +03:30
|
|
|
<div ref={selfRef}>
|
|
|
|
|
<div id="preload" className={classes.preload}>
|
2021-05-28 05:36:55 -07:00
|
|
|
{
|
|
|
|
|
pages.map((page) => (
|
2021-05-28 11:55:04 -07:00
|
|
|
<img
|
|
|
|
|
ref={(e:HTMLImageElement) => { pagesRef.current[page.index] = e; }}
|
|
|
|
|
key={`${page.index}`}
|
2021-05-28 05:36:55 -07:00
|
|
|
src={page.src}
|
2021-05-28 11:55:04 -07:00
|
|
|
onLoad={handleImageLoad(page.index)}
|
|
|
|
|
alt={`${page.index}`}
|
2021-05-28 05:36:55 -07:00
|
|
|
/>
|
|
|
|
|
))
|
|
|
|
|
}
|
2021-11-15 17:21:31 +03:30
|
|
|
</div>
|
|
|
|
|
<div id="display" className={classes.reader} />
|
|
|
|
|
</div>
|
2021-05-28 05:36:55 -07:00
|
|
|
);
|
|
|
|
|
}
|