Add infinite scroll to reader
This commit is contained in:
@@ -505,6 +505,29 @@ export const isSpreadPage = (image: HTMLImageElement): boolean => {
|
||||
return aspectRatio < 1;
|
||||
};
|
||||
|
||||
const MIN_PREVIOUS_NEXT_CHAPTER_IMAGE_LOAD_AMOUNT = 0;
|
||||
const MAX_PREVIOUS_NEXT_CHAPTER_IMAGE_LOAD_AMOUNT = 1;
|
||||
const getImagePreLoadAmount = (
|
||||
isCurrentChapter: boolean,
|
||||
isPreviousChapter: boolean,
|
||||
isNextChapter: boolean,
|
||||
imagePreLoadAmount: number,
|
||||
): number => {
|
||||
if (isCurrentChapter) {
|
||||
return imagePreLoadAmount;
|
||||
}
|
||||
|
||||
if (isPreviousChapter || isNextChapter) {
|
||||
return coerceIn(
|
||||
MAX_PREVIOUS_NEXT_CHAPTER_IMAGE_LOAD_AMOUNT,
|
||||
MIN_PREVIOUS_NEXT_CHAPTER_IMAGE_LOAD_AMOUNT,
|
||||
imagePreLoadAmount,
|
||||
);
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
const PREVIOUS_IMAGE_LOAD_AMOUNT = 2;
|
||||
export const getPageIndexesToLoad = (
|
||||
currentPageIndex: number,
|
||||
@@ -512,20 +535,33 @@ export const getPageIndexesToLoad = (
|
||||
previousCurrentPageIndex: number,
|
||||
imagePreLoadAmount: number,
|
||||
readingMode: ReadingMode,
|
||||
isCurrentChapter: boolean,
|
||||
isPreviousChapter: boolean,
|
||||
isNextChapter: boolean,
|
||||
): number[] => {
|
||||
const currentPagesIndex = getPage(currentPageIndex, pages).pagesIndex;
|
||||
if (!isCurrentChapter && !isPreviousChapter && !isNextChapter) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const directionInvert = previousCurrentPageIndex <= currentPageIndex ? 1 : -1;
|
||||
const currentPagesIndex = getPage(currentPageIndex, pages).pagesIndex;
|
||||
const finalImagePreLoadAmount = getImagePreLoadAmount(
|
||||
isCurrentChapter,
|
||||
isPreviousChapter,
|
||||
isNextChapter,
|
||||
imagePreLoadAmount,
|
||||
);
|
||||
|
||||
const directionInvert = previousCurrentPageIndex <= currentPageIndex && !isPreviousChapter ? 1 : -1;
|
||||
// load at most PREVIOUS_IMAGE_LOAD_AMOUNT of the previous pages to ensure that you do not have to wait too long
|
||||
// when going back to the previous pages
|
||||
const startPagesIndexTrailingIncluded = Math.max(
|
||||
0,
|
||||
currentPagesIndex - Math.min(PREVIOUS_IMAGE_LOAD_AMOUNT, imagePreLoadAmount) * directionInvert,
|
||||
currentPagesIndex - Math.min(PREVIOUS_IMAGE_LOAD_AMOUNT, finalImagePreLoadAmount) * directionInvert,
|
||||
);
|
||||
// do not load previous pages for continuous pagers to prevent layout shifts due to leading pages getting loaded
|
||||
const startPagesIndex = !isContinuousReadingMode(readingMode) ? startPagesIndexTrailingIncluded : currentPageIndex;
|
||||
const endPagesIndex = currentPagesIndex + imagePreLoadAmount * directionInvert;
|
||||
const pagesToRenderLength = Math.abs(endPagesIndex - startPagesIndex) + 1;
|
||||
const endPagesIndex = currentPagesIndex + finalImagePreLoadAmount * directionInvert;
|
||||
const pagesToRenderLength = Math.max(1, Math.abs(endPagesIndex - startPagesIndex));
|
||||
|
||||
return Array(pagesToRenderLength)
|
||||
.fill(1)
|
||||
|
||||
Reference in New Issue
Block a user