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-26 21:01:46 +01:00
|
|
|
import { MutableRefObject, RefObject, useCallback, useEffect, 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-26 21:01:46 +01:00
|
|
|
import {
|
|
|
|
|
getNextIndexFromPage,
|
|
|
|
|
getNextPageIndex,
|
|
|
|
|
getPage,
|
|
|
|
|
getPageForMousePos,
|
|
|
|
|
getProgressBarPositionInfo,
|
|
|
|
|
} 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-26 21:01:46 +01:00
|
|
|
ProgressBarPosition,
|
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-30 14:11:28 +01:00
|
|
|
import {
|
2024-12-31 04:24:47 +01:00
|
|
|
isATransitionPageVisible,
|
2024-12-30 14:11:28 +01:00
|
|
|
isEndOfPageInViewport,
|
|
|
|
|
isPageInViewport,
|
|
|
|
|
} from '@/modules/reader/utils/ReaderPager.utils.tsx';
|
2024-12-20 21:42:54 +01:00
|
|
|
import { useReaderOverlayContext } from '@/modules/reader/contexts/ReaderOverlayContext.tsx';
|
2024-11-06 13:00:36 +01:00
|
|
|
import { useReaderTapZoneContext } from '@/modules/reader/contexts/ReaderTapZoneContext.tsx';
|
2024-12-30 16:50:54 +01:00
|
|
|
import { TapZoneRegionType, TReaderTapZoneContext } from '@/modules/reader/types/TapZoneLayout.types.ts';
|
2024-11-06 13:00:36 +01:00
|
|
|
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-12-20 21:42:54 +01:00
|
|
|
import { TReaderOverlayContext } from '@/modules/reader/types/ReaderOverlay.types.ts';
|
2024-12-26 21:01:46 +01:00
|
|
|
import { ReaderProgressBarProps, TReaderProgressCurrentPage } from '@/modules/reader/types/ReaderProgressBar.types.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-20 21:42:54 +01:00
|
|
|
setIsOverlayVisible: TReaderOverlayContext['setIsVisible'],
|
2024-12-30 16:50:54 +01:00
|
|
|
setShowPreview: TReaderTapZoneContext['setShowPreview'],
|
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-12-30 16:50:54 +01:00
|
|
|
setShowPreview(false);
|
2024-12-11 21:04:44 +01:00
|
|
|
|
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();
|
2025-01-01 17:24:36 +01:00
|
|
|
const { readingMode, shouldInformAboutMissingChapter, shouldInformAboutScanlatorChange } =
|
|
|
|
|
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':
|
2025-01-01 17:24:36 +01:00
|
|
|
ReaderControls.checkNextChapterConsistency(
|
|
|
|
|
t,
|
|
|
|
|
offset,
|
|
|
|
|
currentChapter,
|
|
|
|
|
previousChapter,
|
|
|
|
|
shouldInformAboutMissingChapter,
|
|
|
|
|
shouldInformAboutScanlatorChange,
|
|
|
|
|
)
|
2024-12-11 22:40:06 +01:00
|
|
|
.then(openPreviousChapter)
|
|
|
|
|
.catch(defaultPromiseErrorHandler('ReaderControls::checkNextChapterConsistency: previous'));
|
2024-11-03 03:09:00 +01:00
|
|
|
break;
|
|
|
|
|
case 'next':
|
2025-01-01 17:24:36 +01:00
|
|
|
ReaderControls.checkNextChapterConsistency(
|
|
|
|
|
t,
|
|
|
|
|
offset,
|
|
|
|
|
currentChapter,
|
|
|
|
|
nextChapter,
|
|
|
|
|
shouldInformAboutMissingChapter,
|
|
|
|
|
shouldInformAboutScanlatorChange,
|
|
|
|
|
)
|
2024-12-11 22:40:06 +01:00
|
|
|
.then(openNextChapter)
|
|
|
|
|
.catch(defaultPromiseErrorHandler('ReaderControls::checkNextChapterConsistency: next'));
|
2024-11-03 03:09:00 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unexpected "offset" (${offset})`);
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-01-01 17:24:36 +01:00
|
|
|
[
|
|
|
|
|
t,
|
|
|
|
|
currentChapter?.id,
|
|
|
|
|
openPreviousChapter,
|
|
|
|
|
openNextChapter,
|
|
|
|
|
readingMode.value,
|
|
|
|
|
shouldInformAboutMissingChapter,
|
|
|
|
|
shouldInformAboutScanlatorChange,
|
|
|
|
|
],
|
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',
|
2025-01-01 17:24:36 +01:00
|
|
|
currentChapter: TChapterReader | null | undefined,
|
|
|
|
|
chapterToOpen: TChapterReader | null | undefined,
|
|
|
|
|
shouldInformAboutMissingChapter: boolean,
|
|
|
|
|
shouldInformAboutScanlatorChange: boolean,
|
2024-12-11 22:33:04 +01:00
|
|
|
): Promise<void> {
|
|
|
|
|
if (!currentChapter || !chapterToOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const missingChapters = Math.abs(currentChapter.chapterNumber - chapterToOpen.chapterNumber);
|
2025-01-01 17:24:36 +01:00
|
|
|
const isSameScanlator =
|
|
|
|
|
!shouldInformAboutScanlatorChange || currentChapter.scanlator === chapterToOpen.scanlator;
|
|
|
|
|
const isContinuousChapter = !shouldInformAboutMissingChapter || 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, {
|
2024-12-20 12:05:36 +00:00
|
|
|
count: missingChapters - 1,
|
2024-12-11 22:33:04 +01:00
|
|
|
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-30 16:50:54 +01:00
|
|
|
const { setShowPreview } = useReaderTapZoneContext();
|
2024-12-31 17:03:42 +01:00
|
|
|
const { readingDirection, readingMode } = ReaderService.useSettings();
|
2024-12-03 18:00:46 +01:00
|
|
|
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-31 17:03:42 +01:00
|
|
|
const isATransitionPageVisibleFlag = isATransitionPageVisible(transitionPageMode, readingMode.value);
|
|
|
|
|
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode.value);
|
2024-12-03 18:00:46 +01:00
|
|
|
|
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-30 16:50:54 +01:00
|
|
|
setShowPreview(false);
|
2024-12-13 02:15:18 +01:00
|
|
|
}
|
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 =
|
2024-12-31 04:24:47 +01:00
|
|
|
isFirstPage && isATransitionPageVisibleFlag && convertedPage === 'previous' && !!previousChapter;
|
2024-12-03 18:00:46 +01:00
|
|
|
if (shouldOpenPreviousChapter) {
|
|
|
|
|
openChapter('previous');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldOpenNextChapter =
|
2024-12-31 04:24:47 +01:00
|
|
|
isLastPage && isATransitionPageVisibleFlag && convertedPage === 'next' && !!nextChapter;
|
2024-12-03 18:00:46 +01:00
|
|
|
if (shouldOpenNextChapter) {
|
|
|
|
|
openChapter('next');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-31 17:03:42 +01:00
|
|
|
const needToHideTransitionPage = isATransitionPageVisibleFlag && !isContinuousReadingModeActive;
|
2024-12-03 18:00:46 +01:00
|
|
|
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-31 04:24:47 +01:00
|
|
|
isATransitionPageVisibleFlag,
|
2024-12-31 17:03:42 +01:00
|
|
|
isContinuousReadingModeActive,
|
2024-12-03 18:00:46 +01:00
|
|
|
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
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-21 03:46:55 +01:00
|
|
|
clearTimeout(ReaderControls.updateCurrentPageTimeout);
|
2024-12-07 03:43:18 +01:00
|
|
|
if (debounceChapterUpdate) {
|
2024-12-21 03:46:55 +01:00
|
|
|
ReaderControls.updateCurrentPageTimeout = setTimeout(handleCurrentPageIndexChange, 1000);
|
2024-12-07 03:43:18 +01:00
|
|
|
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
|
|
|
) {
|
2024-12-31 16:43:46 +01:00
|
|
|
const themeDirectionForReadingDirection = READING_DIRECTION_TO_THEME_DIRECTION[readingDirection];
|
|
|
|
|
|
|
|
|
|
const firstVisibleImageIndex = imageRefs.current.findIndex(
|
|
|
|
|
(image) =>
|
|
|
|
|
image &&
|
|
|
|
|
isPageInViewport(
|
|
|
|
|
image,
|
|
|
|
|
type,
|
|
|
|
|
// the pages aren't always properly scrolled to the top which results in the leading page to still be slightly
|
|
|
|
|
// visible (e.g. 0.0123px) breaking the "first visible image" detection
|
|
|
|
|
{
|
|
|
|
|
bottom: 1,
|
|
|
|
|
left: getOptionForDirection(0, window.innerWidth + 1, themeDirectionForReadingDirection),
|
|
|
|
|
right: getOptionForDirection(1, 0, themeDirectionForReadingDirection),
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-12-07 03:43:18 +01:00
|
|
|
const lastPage = imageRefs.current?.[imageRefs.current.length - 1];
|
|
|
|
|
const isEndReached = lastPage && isEndOfPageInViewport(lastPage, type, readingDirection);
|
|
|
|
|
|
2024-12-31 16:54:21 +01:00
|
|
|
if (firstVisibleImageIndex === -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 03:43:18 +01:00
|
|
|
// 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-31 16:54:21 +01:00
|
|
|
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-21 03:46:55 +01:00
|
|
|
ReaderControls.scroll(
|
2024-12-03 18:00:46 +01:00
|
|
|
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-30 16:50:54 +01:00
|
|
|
setShowPreview,
|
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-21 03:46:55 +01:00
|
|
|
ReaderControls.scroll(
|
2024-12-03 18:00:46 +01:00
|
|
|
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-30 16:50:54 +01:00
|
|
|
setShowPreview,
|
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-12-26 21:01:46 +01:00
|
|
|
|
|
|
|
|
static useHandleProgressDragging(
|
|
|
|
|
openPage: ReturnType<(typeof ReaderControls)['useOpenPage']>,
|
|
|
|
|
progressBarRef: RefObject<HTMLDivElement | null>,
|
|
|
|
|
isDragging: boolean,
|
|
|
|
|
currentPage: TReaderProgressCurrentPage,
|
|
|
|
|
pages: ReaderProgressBarProps['pages'],
|
|
|
|
|
progressBarPosition: ProgressBarPosition,
|
|
|
|
|
getOptionForDirectionFn: typeof getOptionForDirection,
|
2024-12-28 04:05:29 +01:00
|
|
|
fullSegmentClicks: boolean,
|
2024-12-26 21:01:46 +01:00
|
|
|
): void {
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isDragging) {
|
|
|
|
|
return () => undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { isHorizontal } = getProgressBarPositionInfo(progressBarPosition);
|
|
|
|
|
|
|
|
|
|
const handleMove = (coordinates: { clientX: number; clientY: number }) => {
|
|
|
|
|
if (!progressBarRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newPageIndex = getNextIndexFromPage(
|
|
|
|
|
getPageForMousePos(
|
|
|
|
|
coordinates,
|
2025-01-02 21:25:35 +01:00
|
|
|
progressBarRef.current,
|
2024-12-26 21:01:46 +01:00
|
|
|
pages,
|
|
|
|
|
isHorizontal,
|
2024-12-28 04:05:29 +01:00
|
|
|
fullSegmentClicks,
|
2024-12-26 21:01:46 +01:00
|
|
|
getOptionForDirectionFn,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const hasCurrentPageIndexChanged = getNextIndexFromPage(currentPage) !== newPageIndex;
|
|
|
|
|
if (!hasCurrentPageIndexChanged) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openPage(newPageIndex, undefined, false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleMouseMove = (e: MouseEvent) => {
|
|
|
|
|
handleMove(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleTouchMove = (e: TouchEvent) => {
|
|
|
|
|
if (e.touches.length > 0) {
|
|
|
|
|
handleMove(e.touches[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener('mousemove', handleMouseMove);
|
|
|
|
|
document.addEventListener('touchmove', handleTouchMove);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('mousemove', handleMouseMove);
|
|
|
|
|
document.removeEventListener('touchmove', handleTouchMove);
|
|
|
|
|
};
|
|
|
|
|
}, [openPage, isDragging, currentPage, pages, progressBarPosition]);
|
|
|
|
|
}
|
2024-11-03 03:09:00 +01:00
|
|
|
}
|