From 039ac1fe618494563d1513d99d55023cef61b06d Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Mon, 10 Feb 2025 19:57:33 -0700 Subject: [PATCH] Fix epub last location first page --- src/components/EPUBViewer.tsx | 28 ++++++++++++---------------- src/contexts/TTSContext.tsx | 9 +++++---- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/components/EPUBViewer.tsx b/src/components/EPUBViewer.tsx index 58440f2..d70a22b 100644 --- a/src/components/EPUBViewer.tsx +++ b/src/components/EPUBViewer.tsx @@ -28,20 +28,7 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) { const toc = useRef([]); const locationRef = useRef(currDocPage); - // Load the last location when component mounts - // useEffect(() => { - // const loadLastLocation = async () => { - // if (id) { - // const lastLocation = await getLastDocumentLocation(id as string); - // if (lastLocation) { - // locationRef.current = lastLocation; - // } - // } - // }; - // loadLastLocation(); - // }, [id]); - - const handleLocationChanged = useCallback((location: string | number) => { + const handleLocationChanged = useCallback((location: string | number, initial = false) => { // Handle special 'next' and 'prev' cases if (location === 'next' && rendition.current) { rendition.current.next(); @@ -58,7 +45,6 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) { console.log('Displayed:', displayed, 'Chapter:', chapter); - if (locationRef.current !== 1) { // Save the location to IndexedDB if (id) { @@ -70,7 +56,17 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) { locationRef.current = location; setEPUBPageInChapter(displayed.page, displayed.total, chapter?.label || ''); - extractPageText(bookRef.current, rendition.current); + + // Add a small delay for initial load to ensure rendition is ready + if (initial) { + setTimeout(() => { + if (bookRef.current && rendition.current) { + extractPageText(bookRef.current, rendition.current); + } + }, 100); + } else { + extractPageText(bookRef.current, rendition.current); + } } }, [id, setEPUBPageInChapter, extractPageText]); diff --git a/src/contexts/TTSContext.tsx b/src/contexts/TTSContext.tsx index e587d30..c69d1a4 100644 --- a/src/contexts/TTSContext.tsx +++ b/src/contexts/TTSContext.tsx @@ -73,7 +73,7 @@ interface TTSContextType { setVoiceAndRestart: (voice: string) => void; skipToPage: (page: number) => void; setEPUBPageInChapter: (page: string | number, total: number, chapter: string | number) => void; // Add this line - registerLocationChangeHandler: (handler: (location: string | number) => void) => void; + registerLocationChangeHandler: (handler: (location: string | number, initial?: boolean) => void) => void; } // Create the context @@ -106,10 +106,10 @@ export function TTSProvider({ children }: { children: React.ReactNode }) { const { availableVoices, fetchVoices } = useVoiceManagement(openApiKey, openApiBaseUrl); // Add ref for location change handler - const locationChangeHandlerRef = useRef<((location: string | number) => void) | null>(null); + const locationChangeHandlerRef = useRef<((location: string | number, initial?: boolean) => void) | null>(null); // Add method to register location change handler - const registerLocationChangeHandler = useCallback((handler: (location: string | number) => void) => { + const registerLocationChangeHandler = useCallback((handler: (location: string | number, initial?: boolean) => void) => { locationChangeHandlerRef.current = handler; }, []); @@ -695,9 +695,10 @@ export function TTSProvider({ children }: { children: React.ReactNode }) { if (id && isEPUB) { getLastDocumentLocation(id as string).then(lastLocation => { if (lastLocation) { + console.log('Setting last location:', lastLocation); setCurrDocPage(lastLocation); if (locationChangeHandlerRef.current) { - locationChangeHandlerRef.current(lastLocation); + locationChangeHandlerRef.current(lastLocation, true); } } });