Fix epub last location first page

This commit is contained in:
Richard Roberson 2025-02-10 19:57:33 -07:00
parent e8008b68ab
commit 039ac1fe61
2 changed files with 17 additions and 20 deletions

View file

@ -28,20 +28,7 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
const toc = useRef<NavItem[]>([]);
const locationRef = useRef<string | number>(currDocPage);
// Load the last location when component mounts
// useEffect(() => {
// const loadLastLocation = async () => {
// if (id) {
// const lastLocation = await getLastDocumentLocation(id as string);
// if (lastLocation) {
// locationRef.current = lastLocation;
// }
// }
// };
// loadLastLocation();
// }, [id]);
const handleLocationChanged = useCallback((location: string | number) => {
const handleLocationChanged = useCallback((location: string | number, initial = false) => {
// Handle special 'next' and 'prev' cases
if (location === 'next' && rendition.current) {
rendition.current.next();
@ -58,7 +45,6 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
console.log('Displayed:', displayed, 'Chapter:', chapter);
if (locationRef.current !== 1) {
// Save the location to IndexedDB
if (id) {
@ -70,7 +56,17 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
locationRef.current = location;
setEPUBPageInChapter(displayed.page, displayed.total, chapter?.label || '');
extractPageText(bookRef.current, rendition.current);
// Add a small delay for initial load to ensure rendition is ready
if (initial) {
setTimeout(() => {
if (bookRef.current && rendition.current) {
extractPageText(bookRef.current, rendition.current);
}
}, 100);
} else {
extractPageText(bookRef.current, rendition.current);
}
}
}, [id, setEPUBPageInChapter, extractPageText]);

View file

@ -73,7 +73,7 @@ interface TTSContextType {
setVoiceAndRestart: (voice: string) => void;
skipToPage: (page: number) => void;
setEPUBPageInChapter: (page: string | number, total: number, chapter: string | number) => void; // Add this line
registerLocationChangeHandler: (handler: (location: string | number) => void) => void;
registerLocationChangeHandler: (handler: (location: string | number, initial?: boolean) => void) => void;
}
// Create the context
@ -106,10 +106,10 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
const { availableVoices, fetchVoices } = useVoiceManagement(openApiKey, openApiBaseUrl);
// Add ref for location change handler
const locationChangeHandlerRef = useRef<((location: string | number) => void) | null>(null);
const locationChangeHandlerRef = useRef<((location: string | number, initial?: boolean) => void) | null>(null);
// Add method to register location change handler
const registerLocationChangeHandler = useCallback((handler: (location: string | number) => void) => {
const registerLocationChangeHandler = useCallback((handler: (location: string | number, initial?: boolean) => void) => {
locationChangeHandlerRef.current = handler;
}, []);
@ -695,9 +695,10 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
if (id && isEPUB) {
getLastDocumentLocation(id as string).then(lastLocation => {
if (lastLocation) {
console.log('Setting last location:', lastLocation);
setCurrDocPage(lastLocation);
if (locationChangeHandlerRef.current) {
locationChangeHandlerRef.current(lastLocation);
locationChangeHandlerRef.current(lastLocation, true);
}
}
});