From 7ba807fd95e0a44df67c71ae17b2eb41328dc19e Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Sun, 23 Mar 2025 17:18:21 -0600 Subject: [PATCH] Pause on page changes + Fix ePub bug when reloading --- src/contexts/EPUBContext.tsx | 12 +++++++++++- src/contexts/PDFContext.tsx | 2 +- src/contexts/TTSContext.tsx | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/contexts/EPUBContext.tsx b/src/contexts/EPUBContext.tsx index d88113e..a7b5a6c 100644 --- a/src/contexts/EPUBContext.tsx +++ b/src/contexts/EPUBContext.tsx @@ -71,6 +71,8 @@ export function EPUBProvider({ children }: { children: ReactNode }) { const tocRef = useRef([]); const locationRef = useRef(currDocPage); const isEPUBSetOnce = useRef(false); + // Should pause ref + const shouldPauseRef = useRef(true); /** * Clears all current document state and stops any active TTS @@ -80,6 +82,11 @@ export function EPUBProvider({ children }: { children: ReactNode }) { setCurrDocName(undefined); setCurrDocText(undefined); setCurrDocPages(undefined); + isEPUBSetOnce.current = false; + bookRef.current = null; + renditionRef.current = undefined; + locationRef.current = 1; + tocRef.current = []; stop(); }, [setCurrDocPages, stop]); @@ -341,10 +348,12 @@ export function EPUBProvider({ children }: { children: ReactNode }) { // Handle special 'next' and 'prev' cases if (location === 'next' && renditionRef.current) { + shouldPauseRef.current = false; renditionRef.current.next(); return; } if (location === 'prev' && renditionRef.current) { + shouldPauseRef.current = false; renditionRef.current.prev(); return; } @@ -359,7 +368,8 @@ export function EPUBProvider({ children }: { children: ReactNode }) { locationRef.current = location; if (bookRef.current && renditionRef.current) { - extractPageText(bookRef.current, renditionRef.current); + extractPageText(bookRef.current, renditionRef.current, shouldPauseRef.current); + shouldPauseRef.current = true; } }, [id, skipToLocation, extractPageText, setIsEPUB]); diff --git a/src/contexts/PDFContext.tsx b/src/contexts/PDFContext.tsx index 96cad55..33ea8ec 100644 --- a/src/contexts/PDFContext.tsx +++ b/src/contexts/PDFContext.tsx @@ -134,7 +134,7 @@ export function PDFProvider({ children }: { children: ReactNode }) { // This prevents unnecessary resets of the sentence index if (text !== currDocText || text === '') { setCurrDocText(text); - setTTSText(text); + setTTSText(text, true); } } catch (error) { console.error('Error loading PDF text:', error); diff --git a/src/contexts/TTSContext.tsx b/src/contexts/TTSContext.tsx index dc400c9..2223fd6 100644 --- a/src/contexts/TTSContext.tsx +++ b/src/contexts/TTSContext.tsx @@ -222,6 +222,7 @@ export function TTSProvider({ children }: { children: ReactNode }) { // Handle within current page bounds if (nextIndex < sentences.length && nextIndex >= 0) { + console.log('isEPUB', isEPUB!); setCurrentIndex(nextIndex); return; }