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

152 lines
4.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, { useCallback, useEffect, useRef } from 'react';
2021-12-03 10:35:55 +03:30
import { Box } from '@mui/system';
import Page from '../Page';
2021-05-15 17:18:57 +04:30
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?
const SCROLL_SAFE_ZONE = 5; // px
const SCROLL_OFFSET = 0.95;
const SCROLL_BEHAVIOR: ScrollBehavior = 'smooth';
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;
};
const isAtTop = () => window.scrollY <= 0;
export default function VerticalPager(props: IReaderProps) {
const {
pages, settings, setCurPage, initialPage, nextChapter, prevChapter,
} = props;
2021-05-15 17:18:57 +04:30
const currentPageRef = useRef(initialPage);
const selfRef = useRef<HTMLDivElement>(null);
const pagesRef = useRef<HTMLDivElement[]>([]);
2021-05-28 19:37:26 +04:30
useEffect(() => {
const handleScroll = () => {
if (!selfRef.current) return;
if (isAtBottom()) {
// 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.
// See https://github.com/Suwayomi/Tachidesk-WebUI/issues/14 for more info
currentPageRef.current = pages.length - 1;
setCurPage(currentPageRef.current);
// Go to next chapter if configured to and at bottom
if (settings.loadNextonEnding) {
nextChapter();
}
} else {
// Update current page in parent
const currentPage = findCurrentPageIndex(selfRef.current);
if (currentPage !== currentPageRef.current) {
currentPageRef.current = currentPage;
setCurPage(currentPage);
}
}
};
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, [settings.loadNextonEnding]);
const go = useCallback((direction: 'up' | 'down') => {
if (direction === 'down' && isAtBottom()) {
nextChapter();
return;
}
2021-05-18 02:26:45 +04:30
if (direction === 'up' && isAtTop()) {
prevChapter();
return;
}
window.scroll({
top: window.scrollY + (window.innerHeight * SCROLL_OFFSET) * (direction === 'up' ? -1 : 1),
behavior: SCROLL_BEHAVIOR,
});
}, [nextChapter, prevChapter]);
useEffect(() => {
const handleKeyboard = (e:KeyboardEvent) => {
switch (e.code) {
case 'Space':
case 'ArrowRight':
e.preventDefault();
go('down');
break;
case 'ArrowLeft':
e.preventDefault();
go('up');
break;
default:
break;
}
};
document.addEventListener('keydown', handleKeyboard, false);
return () => {
document.removeEventListener('keydown', handleKeyboard);
};
}, []);
2021-05-15 17:18:57 +04:30
2021-05-18 02:26:45 +04:30
useEffect(() => {
// Delay scrolling to next cycle
setTimeout(() => {
// scroll last read page into view when initialPage changes
pagesRef.current[initialPage]?.scrollIntoView();
}, 0);
}, [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',
margin: '0 auto',
width: '100%',
}}
onClick={(e) => go(e.clientX > window.innerWidth / 2 ? 'down' : 'up')}
2021-12-03 10:35:55 +03:30
>
2021-05-15 17:18:57 +04:30
{
pages.map((page) => (
<Page
key={page.index}
index={page.index}
src={page.src}
onImageLoad={() => {}}
2021-05-15 17:18:57 +04:30
settings={settings}
ref={(e:HTMLDivElement) => { pagesRef.current[page.index] = e; }}
2021-05-15 17:18:57 +04:30
/>
))
}
2021-12-03 10:35:55 +03:30
</Box>
2021-05-15 17:18:57 +04:30
);
}