diff --git a/src/app/api/nlp/route.ts b/src/app/api/nlp/route.ts index b524450..95d93a1 100644 --- a/src/app/api/nlp/route.ts +++ b/src/app/api/nlp/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from 'next/server'; import nlp from 'compromise'; -const MAX_BLOCK_LENGTH = 350; +const MAX_BLOCK_LENGTH = 300; const preprocessSentenceForAudio = (text: string): string => { return text diff --git a/src/components/EPUBViewer.tsx b/src/components/EPUBViewer.tsx index 93d8019..094ee57 100644 --- a/src/components/EPUBViewer.tsx +++ b/src/components/EPUBViewer.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useEffect, useRef, useCallback, useState, useMemo } from 'react'; +import { useEffect, useRef, useCallback, useMemo } from 'react'; import { useParams } from 'next/navigation'; import dynamic from 'next/dynamic'; import { useEPUB } from '@/contexts/EPUBContext'; @@ -24,18 +24,29 @@ interface EPUBViewerProps { export function EPUBViewer({ className = '' }: EPUBViewerProps) { const { id } = useParams(); const { currDocData, currDocName, currDocPage, extractPageText } = useEPUB(); - const { setEPUBPageInChapter, registerLocationChangeHandler } = useTTS(); + const { skipToLocation, registerLocationChangeHandler, setIsEPUB } = useTTS(); const { epubTheme } = useConfig(); const bookRef = useRef(null); const rendition = useRef(undefined); const toc = useRef([]); const locationRef = useRef(currDocPage); - const [initialPrevLocLoad, setInitialPrevLocLoad] = useState(false); const { updateTheme } = useEPUBTheme(epubTheme, rendition.current); - const handleLocationChanged = useCallback((location: string | number, initial = false) => { + const isEPUBSetOnce = useRef(false); + const handleLocationChanged = useCallback((location: string | number) => { + // Set the EPUB flag once the location changes + if (!isEPUBSetOnce.current) { + setIsEPUB(true); + isEPUBSetOnce.current = true; + + rendition.current?.display(location.toString()); + + return; + } + if (!bookRef.current?.isOpen || !rendition.current) return; - // Handle special 'next' and 'prev' cases, which + + // Handle special 'next' and 'prev' cases if (location === 'next' && rendition.current) { rendition.current.next(); return; @@ -45,33 +56,18 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) { return; } - - const { displayed, href } = rendition.current.location.start; - const chapter = toc.current.find((item) => item.href === href); - - console.log('Displayed:', displayed, 'Chapter:', chapter); - - if (locationRef.current !== 1) { - // Save the location to IndexedDB - if (id) { - console.log('Saving location:', location); - setLastDocumentLocation(id as string, location.toString()); - } + // Save the location to IndexedDB if not initial + if (id && locationRef.current !== 1) { + console.log('Saving location:', location); + setLastDocumentLocation(id as string, location.toString()); } - setEPUBPageInChapter(displayed.page, displayed.total, chapter?.label || ''); + skipToLocation(location); - // Add a small delay for initial load to ensure rendition is ready - if (initial) { - rendition.current.display(location.toString()).then(() => { - setInitialPrevLocLoad(true); - }); - } else { - locationRef.current = location; - extractPageText(bookRef.current, rendition.current); - } + locationRef.current = location; + extractPageText(bookRef.current, rendition.current); - }, [id, setEPUBPageInChapter, extractPageText]); + }, [id, skipToLocation, extractPageText, setIsEPUB]); // Replace the debounced text extraction with a proper implementation using useMemo const debouncedExtractText = useMemo(() => { @@ -86,27 +82,27 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) { // Load the initial location and setup resize handler useEffect(() => { - if (bookRef.current && rendition.current) { - extractPageText(bookRef.current, rendition.current); + if (!bookRef.current || !rendition.current || isEPUBSetOnce.current) return; - // Add resize observer - const resizeObserver = new ResizeObserver(() => { - if (bookRef.current && rendition.current) { - debouncedExtractText(bookRef.current, rendition.current); - } - }); + extractPageText(bookRef.current, rendition.current); - // Observe the container element - const container = document.querySelector('.epub-container'); - if (container) { - resizeObserver.observe(container); + // Add resize observer + const resizeObserver = new ResizeObserver(() => { + if (bookRef.current && rendition.current) { + debouncedExtractText(bookRef.current, rendition.current); } + }); - return () => { - resizeObserver.disconnect(); - }; + // Observe the container element + const container = document.querySelector('.epub-container'); + if (container) { + resizeObserver.observe(container); } - }, [extractPageText, debouncedExtractText, initialPrevLocLoad]); + + return () => { + resizeObserver.disconnect(); + }; + }, [extractPageText, debouncedExtractText]); // Register the location change handler useEffect(() => { diff --git a/src/components/PDFViewer.tsx b/src/components/PDFViewer.tsx index 2cb8f22..9e995d0 100644 --- a/src/components/PDFViewer.tsx +++ b/src/components/PDFViewer.tsx @@ -27,7 +27,7 @@ export function PDFViewer({ zoomLevel }: PDFViewerProps) { const { currentSentence, stopAndPlayFromIndex, - isProcessing + isProcessing, } = useTTS(); // PDF context diff --git a/src/components/player/Navigator.tsx b/src/components/player/Navigator.tsx index 3584bd2..103b9ef 100644 --- a/src/components/player/Navigator.tsx +++ b/src/components/player/Navigator.tsx @@ -2,16 +2,16 @@ import { Button } from '@headlessui/react'; -export const Navigator = ({ currentPage, numPages, skipToPage }: { +export const Navigator = ({ currentPage, numPages, skipToLocation }: { currentPage: number; numPages: number | undefined; - skipToPage: (page: number) => void; + skipToLocation: (location: string | number) => void; }) => { return (
{/* Page back */}