@@ -5,84 +5,18 @@ import { useParams } from 'next/navigation';
55import dynamic from 'next/dynamic' ;
66import { useEPUB } from '@/contexts/EPUBContext' ;
77import { useTTS } from '@/contexts/TTSContext' ;
8+ import { useConfig } from '@/contexts/ConfigContext' ;
89import { DocumentSkeleton } from '@/components/DocumentSkeleton' ;
910import TTSPlayer from '@/components/player/TTSPlayer' ;
1011import { setLastDocumentLocation } from '@/utils/indexedDB' ;
1112import type { Rendition , Book , NavItem } from 'epubjs' ;
12- import { ReactReaderStyle , type IReactReaderStyle } from 'react-reader' ;
13- import { useConfig } from '@/contexts/ConfigContext' ;
13+ import { useEPUBTheme , getThemeStyles } from '@/hooks/useEPUBTheme' ;
1414
1515const ReactReader = dynamic ( ( ) => import ( 'react-reader' ) . then ( mod => mod . ReactReader ) , {
1616 ssr : false ,
1717 loading : ( ) => < DocumentSkeleton />
1818} ) ;
1919
20- const getThemeStyles = ( ) : IReactReaderStyle => {
21- const baseStyle = {
22- ...ReactReaderStyle ,
23- readerArea : {
24- ...ReactReaderStyle . readerArea ,
25- transition : undefined ,
26- }
27- } ;
28-
29- const colors = {
30- background : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--background' ) ,
31- foreground : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--foreground' ) ,
32- base : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--base' ) ,
33- offbase : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--offbase' ) ,
34- muted : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--muted' ) ,
35- } ;
36-
37- return {
38- ...baseStyle ,
39- arrow : {
40- ...baseStyle . arrow ,
41- color : colors . foreground ,
42- } ,
43- arrowHover : {
44- ...baseStyle . arrowHover ,
45- color : colors . muted ,
46- } ,
47- readerArea : {
48- ...baseStyle . readerArea ,
49- backgroundColor : colors . base ,
50- } ,
51- titleArea : {
52- ...baseStyle . titleArea ,
53- color : colors . foreground ,
54- display : 'none' ,
55- } ,
56- tocArea : {
57- ...baseStyle . tocArea ,
58- background : colors . base ,
59- } ,
60- tocButtonExpanded : {
61- ...baseStyle . tocButtonExpanded ,
62- background : colors . offbase ,
63- } ,
64- tocButtonBar : {
65- ...baseStyle . tocButtonBar ,
66- background : colors . muted ,
67- } ,
68- tocButton : {
69- ...baseStyle . tocButton ,
70- color : colors . muted ,
71- } ,
72- tocAreaButton : {
73- ...baseStyle . tocAreaButton ,
74- color : colors . muted ,
75- backgroundColor : colors . offbase ,
76- padding : '0.25rem' ,
77- paddingLeft : '0.5rem' ,
78- paddingRight : '0.5rem' ,
79- marginBottom : '0.25rem' ,
80- borderRadius : '0.25rem' ,
81- borderColor : 'transparent' ,
82- } ,
83- } ;
84- } ;
85-
8620interface EPUBViewerProps {
8721 className ?: string ;
8822}
@@ -96,11 +30,11 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
9630 const rendition = useRef < Rendition | undefined > ( undefined ) ;
9731 const toc = useRef < NavItem [ ] > ( [ ] ) ;
9832 const locationRef = useRef < string | number > ( currDocPage ) ;
99- const [ reloadKey , setReloadKey ] = useState ( 0 ) ;
10033 const [ initialPrevLocLoad , setInitialPrevLocLoad ] = useState ( false ) ;
34+ const { updateTheme } = useEPUBTheme ( epubTheme , rendition . current ) ;
10135
10236 const handleLocationChanged = useCallback ( ( location : string | number , initial = false ) => {
103- if ( ! bookRef . current ?. isOpen ) return ;
37+ if ( ! bookRef . current ?. isOpen || ! rendition . current ) return ;
10438 // Handle special 'next' and 'prev' cases, which
10539 if ( location === 'next' && rendition . current ) {
10640 rendition . current . next ( ) ;
@@ -111,32 +45,32 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
11145 return ;
11246 }
11347
114- if ( bookRef . current && rendition . current ) {
115- const { displayed, href } = rendition . current . location . start ;
116- const chapter = toc . current . find ( ( item ) => item . href === href ) ;
117-
118- console . log ( 'Displayed:' , displayed , 'Chapter:' , chapter ) ;
119-
120- if ( locationRef . current !== 1 ) {
121- // Save the location to IndexedDB
122- if ( id ) {
123- console . log ( 'Saving location:' , location ) ;
124- setLastDocumentLocation ( id as string , location . toString ( ) ) ;
125- }
126- }
127-
128- setEPUBPageInChapter ( displayed . page , displayed . total , chapter ?. label || '' ) ;
129-
130- // Add a small delay for initial load to ensure rendition is ready
131- if ( initial ) {
132- rendition . current . display ( location . toString ( ) ) . then ( ( ) => {
133- setInitialPrevLocLoad ( true ) ;
134- } ) ;
135- } else {
136- locationRef . current = location ;
137- extractPageText ( bookRef . current , rendition . current ) ;
48+
49+ const { displayed, href } = rendition . current . location . start ;
50+ const chapter = toc . current . find ( ( item ) => item . href === href ) ;
51+
52+ console . log ( 'Displayed:' , displayed , 'Chapter:' , chapter ) ;
53+
54+ if ( locationRef . current !== 1 ) {
55+ // Save the location to IndexedDB
56+ if ( id ) {
57+ console . log ( 'Saving location:' , location ) ;
58+ setLastDocumentLocation ( id as string , location . toString ( ) ) ;
13859 }
13960 }
61+
62+ setEPUBPageInChapter ( displayed . page , displayed . total , chapter ?. label || '' ) ;
63+
64+ // Add a small delay for initial load to ensure rendition is ready
65+ if ( initial ) {
66+ rendition . current . display ( location . toString ( ) ) . then ( ( ) => {
67+ setInitialPrevLocLoad ( true ) ;
68+ } ) ;
69+ } else {
70+ locationRef . current = location ;
71+ extractPageText ( bookRef . current , rendition . current ) ;
72+ }
73+
14074 } , [ id , setEPUBPageInChapter , extractPageText ] ) ;
14175
14276 // Replace the debounced text extraction with a proper implementation using useMemo
@@ -174,46 +108,6 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
174108 }
175109 } , [ extractPageText , debouncedExtractText , initialPrevLocLoad ] ) ;
176110
177- const updateTheme = useCallback ( ( rendition : Rendition ) => {
178- if ( ! epubTheme ) return ; // Only apply theme if enabled
179-
180- const colors = {
181- foreground : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--foreground' ) ,
182- base : getComputedStyle ( document . documentElement ) . getPropertyValue ( '--base' ) ,
183- } ;
184-
185- rendition . themes . override ( 'color' , colors . foreground ) ;
186- rendition . themes . override ( 'background' , colors . base ) ;
187- } , [ epubTheme ] ) ;
188-
189- // Watch for theme changes
190- useEffect ( ( ) => {
191- if ( ! epubTheme || ! bookRef . current ?. isOpen || ! rendition . current ) return ;
192-
193- const observer = new MutationObserver ( ( mutations ) => {
194- mutations . forEach ( ( mutation ) => {
195- if ( mutation . attributeName === 'class' ) {
196- if ( epubTheme ) {
197- setReloadKey ( prev => prev + 1 ) ;
198- }
199- }
200- } ) ;
201- } ) ;
202-
203- observer . observe ( document . documentElement , {
204- attributes : true ,
205- attributeFilter : [ 'class' ]
206- } ) ;
207-
208- return ( ) => observer . disconnect ( ) ;
209- } , [ epubTheme ] ) ;
210-
211- // Watch for epubTheme changes
212- useEffect ( ( ) => {
213- if ( ! epubTheme || ! bookRef . current ?. isOpen || ! rendition . current ) return ;
214- setReloadKey ( prev => prev + 1 ) ;
215- } , [ epubTheme ] ) ;
216-
217111 // Register the location change handler
218112 useEffect ( ( ) => {
219113 registerLocationChangeHandler ( handleLocationChanged ) ;
@@ -230,7 +124,7 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
230124 </ div >
231125 < div className = "flex-1 -mt-16 pt-16" >
232126 < ReactReader
233- key = { reloadKey } // Add this line to force remount
127+ key = { 'epub-reader' }
234128 location = { locationRef . current }
235129 locationChanged = { handleLocationChanged }
236130 url = { currDocData }
@@ -239,10 +133,9 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
239133 showToc = { true }
240134 readerStyles = { epubTheme && getThemeStyles ( ) || undefined }
241135 getRendition = { ( _rendition : Rendition ) => {
242- updateTheme ( _rendition ) ;
243-
244136 bookRef . current = _rendition . book ;
245137 rendition . current = _rendition ;
138+ updateTheme ( ) ;
246139 } }
247140 />
248141 </ div >
0 commit comments