|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import { useEffect, useRef, useCallback, useState } from 'react'; |
| 3 | +import { useEffect, useRef, useCallback, useState, useMemo } from 'react'; |
4 | 4 | import { useParams } from 'next/navigation'; |
5 | 5 | import dynamic from 'next/dynamic'; |
6 | 6 | import { useEPUB } from '@/contexts/EPUBContext'; |
@@ -139,12 +139,40 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) { |
139 | 139 | } |
140 | 140 | }, [id, setEPUBPageInChapter, extractPageText]); |
141 | 141 |
|
142 | | - // Load the initial location |
| 142 | + // Replace the debounced text extraction with a proper implementation using useMemo |
| 143 | + const debouncedExtractText = useMemo(() => { |
| 144 | + let timeout: NodeJS.Timeout; |
| 145 | + return (book: Book, rendition: Rendition) => { |
| 146 | + clearTimeout(timeout); |
| 147 | + timeout = setTimeout(() => { |
| 148 | + extractPageText(book, rendition); |
| 149 | + }, 150); |
| 150 | + }; |
| 151 | + }, [extractPageText]); |
| 152 | + |
| 153 | + // Load the initial location and setup resize handler |
143 | 154 | useEffect(() => { |
144 | 155 | if (bookRef.current && rendition.current) { |
145 | 156 | extractPageText(bookRef.current, rendition.current); |
| 157 | + |
| 158 | + // Add resize observer |
| 159 | + const resizeObserver = new ResizeObserver(() => { |
| 160 | + if (bookRef.current && rendition.current) { |
| 161 | + debouncedExtractText(bookRef.current, rendition.current); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + // Observe the container element |
| 166 | + const container = document.querySelector('.epub-container'); |
| 167 | + if (container) { |
| 168 | + resizeObserver.observe(container); |
| 169 | + } |
| 170 | + |
| 171 | + return () => { |
| 172 | + resizeObserver.disconnect(); |
| 173 | + }; |
146 | 174 | } |
147 | | - }, [extractPageText, initialPrevLocLoad]); |
| 175 | + }, [extractPageText, debouncedExtractText, initialPrevLocLoad]); |
148 | 176 |
|
149 | 177 | const updateTheme = useCallback((rendition: Rendition) => { |
150 | 178 | if (!epubTheme) return; // Only apply theme if enabled |
|
0 commit comments