2025-01-28 02:16:00 +01: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-05 01:26:27 +02:00
|
|
|
import { useCallback, useEffect, useMemo } from 'react';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { ReaderControls } from '@/features/reader/services/ReaderControls.ts';
|
2025-08-16 02:02:13 +02:00
|
|
|
import { ReadingDirection, ReadingMode } from '@/features/reader/Reader.types.ts';
|
2025-01-28 02:16:00 +01:00
|
|
|
import {
|
|
|
|
|
isContinuousReadingMode,
|
|
|
|
|
isContinuousVerticalReadingMode,
|
2025-08-16 02:02:13 +02:00
|
|
|
} from '@/features/reader/settings/ReaderSettings.utils.tsx';
|
|
|
|
|
import { READING_DIRECTION_TO_THEME_DIRECTION } from '@/features/reader/settings/ReaderSettings.constants.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { getOptionForDirection } from '@/features/theme/services/ThemeCreator.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { useIntersectionObserver } from '@/base/hooks/useIntersectionObserver.tsx';
|
2025-09-20 14:15:40 +02:00
|
|
|
import { getReaderStore, useReaderStoreShallow } from '@/features/reader/ReaderStore.ts';
|
2025-01-28 02:16:00 +01:00
|
|
|
|
2025-02-05 15:14:59 +01:00
|
|
|
interface ElementIntersection {
|
|
|
|
|
start: boolean;
|
|
|
|
|
end: boolean;
|
2025-02-05 14:15:28 +01:00
|
|
|
}
|
2025-02-05 15:14:59 +01:00
|
|
|
interface ElementIntersectionInfo {
|
|
|
|
|
[ReadingMode.CONTINUOUS_VERTICAL]: ElementIntersection;
|
|
|
|
|
[ReadingMode.CONTINUOUS_HORIZONTAL]: ElementIntersection;
|
2025-02-05 14:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-17 14:31:18 +01:00
|
|
|
type PageType = 'first' | 'last';
|
|
|
|
|
|
2025-02-05 21:23:20 +01:00
|
|
|
const OPEN_CHAPTER_INTERSECTION_RATIO = 0;
|
2025-02-17 14:31:18 +01:00
|
|
|
const INTERSECTION_THRESHOLD = '-10px';
|
|
|
|
|
|
|
|
|
|
const getRootMargin = (
|
|
|
|
|
pageType: PageType,
|
|
|
|
|
readingMode: ReadingMode,
|
|
|
|
|
readingDirection: ReadingDirection,
|
|
|
|
|
): string | undefined => {
|
|
|
|
|
if (!isContinuousReadingMode(readingMode)) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const themeDirectionOfReadingDirection = READING_DIRECTION_TO_THEME_DIRECTION[readingDirection];
|
|
|
|
|
|
|
|
|
|
const firstPageMarginHorizontal = `0px ${INTERSECTION_THRESHOLD} 0px 0px`;
|
|
|
|
|
const lastPageMarginHorizontal = `0px 0px 0px ${INTERSECTION_THRESHOLD}`;
|
|
|
|
|
|
|
|
|
|
if (isContinuousVerticalReadingMode(readingMode)) {
|
|
|
|
|
if (pageType === 'first') {
|
|
|
|
|
return `0px 0px ${INTERSECTION_THRESHOLD} 0px`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${INTERSECTION_THRESHOLD} 0px 0px 0px`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pageType === 'first') {
|
|
|
|
|
return getOptionForDirection(
|
|
|
|
|
firstPageMarginHorizontal,
|
|
|
|
|
lastPageMarginHorizontal,
|
|
|
|
|
themeDirectionOfReadingDirection,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getOptionForDirection(lastPageMarginHorizontal, firstPageMarginHorizontal, themeDirectionOfReadingDirection);
|
|
|
|
|
};
|
2025-01-28 02:16:00 +01:00
|
|
|
|
2025-02-18 00:58:08 +01:00
|
|
|
const getElementIntersectionInfoHorizontal = (
|
|
|
|
|
readingDirection: ReadingDirection,
|
|
|
|
|
left: number,
|
|
|
|
|
right: number,
|
|
|
|
|
viewportWidth: number,
|
|
|
|
|
): ElementIntersectionInfo[ReadingMode.CONTINUOUS_HORIZONTAL] => {
|
|
|
|
|
if (readingDirection === ReadingDirection.LTR) {
|
|
|
|
|
return {
|
|
|
|
|
start: right >= viewportWidth,
|
|
|
|
|
end: left < 0,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
start: left < 0,
|
|
|
|
|
end: right >= viewportWidth,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-05 15:15:27 +01:00
|
|
|
/**
|
|
|
|
|
* Returns info about if the start or end of an element is intersecting.
|
|
|
|
|
*
|
|
|
|
|
* The info is mapped to the ReadingMode since depending on the selected mode, the start and end of the element changes
|
|
|
|
|
*/
|
2025-02-05 15:14:59 +01:00
|
|
|
const getElementIntersectionInfo = (
|
2025-01-28 02:16:00 +01:00
|
|
|
readingDirection: ReadingDirection,
|
2025-02-05 14:15:28 +01:00
|
|
|
{ top, right, bottom, left }: DOMRect,
|
2025-02-10 22:27:28 +01:00
|
|
|
scrollbarXSize: number,
|
|
|
|
|
scrollbarYSize: number,
|
2025-02-05 15:14:59 +01:00
|
|
|
): ElementIntersectionInfo => {
|
2025-02-10 22:27:28 +01:00
|
|
|
const viewportWidth = window.innerWidth - scrollbarYSize;
|
|
|
|
|
const viewportHeight = window.innerHeight - scrollbarXSize;
|
|
|
|
|
|
2025-02-05 14:15:28 +01:00
|
|
|
return {
|
|
|
|
|
[ReadingMode.CONTINUOUS_VERTICAL]: {
|
2025-02-10 22:27:28 +01:00
|
|
|
start: bottom >= viewportHeight,
|
2025-02-05 15:14:59 +01:00
|
|
|
end: top < 0,
|
2025-02-05 14:15:28 +01:00
|
|
|
},
|
2025-02-18 00:58:08 +01:00
|
|
|
[ReadingMode.CONTINUOUS_HORIZONTAL]: getElementIntersectionInfoHorizontal(
|
|
|
|
|
readingDirection,
|
|
|
|
|
left,
|
|
|
|
|
right,
|
|
|
|
|
viewportWidth,
|
|
|
|
|
),
|
2025-02-05 14:15:28 +01:00
|
|
|
};
|
|
|
|
|
};
|
2025-01-28 02:16:00 +01:00
|
|
|
|
2025-02-05 14:15:28 +01:00
|
|
|
const shouldHandleIntersectionEvent = (
|
|
|
|
|
{
|
|
|
|
|
[ReadingMode.CONTINUOUS_VERTICAL]: verticalInfo,
|
|
|
|
|
[ReadingMode.CONTINUOUS_HORIZONTAL]: horizontalInfo,
|
2025-02-05 15:14:59 +01:00
|
|
|
}: ElementIntersectionInfo,
|
2025-02-05 14:15:28 +01:00
|
|
|
readingMode: ReadingMode,
|
|
|
|
|
): boolean => {
|
|
|
|
|
if (isContinuousVerticalReadingMode(readingMode)) {
|
2025-02-05 15:14:59 +01:00
|
|
|
return verticalInfo.start || verticalInfo.end;
|
2025-01-28 02:16:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-05 15:14:59 +01:00
|
|
|
return horizontalInfo.start || horizontalInfo.end;
|
2025-02-05 14:15:28 +01:00
|
|
|
};
|
2025-01-28 02:16:00 +01:00
|
|
|
|
2025-02-05 15:14:59 +01:00
|
|
|
const getElementIntersection = (
|
2025-02-05 14:15:28 +01:00
|
|
|
{
|
|
|
|
|
[ReadingMode.CONTINUOUS_VERTICAL]: verticalInfo,
|
|
|
|
|
[ReadingMode.CONTINUOUS_HORIZONTAL]: horizontalInfo,
|
2025-02-05 15:14:59 +01:00
|
|
|
}: ElementIntersectionInfo,
|
2025-02-05 14:15:28 +01:00
|
|
|
readingMode: ReadingMode,
|
2025-02-05 15:14:59 +01:00
|
|
|
): ElementIntersection => {
|
2025-02-05 14:15:28 +01:00
|
|
|
if (isContinuousVerticalReadingMode(readingMode)) {
|
|
|
|
|
return verticalInfo;
|
2025-01-28 02:16:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-05 14:15:28 +01:00
|
|
|
return horizontalInfo;
|
2025-01-28 02:16:00 +01:00
|
|
|
};
|
|
|
|
|
|
2025-02-05 15:15:27 +01:00
|
|
|
/**
|
2025-05-05 01:26:27 +02:00
|
|
|
* Will handle opening the previous or next chapter for infinite scrolling.
|
2025-02-05 15:15:27 +01:00
|
|
|
*
|
2025-05-05 01:26:27 +02:00
|
|
|
* In case the "transition page" is not enabled, the "initial" load of the previous or next chapter is handled
|
|
|
|
|
* based on the scroll position.
|
|
|
|
|
* After the initial load, the last page of a chapter handles opening the correct chapter.
|
2025-02-05 15:15:27 +01:00
|
|
|
*
|
2025-05-05 01:26:27 +02:00
|
|
|
* In case the "transition page" is enabled, for the initial load of the previous chapter, the intersection of the first
|
|
|
|
|
* page is used, after the initial load of the previous chapter the intersection of the last page handles changing the chapter.
|
|
|
|
|
*
|
|
|
|
|
* @example - How the intersection-based approach works:
|
2025-02-05 15:15:27 +01:00
|
|
|
* |, _ = viewport
|
|
|
|
|
* # = page
|
|
|
|
|
*
|
|
|
|
|
* initial open previous chapter (start of first page intersecting, chapter of page is current chapter, previous chapter not yet loaded):
|
|
|
|
|
*
|
|
|
|
|
* vertical pager:
|
|
|
|
|
* |----------------|
|
|
|
|
|
* | | ↑
|
|
|
|
|
* | | ↑ Scroll direction: backward
|
|
|
|
|
* | ####### | ↑
|
|
|
|
|
* |----#######-----|
|
|
|
|
|
* #######
|
|
|
|
|
* #######
|
|
|
|
|
*
|
|
|
|
|
* horizontal pager (ltr):
|
|
|
|
|
* |----------------|
|
|
|
|
|
* | ######## ←
|
|
|
|
|
* | ######## ← Scroll direction: backward
|
|
|
|
|
* | ######## ←
|
|
|
|
|
* |----------------|
|
|
|
|
|
*
|
|
|
|
|
* horizontal pager (rtl):
|
|
|
|
|
* |----------------|
|
|
|
|
|
* ######## | →
|
|
|
|
|
* ######## | → Scroll direction: backward
|
|
|
|
|
* ######## | →
|
|
|
|
|
* |----------------|
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* open previous chapter (chapter of intersecting page) (end of last page intersecting):
|
|
|
|
|
*
|
|
|
|
|
* vertical pager:
|
|
|
|
|
* #######
|
|
|
|
|
* #######
|
|
|
|
|
* |----#######-----|
|
|
|
|
|
* | ####### | ↑
|
|
|
|
|
* | | ↑ Scroll direction: backward
|
|
|
|
|
* | | ↑
|
|
|
|
|
* |________________|
|
|
|
|
|
*
|
|
|
|
|
* horizontal pager (ltr):
|
|
|
|
|
* |----------------|
|
|
|
|
|
* ######## | ←
|
|
|
|
|
* ######## | ← Scroll direction: backward
|
|
|
|
|
* ######## | ←
|
|
|
|
|
* |----------------|
|
|
|
|
|
*
|
|
|
|
|
* horizontal pager (rtl):
|
|
|
|
|
* |----------------|
|
|
|
|
|
* | ######## →
|
|
|
|
|
* | ######## → Scroll direction: backward
|
|
|
|
|
* | ######## →
|
|
|
|
|
* |----------------|
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* open next chapter (end of last page intersecting):
|
|
|
|
|
*
|
|
|
|
|
* vertical pager:
|
|
|
|
|
* #######
|
|
|
|
|
* #######
|
|
|
|
|
* |----#######-----|
|
|
|
|
|
* | ####### | ↓
|
|
|
|
|
* | | ↓ Scroll direction: forward
|
|
|
|
|
* | | ↓
|
|
|
|
|
* |________________|
|
|
|
|
|
*
|
|
|
|
|
* horizontal pager (ltr):
|
|
|
|
|
* |----------------|
|
|
|
|
|
* ######## | →
|
|
|
|
|
* ######## | → Scroll direction: forward
|
|
|
|
|
* ######## | →
|
|
|
|
|
* |----------------|
|
|
|
|
|
*
|
|
|
|
|
* horizontal pager (rtl):
|
|
|
|
|
* |----------------|
|
|
|
|
|
* | ######## ←
|
|
|
|
|
* | ######## ← Scroll direction: forward
|
|
|
|
|
* | ######## ←
|
|
|
|
|
* |----------------|
|
|
|
|
|
*
|
|
|
|
|
*/
|
2025-01-28 02:16:00 +01:00
|
|
|
export const useReaderInfiniteScrollUpdateChapter = (
|
2025-02-17 14:31:18 +01:00
|
|
|
pageType: PageType,
|
2025-01-28 02:16:00 +01:00
|
|
|
chapterId: number,
|
|
|
|
|
chapterToOpenId: number | undefined,
|
|
|
|
|
isCurrentChapter: boolean,
|
|
|
|
|
isChapterToOpenVisible: boolean,
|
|
|
|
|
openChapter: ReturnType<typeof ReaderControls.useOpenChapter>,
|
|
|
|
|
image: HTMLElement | null,
|
2025-04-03 22:30:35 +02:00
|
|
|
scrollElement: HTMLElement | null,
|
2025-01-28 02:16:00 +01:00
|
|
|
) => {
|
2025-09-20 14:15:40 +02:00
|
|
|
const { readingMode, readingDirection, shouldUseInfiniteScroll, shouldShowTransitionPage } = useReaderStoreShallow(
|
|
|
|
|
(state) => ({
|
|
|
|
|
readingMode: state.settings.readingMode.value,
|
|
|
|
|
readingDirection: state.settings.readingDirection.value,
|
|
|
|
|
shouldUseInfiniteScroll: state.settings.shouldUseInfiniteScroll,
|
|
|
|
|
shouldShowTransitionPage: state.settings.shouldShowTransitionPage,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-05 01:26:27 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode);
|
|
|
|
|
const isContinuousVerticalReadingModeActive = isContinuousVerticalReadingMode(readingMode);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
shouldShowTransitionPage ||
|
|
|
|
|
!scrollElement ||
|
|
|
|
|
!isContinuousReadingModeActive ||
|
|
|
|
|
!shouldUseInfiniteScroll ||
|
|
|
|
|
!isCurrentChapter ||
|
|
|
|
|
isChapterToOpenVisible ||
|
|
|
|
|
chapterToOpenId === undefined
|
|
|
|
|
) {
|
|
|
|
|
return () => {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onScroll = () => {
|
|
|
|
|
const isAtStartX = scrollElement.scrollLeft === 0;
|
|
|
|
|
const isAtEndX = scrollElement.scrollLeft === scrollElement.scrollWidth - scrollElement.clientWidth;
|
|
|
|
|
|
|
|
|
|
const isAtStartY = scrollElement.scrollTop === 0;
|
|
|
|
|
const isAtEndY = scrollElement.scrollTop === scrollElement.scrollHeight - scrollElement.clientHeight;
|
|
|
|
|
|
|
|
|
|
const isAtStart = isContinuousVerticalReadingModeActive ? isAtStartY : isAtStartX;
|
|
|
|
|
const isAtEnd = isContinuousVerticalReadingModeActive ? isAtEndY : isAtEndX;
|
|
|
|
|
|
|
|
|
|
const loadPreviousChapter = pageType === 'first' && isAtStart;
|
|
|
|
|
const loadNextChapter = pageType === 'last' && isAtEnd;
|
|
|
|
|
|
|
|
|
|
const loadChapter = loadPreviousChapter || loadNextChapter;
|
|
|
|
|
if (loadChapter) {
|
|
|
|
|
openChapter(chapterToOpenId, undefined, false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
scrollElement.addEventListener('scroll', onScroll);
|
|
|
|
|
return () => scrollElement.removeEventListener('scroll', onScroll);
|
|
|
|
|
}, [
|
|
|
|
|
readingMode,
|
|
|
|
|
scrollElement,
|
|
|
|
|
shouldShowTransitionPage,
|
|
|
|
|
shouldUseInfiniteScroll,
|
|
|
|
|
isCurrentChapter,
|
|
|
|
|
isChapterToOpenVisible,
|
|
|
|
|
chapterToOpenId,
|
|
|
|
|
openChapter,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-02-08 17:51:16 +01:00
|
|
|
useIntersectionObserver(
|
|
|
|
|
image,
|
|
|
|
|
useCallback(
|
2025-01-28 02:16:00 +01:00
|
|
|
(entries) => {
|
2025-04-26 23:19:01 +02:00
|
|
|
if (
|
2025-05-05 01:26:27 +02:00
|
|
|
// !shouldShowTransitionPage ||
|
2025-04-26 23:19:01 +02:00
|
|
|
!shouldUseInfiniteScroll ||
|
|
|
|
|
!isContinuousReadingMode(readingMode) ||
|
|
|
|
|
chapterToOpenId === undefined
|
|
|
|
|
) {
|
2025-01-28 02:16:00 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:26:27 +02:00
|
|
|
if (!shouldShowTransitionPage && !isChapterToOpenVisible) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 02:16:00 +01:00
|
|
|
const entry = entries[entries.length - 1];
|
|
|
|
|
|
2025-02-05 15:14:59 +01:00
|
|
|
const elementIntersectionInfo = getElementIntersectionInfo(
|
2025-02-05 14:15:28 +01:00
|
|
|
readingDirection,
|
|
|
|
|
entry.target.getBoundingClientRect(),
|
2025-08-31 16:38:26 +02:00
|
|
|
getReaderStore().scrollbar.xSize,
|
|
|
|
|
getReaderStore().scrollbar.ySize,
|
2025-02-05 14:15:28 +01:00
|
|
|
);
|
2025-02-05 15:14:59 +01:00
|
|
|
const { start: isStartIntersecting, end: isEndIntersecting } = getElementIntersection(
|
|
|
|
|
elementIntersectionInfo,
|
2025-02-05 14:15:28 +01:00
|
|
|
readingMode,
|
|
|
|
|
);
|
2025-02-05 21:23:20 +01:00
|
|
|
const wasPageScrolledOutOfView = entry.intersectionRatio <= OPEN_CHAPTER_INTERSECTION_RATIO;
|
2025-02-05 14:15:28 +01:00
|
|
|
|
2025-02-05 15:14:59 +01:00
|
|
|
if (!shouldHandleIntersectionEvent(elementIntersectionInfo, readingMode)) {
|
2025-02-05 14:15:28 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-01-28 02:16:00 +01:00
|
|
|
|
|
|
|
|
// the first page only opens the previous chapter in case it hasn't been loaded yet, otherwise, the last
|
|
|
|
|
// page handles setting the correct chapter
|
|
|
|
|
const initialOpenPreviousChapter =
|
2025-02-05 15:14:59 +01:00
|
|
|
pageType === 'first' && isStartIntersecting && !isChapterToOpenVisible;
|
|
|
|
|
const openPreviousChapter = pageType === 'last' && !isCurrentChapter && isEndIntersecting;
|
|
|
|
|
const openNextChapter = pageType === 'last' && isEndIntersecting && wasPageScrolledOutOfView;
|
2025-01-28 02:16:00 +01:00
|
|
|
|
|
|
|
|
const openChapterToOpen = initialOpenPreviousChapter || openNextChapter;
|
|
|
|
|
if (openChapterToOpen) {
|
|
|
|
|
openChapter(chapterToOpenId, !isChapterToOpenVisible, false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (openPreviousChapter) {
|
|
|
|
|
openChapter(chapterId, false, false);
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-02-08 17:51:16 +01:00
|
|
|
[
|
|
|
|
|
pageType,
|
|
|
|
|
chapterId,
|
|
|
|
|
chapterToOpenId,
|
|
|
|
|
isCurrentChapter,
|
|
|
|
|
isChapterToOpenVisible,
|
|
|
|
|
readingMode,
|
|
|
|
|
readingDirection,
|
2025-04-26 23:19:01 +02:00
|
|
|
shouldUseInfiniteScroll,
|
2025-05-05 01:26:27 +02:00
|
|
|
shouldShowTransitionPage,
|
2025-02-08 17:51:16 +01:00
|
|
|
openChapter,
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
useMemo(
|
|
|
|
|
() => ({
|
2025-04-03 22:30:35 +02:00
|
|
|
root: scrollElement,
|
2025-02-05 22:25:35 +01:00
|
|
|
threshold: [OPEN_CHAPTER_INTERSECTION_RATIO],
|
2025-02-17 14:31:18 +01:00
|
|
|
rootMargin: getRootMargin(pageType, readingMode, readingDirection),
|
2025-02-08 17:51:16 +01:00
|
|
|
// gets immediately observed once on initial render
|
|
|
|
|
ignoreInitialObserve: true,
|
|
|
|
|
}),
|
2025-02-17 14:31:18 +01:00
|
|
|
[pageType, readingMode, readingDirection],
|
2025-02-08 17:51:16 +01:00
|
|
|
),
|
|
|
|
|
);
|
2025-01-28 02:16:00 +01:00
|
|
|
};
|