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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-05-17 01:38:59 +04:30
|
|
|
|
2024-07-12 23:12:11 +02:00
|
|
|
import { useCallback, useEffect, useRef } from 'react';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Box from '@mui/material/Box';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { IReaderProps } from '@/typings';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { Page } from '@/components/reader/Page';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
|
2021-05-15 17:18:57 +04:30
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
const findCurrentPageIndex = (wrapper: HTMLDivElement): number => {
|
|
|
|
|
for (let i = 0; i < wrapper.children.length; i++) {
|
|
|
|
|
const child = wrapper.children.item(i);
|
|
|
|
|
if (child) {
|
|
|
|
|
const { top, bottom } = child.getBoundingClientRect();
|
|
|
|
|
if (top <= window.innerHeight && bottom > 1) return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO: make configurable?
|
2022-11-26 13:50:53 +01:00
|
|
|
const SCROLL_SAFE_ZONE = 5; // px
|
2022-11-24 21:05:05 +01:00
|
|
|
const SCROLL_OFFSET = 0.95;
|
2024-02-01 19:29:21 +01:00
|
|
|
const SCROLL_OFFSET_SLIGHT = 0.25;
|
2022-11-24 21:05:05 +01:00
|
|
|
const SCROLL_BEHAVIOR: ScrollBehavior = 'smooth';
|
|
|
|
|
|
2022-11-26 13:50:53 +01:00
|
|
|
const isAtBottom = () => {
|
|
|
|
|
const visibleBottom = window.innerHeight + window.scrollY;
|
|
|
|
|
// SCROLL_SAFE_ZONE is here for special cases when window might be .5px shorter
|
|
|
|
|
// and math just dont add up correctly
|
|
|
|
|
return visibleBottom >= document.body.offsetHeight - SCROLL_SAFE_ZONE;
|
|
|
|
|
};
|
2022-11-24 21:05:05 +01:00
|
|
|
const isAtTop = () => window.scrollY <= 0;
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function VerticalPager(props: IReaderProps) {
|
2023-02-06 10:06:33 +01:00
|
|
|
const { pages, settings, setCurPage, initialPage, nextChapter, prevChapter } = props;
|
2021-05-15 17:18:57 +04:30
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
const currentPageRef = useRef(initialPage);
|
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(() => {
|
2023-04-05 13:37:12 +02:00
|
|
|
let handlingEndOfPage = false;
|
2022-11-24 21:05:05 +01:00
|
|
|
const handleScroll = () => {
|
|
|
|
|
if (!selfRef.current) return;
|
|
|
|
|
|
|
|
|
|
if (isAtBottom()) {
|
2023-04-05 13:37:12 +02:00
|
|
|
if (handlingEndOfPage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handlingEndOfPage = true;
|
|
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
// If scroll is moved all the way to the bottom
|
|
|
|
|
// This handles cases when last page is show, but is smaller then
|
|
|
|
|
// window, in which case it would never get marked as read.
|
2024-01-05 20:11:29 +01:00
|
|
|
// See https://github.com/Suwayomi/Suwayomi-WebUI/issues/14 for more info
|
2022-11-24 21:05:05 +01:00
|
|
|
currentPageRef.current = pages.length - 1;
|
|
|
|
|
setCurPage(currentPageRef.current);
|
2021-05-24 12:16:05 -07:00
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
// Go to next chapter if configured to and at bottom
|
2023-01-06 18:19:45 +01:00
|
|
|
if (settings.loadNextOnEnding) {
|
2022-11-24 21:05:05 +01:00
|
|
|
nextChapter();
|
|
|
|
|
}
|
2021-05-24 12:16:05 -07:00
|
|
|
} else {
|
2023-04-05 13:37:12 +02:00
|
|
|
handlingEndOfPage = false;
|
|
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
// Update current page in parent
|
|
|
|
|
const currentPage = findCurrentPageIndex(selfRef.current);
|
|
|
|
|
if (currentPage !== currentPageRef.current) {
|
|
|
|
|
currentPageRef.current = currentPage;
|
|
|
|
|
setCurPage(currentPage);
|
|
|
|
|
}
|
2021-05-24 12:16:05 -07:00
|
|
|
}
|
2022-11-24 21:05:05 +01:00
|
|
|
};
|
2021-05-24 12:16:05 -07:00
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
window.addEventListener('scroll', handleScroll);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('scroll', handleScroll);
|
|
|
|
|
};
|
2023-04-05 13:37:12 +02:00
|
|
|
}, [settings.loadNextOnEnding, nextChapter]);
|
2021-05-24 12:16:05 -07:00
|
|
|
|
2023-11-12 01:41:20 +01:00
|
|
|
const mouseYPos = useRef<number>(0);
|
|
|
|
|
const didMouseMove = useRef(false);
|
|
|
|
|
|
|
|
|
|
function dragScreen(e: MouseEvent) {
|
|
|
|
|
didMouseMove.current = true;
|
|
|
|
|
window.scrollBy(0, mouseYPos.current - e.pageY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dragControl(e: MouseEvent) {
|
|
|
|
|
mouseYPos.current = e.pageY;
|
|
|
|
|
selfRef.current?.addEventListener('mousemove', dragScreen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeDragControl() {
|
|
|
|
|
selfRef.current?.removeEventListener('mousemove', dragScreen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
selfRef.current?.addEventListener('mousedown', dragControl);
|
|
|
|
|
selfRef.current?.addEventListener('mouseup', removeDragControl);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
selfRef.current?.removeEventListener('mousedown', dragControl);
|
|
|
|
|
selfRef.current?.removeEventListener('mouseup', removeDragControl);
|
|
|
|
|
};
|
|
|
|
|
}, [selfRef]);
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const go = useCallback(
|
2024-02-01 19:29:21 +01:00
|
|
|
(direction: 'up' | 'down', offset: number = SCROLL_OFFSET) => {
|
2023-02-06 10:06:33 +01:00
|
|
|
if (direction === 'down' && isAtBottom()) {
|
|
|
|
|
nextChapter();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-18 02:26:45 +04:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
if (direction === 'up' && isAtTop()) {
|
|
|
|
|
prevChapter();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-24 21:05:05 +01:00
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
window.scroll({
|
2024-02-01 19:29:21 +01:00
|
|
|
top: window.scrollY + window.innerHeight * offset * (direction === 'up' ? -1 : 1),
|
2023-02-06 10:06:33 +01:00
|
|
|
behavior: SCROLL_BEHAVIOR,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[nextChapter, prevChapter],
|
|
|
|
|
);
|
2021-05-24 12:16:05 -07:00
|
|
|
|
2021-05-17 09:57:14 +02:00
|
|
|
useEffect(() => {
|
2023-02-06 10:06:33 +01:00
|
|
|
const handleKeyboard = (e: KeyboardEvent) => {
|
2023-11-26 14:29:03 +01:00
|
|
|
switch (e.key) {
|
2022-11-24 21:05:05 +01:00
|
|
|
case 'Space':
|
2023-11-12 01:41:20 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
go(e.shiftKey ? 'up' : 'down');
|
|
|
|
|
break;
|
|
|
|
|
case 'ArrowDown':
|
2024-02-01 19:29:21 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
go(e.shiftKey ? 'up' : 'down', SCROLL_OFFSET_SLIGHT);
|
|
|
|
|
break;
|
2022-11-24 21:05:05 +01:00
|
|
|
case 'ArrowRight':
|
|
|
|
|
e.preventDefault();
|
2023-11-12 01:41:20 +01:00
|
|
|
go(e.shiftKey ? 'up' : 'down');
|
2022-11-24 21:05:05 +01:00
|
|
|
break;
|
2023-11-12 01:41:20 +01:00
|
|
|
case 'ArrowUp':
|
2024-02-01 19:29:21 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
go(e.shiftKey ? 'down' : 'up', SCROLL_OFFSET_SLIGHT);
|
|
|
|
|
break;
|
2022-11-24 21:05:05 +01:00
|
|
|
case 'ArrowLeft':
|
|
|
|
|
e.preventDefault();
|
2023-11-12 01:41:20 +01:00
|
|
|
go(e.shiftKey ? 'down' : 'up');
|
2022-11-24 21:05:05 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-05-17 09:57:14 +02:00
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
document.addEventListener('keydown', handleKeyboard, false);
|
2021-05-17 09:57:14 +02:00
|
|
|
return () => {
|
2022-11-24 21:05:05 +01:00
|
|
|
document.removeEventListener('keydown', handleKeyboard);
|
2021-05-17 09:57:14 +02:00
|
|
|
};
|
2023-04-05 13:37:12 +02:00
|
|
|
}, [go]);
|
2021-05-15 17:18:57 +04:30
|
|
|
|
2024-07-12 23:12:11 +02:00
|
|
|
useResizeObserver(
|
|
|
|
|
pagesRef.current[initialPage],
|
|
|
|
|
useCallback(
|
|
|
|
|
(_, resizeObserver) => {
|
|
|
|
|
const initialPageElement = pagesRef.current[initialPage];
|
|
|
|
|
if (!initialPageElement?.offsetHeight) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-03-01 01:17:13 +01:00
|
|
|
|
2024-07-12 23:12:11 +02:00
|
|
|
initialPageElement.scrollIntoView();
|
|
|
|
|
resizeObserver.disconnect();
|
|
|
|
|
},
|
|
|
|
|
[pagesRef.current[initialPage], initialPage],
|
|
|
|
|
),
|
|
|
|
|
);
|
2021-05-18 02:26:45 +04:30
|
|
|
|
2021-05-15 17:18:57 +04:30
|
|
|
return (
|
2021-12-03 10:35:55 +03:30
|
|
|
<Box
|
|
|
|
|
ref={selfRef}
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
justifyContent: 'center',
|
2024-01-26 11:51:08 -08:00
|
|
|
width: 'auto',
|
|
|
|
|
height: 'auto',
|
2023-11-12 01:48:10 +01:00
|
|
|
userSelect: 'none',
|
2021-12-03 10:35:55 +03:30
|
|
|
}}
|
2023-11-12 01:41:20 +01:00
|
|
|
onClick={(e) => {
|
|
|
|
|
if (didMouseMove.current) {
|
|
|
|
|
didMouseMove.current = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go(e.clientX > window.innerWidth / 2 ? 'down' : 'up');
|
|
|
|
|
}}
|
2021-12-03 10:35:55 +03:30
|
|
|
>
|
2023-02-06 10:06:33 +01:00
|
|
|
{pages.map((page) => (
|
|
|
|
|
<Page
|
|
|
|
|
key={page.index}
|
|
|
|
|
index={page.index}
|
|
|
|
|
src={page.src}
|
|
|
|
|
onImageLoad={() => {}}
|
|
|
|
|
settings={settings}
|
|
|
|
|
ref={(e: HTMLDivElement) => {
|
|
|
|
|
pagesRef.current[page.index] = e;
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2021-12-03 10:35:55 +03:30
|
|
|
</Box>
|
2021-05-15 17:18:57 +04:30
|
|
|
);
|
|
|
|
|
}
|