Skip to content

Commit 039ac1f

Browse files
committed
Fix epub last location first page
1 parent e8008b6 commit 039ac1f

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/components/EPUBViewer.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/contexts/TTSContext.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ interface TTSContextType {
7373
setVoiceAndRestart: (voice: string) => void;
7474
skipToPage: (page: number) => void;
7575
setEPUBPageInChapter: (page: string | number, total: number, chapter: string | number) => void; // Add this line
76-
registerLocationChangeHandler: (handler: (location: string | number) => void) => void;
76+
registerLocationChangeHandler: (handler: (location: string | number, initial?: boolean) => void) => void;
7777
}
7878

7979
// Create the context
@@ -106,10 +106,10 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
106106
const { availableVoices, fetchVoices } = useVoiceManagement(openApiKey, openApiBaseUrl);
107107

108108
// Add ref for location change handler
109-
const locationChangeHandlerRef = useRef<((location: string | number) => void) | null>(null);
109+
const locationChangeHandlerRef = useRef<((location: string | number, initial?: boolean) => void) | null>(null);
110110

111111
// Add method to register location change handler
112-
const registerLocationChangeHandler = useCallback((handler: (location: string | number) => void) => {
112+
const registerLocationChangeHandler = useCallback((handler: (location: string | number, initial?: boolean) => void) => {
113113
locationChangeHandlerRef.current = handler;
114114
}, []);
115115

@@ -695,9 +695,10 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
695695
if (id && isEPUB) {
696696
getLastDocumentLocation(id as string).then(lastLocation => {
697697
if (lastLocation) {
698+
console.log('Setting last location:', lastLocation);
698699
setCurrDocPage(lastLocation);
699700
if (locationChangeHandlerRef.current) {
700-
locationChangeHandlerRef.current(lastLocation);
701+
locationChangeHandlerRef.current(lastLocation, true);
701702
}
702703
}
703704
});

0 commit comments

Comments
 (0)