fix animation

This commit is contained in:
hamid zarghami
2026-07-01 16:17:03 +03:30
parent cda1b5b5bd
commit 4af7181384
2 changed files with 49 additions and 27 deletions
+35 -12
View File
@@ -6,6 +6,7 @@ import { useIsBrowserFullscreen } from "@/pages/viewer/hooks/useIsBrowserFullscr
import { useViewerViewport } from "@/pages/viewer/hooks/useViewerViewport";
import { isLegacyIOSSafari } from "@/pages/viewer/utils/isLegacyIOSSafari";
import { resolvePageBackground } from "@/pages/viewer/utils/pageBackground";
import { getVisiblePageIndices } from "@/pages/viewer/utils/visiblePageIndices";
import { toggleViewerFullscreen } from "@/pages/viewer/utils/viewerFullscreen";
import { ArrowLeft2, ArrowRight2, Maximize4, Pause, Play, RowVertical } from "iconsax-react";
import { type FC, useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
@@ -222,22 +223,44 @@ const BookViewer: FC<BookViewerProps> = ({ pages, catalogSize, documentSettings
const flipKey = `${usePortrait ? "portrait" : "spread"}-${pagePixelWidth}-${pagePixelHeight}-${flipIndexCount}`;
const {
scheduleEntranceForSpread,
scheduleEntranceForIndices,
getEntrancePhase,
reset: resetEntrance,
} = useBookEntranceController({
pages,
portrait: usePortrait,
showCover: hasMultiplePages,
});
} = useBookEntranceController({ pages });
/**
* ایندکس‌های منطقیِ صفحات قابل‌مشاهده در اسپرد جاری را برمی‌گرداند.
* جفت‌سازی (کدام دو صفحه با هم دیده می‌شوند) باید در فضای نمایشیِ کتاب
* (flip index) انجام شود، نه فضای منطقی: در کتاب RTL این دو فضا برعکس
* هم‌اند، پس مثلاً «ایندکس منطقی + ۱» لزوماً هم‌صفحه‌ی واقعی نیست.
*/
const getVisibleLogicalIndices = useCallback(
(flipIdx: number): number[] => {
const displayIndices = getVisiblePageIndices(flipIdx, flipIndexCount, {
portrait: usePortrait,
showCover: hasMultiplePages,
});
const seen = new Set<number>();
const result: number[] = [];
for (const di of displayIndices) {
const logical = toLogicalIndex(di);
if (logical >= 0 && logical < pages.length && !seen.has(logical)) {
seen.add(logical);
result.push(logical);
}
}
return result;
},
[flipIndexCount, usePortrait, hasMultiplePages, toLogicalIndex, pages.length],
);
const commitEntranceAtCurrentSpread = useCallback(() => {
const api = bookRef.current?.pageFlip();
const idx = api?.getCurrentPageIndex?.();
if (typeof idx !== "number" || flipIndexCount <= 0) return;
const flipIdx = Math.max(0, Math.min(idx, flipIndexCount - 1));
scheduleEntranceForSpread(toLogicalIndex(flipIdx));
}, [flipIndexCount, scheduleEntranceForSpread, toLogicalIndex]);
scheduleEntranceForIndices(getVisibleLogicalIndices(flipIdx));
}, [flipIndexCount, scheduleEntranceForIndices, getVisibleLogicalIndices]);
useEffect(() => {
resetEntrance();
@@ -359,9 +382,9 @@ const BookViewer: FC<BookViewerProps> = ({ pages, catalogSize, documentSettings
setCurrentPage(logicalIdx);
lastFlipPageRef.current = logicalIdx;
// اولین اسپرد قبل از onChangeState('read') هم باید انیمیشن ورود بگیرد
scheduleEntranceForSpread(logicalIdx);
scheduleEntranceForIndices(getVisibleLogicalIndices(flipIdx));
},
[flipIndexCount, scheduleEntranceForSpread, toLogicalIndex],
[flipIndexCount, scheduleEntranceForIndices, getVisibleLogicalIndices, toLogicalIndex],
);
/** بعد از اتمام انیمیشن ورق، ایندکس واقعی کتاب را می‌گیریم و انیمیشن ورود را یک‌بار شروع می‌کنیم */
@@ -429,9 +452,9 @@ const BookViewer: FC<BookViewerProps> = ({ pages, catalogSize, documentSettings
const flipIdx = toFlipIndex(logicalIdx);
pageFlipAPI.turnToPage(flipIdx);
lastFlipPageRef.current = logicalIdx;
scheduleEntranceForSpread(logicalIdx);
scheduleEntranceForIndices(getVisibleLogicalIndices(flipIdx));
},
[pages.length, scheduleEntranceForSpread, toFlipIndex],
[pages.length, scheduleEntranceForIndices, getVisibleLogicalIndices, toFlipIndex],
);
const handleLinkClick = useCallback(