feat: review bookreader zoom and pages on mobile

This commit is contained in:
Julien Froidefond
2025-02-28 14:16:34 +01:00
parent 29b9eca599
commit 7542f39f7e
4 changed files with 52 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
handlePreviousPage, handlePreviousPage,
handleNextPage, handleNextPage,
shouldShowDoublePage, shouldShowDoublePage,
zoomLevel,
} = usePageNavigation({ } = usePageNavigation({
book, book,
pages, pages,
@@ -116,6 +117,7 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
shouldShowDoublePage={shouldShowDoublePage} shouldShowDoublePage={shouldShowDoublePage}
isRTL={isRTL} isRTL={isRTL}
onThumbnailLoad={handleThumbnailLoad} onThumbnailLoad={handleThumbnailLoad}
zoomLevel={zoomLevel}
/> />
<NavigationBar <NavigationBar

View File

@@ -10,6 +10,7 @@ interface ReaderContentProps {
shouldShowDoublePage: (page: number) => boolean; shouldShowDoublePage: (page: number) => boolean;
isRTL: boolean; isRTL: boolean;
onThumbnailLoad: (pageNumber: number) => void; onThumbnailLoad: (pageNumber: number) => void;
zoomLevel: number;
} }
export const ReaderContent = ({ export const ReaderContent = ({
@@ -22,6 +23,7 @@ export const ReaderContent = ({
shouldShowDoublePage, shouldShowDoublePage,
isRTL, isRTL,
onThumbnailLoad, onThumbnailLoad,
zoomLevel,
}: ReaderContentProps) => { }: ReaderContentProps) => {
return ( return (
<div className="relative flex-1 flex items-center justify-center overflow-hidden p-1"> <div className="relative flex-1 flex items-center justify-center overflow-hidden p-1">
@@ -34,6 +36,7 @@ export const ReaderContent = ({
isDoublePage={isDoublePage} isDoublePage={isDoublePage}
isRTL={isRTL} isRTL={isRTL}
order="first" order="first"
zoomLevel={zoomLevel}
/> />
{isDoublePage && shouldShowDoublePage(currentPage) && ( {isDoublePage && shouldShowDoublePage(currentPage) && (

View File

@@ -9,6 +9,7 @@ interface SinglePageProps {
isDoublePage?: boolean; isDoublePage?: boolean;
isRTL?: boolean; isRTL?: boolean;
order?: "first" | "second"; order?: "first" | "second";
zoomLevel?: number;
} }
export const SinglePage = ({ export const SinglePage = ({
@@ -19,6 +20,7 @@ export const SinglePage = ({
isDoublePage = false, isDoublePage = false,
isRTL = false, isRTL = false,
order = "first", order = "first",
zoomLevel,
}: SinglePageProps) => { }: SinglePageProps) => {
return ( return (
<div <div
@@ -36,6 +38,9 @@ export const SinglePage = ({
{pageUrl && ( {pageUrl && (
<img <img
src={pageUrl} src={pageUrl}
style={{
transform: `scale(${zoomLevel})`,
}}
alt={`Page ${pageNumber}`} alt={`Page ${pageNumber}`}
className={cn( className={cn(
"max-h-full w-auto object-contain transition-opacity duration-300", "max-h-full w-auto object-contain transition-opacity duration-300",

View File

@@ -24,6 +24,8 @@ export const usePageNavigation = ({
const touchStartYRef = useRef<number | null>(null); const touchStartYRef = useRef<number | null>(null);
const currentPageRef = useRef(currentPage); const currentPageRef = useRef(currentPage);
const isRTL = direction === "rtl"; const isRTL = direction === "rtl";
const [zoomLevel, setZoomLevel] = useState(1);
const initialDistanceRef = useRef<number | null>(null);
useEffect(() => { useEffect(() => {
currentPageRef.current = currentPage; currentPageRef.current = currentPage;
@@ -68,6 +70,8 @@ export const usePageNavigation = ({
const shouldShowDoublePage = useCallback( const shouldShowDoublePage = useCallback(
(pageNumber: number) => { (pageNumber: number) => {
const isMobile = window.innerHeight < 700;
if (isMobile) return false;
if (!isDoublePage) return false; if (!isDoublePage) return false;
if (pageNumber === 1) return false; if (pageNumber === 1) return false;
return pageNumber < pages.length; return pageNumber < pages.length;
@@ -103,17 +107,40 @@ export const usePageNavigation = ({
} }
}, [currentPage, isDoublePage, navigateToPage, pages.length, shouldShowDoublePage]); }, [currentPage, isDoublePage, navigateToPage, pages.length, shouldShowDoublePage]);
const calculateDistance = (touch1: Touch, touch2: Touch) => {
const dx = touch2.clientX - touch1.clientX;
const dy = touch2.clientY - touch1.clientY;
return Math.sqrt(dx * dx + dy * dy);
};
const handleTouchMove = useCallback((event: TouchEvent) => {
if (event.touches.length === 2) {
const distance = calculateDistance(event.touches[0], event.touches[1]);
if (initialDistanceRef.current !== null) {
const scale = distance / initialDistanceRef.current;
setZoomLevel((prevZoomLevel) => Math.max(1, prevZoomLevel * scale));
}
}
}, []);
const handleTouchStart = useCallback( const handleTouchStart = useCallback(
(event: TouchEvent) => { (event: TouchEvent) => {
if (event.touches.length === 2) {
initialDistanceRef.current = calculateDistance(event.touches[0], event.touches[1]);
} else {
touchStartXRef.current = event.touches[0].clientX; touchStartXRef.current = event.touches[0].clientX;
touchStartYRef.current = event.touches[0].clientY; touchStartYRef.current = event.touches[0].clientY;
currentPageRef.current = currentPage; currentPageRef.current = currentPage;
}
}, },
[currentPage] [currentPage]
); );
const handleTouchEnd = useCallback( const handleTouchEnd = useCallback(
(event: TouchEvent) => { (event: TouchEvent) => {
if (event.touches.length < 2) {
initialDistanceRef.current = null;
}
if (touchStartXRef.current === null || touchStartYRef.current === null) return; if (touchStartXRef.current === null || touchStartYRef.current === null) return;
const touchEndX = event.changedTouches[0].clientX; const touchEndX = event.changedTouches[0].clientX;
@@ -180,13 +207,23 @@ export const usePageNavigation = ({
window.addEventListener("keydown", handleKeyDown); window.addEventListener("keydown", handleKeyDown);
window.addEventListener("touchstart", handleTouchStart); window.addEventListener("touchstart", handleTouchStart);
window.addEventListener("touchend", handleTouchEnd); window.addEventListener("touchend", handleTouchEnd);
window.addEventListener("touchmove", handleTouchMove);
return () => { return () => {
window.removeEventListener("keydown", handleKeyDown); window.removeEventListener("keydown", handleKeyDown);
window.removeEventListener("touchstart", handleTouchStart); window.removeEventListener("touchstart", handleTouchStart);
window.removeEventListener("touchend", handleTouchEnd); window.removeEventListener("touchend", handleTouchEnd);
window.removeEventListener("touchmove", handleTouchMove);
}; };
}, [handleNextPage, handlePreviousPage, handleTouchStart, handleTouchEnd, onClose, isRTL]); }, [
handleNextPage,
handlePreviousPage,
handleTouchStart,
handleTouchEnd,
onClose,
isRTL,
handleTouchMove,
]);
useEffect(() => { useEffect(() => {
return () => { return () => {
@@ -207,5 +244,6 @@ export const usePageNavigation = ({
handlePreviousPage, handlePreviousPage,
handleNextPage, handleNextPage,
shouldShowDoublePage, shouldShowDoublePage,
zoomLevel,
}; };
}; };