feat: doublick zoom and pan position if zoomed
This commit is contained in:
@@ -33,6 +33,8 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
handleNextPage,
|
handleNextPage,
|
||||||
shouldShowDoublePage,
|
shouldShowDoublePage,
|
||||||
zoomLevel,
|
zoomLevel,
|
||||||
|
panPosition,
|
||||||
|
handleDoubleClick,
|
||||||
} = usePageNavigation({
|
} = usePageNavigation({
|
||||||
book,
|
book,
|
||||||
pages,
|
pages,
|
||||||
@@ -118,6 +120,8 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
isRTL={isRTL}
|
isRTL={isRTL}
|
||||||
onThumbnailLoad={handleThumbnailLoad}
|
onThumbnailLoad={handleThumbnailLoad}
|
||||||
zoomLevel={zoomLevel}
|
zoomLevel={zoomLevel}
|
||||||
|
panPosition={panPosition}
|
||||||
|
onDoubleClick={handleDoubleClick}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<NavigationBar
|
<NavigationBar
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ interface ReaderContentProps {
|
|||||||
isRTL: boolean;
|
isRTL: boolean;
|
||||||
onThumbnailLoad: (pageNumber: number) => void;
|
onThumbnailLoad: (pageNumber: number) => void;
|
||||||
zoomLevel: number;
|
zoomLevel: number;
|
||||||
|
panPosition: { x: number; y: number };
|
||||||
|
onDoubleClick: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ReaderContent = ({
|
export const ReaderContent = ({
|
||||||
@@ -24,6 +26,8 @@ export const ReaderContent = ({
|
|||||||
isRTL,
|
isRTL,
|
||||||
onThumbnailLoad,
|
onThumbnailLoad,
|
||||||
zoomLevel,
|
zoomLevel,
|
||||||
|
panPosition,
|
||||||
|
onDoubleClick,
|
||||||
}: 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">
|
||||||
@@ -37,6 +41,8 @@ export const ReaderContent = ({
|
|||||||
isRTL={isRTL}
|
isRTL={isRTL}
|
||||||
order="first"
|
order="first"
|
||||||
zoomLevel={zoomLevel}
|
zoomLevel={zoomLevel}
|
||||||
|
panPosition={panPosition}
|
||||||
|
onDoubleClick={onDoubleClick}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{isDoublePage && shouldShowDoublePage(currentPage) && (
|
{isDoublePage && shouldShowDoublePage(currentPage) && (
|
||||||
@@ -48,6 +54,9 @@ export const ReaderContent = ({
|
|||||||
isDoublePage={true}
|
isDoublePage={true}
|
||||||
isRTL={isRTL}
|
isRTL={isRTL}
|
||||||
order="second"
|
order="second"
|
||||||
|
zoomLevel={zoomLevel}
|
||||||
|
panPosition={panPosition}
|
||||||
|
onDoubleClick={onDoubleClick}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ interface SinglePageProps {
|
|||||||
isRTL?: boolean;
|
isRTL?: boolean;
|
||||||
order?: "first" | "second";
|
order?: "first" | "second";
|
||||||
zoomLevel?: number;
|
zoomLevel?: number;
|
||||||
|
panPosition?: { x: number; y: number };
|
||||||
|
onDoubleClick?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SinglePage = ({
|
export const SinglePage = ({
|
||||||
@@ -20,7 +22,9 @@ export const SinglePage = ({
|
|||||||
isDoublePage = false,
|
isDoublePage = false,
|
||||||
isRTL = false,
|
isRTL = false,
|
||||||
order = "first",
|
order = "first",
|
||||||
zoomLevel,
|
zoomLevel = 1,
|
||||||
|
panPosition = { x: 0, y: 0 },
|
||||||
|
onDoubleClick,
|
||||||
}: SinglePageProps) => {
|
}: SinglePageProps) => {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -39,14 +43,18 @@ export const SinglePage = ({
|
|||||||
<img
|
<img
|
||||||
src={pageUrl}
|
src={pageUrl}
|
||||||
style={{
|
style={{
|
||||||
transform: `scale(${zoomLevel})`,
|
transform: `scale(${zoomLevel}) translate(${panPosition.x}px, ${panPosition.y}px)`,
|
||||||
|
transformOrigin: "center",
|
||||||
|
transition: zoomLevel === 1 ? "transform 0.3s ease-out" : "none",
|
||||||
|
cursor: "pointer",
|
||||||
}}
|
}}
|
||||||
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",
|
||||||
isLoading ? "opacity-0" : "opacity-100"
|
isLoading ? "opacity-0" : "opacity-100"
|
||||||
)}
|
)}
|
||||||
onLoad={() => onLoad(pageNumber)}
|
onLoad={() => onLoad(pageNumber)}
|
||||||
|
onDoubleClick={onDoubleClick}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -19,13 +19,16 @@ export const usePageNavigation = ({
|
|||||||
const [currentPage, setCurrentPage] = useState(book.readProgress?.page || 1);
|
const [currentPage, setCurrentPage] = useState(book.readProgress?.page || 1);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [secondPageLoading, setSecondPageLoading] = useState(true);
|
const [secondPageLoading, setSecondPageLoading] = useState(true);
|
||||||
|
const [zoomLevel, setZoomLevel] = useState(1);
|
||||||
|
const [panPosition, setPanPosition] = useState({ x: 0, y: 0 });
|
||||||
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
const touchStartXRef = useRef<number | null>(null);
|
const touchStartXRef = useRef<number | null>(null);
|
||||||
const touchStartYRef = useRef<number | null>(null);
|
const touchStartYRef = useRef<number | null>(null);
|
||||||
|
const lastPanPositionRef = useRef({ x: 0, y: 0 });
|
||||||
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);
|
const initialDistanceRef = useRef<number | null>(null);
|
||||||
|
const DEFAULT_ZOOM_LEVEL = 2;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
currentPageRef.current = currentPage;
|
currentPageRef.current = currentPage;
|
||||||
@@ -113,7 +116,8 @@ export const usePageNavigation = ({
|
|||||||
return Math.sqrt(dx * dx + dy * dy);
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleTouchMove = useCallback((event: TouchEvent) => {
|
const handleTouchMove = useCallback(
|
||||||
|
(event: TouchEvent) => {
|
||||||
if (event.touches.length === 2) {
|
if (event.touches.length === 2) {
|
||||||
const distance = calculateDistance(event.touches[0], event.touches[1]);
|
const distance = calculateDistance(event.touches[0], event.touches[1]);
|
||||||
if (initialDistanceRef.current !== null) {
|
if (initialDistanceRef.current !== null) {
|
||||||
@@ -123,8 +127,22 @@ export const usePageNavigation = ({
|
|||||||
Math.min(3, Math.max(1, prevZoomLevel + (scale - 1) * zoomFactor))
|
Math.min(3, Math.max(1, prevZoomLevel + (scale - 1) * zoomFactor))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} else if (event.touches.length === 1 && zoomLevel > 1) {
|
||||||
|
// Gestion du pan uniquement quand on est zoomé
|
||||||
|
if (touchStartXRef.current !== null && touchStartYRef.current !== null) {
|
||||||
|
const deltaX = event.touches[0].clientX - touchStartXRef.current;
|
||||||
|
const deltaY = event.touches[0].clientY - touchStartYRef.current;
|
||||||
|
|
||||||
|
setPanPosition({
|
||||||
|
x: lastPanPositionRef.current.x + deltaX,
|
||||||
|
y: lastPanPositionRef.current.y + deltaY,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, []);
|
event.preventDefault(); // Empêcher le scroll de la page
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[zoomLevel]
|
||||||
|
);
|
||||||
|
|
||||||
const handleTouchStart = useCallback(
|
const handleTouchStart = useCallback(
|
||||||
(event: TouchEvent) => {
|
(event: TouchEvent) => {
|
||||||
@@ -133,10 +151,11 @@ export const usePageNavigation = ({
|
|||||||
} else {
|
} 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;
|
||||||
|
lastPanPositionRef.current = panPosition;
|
||||||
currentPageRef.current = currentPage;
|
currentPageRef.current = currentPage;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[currentPage]
|
[currentPage, panPosition]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleTouchEnd = useCallback(
|
const handleTouchEnd = useCallback(
|
||||||
@@ -151,6 +170,15 @@ export const usePageNavigation = ({
|
|||||||
const deltaX = touchEndX - touchStartXRef.current;
|
const deltaX = touchEndX - touchStartXRef.current;
|
||||||
const deltaY = touchEndY - touchStartYRef.current;
|
const deltaY = touchEndY - touchStartYRef.current;
|
||||||
|
|
||||||
|
// Si on est zoomé, on met à jour la position finale du pan
|
||||||
|
if (zoomLevel > 1) {
|
||||||
|
lastPanPositionRef.current = {
|
||||||
|
x: lastPanPositionRef.current.x + deltaX,
|
||||||
|
y: lastPanPositionRef.current.y + deltaY,
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Si le déplacement vertical est plus important que le déplacement horizontal,
|
// Si le déplacement vertical est plus important que le déplacement horizontal,
|
||||||
// on ne fait rien (pour éviter de confondre avec un scroll)
|
// on ne fait rien (pour éviter de confondre avec un scroll)
|
||||||
if (Math.abs(deltaY) > Math.abs(deltaX)) return;
|
if (Math.abs(deltaY) > Math.abs(deltaX)) return;
|
||||||
@@ -177,9 +205,17 @@ export const usePageNavigation = ({
|
|||||||
touchStartXRef.current = null;
|
touchStartXRef.current = null;
|
||||||
touchStartYRef.current = null;
|
touchStartYRef.current = null;
|
||||||
},
|
},
|
||||||
[handleNextPage, handlePreviousPage, isRTL]
|
[handleNextPage, handlePreviousPage, isRTL, zoomLevel]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Reset du pan quand on change de page ou dezoom
|
||||||
|
useEffect(() => {
|
||||||
|
if (zoomLevel === 1) {
|
||||||
|
setPanPosition({ x: 0, y: 0 });
|
||||||
|
lastPanPositionRef.current = { x: 0, y: 0 };
|
||||||
|
}
|
||||||
|
}, [zoomLevel, currentPage]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setSecondPageLoading(true);
|
setSecondPageLoading(true);
|
||||||
@@ -237,6 +273,15 @@ export const usePageNavigation = ({
|
|||||||
};
|
};
|
||||||
}, [syncReadProgress]);
|
}, [syncReadProgress]);
|
||||||
|
|
||||||
|
const handleDoubleClick = useCallback(() => {
|
||||||
|
setZoomLevel((prevZoom) => {
|
||||||
|
if (prevZoom === 1) {
|
||||||
|
return DEFAULT_ZOOM_LEVEL;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currentPage,
|
currentPage,
|
||||||
navigateToPage,
|
navigateToPage,
|
||||||
@@ -248,5 +293,7 @@ export const usePageNavigation = ({
|
|||||||
handleNextPage,
|
handleNextPage,
|
||||||
shouldShowDoublePage,
|
shouldShowDoublePage,
|
||||||
zoomLevel,
|
zoomLevel,
|
||||||
|
panPosition,
|
||||||
|
handleDoubleClick,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user