@@ -28,20 +28,7 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
2828 const toc = useRef < NavItem [ ] > ( [ ] ) ;
2929 const locationRef = useRef < string | number > ( currDocPage ) ;
3030
31- // Load the last location when component mounts
32- // useEffect(() => {
33- // const loadLastLocation = async () => {
34- // if (id) {
35- // const lastLocation = await getLastDocumentLocation(id as string);
36- // if (lastLocation) {
37- // locationRef.current = lastLocation;
38- // }
39- // }
40- // };
41- // loadLastLocation();
42- // }, [id]);
43-
44- const handleLocationChanged = useCallback ( ( location : string | number ) => {
31+ const handleLocationChanged = useCallback ( ( location : string | number , initial = false ) => {
4532 // Handle special 'next' and 'prev' cases
4633 if ( location === 'next' && rendition . current ) {
4734 rendition . current . next ( ) ;
@@ -58,7 +45,6 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
5845
5946 console . log ( 'Displayed:' , displayed , 'Chapter:' , chapter ) ;
6047
61-
6248 if ( locationRef . current !== 1 ) {
6349 // Save the location to IndexedDB
6450 if ( id ) {
@@ -70,7 +56,17 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
7056 locationRef . current = location ;
7157
7258 setEPUBPageInChapter ( displayed . page , displayed . total , chapter ?. label || '' ) ;
73- extractPageText ( bookRef . current , rendition . current ) ;
59+
60+ // Add a small delay for initial load to ensure rendition is ready
61+ if ( initial ) {
62+ setTimeout ( ( ) => {
63+ if ( bookRef . current && rendition . current ) {
64+ extractPageText ( bookRef . current , rendition . current ) ;
65+ }
66+ } , 100 ) ;
67+ } else {
68+ extractPageText ( bookRef . current , rendition . current ) ;
69+ }
7470 }
7571 } , [ id , setEPUBPageInChapter , extractPageText ] ) ;
7672
0 commit comments