@@ -25,7 +25,7 @@ interface EPUBViewerProps {
2525export function EPUBViewer ( { className = '' } : EPUBViewerProps ) {
2626 const { id } = useParams ( ) ;
2727 const { currDocData, currDocName, currDocPage, extractPageText } = useEPUB ( ) ;
28- const { skipToLocation, registerLocationChangeHandler, setIsEPUB } = useTTS ( ) ;
28+ const { skipToLocation, registerLocationChangeHandler, setIsEPUB, pause } = useTTS ( ) ;
2929 const { epubTheme } = useConfig ( ) ;
3030 const bookRef = useRef < Book | null > ( null ) ;
3131 const rendition = useRef < Rendition | undefined > ( undefined ) ;
@@ -35,9 +35,7 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
3535 const containerRef = useRef < HTMLDivElement > ( null ) ;
3636
3737 const isEPUBSetOnce = useRef ( false ) ;
38- const isResizing = useRef ( false ) ;
39-
40- useEPUBResize ( containerRef , isResizing ) ;
38+ const { isResizing, setIsResizing, dimensions } = useEPUBResize ( containerRef ) ;
4139
4240 const handleLocationChanged = useCallback ( ( location : string | number ) => {
4341 // Set the EPUB flag once the location changes
@@ -68,23 +66,45 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
6866 setLastDocumentLocation ( id as string , location . toString ( ) ) ;
6967 }
7068
71- if ( isResizing . current ) {
72- skipToLocation ( location , false ) ;
73- isResizing . current = false ;
74- } else {
75- skipToLocation ( location , true ) ;
76- }
69+ skipToLocation ( location ) ;
7770
7871 locationRef . current = location ;
7972 extractPageText ( bookRef . current , rendition . current ) ;
73+
8074 } , [ id , skipToLocation , extractPageText , setIsEPUB ] ) ;
8175
82- // Load the initial location
76+ const initialExtract = useCallback ( ( ) => {
77+ if ( ! bookRef . current || ! rendition . current ?. location || isEPUBSetOnce . current ) return ;
78+ extractPageText ( bookRef . current , rendition . current , false ) ;
79+ } , [ extractPageText ] ) ;
80+
81+ const checkResize = useCallback ( ( ) => {
82+ if ( isResizing && dimensions && bookRef . current ?. isOpen && rendition . current && isEPUBSetOnce . current ) {
83+ pause ( ) ;
84+ // Only extract text when we have dimensions, ensuring the resize is complete
85+ extractPageText ( bookRef . current , rendition . current , true ) ;
86+ setIsResizing ( false ) ;
87+
88+ return true ;
89+ } else {
90+ return false ;
91+ }
92+ } , [ isResizing , setIsResizing , dimensions , pause , extractPageText ] ) ;
93+
94+ // Check for isResizing to pause TTS and re-extract text
8395 useEffect ( ( ) => {
84- if ( ! bookRef . current || ! rendition . current || isEPUBSetOnce . current ) return ;
96+ if ( checkResize ( ) ) return ;
8597
86- extractPageText ( bookRef . current , rendition . current ) ;
87- } , [ extractPageText ] ) ;
98+ // Load initial location when not resizing
99+ initialExtract ( ) ;
100+ } , [ checkResize , initialExtract ] ) ;
101+
102+ // Load the initial location
103+ // useEffect(() => {
104+ // if (!bookRef.current || !rendition.current || isEPUBSetOnce.current) return;
105+
106+ // extractPageText(bookRef.current, rendition.current, false);
107+ // }, [extractPageText]);
88108
89109 // Register the location change handler
90110 useEffect ( ( ) => {
0 commit comments