fix: refine touch handling in PhotoswipeReader to improve swipe detection and pinch gesture management

This commit is contained in:
Julien Froidefond
2025-10-19 09:38:07 +02:00
parent 8c88c4f1a7
commit 3704a8d88b

View File

@@ -154,20 +154,34 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
return; return;
} }
// Un seul doigt - seulement si on n'était pas en train de pinch
// On réinitialise isPinchingRef seulement ici, quand on commence un nouveau geste à 1 doigt
if (e.touches.length === 1) {
isPinchingRef.current = false; isPinchingRef.current = false;
touchStartXRef.current = e.touches[0].clientX; touchStartXRef.current = e.touches[0].clientX;
touchStartYRef.current = e.touches[0].clientY; touchStartYRef.current = e.touches[0].clientY;
}
}, []);
const handleTouchMove = useCallback((e: TouchEvent) => {
// Détecter le pinch pendant le mouvement
if (e.touches.length > 1) {
isPinchingRef.current = true;
touchStartXRef.current = null;
touchStartYRef.current = null;
}
}, []); }, []);
const handleTouchEnd = useCallback((e: TouchEvent) => { const handleTouchEnd = useCallback((e: TouchEvent) => {
// Ignorer si c'était un pinch // Si on était en mode pinch, ne JAMAIS traiter le swipe
if (isPinchingRef.current) { if (isPinchingRef.current) {
isPinchingRef.current = false;
touchStartXRef.current = null; touchStartXRef.current = null;
touchStartYRef.current = null; touchStartYRef.current = null;
// Ne PAS réinitialiser isPinchingRef ici, on le fera au prochain touchstart
return; return;
} }
// Vérifier qu'on a bien des coordonnées de départ
if (touchStartXRef.current === null || touchStartYRef.current === null) return; if (touchStartXRef.current === null || touchStartYRef.current === null) return;
if (pswpRef.current) return; // Ne pas gérer si Photoswipe est ouvert if (pswpRef.current) return; // Ne pas gérer si Photoswipe est ouvert
@@ -183,8 +197,8 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
return; return;
} }
// Seuil de 100px pour changer de page // Seuil de 50px pour changer de page
if (Math.abs(deltaX) > 100) { if (Math.abs(deltaX) > 50) {
if (deltaX > 0) { if (deltaX > 0) {
// Swipe vers la droite // Swipe vers la droite
if (isRTL) { if (isRTL) {
@@ -231,14 +245,16 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
window.addEventListener("keydown", handleKeyDown); window.addEventListener("keydown", handleKeyDown);
window.addEventListener("touchstart", handleTouchStart); window.addEventListener("touchstart", handleTouchStart);
window.addEventListener("touchmove", handleTouchMove);
window.addEventListener("touchend", handleTouchEnd); window.addEventListener("touchend", handleTouchEnd);
return () => { return () => {
window.removeEventListener("keydown", handleKeyDown); window.removeEventListener("keydown", handleKeyDown);
window.removeEventListener("touchstart", handleTouchStart); window.removeEventListener("touchstart", handleTouchStart);
window.removeEventListener("touchmove", handleTouchMove);
window.removeEventListener("touchend", handleTouchEnd); window.removeEventListener("touchend", handleTouchEnd);
}; };
}, [handleNextPage, handlePreviousPage, handleTouchStart, handleTouchEnd, onClose, isRTL, currentPage]); }, [handleNextPage, handlePreviousPage, handleTouchStart, handleTouchMove, handleTouchEnd, onClose, isRTL, currentPage]);
// Cleanup - Sync final sans debounce // Cleanup - Sync final sans debounce
useEffect(() => { useEffect(() => {