2024-11-03 03:09: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-12-11 21:04:44 +01:00
|
|
|
import { ContextType, MutableRefObject, useCallback, useMemo } from 'react';
|
2024-12-08 22:28:00 +01:00
|
|
|
import { Direction, useTheme } from '@mui/material/styles';
|
2024-12-11 22:33:04 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { TFunction } from 'i18next';
|
2024-11-03 03:09:00 +01:00
|
|
|
import { userReaderStatePagesContext } from '@/modules/reader/contexts/state/ReaderStatePagesContext.tsx';
|
2024-12-07 03:43:18 +01:00
|
|
|
import { getNextIndexFromPage, getNextPageIndex, getPage } from '@/modules/reader/utils/ReaderProgressBar.utils.tsx';
|
2024-12-11 13:59:50 +01:00
|
|
|
import { getOptionForDirection } from '@/modules/theme/services/ThemeCreator.ts';
|
2024-11-03 03:09:00 +01:00
|
|
|
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
|
|
|
|
|
import { useReaderStateChaptersContext } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
|
2024-12-03 18:00:46 +01:00
|
|
|
import {
|
|
|
|
|
PageInViewportType,
|
2024-12-06 23:59:39 +01:00
|
|
|
ReaderResumeMode,
|
|
|
|
|
ReaderTransitionPageMode,
|
2024-12-03 18:00:46 +01:00
|
|
|
ReadingDirection,
|
|
|
|
|
ReadingMode,
|
|
|
|
|
} from '@/modules/reader/types/Reader.types.ts';
|
2024-11-03 03:09:00 +01:00
|
|
|
import { ScrollDirection, ScrollOffset } from '@/modules/core/Core.types.ts';
|
2024-12-08 22:28:00 +01:00
|
|
|
import {
|
|
|
|
|
ReaderScrollAmount,
|
|
|
|
|
READING_DIRECTION_TO_THEME_DIRECTION,
|
|
|
|
|
} from '@/modules/reader/constants/ReaderSettings.constants.tsx';
|
2024-12-07 03:43:18 +01:00
|
|
|
import { isEndOfPageInViewport, isPageInViewport } from '@/modules/reader/utils/ReaderPager.utils.tsx';
|
2024-12-11 21:04:44 +01:00
|
|
|
import { ReaderOverlayContext, useReaderOverlayContext } from '@/modules/reader/contexts/ReaderOverlayContext.tsx';
|
2024-11-06 13:00:36 +01:00
|
|
|
import { useReaderTapZoneContext } from '@/modules/reader/contexts/ReaderTapZoneContext.tsx';
|
|
|
|
|
import { TapZoneRegionType } from '@/modules/reader/types/TapZoneLayout.types.ts';
|
|
|
|
|
import { ReaderTapZoneService } from '@/modules/reader/services/ReaderTapZoneService.ts';
|
|
|
|
|
import { isContinuousReadingMode } from '@/modules/reader/utils/ReaderSettings.utils.tsx';
|
2024-12-07 03:43:18 +01:00
|
|
|
import { getReaderChapterFromCache } from '@/modules/reader/utils/Reader.utils.ts';
|
|
|
|
|
import { Chapters } from '@/modules/chapter/services/Chapters.ts';
|
2024-12-11 22:33:04 +01:00
|
|
|
import { DirectionOffset, TranslationKey } from '@/Base.types.ts';
|
2024-12-07 03:43:18 +01:00
|
|
|
import { useMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
|
2024-12-11 22:33:04 +01:00
|
|
|
import { TChapterReader } from '@/modules/chapter/Chapter.types.ts';
|
|
|
|
|
import { awaitConfirmation } from '@/modules/core/utils/AwaitableDialog.tsx';
|
2024-12-11 22:40:06 +01:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-11-03 03:09:00 +01:00
|
|
|
|
2024-12-08 22:28:00 +01:00
|
|
|
const getScrollDirectionInvert = (
|
|
|
|
|
scrollDirection: ScrollDirection,
|
|
|
|
|
scrollOffset: ScrollOffset,
|
|
|
|
|
themeDirection: Direction,
|
|
|
|
|
): 1 | -1 => {
|
|
|
|
|
if (scrollDirection === ScrollDirection.X) {
|
|
|
|
|
if (scrollOffset === ScrollOffset.BACKWARD) {
|
|
|
|
|
if (themeDirection === 'ltr') {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (scrollOffset === ScrollOffset.FORWARD) {
|
|
|
|
|
if (themeDirection === 'ltr') {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-03 03:09:00 +01:00
|
|
|
|
2024-12-08 22:28:00 +01:00
|
|
|
// handle ScrollDirection.XY the same as ScrollDirection.Y
|
|
|
|
|
if (scrollOffset === ScrollOffset.BACKWARD) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
2024-11-03 03:09:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export class ReaderControls {
|
2024-12-07 03:43:18 +01:00
|
|
|
private static updateCurrentPageTimeout: NodeJS.Timeout;
|
|
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
static scroll(
|
|
|
|
|
offset: ScrollOffset,
|
|
|
|
|
direction: ScrollDirection,
|
2024-12-08 22:28:00 +01:00
|
|
|
readingMode: ReadingMode,
|
2024-11-03 03:09:00 +01:00
|
|
|
readingDirection: ReadingDirection,
|
2024-12-08 22:28:00 +01:00
|
|
|
themeDirection: Direction,
|
2024-11-03 03:09:00 +01:00
|
|
|
element: HTMLElement,
|
2024-12-03 18:00:46 +01:00
|
|
|
openChapter: ReturnType<(typeof ReaderControls)['useOpenChapter']>,
|
2024-12-11 21:04:44 +01:00
|
|
|
setIsOverlayVisible: ContextType<typeof ReaderOverlayContext>['setIsVisible'],
|
2024-11-03 03:09:00 +01:00
|
|
|
scrollAmountPercentage: number = ReaderScrollAmount.LARGE,
|
|
|
|
|
): void {
|
|
|
|
|
if (!element) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-08 22:28:00 +01:00
|
|
|
const themeDirectionOfReadingDirection = READING_DIRECTION_TO_THEME_DIRECTION[readingDirection];
|
|
|
|
|
const areReadingDirectionsEqual = themeDirection === themeDirectionOfReadingDirection;
|
|
|
|
|
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode);
|
|
|
|
|
|
2024-12-09 13:58:17 +01:00
|
|
|
const isAtStartY = Math.abs(element.scrollTop) <= 1;
|
2024-12-03 18:00:46 +01:00
|
|
|
const isAtEndY =
|
|
|
|
|
Math.floor(element.scrollTop) === element.scrollHeight - element.clientHeight ||
|
|
|
|
|
Math.ceil(element.scrollTop) === element.scrollHeight - element.clientHeight;
|
2024-12-09 13:58:17 +01:00
|
|
|
const isAtStartX = Math.abs(element.scrollLeft) <= 1;
|
2024-12-03 18:00:46 +01:00
|
|
|
const isAtEndX =
|
2024-12-09 13:58:17 +01:00
|
|
|
element.scrollWidth - element.clientWidth - Math.floor(Math.abs(element.scrollLeft)) <= 1 ||
|
|
|
|
|
element.scrollWidth - element.clientWidth - Math.ceil(Math.abs(element.scrollLeft)) <= 1;
|
2024-12-08 22:28:00 +01:00
|
|
|
const isAtStartXForDirection = areReadingDirectionsEqual ? isAtStartX : isAtEndX;
|
|
|
|
|
const isAtEndXForDirection = areReadingDirectionsEqual ? isAtEndX : isAtStartX;
|
2024-12-03 18:00:46 +01:00
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
const scrollAmount = scrollAmountPercentage / 100;
|
2024-12-08 22:28:00 +01:00
|
|
|
const scrollDirection = getScrollDirectionInvert(direction, offset, themeDirectionOfReadingDirection);
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
const getNewScrollPosition = (currentPos: number, elementSize: number) =>
|
|
|
|
|
currentPos + elementSize * scrollAmount * scrollDirection;
|
|
|
|
|
|
2024-12-11 21:04:44 +01:00
|
|
|
setIsOverlayVisible(false);
|
|
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
switch (direction) {
|
|
|
|
|
case ScrollDirection.X:
|
2024-12-08 22:28:00 +01:00
|
|
|
if (isAtStartXForDirection && offset === ScrollOffset.BACKWARD && isContinuousReadingModeActive) {
|
2024-12-09 13:58:17 +01:00
|
|
|
openChapter('previous');
|
2024-12-03 18:00:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-08 22:28:00 +01:00
|
|
|
if (isAtEndXForDirection && offset === ScrollOffset.FORWARD && isContinuousReadingModeActive) {
|
2024-12-09 13:58:17 +01:00
|
|
|
openChapter('next');
|
2024-12-03 18:00:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
element.scroll({
|
|
|
|
|
left: getNewScrollPosition(element.scrollLeft, element.clientWidth),
|
|
|
|
|
behavior: 'smooth',
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case ScrollDirection.Y:
|
2024-12-08 22:28:00 +01:00
|
|
|
if (isAtStartY && offset === ScrollOffset.BACKWARD && isContinuousReadingModeActive) {
|
2024-12-09 13:58:17 +01:00
|
|
|
openChapter('previous');
|
2024-12-03 18:00:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-08 22:28:00 +01:00
|
|
|
if (isAtEndY && offset === ScrollOffset.FORWARD && isContinuousReadingModeActive) {
|
2024-12-09 13:58:17 +01:00
|
|
|
openChapter('next');
|
2024-12-03 18:00:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
element.scroll({
|
|
|
|
|
top: getNewScrollPosition(element.scrollTop, element.clientHeight),
|
|
|
|
|
behavior: 'smooth',
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unexpected "ScrollDirection" (${direction})`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 13:58:17 +01:00
|
|
|
static useOpenChapter(): (offset: 'previous' | 'next') => void {
|
2024-12-11 22:33:04 +01:00
|
|
|
const { t } = useTranslation();
|
2024-12-09 13:58:17 +01:00
|
|
|
const { readingMode } = ReaderService.useSettings();
|
2024-12-11 22:33:04 +01:00
|
|
|
const { currentChapter, previousChapter, nextChapter } = useReaderStateChaptersContext();
|
2024-11-03 03:09:00 +01:00
|
|
|
|
2024-12-06 23:59:39 +01:00
|
|
|
const openPreviousChapter = ReaderService.useNavigateToChapter(previousChapter, ReaderResumeMode.END);
|
|
|
|
|
const openNextChapter = ReaderService.useNavigateToChapter(nextChapter, ReaderResumeMode.START);
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
return useCallback(
|
2024-12-09 13:58:17 +01:00
|
|
|
(offset) => {
|
2024-11-03 03:09:00 +01:00
|
|
|
switch (offset) {
|
|
|
|
|
case 'previous':
|
2024-12-11 22:40:06 +01:00
|
|
|
ReaderControls.checkNextChapterConsistency(t, offset, currentChapter, previousChapter)
|
|
|
|
|
.then(openPreviousChapter)
|
|
|
|
|
.catch(defaultPromiseErrorHandler('ReaderControls::checkNextChapterConsistency: previous'));
|
2024-11-03 03:09:00 +01:00
|
|
|
break;
|
|
|
|
|
case 'next':
|
2024-12-11 22:40:06 +01:00
|
|
|
ReaderControls.checkNextChapterConsistency(t, offset, currentChapter, nextChapter)
|
|
|
|
|
.then(openNextChapter)
|
|
|
|
|
.catch(defaultPromiseErrorHandler('ReaderControls::checkNextChapterConsistency: next'));
|
2024-11-03 03:09:00 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unexpected "offset" (${offset})`);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-12-11 22:33:04 +01:00
|
|
|
[t, currentChapter?.id, openPreviousChapter, openNextChapter, readingMode.value],
|
2024-11-03 03:09:00 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 22:33:04 +01:00
|
|
|
private static async checkNextChapterConsistency(
|
|
|
|
|
t: TFunction,
|
|
|
|
|
offset: 'previous' | 'next',
|
|
|
|
|
currentChapter?: TChapterReader | null,
|
|
|
|
|
chapterToOpen?: TChapterReader | null,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
if (!currentChapter || !chapterToOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const missingChapters = Math.abs(currentChapter.chapterNumber - chapterToOpen.chapterNumber);
|
|
|
|
|
const isSameScanlator = currentChapter.scanlator === chapterToOpen.scanlator;
|
2024-12-12 12:56:40 +01:00
|
|
|
const isContinuousChapter = missingChapters <= 1;
|
2024-12-11 22:33:04 +01:00
|
|
|
|
|
|
|
|
const showWarning = !isSameScanlator || !isContinuousChapter;
|
|
|
|
|
if (!showWarning) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const offsetToTranslationKeys: Record<typeof offset, Record<'scanlator' | 'chapter_number', TranslationKey>> = {
|
|
|
|
|
previous: {
|
|
|
|
|
scanlator: 'reader.chapter_transition.warning.previous.scanlator',
|
|
|
|
|
chapter_number: 'reader.chapter_transition.warning.previous.chapter_number',
|
|
|
|
|
},
|
|
|
|
|
next: {
|
|
|
|
|
scanlator: 'reader.chapter_transition.warning.next.scanlator',
|
|
|
|
|
chapter_number: 'reader.chapter_transition.warning.next.chapter_number',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sameScanlator = isSameScanlator
|
|
|
|
|
? ''
|
|
|
|
|
: t(offsetToTranslationKeys[offset].scanlator, {
|
|
|
|
|
nextScanlator: chapterToOpen.scanlator,
|
|
|
|
|
currentScanlator: currentChapter.scanlator,
|
|
|
|
|
});
|
|
|
|
|
const continuousChapter = isContinuousChapter
|
|
|
|
|
? ''
|
|
|
|
|
: t(offsetToTranslationKeys[offset].chapter_number, {
|
|
|
|
|
count: missingChapters,
|
|
|
|
|
nextChapter: `#${chapterToOpen.chapterNumber} ${chapterToOpen.name}`,
|
|
|
|
|
currentChapter: `#${currentChapter.chapterNumber} ${currentChapter.name}`,
|
|
|
|
|
});
|
|
|
|
|
const warningLineBreak = !isSameScanlator && !isContinuousChapter ? '\n\n' : '';
|
|
|
|
|
const warning = `${sameScanlator}${warningLineBreak}${continuousChapter}`;
|
|
|
|
|
|
|
|
|
|
await awaitConfirmation({
|
|
|
|
|
title: t('reader.chapter_transition.warning.title'),
|
|
|
|
|
message: warning,
|
|
|
|
|
actions: {
|
|
|
|
|
confirm: {
|
|
|
|
|
title: t('global.button.open'),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 02:15:18 +01:00
|
|
|
static useOpenPage(): (
|
|
|
|
|
page: number | 'previous' | 'next',
|
|
|
|
|
forceDirection?: Direction,
|
|
|
|
|
hideOverlay?: boolean,
|
|
|
|
|
) => void {
|
2024-12-03 18:00:46 +01:00
|
|
|
const { currentPageIndex, setPageToScrollToIndex, pages, transitionPageMode, setTransitionPageMode } =
|
|
|
|
|
userReaderStatePagesContext();
|
|
|
|
|
const { previousChapter, nextChapter } = useReaderStateChaptersContext();
|
2024-12-11 21:04:44 +01:00
|
|
|
const { setIsVisible: setIsOverlayVisible } = useReaderOverlayContext();
|
2024-12-03 18:00:46 +01:00
|
|
|
const { readingDirection, readingMode } = ReaderService.useSettings();
|
|
|
|
|
const openChapter = ReaderControls.useOpenChapter();
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
const currentPage = useMemo(() => getPage(currentPageIndex, pages), [currentPageIndex, pages]);
|
|
|
|
|
const previousPageIndex = useMemo(
|
|
|
|
|
() => getNextPageIndex('previous', currentPage.pagesIndex, pages),
|
|
|
|
|
[currentPage, pages],
|
|
|
|
|
);
|
|
|
|
|
const nextPageIndex = useMemo(
|
|
|
|
|
() => getNextPageIndex('next', currentPage.pagesIndex, pages),
|
|
|
|
|
[currentPage, pages],
|
|
|
|
|
);
|
2024-12-17 02:05:49 +01:00
|
|
|
const indexOfFirstPage = getNextIndexFromPage(pages[0]);
|
2024-12-07 03:43:18 +01:00
|
|
|
const indexOfLastPage = getNextIndexFromPage(pages[pages.length - 1]);
|
2024-12-08 22:28:00 +01:00
|
|
|
const direction = READING_DIRECTION_TO_THEME_DIRECTION[readingDirection.value];
|
2024-11-03 03:09:00 +01:00
|
|
|
|
2024-12-13 02:30:49 +01:00
|
|
|
const isFirstPage = currentPage.primary.index === 0;
|
2024-12-07 03:43:18 +01:00
|
|
|
const isLastPage = currentPageIndex === indexOfLastPage;
|
2024-12-03 18:00:46 +01:00
|
|
|
const isATransitionPageVisible = transitionPageMode !== ReaderTransitionPageMode.NONE;
|
|
|
|
|
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode.value);
|
|
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
return useCallback(
|
2024-12-13 02:15:18 +01:00
|
|
|
(page, forceDirection = direction, hideOverlay: boolean = true) => {
|
2024-12-03 18:00:46 +01:00
|
|
|
const convertedPage = getOptionForDirection(
|
|
|
|
|
page,
|
|
|
|
|
page === 'previous' ? 'next' : 'previous',
|
|
|
|
|
forceDirection,
|
|
|
|
|
);
|
|
|
|
|
|
2024-12-13 02:15:18 +01:00
|
|
|
if (hideOverlay) {
|
|
|
|
|
setIsOverlayVisible(false);
|
|
|
|
|
}
|
2024-12-11 21:04:44 +01:00
|
|
|
|
2024-12-13 02:32:36 +01:00
|
|
|
const hideTransitionPage = () => setTransitionPageMode(ReaderTransitionPageMode.NONE);
|
|
|
|
|
|
|
|
|
|
if (typeof page === 'number') {
|
|
|
|
|
setPageToScrollToIndex(page);
|
|
|
|
|
hideTransitionPage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 18:00:46 +01:00
|
|
|
const shouldOpenPreviousChapter =
|
|
|
|
|
isFirstPage && isATransitionPageVisible && convertedPage === 'previous' && !!previousChapter;
|
|
|
|
|
if (shouldOpenPreviousChapter) {
|
|
|
|
|
openChapter('previous');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldOpenNextChapter =
|
|
|
|
|
isLastPage && isATransitionPageVisible && convertedPage === 'next' && !!nextChapter;
|
|
|
|
|
if (shouldOpenNextChapter) {
|
|
|
|
|
openChapter('next');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const needToHideTransitionPage = isATransitionPageVisible && !isContinuousReadingModeActive;
|
|
|
|
|
switch (convertedPage) {
|
2024-11-03 03:09:00 +01:00
|
|
|
case 'previous':
|
2024-12-03 18:00:46 +01:00
|
|
|
if (isFirstPage) {
|
|
|
|
|
setTransitionPageMode(ReaderTransitionPageMode.PREVIOUS);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (needToHideTransitionPage) {
|
|
|
|
|
hideTransitionPage();
|
2024-12-17 02:05:49 +01:00
|
|
|
setPageToScrollToIndex(indexOfLastPage);
|
2024-12-03 18:00:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setPageToScrollToIndex(previousPageIndex);
|
2024-11-03 03:09:00 +01:00
|
|
|
break;
|
|
|
|
|
case 'next':
|
2024-12-03 18:00:46 +01:00
|
|
|
if (isLastPage) {
|
|
|
|
|
setTransitionPageMode(ReaderTransitionPageMode.NEXT);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (needToHideTransitionPage) {
|
|
|
|
|
hideTransitionPage();
|
2024-12-17 02:05:49 +01:00
|
|
|
setPageToScrollToIndex(indexOfFirstPage);
|
2024-12-03 18:00:46 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setPageToScrollToIndex(nextPageIndex);
|
2024-11-03 03:09:00 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
2024-12-03 18:00:46 +01:00
|
|
|
throw new Error(`Unexpected "offset" (${page})`);
|
2024-11-03 03:09:00 +01:00
|
|
|
}
|
|
|
|
|
},
|
2024-12-03 18:00:46 +01:00
|
|
|
[
|
|
|
|
|
direction,
|
2024-12-17 02:05:49 +01:00
|
|
|
indexOfFirstPage,
|
|
|
|
|
indexOfLastPage,
|
2024-12-03 18:00:46 +01:00
|
|
|
previousPageIndex,
|
|
|
|
|
nextPageIndex,
|
2024-12-07 03:43:18 +01:00
|
|
|
indexOfLastPage,
|
2024-12-03 18:00:46 +01:00
|
|
|
isATransitionPageVisible,
|
|
|
|
|
isContinuousReadingModeActive,
|
|
|
|
|
isFirstPage,
|
|
|
|
|
isLastPage,
|
|
|
|
|
openChapter,
|
|
|
|
|
!!previousChapter,
|
|
|
|
|
!!nextChapter,
|
|
|
|
|
],
|
2024-11-03 03:09:00 +01:00
|
|
|
);
|
|
|
|
|
}
|
2024-11-06 13:00:36 +01:00
|
|
|
|
2024-12-07 03:43:18 +01:00
|
|
|
static useUpdateCurrentPageIndex(): (
|
|
|
|
|
pageIndex: number,
|
|
|
|
|
debounceChapterUpdate?: boolean,
|
|
|
|
|
endReached?: boolean,
|
|
|
|
|
) => void {
|
|
|
|
|
const { currentPageIndex, setCurrentPageIndex } = userReaderStatePagesContext();
|
|
|
|
|
const { initialChapter, currentChapter, nextChapter, mangaChapters } = useReaderStateChaptersContext();
|
|
|
|
|
const updateChapter = ReaderService.useUpdateChapter();
|
|
|
|
|
const { shouldSkipDupChapters } = ReaderService.useSettings();
|
|
|
|
|
const {
|
|
|
|
|
settings: { downloadAheadLimit },
|
|
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
|
|
|
|
|
const nextChapters = useMemo(() => {
|
|
|
|
|
if (!initialChapter || !currentChapter) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Chapters.getNextChapters(currentChapter, mangaChapters, {
|
|
|
|
|
offset: DirectionOffset.NEXT,
|
|
|
|
|
skipDupe: shouldSkipDupChapters,
|
|
|
|
|
skipDupeChapter: initialChapter,
|
|
|
|
|
});
|
|
|
|
|
}, [initialChapter?.id, currentChapter?.id, mangaChapters, shouldSkipDupChapters]);
|
|
|
|
|
|
|
|
|
|
return useCallback(
|
|
|
|
|
(pageIndex, debounceChapterUpdate = true, endReached = false) => {
|
|
|
|
|
setCurrentPageIndex(pageIndex);
|
|
|
|
|
|
|
|
|
|
if (!currentChapter) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReaderService.downloadAhead(currentChapter, nextChapter, nextChapters, pageIndex, downloadAheadLimit);
|
|
|
|
|
|
|
|
|
|
const handleCurrentPageIndexChange = () => {
|
|
|
|
|
const currentChapterUpToDate = getReaderChapterFromCache(currentChapter.id);
|
|
|
|
|
if (!currentChapterUpToDate) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 14:48:30 +01:00
|
|
|
const actualPageIndex = endReached ? currentChapterUpToDate.pageCount - 1 : pageIndex;
|
|
|
|
|
|
|
|
|
|
const hasLastPageReadChanged = actualPageIndex !== currentChapterUpToDate.lastPageRead;
|
|
|
|
|
const isLastPage = actualPageIndex === currentChapterUpToDate.pageCount - 1;
|
2024-12-10 02:12:38 +01:00
|
|
|
const hasIsReadChanged = (isLastPage || endReached) && !currentChapterUpToDate.isRead;
|
|
|
|
|
|
|
|
|
|
const shouldUpdateChapter = hasLastPageReadChanged || hasIsReadChanged;
|
2024-12-07 03:43:18 +01:00
|
|
|
if (!shouldUpdateChapter) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateChapter({
|
2024-12-16 14:48:30 +01:00
|
|
|
lastPageRead: hasLastPageReadChanged ? actualPageIndex : undefined,
|
2024-12-10 02:12:38 +01:00
|
|
|
isRead: isLastPage ? true : undefined,
|
2024-12-07 03:43:18 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clearTimeout(this.updateCurrentPageTimeout);
|
|
|
|
|
if (debounceChapterUpdate) {
|
|
|
|
|
this.updateCurrentPageTimeout = setTimeout(handleCurrentPageIndexChange, 1000);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleCurrentPageIndexChange();
|
|
|
|
|
},
|
|
|
|
|
[currentChapter?.id, nextChapter?.id, nextChapters, currentPageIndex, downloadAheadLimit],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 13:00:36 +01:00
|
|
|
static updateCurrentPageOnScroll(
|
|
|
|
|
imageRefs: MutableRefObject<(HTMLElement | null)[]>,
|
2024-12-07 03:43:18 +01:00
|
|
|
lastPageIndex: number,
|
|
|
|
|
updateCurrentPageIndex: ReturnType<typeof ReaderControls.useUpdateCurrentPageIndex>,
|
2024-11-06 13:00:36 +01:00
|
|
|
type: PageInViewportType,
|
2024-12-07 03:43:18 +01:00
|
|
|
readingDirection: ReadingDirection,
|
2024-11-06 13:00:36 +01:00
|
|
|
) {
|
|
|
|
|
const firstVisibleImageIndex = imageRefs.current.findIndex((image) => image && isPageInViewport(image, type));
|
2024-12-07 03:43:18 +01:00
|
|
|
const lastPage = imageRefs.current?.[imageRefs.current.length - 1];
|
|
|
|
|
const isEndReached = lastPage && isEndOfPageInViewport(lastPage, type, readingDirection);
|
|
|
|
|
|
|
|
|
|
// handle cases where the last page is too small to ever be the "firstVisibleImageIndex"
|
|
|
|
|
if (isEndReached) {
|
2024-12-09 15:07:52 +01:00
|
|
|
updateCurrentPageIndex(firstVisibleImageIndex, false, true);
|
2024-12-07 03:43:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2024-11-06 13:00:36 +01:00
|
|
|
|
2024-12-07 03:43:18 +01:00
|
|
|
if (firstVisibleImageIndex !== -1) {
|
|
|
|
|
updateCurrentPageIndex(firstVisibleImageIndex, firstVisibleImageIndex !== lastPageIndex);
|
2024-11-06 13:00:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static useHandleClick(
|
|
|
|
|
scrollElement: HTMLElement | null,
|
|
|
|
|
): (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void {
|
2024-12-08 22:28:00 +01:00
|
|
|
const { direction: themeDirection } = useTheme();
|
2024-11-06 13:00:36 +01:00
|
|
|
const { setIsVisible: setIsOverlayVisible } = useReaderOverlayContext();
|
|
|
|
|
const { currentPageIndex, pages } = userReaderStatePagesContext();
|
|
|
|
|
const { setShowPreview } = useReaderTapZoneContext();
|
|
|
|
|
const { readingMode, readingDirection, isStaticNav } = ReaderService.useSettings();
|
|
|
|
|
const openPage = ReaderControls.useOpenPage();
|
2024-12-03 18:00:46 +01:00
|
|
|
const openChapter = ReaderControls.useOpenChapter();
|
2024-11-06 13:00:36 +01:00
|
|
|
|
|
|
|
|
return useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
if (!scrollElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rect = e.currentTarget.getBoundingClientRect();
|
|
|
|
|
const rectRelativeX = e.clientX - rect.left;
|
|
|
|
|
const rectRelativeY = e.clientY - rect.top;
|
|
|
|
|
const action = ReaderTapZoneService.getAction(rectRelativeX, rectRelativeY);
|
|
|
|
|
|
|
|
|
|
setShowPreview(false);
|
|
|
|
|
|
|
|
|
|
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode.value);
|
|
|
|
|
const scrollDirection =
|
|
|
|
|
readingMode.value === ReadingMode.CONTINUOUS_HORIZONTAL ? ScrollDirection.X : ScrollDirection.Y;
|
2024-12-09 16:26:03 +01:00
|
|
|
|
2024-11-06 13:00:36 +01:00
|
|
|
switch (action) {
|
|
|
|
|
case TapZoneRegionType.MENU:
|
|
|
|
|
setIsOverlayVisible((isVisible) => isStaticNav || !isVisible);
|
|
|
|
|
break;
|
|
|
|
|
case TapZoneRegionType.PREVIOUS:
|
|
|
|
|
if (isContinuousReadingModeActive) {
|
2024-12-03 18:00:46 +01:00
|
|
|
this.scroll(
|
|
|
|
|
ScrollOffset.BACKWARD,
|
|
|
|
|
scrollDirection,
|
2024-12-08 22:28:00 +01:00
|
|
|
readingMode.value,
|
2024-12-03 18:00:46 +01:00
|
|
|
readingDirection.value,
|
2024-12-08 22:28:00 +01:00
|
|
|
themeDirection,
|
2024-12-03 18:00:46 +01:00
|
|
|
scrollElement,
|
|
|
|
|
openChapter,
|
2024-12-11 21:04:44 +01:00
|
|
|
setIsOverlayVisible,
|
2024-12-03 18:00:46 +01:00
|
|
|
);
|
2024-11-06 13:00:36 +01:00
|
|
|
} else {
|
2024-12-09 03:11:45 +01:00
|
|
|
openPage('previous', 'ltr');
|
2024-11-06 13:00:36 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case TapZoneRegionType.NEXT:
|
|
|
|
|
if (isContinuousReadingModeActive) {
|
2024-12-03 18:00:46 +01:00
|
|
|
this.scroll(
|
|
|
|
|
ScrollOffset.FORWARD,
|
|
|
|
|
scrollDirection,
|
2024-12-08 22:28:00 +01:00
|
|
|
readingMode.value,
|
2024-12-03 18:00:46 +01:00
|
|
|
readingDirection.value,
|
2024-12-08 22:28:00 +01:00
|
|
|
themeDirection,
|
2024-12-03 18:00:46 +01:00
|
|
|
scrollElement,
|
|
|
|
|
openChapter,
|
2024-12-11 21:04:44 +01:00
|
|
|
setIsOverlayVisible,
|
2024-12-03 18:00:46 +01:00
|
|
|
);
|
2024-11-06 13:00:36 +01:00
|
|
|
} else {
|
2024-12-09 03:11:45 +01:00
|
|
|
openPage('next', 'ltr');
|
2024-11-06 13:00:36 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unexpected "TapZoneRegionType" (${action})`);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-12-08 22:28:00 +01:00
|
|
|
[
|
|
|
|
|
scrollElement,
|
|
|
|
|
currentPageIndex,
|
|
|
|
|
pages,
|
|
|
|
|
readingMode.value,
|
|
|
|
|
openPage,
|
|
|
|
|
readingDirection.value,
|
|
|
|
|
openChapter,
|
|
|
|
|
themeDirection,
|
|
|
|
|
],
|
2024-11-06 13:00:36 +01:00
|
|
|
);
|
|
|
|
|
}
|
2024-11-03 03:09:00 +01:00
|
|
|
}
|