From a3137ed88fd5f0cad1a1a9e89b74a3bb9f741c80 Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Sat, 8 Feb 2025 20:35:38 -0700 Subject: [PATCH] Manual page turn - kind of working --- src/components/EPUBViewer.tsx | 56 +++++++++++++++++++++++------ src/components/player/TTSPlayer.tsx | 7 ++-- src/contexts/EPUBContext.tsx | 47 ++++++++++++++++-------- src/contexts/TTSContext.tsx | 19 ++++++++++ 4 files changed, 101 insertions(+), 28 deletions(-) diff --git a/src/components/EPUBViewer.tsx b/src/components/EPUBViewer.tsx index 428a09c..c0eddc2 100644 --- a/src/components/EPUBViewer.tsx +++ b/src/components/EPUBViewer.tsx @@ -1,9 +1,12 @@ 'use client'; -import { useState } from 'react'; +import { useState, useEffect, useRef } from 'react'; import dynamic from 'next/dynamic'; import { useEPUB } from '@/contexts/EPUBContext'; +import { useTTS } from '@/contexts/TTSContext'; import { DocumentSkeleton } from '@/components/DocumentSkeleton'; +import TTSPlayer from '@/components/player/TTSPlayer'; +import type { Rendition, Book, NavItem } from 'epubjs'; const ReactReader = dynamic(() => import('react-reader').then(mod => mod.ReactReader), { ssr: false, @@ -15,22 +18,53 @@ interface EPUBViewerProps { } export function EPUBViewer({ className = '' }: EPUBViewerProps) { - const { currDocData, currDocName } = useEPUB(); // Changed from currDocURL - const [location, setLocation] = useState(0); + const { currDocData, currDocName, currDocPage, currDocPages, extractPageText } = useEPUB(); + const { skipToLocation } = useTTS(); + const bookRef = useRef(null); + const rendition = useRef(undefined); + const toc = useRef([]); + const locationRef = useRef(currDocPage); + + const handleLocationChanged = async (location: string | number) => { + if (bookRef.current && rendition.current) { + const { displayed, href } = rendition.current.location.start + const chapter = toc.current.find((item) => item.href === href) + + console.log('Displayed:', displayed, 'Chapter:', chapter); + + // Update the current location + locationRef.current = location; + // Skip to the current location in the TTS + skipToLocation(displayed.page, displayed.total); + + // Extract text using the current rendition + await extractPageText(bookRef.current, rendition.current); + } + }; if (!currDocData) { return ; } return ( -
- +
+
+ +
+
+ (toc.current = _toc)} + showToc={true} + getRendition={(_rendition: Rendition) => { + bookRef.current = _rendition.book; + rendition.current = _rendition; + }} + /> +
); } \ No newline at end of file diff --git a/src/components/player/TTSPlayer.tsx b/src/components/player/TTSPlayer.tsx index ffea7f7..5909575 100644 --- a/src/components/player/TTSPlayer.tsx +++ b/src/components/player/TTSPlayer.tsx @@ -14,8 +14,8 @@ import { SpeedControl } from '@/components/player/SpeedControl'; import { Navigator } from '@/components/player/Navigator'; export default function TTSPlayer({ currentPage, numPages }: { - currentPage: number; - numPages: number | undefined; + currentPage?: number; + numPages?: number | undefined; }) { const { isPlaying, @@ -36,7 +36,8 @@ export default function TTSPlayer({ currentPage, numPages }: { {/* Page Navigation */} - + {currentPage && numPages + && } {/* Playback Controls */}