Skip to content

Commit 737cf5c

Browse files
committed
Fix + debounce epub text extraction on window resize
1 parent 36fdbc2 commit 737cf5c

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/app/epub/[id]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default function EPUBPage() {
6565
<>
6666
<div className="p-2 pb-2 border-b border-offbase">
6767
<div className="flex flex-wrap items-center justify-between">
68-
<div className="flex items-center gap-2">
68+
<div className="flex items-center gap-1">
6969
<Link
7070
href="/"
7171
onClick={() => clearCurrDoc()}

src/components/EPUBViewer.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useEffect, useRef, useCallback, useState } from 'react';
3+
import { useEffect, useRef, useCallback, useState, useMemo } from 'react';
44
import { useParams } from 'next/navigation';
55
import dynamic from 'next/dynamic';
66
import { useEPUB } from '@/contexts/EPUBContext';
@@ -139,12 +139,40 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
139139
}
140140
}, [id, setEPUBPageInChapter, extractPageText]);
141141

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
143154
useEffect(() => {
144155
if (bookRef.current && rendition.current) {
145156
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+
};
146174
}
147-
}, [extractPageText, initialPrevLocLoad]);
175+
}, [extractPageText, debouncedExtractText, initialPrevLocLoad]);
148176

149177
const updateTheme = useCallback((rendition: Rendition) => {
150178
if (!epubTheme) return; // Only apply theme if enabled

src/utils/pdf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export function handleTextClick(
260260
}
261261
}
262262

263-
// Add debounce utility at the top of the file
263+
// Debounce for PDF viewer
264264
export function debounce<T extends (...args: unknown[]) => unknown>(
265265
func: T,
266266
wait: number

0 commit comments

Comments
 (0)