Fix + debounce epub text extraction on window resize
This commit is contained in:
parent
36fdbc276d
commit
737cf5c9f5
3 changed files with 33 additions and 5 deletions
|
|
@ -65,7 +65,7 @@ export default function EPUBPage() {
|
||||||
<>
|
<>
|
||||||
<div className="p-2 pb-2 border-b border-offbase">
|
<div className="p-2 pb-2 border-b border-offbase">
|
||||||
<div className="flex flex-wrap items-center justify-between">
|
<div className="flex flex-wrap items-center justify-between">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-1">
|
||||||
<Link
|
<Link
|
||||||
href="/"
|
href="/"
|
||||||
onClick={() => clearCurrDoc()}
|
onClick={() => clearCurrDoc()}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useRef, useCallback, useState } from 'react';
|
import { useEffect, useRef, useCallback, useState, useMemo } from 'react';
|
||||||
import { useParams } from 'next/navigation';
|
import { useParams } from 'next/navigation';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
import { useEPUB } from '@/contexts/EPUBContext';
|
import { useEPUB } from '@/contexts/EPUBContext';
|
||||||
|
|
@ -139,12 +139,40 @@ export function EPUBViewer({ className = '' }: EPUBViewerProps) {
|
||||||
}
|
}
|
||||||
}, [id, setEPUBPageInChapter, extractPageText]);
|
}, [id, setEPUBPageInChapter, extractPageText]);
|
||||||
|
|
||||||
// Load the initial location
|
// Replace the debounced text extraction with a proper implementation using useMemo
|
||||||
|
const debouncedExtractText = useMemo(() => {
|
||||||
|
let timeout: NodeJS.Timeout;
|
||||||
|
return (book: Book, rendition: Rendition) => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
extractPageText(book, rendition);
|
||||||
|
}, 150);
|
||||||
|
};
|
||||||
|
}, [extractPageText]);
|
||||||
|
|
||||||
|
// Load the initial location and setup resize handler
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (bookRef.current && rendition.current) {
|
if (bookRef.current && rendition.current) {
|
||||||
extractPageText(bookRef.current, rendition.current);
|
extractPageText(bookRef.current, rendition.current);
|
||||||
|
|
||||||
|
// Add resize observer
|
||||||
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
|
if (bookRef.current && rendition.current) {
|
||||||
|
debouncedExtractText(bookRef.current, rendition.current);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Observe the container element
|
||||||
|
const container = document.querySelector('.epub-container');
|
||||||
|
if (container) {
|
||||||
|
resizeObserver.observe(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
resizeObserver.disconnect();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}, [extractPageText, initialPrevLocLoad]);
|
}, [extractPageText, debouncedExtractText, initialPrevLocLoad]);
|
||||||
|
|
||||||
const updateTheme = useCallback((rendition: Rendition) => {
|
const updateTheme = useCallback((rendition: Rendition) => {
|
||||||
if (!epubTheme) return; // Only apply theme if enabled
|
if (!epubTheme) return; // Only apply theme if enabled
|
||||||
|
|
|
||||||
|
|
@ -260,7 +260,7 @@ export function handleTextClick(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add debounce utility at the top of the file
|
// Debounce for PDF viewer
|
||||||
export function debounce<T extends (...args: unknown[]) => unknown>(
|
export function debounce<T extends (...args: unknown[]) => unknown>(
|
||||||
func: T,
|
func: T,
|
||||||
wait: number
|
wait: number
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue