feat(reader): right to left read
This commit is contained in:
@@ -10,6 +10,7 @@ import { NavigationBar } from "./components/NavigationBar";
|
|||||||
import { ControlButtons } from "./components/ControlButtons";
|
import { ControlButtons } from "./components/ControlButtons";
|
||||||
import { ImageLoader } from "@/components/ui/image-loader";
|
import { ImageLoader } from "@/components/ui/image-loader";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
import { useReadingDirection } from "./hooks/useReadingDirection";
|
||||||
|
|
||||||
export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
||||||
const [isDoublePage, setIsDoublePage] = useState(false);
|
const [isDoublePage, setIsDoublePage] = useState(false);
|
||||||
@@ -17,6 +18,7 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
const [isFullscreen, setIsFullscreen] = useState(false);
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
||||||
const readerRef = useRef<HTMLDivElement>(null);
|
const readerRef = useRef<HTMLDivElement>(null);
|
||||||
const isLandscape = useOrientation();
|
const isLandscape = useOrientation();
|
||||||
|
const { direction, toggleDirection, isRTL } = useReadingDirection();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
currentPage,
|
currentPage,
|
||||||
@@ -33,6 +35,7 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
pages,
|
pages,
|
||||||
isDoublePage,
|
isDoublePage,
|
||||||
onClose,
|
onClose,
|
||||||
|
direction,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { preloadPage, getPageUrl, cleanCache } = usePageCache({
|
const { preloadPage, getPageUrl, cleanCache } = usePageCache({
|
||||||
@@ -133,7 +136,15 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
return () => {
|
return () => {
|
||||||
isMounted = false;
|
isMounted = false;
|
||||||
};
|
};
|
||||||
}, [currentPage, isDoublePage, shouldShowDoublePage, preloadPage, cleanCache, pages.length]);
|
}, [
|
||||||
|
currentPage,
|
||||||
|
isDoublePage,
|
||||||
|
shouldShowDoublePage,
|
||||||
|
preloadPage,
|
||||||
|
cleanCache,
|
||||||
|
pages.length,
|
||||||
|
isRTL,
|
||||||
|
]);
|
||||||
|
|
||||||
// Effet pour gérer le mode double page automatiquement en paysage
|
// Effet pour gérer le mode double page automatiquement en paysage
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -200,6 +211,8 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
console.error("Erreur lors du changement de mode plein écran:", error);
|
console.error("Erreur lors du changement de mode plein écran:", error);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
direction={direction}
|
||||||
|
onToggleDirection={toggleDirection}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Pages */}
|
{/* Pages */}
|
||||||
@@ -216,7 +229,8 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"relative h-full flex items-center",
|
"relative h-full flex items-center",
|
||||||
isDoublePage ? "w-1/2 justify-end" : "w-full justify-center"
|
isDoublePage ? "w-1/2 justify-end" : "w-full justify-center",
|
||||||
|
direction === "rtl" && isDoublePage && "justify-start"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<ImageLoader isLoading={isLoading} />
|
<ImageLoader isLoading={isLoading} />
|
||||||
@@ -235,7 +249,12 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
|
|
||||||
{/* Deuxième page en mode double page */}
|
{/* Deuxième page en mode double page */}
|
||||||
{isDoublePage && shouldShowDoublePage(currentPage) && (
|
{isDoublePage && shouldShowDoublePage(currentPage) && (
|
||||||
<div className="relative h-full w-1/2 flex items-center justify-start">
|
<div
|
||||||
|
className={cn(
|
||||||
|
"relative h-full w-1/2 flex items-center",
|
||||||
|
direction === "rtl" ? "justify-end" : "justify-start"
|
||||||
|
)}
|
||||||
|
>
|
||||||
<ImageLoader isLoading={secondPageLoading} />
|
<ImageLoader isLoading={secondPageLoading} />
|
||||||
{nextPageUrl && (
|
{nextPageUrl && (
|
||||||
<img
|
<img
|
||||||
@@ -252,15 +271,16 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<NavigationBar
|
{/* Barre de navigation */}
|
||||||
currentPage={currentPage}
|
<NavigationBar
|
||||||
pages={pages}
|
currentPage={currentPage}
|
||||||
onPageChange={navigateToPage}
|
pages={pages}
|
||||||
showControls={showControls}
|
onPageChange={navigateToPage}
|
||||||
book={book}
|
showControls={showControls}
|
||||||
/>
|
book={book}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
LayoutTemplate,
|
LayoutTemplate,
|
||||||
Maximize2,
|
Maximize2,
|
||||||
Minimize2,
|
Minimize2,
|
||||||
|
ArrowLeftRight,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
@@ -22,6 +23,8 @@ export const ControlButtons = ({
|
|||||||
onToggleDoublePage,
|
onToggleDoublePage,
|
||||||
isFullscreen,
|
isFullscreen,
|
||||||
onToggleFullscreen,
|
onToggleFullscreen,
|
||||||
|
direction,
|
||||||
|
onToggleDirection,
|
||||||
}: ControlButtonsProps) => {
|
}: ControlButtonsProps) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -52,6 +55,20 @@ export const ControlButtons = ({
|
|||||||
<SplitSquareVertical className="h-6 w-6" />
|
<SplitSquareVertical className="h-6 w-6" />
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
onToggleDirection();
|
||||||
|
}}
|
||||||
|
className="p-2 rounded-full bg-background/50 hover:bg-background/80 transition-colors"
|
||||||
|
aria-label={`Changer le sens de lecture (actuellement de ${
|
||||||
|
direction === "ltr" ? "gauche à droite" : "droite à gauche"
|
||||||
|
})`}
|
||||||
|
>
|
||||||
|
<ArrowLeftRight
|
||||||
|
className={cn("h-6 w-6 transition-transform", direction === "rtl" && "rotate-180")}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
@@ -89,7 +106,8 @@ export const ControlButtons = ({
|
|||||||
onPreviousPage();
|
onPreviousPage();
|
||||||
}}
|
}}
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute left-4 top-1/2 -translate-y-1/2 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-all duration-300 z-20",
|
"absolute top-1/2 -translate-y-1/2 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-all duration-300 z-20",
|
||||||
|
direction === "rtl" ? "right-4" : "left-4",
|
||||||
showControls ? "opacity-100" : "opacity-0 pointer-events-none"
|
showControls ? "opacity-100" : "opacity-0 pointer-events-none"
|
||||||
)}
|
)}
|
||||||
aria-label="Page précédente"
|
aria-label="Page précédente"
|
||||||
@@ -106,7 +124,8 @@ export const ControlButtons = ({
|
|||||||
onNextPage();
|
onNextPage();
|
||||||
}}
|
}}
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute right-4 top-1/2 -translate-y-1/2 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-all duration-300 z-20",
|
"absolute top-1/2 -translate-y-1/2 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-all duration-300 z-20",
|
||||||
|
direction === "rtl" ? "left-4" : "right-4",
|
||||||
showControls ? "opacity-100" : "opacity-0 pointer-events-none"
|
showControls ? "opacity-100" : "opacity-0 pointer-events-none"
|
||||||
)}
|
)}
|
||||||
aria-label="Page suivante"
|
aria-label="Page suivante"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ interface UsePageNavigationProps {
|
|||||||
pages: number[];
|
pages: number[];
|
||||||
isDoublePage: boolean;
|
isDoublePage: boolean;
|
||||||
onClose?: () => void;
|
onClose?: () => void;
|
||||||
|
direction: "ltr" | "rtl";
|
||||||
}
|
}
|
||||||
|
|
||||||
export const usePageNavigation = ({
|
export const usePageNavigation = ({
|
||||||
@@ -13,6 +14,7 @@ export const usePageNavigation = ({
|
|||||||
pages,
|
pages,
|
||||||
isDoublePage,
|
isDoublePage,
|
||||||
onClose,
|
onClose,
|
||||||
|
direction,
|
||||||
}: UsePageNavigationProps) => {
|
}: UsePageNavigationProps) => {
|
||||||
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);
|
||||||
@@ -21,6 +23,7 @@ export const usePageNavigation = ({
|
|||||||
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 currentPageRef = useRef(currentPage);
|
const currentPageRef = useRef(currentPage);
|
||||||
|
const isRTL = direction === "rtl";
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
currentPageRef.current = currentPage;
|
currentPageRef.current = currentPage;
|
||||||
@@ -74,32 +77,40 @@ export const usePageNavigation = ({
|
|||||||
|
|
||||||
const navigateToPage = useCallback(
|
const navigateToPage = useCallback(
|
||||||
(page: number) => {
|
(page: number) => {
|
||||||
|
// if (page >= 1 && page <= pages.length) {
|
||||||
setCurrentPage(page);
|
setCurrentPage(page);
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setSecondPageLoading(true);
|
setSecondPageLoading(true);
|
||||||
debouncedSyncReadProgress(page);
|
debouncedSyncReadProgress(page);
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
[debouncedSyncReadProgress]
|
[debouncedSyncReadProgress]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handlePreviousPage = useCallback(() => {
|
const handlePreviousPage = useCallback(() => {
|
||||||
if (currentPage > 1) {
|
if (isDoublePage && shouldShowDoublePage(currentPage - 2)) {
|
||||||
const newPage = isDoublePage && currentPage > 2 ? currentPage - 2 : currentPage - 1;
|
navigateToPage(Math.max(1, currentPage - 2));
|
||||||
navigateToPage(newPage);
|
} else {
|
||||||
|
navigateToPage(Math.max(1, currentPage - 1));
|
||||||
}
|
}
|
||||||
}, [currentPage, isDoublePage, navigateToPage]);
|
}, [currentPage, isDoublePage, navigateToPage, shouldShowDoublePage]);
|
||||||
|
|
||||||
const handleNextPage = useCallback(() => {
|
const handleNextPage = useCallback(() => {
|
||||||
if (currentPage < pages.length) {
|
if (isDoublePage && shouldShowDoublePage(currentPage)) {
|
||||||
const newPage = isDoublePage ? Math.min(currentPage + 2, pages.length) : currentPage + 1;
|
navigateToPage(Math.min(pages.length, currentPage + 2));
|
||||||
navigateToPage(newPage);
|
} else {
|
||||||
|
navigateToPage(Math.min(pages.length, currentPage + 1));
|
||||||
}
|
}
|
||||||
}, [currentPage, pages.length, isDoublePage, navigateToPage]);
|
}, [currentPage, isDoublePage, navigateToPage, pages.length, shouldShowDoublePage]);
|
||||||
|
|
||||||
const handleTouchStart = useCallback((event: TouchEvent) => {
|
const handleTouchStart = useCallback(
|
||||||
touchStartXRef.current = event.touches[0].clientX;
|
(event: TouchEvent) => {
|
||||||
touchStartYRef.current = event.touches[0].clientY;
|
touchStartXRef.current = event.touches[0].clientX;
|
||||||
}, []);
|
touchStartYRef.current = event.touches[0].clientY;
|
||||||
|
currentPageRef.current = currentPage;
|
||||||
|
},
|
||||||
|
[currentPage]
|
||||||
|
);
|
||||||
|
|
||||||
const handleTouchEnd = useCallback(
|
const handleTouchEnd = useCallback(
|
||||||
(event: TouchEvent) => {
|
(event: TouchEvent) => {
|
||||||
@@ -108,23 +119,35 @@ export const usePageNavigation = ({
|
|||||||
const touchEndX = event.changedTouches[0].clientX;
|
const touchEndX = event.changedTouches[0].clientX;
|
||||||
const touchEndY = event.changedTouches[0].clientY;
|
const touchEndY = event.changedTouches[0].clientY;
|
||||||
const deltaX = touchEndX - touchStartXRef.current;
|
const deltaX = touchEndX - touchStartXRef.current;
|
||||||
const deltaY = Math.abs(touchEndY - touchStartYRef.current);
|
const deltaY = touchEndY - touchStartYRef.current;
|
||||||
const minSwipeDistance = 50;
|
|
||||||
|
|
||||||
if (deltaY > Math.abs(deltaX)) return;
|
// Si le déplacement vertical est plus important que le déplacement horizontal,
|
||||||
|
// on ne fait rien (pour éviter de confondre avec un scroll)
|
||||||
|
if (Math.abs(deltaY) > Math.abs(deltaX)) return;
|
||||||
|
|
||||||
if (Math.abs(deltaX) > minSwipeDistance) {
|
// On vérifie si le déplacement est suffisant pour changer de page
|
||||||
|
if (Math.abs(deltaX) > 50) {
|
||||||
if (deltaX > 0) {
|
if (deltaX > 0) {
|
||||||
handlePreviousPage();
|
// Swipe vers la droite
|
||||||
|
if (isRTL) {
|
||||||
|
handleNextPage();
|
||||||
|
} else {
|
||||||
|
handlePreviousPage();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
handleNextPage();
|
// Swipe vers la gauche
|
||||||
|
if (isRTL) {
|
||||||
|
handlePreviousPage();
|
||||||
|
} else {
|
||||||
|
handleNextPage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
touchStartXRef.current = null;
|
touchStartXRef.current = null;
|
||||||
touchStartYRef.current = null;
|
touchStartYRef.current = null;
|
||||||
},
|
},
|
||||||
[handlePreviousPage, handleNextPage]
|
[handleNextPage, handlePreviousPage, isRTL]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -133,12 +156,23 @@ export const usePageNavigation = ({
|
|||||||
}, [isDoublePage]);
|
}, [isDoublePage]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (event: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
if (event.key === "ArrowLeft") {
|
if (e.key === "ArrowLeft") {
|
||||||
handlePreviousPage();
|
e.preventDefault();
|
||||||
} else if (event.key === "ArrowRight") {
|
if (isRTL) {
|
||||||
handleNextPage();
|
handleNextPage();
|
||||||
} else if (event.key === "Escape" && onClose) {
|
} else {
|
||||||
|
handlePreviousPage();
|
||||||
|
}
|
||||||
|
} else if (e.key === "ArrowRight") {
|
||||||
|
e.preventDefault();
|
||||||
|
if (isRTL) {
|
||||||
|
handlePreviousPage();
|
||||||
|
} else {
|
||||||
|
handleNextPage();
|
||||||
|
}
|
||||||
|
} else if (e.key === "Escape" && onClose) {
|
||||||
|
e.preventDefault();
|
||||||
onClose();
|
onClose();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -152,7 +186,7 @@ export const usePageNavigation = ({
|
|||||||
window.removeEventListener("touchstart", handleTouchStart);
|
window.removeEventListener("touchstart", handleTouchStart);
|
||||||
window.removeEventListener("touchend", handleTouchEnd);
|
window.removeEventListener("touchend", handleTouchEnd);
|
||||||
};
|
};
|
||||||
}, [handlePreviousPage, handleNextPage, handleTouchStart, handleTouchEnd, onClose]);
|
}, [handleNextPage, handlePreviousPage, handleTouchStart, handleTouchEnd, onClose, isRTL]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
|
|||||||
28
src/components/reader/hooks/useReadingDirection.ts
Normal file
28
src/components/reader/hooks/useReadingDirection.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
|
||||||
|
type ReadingDirection = "ltr" | "rtl";
|
||||||
|
|
||||||
|
export const useReadingDirection = () => {
|
||||||
|
const [direction, setDirection] = useState<ReadingDirection>(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
const savedDirection = localStorage.getItem("reading-direction") as ReadingDirection;
|
||||||
|
return savedDirection === "rtl" ? "rtl" : "ltr";
|
||||||
|
}
|
||||||
|
return "ltr";
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem("reading-direction", direction);
|
||||||
|
}, [direction]);
|
||||||
|
|
||||||
|
const toggleDirection = () => {
|
||||||
|
setDirection((prev) => (prev === "ltr" ? "rtl" : "ltr"));
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
direction,
|
||||||
|
setDirection,
|
||||||
|
toggleDirection,
|
||||||
|
isRTL: direction === "rtl",
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -45,6 +45,8 @@ export interface ControlButtonsProps {
|
|||||||
onToggleDoublePage: () => void;
|
onToggleDoublePage: () => void;
|
||||||
isFullscreen: boolean;
|
isFullscreen: boolean;
|
||||||
onToggleFullscreen: () => void;
|
onToggleFullscreen: () => void;
|
||||||
|
direction: "ltr" | "rtl";
|
||||||
|
onToggleDirection: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UsePageNavigationProps {
|
export interface UsePageNavigationProps {
|
||||||
@@ -52,4 +54,5 @@ export interface UsePageNavigationProps {
|
|||||||
pages: number[];
|
pages: number[];
|
||||||
isDoublePage: boolean;
|
isDoublePage: boolean;
|
||||||
onClose?: () => void;
|
onClose?: () => void;
|
||||||
|
direction: "ltr" | "rtl";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user