11'use client' ;
22
3- import { useEffect , useRef , useCallback , useState , useMemo } from 'react' ;
3+ import { useEffect , useRef , useCallback , useMemo } from 'react' ;
44import { useParams } from 'next/navigation' ;
55import dynamic from 'next/dynamic' ;
66import { useEPUB } from '@/contexts/EPUBContext' ;
@@ -24,18 +24,29 @@ interface EPUBViewerProps {
2424export function EPUBViewer ( { className = '' } : EPUBViewerProps ) {
2525 const { id } = useParams ( ) ;
2626 const { currDocData, currDocName, currDocPage, extractPageText } = useEPUB ( ) ;
27- const { setEPUBPageInChapter , registerLocationChangeHandler } = useTTS ( ) ;
27+ const { skipToLocation , registerLocationChangeHandler, setIsEPUB } = useTTS ( ) ;
2828 const { epubTheme } = useConfig ( ) ;
2929 const bookRef = useRef < Book | null > ( null ) ;
3030 const rendition = useRef < Rendition | undefined > ( undefined ) ;
3131 const toc = useRef < NavItem [ ] > ( [ ] ) ;
3232 const locationRef = useRef < string | number > ( currDocPage ) ;
33- const [ initialPrevLocLoad , setInitialPrevLocLoad ] = useState ( false ) ;
3433 const { updateTheme } = useEPUBTheme ( epubTheme , rendition . current ) ;
3534
36- const handleLocationChanged = useCallback ( ( location : string | number , initial = false ) => {
35+ const isEPUBSetOnce = useRef ( false ) ;
36+ const handleLocationChanged = useCallback ( ( location : string | number ) => {
37+ // Set the EPUB flag once the location changes
38+ if ( ! isEPUBSetOnce . current ) {
39+ setIsEPUB ( true ) ;
40+ isEPUBSetOnce . current = true ;
41+
42+ rendition . current ?. display ( location . toString ( ) ) ;
43+
44+ return ;
45+ }
46+
3747 if ( ! bookRef . current ?. isOpen || ! rendition . current ) return ;
38- // Handle special 'next' and 'prev' cases, which
48+
49+ // Handle special 'next' and 'prev' cases
3950 if ( location === 'next' && rendition . current ) {
4051 rendition . current . next ( ) ;
4152 return ;
@@ -45,33 +56,18 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
4556 return ;
4657 }
4758
48-
49- const { displayed, href } = rendition . current . location . start ;
50- const chapter = toc . current . find ( ( item ) => item . href === href ) ;
51-
52- console . log ( 'Displayed:' , displayed , 'Chapter:' , chapter ) ;
53-
54- if ( locationRef . current !== 1 ) {
55- // Save the location to IndexedDB
56- if ( id ) {
57- console . log ( 'Saving location:' , location ) ;
58- setLastDocumentLocation ( id as string , location . toString ( ) ) ;
59- }
59+ // Save the location to IndexedDB if not initial
60+ if ( id && locationRef . current !== 1 ) {
61+ console . log ( 'Saving location:' , location ) ;
62+ setLastDocumentLocation ( id as string , location . toString ( ) ) ;
6063 }
6164
62- setEPUBPageInChapter ( displayed . page , displayed . total , chapter ?. label || '' ) ;
65+ skipToLocation ( location ) ;
6366
64- // Add a small delay for initial load to ensure rendition is ready
65- if ( initial ) {
66- rendition . current . display ( location . toString ( ) ) . then ( ( ) => {
67- setInitialPrevLocLoad ( true ) ;
68- } ) ;
69- } else {
70- locationRef . current = location ;
71- extractPageText ( bookRef . current , rendition . current ) ;
72- }
67+ locationRef . current = location ;
68+ extractPageText ( bookRef . current , rendition . current ) ;
7369
74- } , [ id , setEPUBPageInChapter , extractPageText ] ) ;
70+ } , [ id , skipToLocation , extractPageText , setIsEPUB ] ) ;
7571
7672 // Replace the debounced text extraction with a proper implementation using useMemo
7773 const debouncedExtractText = useMemo ( ( ) => {
@@ -86,27 +82,27 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
8682
8783 // Load the initial location and setup resize handler
8884 useEffect ( ( ) => {
89- if ( bookRef . current && rendition . current ) {
90- extractPageText ( bookRef . current , rendition . current ) ;
91-
92- // Add resize observer
93- const resizeObserver = new ResizeObserver ( ( ) => {
94- if ( bookRef . current && rendition . current ) {
95- debouncedExtractText ( bookRef . current , rendition . current ) ;
96- }
97- } ) ;
98-
99- // Observe the container element
100- const container = document . querySelector ( '.epub-container' ) ;
101- if ( container ) {
102- resizeObserver . observe ( container ) ;
85+ if ( ! bookRef . current || ! rendition . current || isEPUBSetOnce . current ) return ;
86+
87+ extractPageText ( bookRef . current , rendition . current ) ;
88+
89+ // Add resize observer
90+ const resizeObserver = new ResizeObserver ( ( ) => {
91+ if ( bookRef . current && rendition . current ) {
92+ debouncedExtractText ( bookRef . current , rendition . current ) ;
10393 }
94+ } ) ;
10495
105- return ( ) => {
106- resizeObserver . disconnect ( ) ;
107- } ;
96+ // Observe the container element
97+ const container = document . querySelector ( '.epub-container' ) ;
98+ if ( container ) {
99+ resizeObserver . observe ( container ) ;
108100 }
109- } , [ extractPageText , debouncedExtractText , initialPrevLocLoad ] ) ;
101+
102+ return ( ) => {
103+ resizeObserver . disconnect ( ) ;
104+ } ;
105+ } , [ extractPageText , debouncedExtractText ] ) ;
110106
111107 // Register the location change handler
112108 useEffect ( ( ) => {
0 commit comments