From f948601e709cbb22576ff9c82184e8860ae0039a Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Tue, 25 Feb 2025 03:26:37 -0700 Subject: [PATCH] Add configurable text extraction margin for PDF processing --- src/components/DocumentSettings.tsx | 67 ++++++++++++++++++++++++----- src/contexts/ConfigContext.tsx | 12 ++++++ src/contexts/PDFContext.tsx | 6 ++- src/utils/pdf.ts | 45 +++++++++++++++++-- 4 files changed, 114 insertions(+), 16 deletions(-) diff --git a/src/components/DocumentSettings.tsx b/src/components/DocumentSettings.tsx index c251bbf..eed03c2 100644 --- a/src/components/DocumentSettings.tsx +++ b/src/components/DocumentSettings.tsx @@ -1,6 +1,6 @@ 'use client'; -import { Fragment, useState, useRef } from 'react'; +import { Fragment, useState, useRef, useCallback, useEffect } from 'react'; import { Dialog, DialogPanel, Transition, TransitionChild, Listbox, ListboxButton, ListboxOptions, ListboxOption, Button } from '@headlessui/react'; import { useConfig, ViewType } from '@/contexts/ConfigContext'; import { ChevronUpDownIcon, CheckIcon } from '@/components/icons/Icons'; @@ -21,13 +21,33 @@ const viewTypes = [ ]; export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsProps) { - const { viewType, skipBlank, epubTheme, updateConfigKey } = useConfig(); + const { viewType, skipBlank, epubTheme, textExtractionMargin, updateConfigKey } = useConfig(); const { createFullAudioBook } = useEPUB(); const [progress, setProgress] = useState(0); const [isGenerating, setIsGenerating] = useState(false); + const [localMargin, setLocalMargin] = useState(textExtractionMargin); const abortControllerRef = useRef(null); const selectedView = viewTypes.find(v => v.id === viewType) || viewTypes[0]; + //console.log(localMargin, textExtractionMargin); + + // Sync local margin with global state + useEffect(() => { + setLocalMargin(textExtractionMargin); + }, [textExtractionMargin]); + + // Handler for slider change (updates local state only) + const handleMarginChange = useCallback((event: React.ChangeEvent) => { + setLocalMargin(Number(event.target.value)); + }, []); + + // Handler for slider release + const handleMarginChangeComplete = useCallback(() => { + if (localMargin !== textExtractionMargin) { + updateConfigKey('textExtractionMargin', localMargin); + } + }, [localMargin, textExtractionMargin, updateConfigKey]); + const handleStartGeneration = async () => { setIsGenerating(true); setProgress(0); @@ -132,13 +152,38 @@ export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsPro )} } - {!epub &&
- + {!epub &&
+
+ +
+ 0% + {Math.round(localMargin * 100)}% + 20% +
+ +

+ {"Don't"} include content from outer rim of the page during text extraction (experimental) +

+
updateConfigKey('viewType', newView.id as ViewType)} > -
+
+ {selectedView.name} @@ -177,14 +222,16 @@ export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsPro ))} + {selectedView.id === 'scroll' && ( +

+ Note: Continuous scroll may perform poorly for larger documents. +

+ )}
- {selectedView.id === 'scroll' && ( -

- Note: Continuous scroll may perform poorly for larger documents. -

- )} +
} +