From 9374d3efbe09af4fd3ddcc2190b85b2cfcc48c3e Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Sun, 26 Jan 2025 02:15:14 -0700 Subject: [PATCH] Build fixes --- src/app/pdf/[id]/page.tsx | 2 +- src/contexts/PDFContext.tsx | 162 ++++++++++++++++++------------------ 2 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/app/pdf/[id]/page.tsx b/src/app/pdf/[id]/page.tsx index 42a21f3..cedee84 100644 --- a/src/app/pdf/[id]/page.tsx +++ b/src/app/pdf/[id]/page.tsx @@ -4,7 +4,7 @@ import dynamic from 'next/dynamic'; import { usePDF } from '@/contexts/PDFContext'; import { useParams } from 'next/navigation'; import Link from 'next/link'; -import { use, useCallback, useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { PDFSkeleton } from '@/components/PDFSkeleton'; import { useTTS } from '@/contexts/TTSContext'; diff --git a/src/contexts/PDFContext.tsx b/src/contexts/PDFContext.tsx index 3828fee..54f1623 100644 --- a/src/contexts/PDFContext.tsx +++ b/src/contexts/PDFContext.tsx @@ -148,89 +148,89 @@ export function PDFProvider({ children }: { children: ReactNode }) { } }, []); - function onDocumentLoadSuccess({ numPages }: { numPages: number }): void { + const onDocumentLoadSuccess = useCallback(({ numPages }: { numPages: number }) => { console.log('Document loaded:', numPages); setCurrDocPages(numPages); - } + }, []); // Extract text from a PDF file - const extractTextFromPDF = useCallback(async (pdfURL: string, currDocPage: number): Promise => { - try { - const base64Data = pdfURL.split(',')[1]; - const binaryData = atob(base64Data); - const bytes = new Uint8Array(binaryData.length); - for (let i = 0; i < binaryData.length; i++) { - bytes[i] = binaryData.charCodeAt(i); - } - - const loadingTask = pdfjs.getDocument({ data: bytes }); - const pdf = await loadingTask.promise; - - // Get only the specified page - const page = await pdf.getPage(currDocPage); - const textContent = await page.getTextContent(); - - // Filter out non-text items and assert proper type - const textItems = textContent.items.filter((item): item is TextItem => - 'str' in item && 'transform' in item - ); - - // Group text items into lines based on their vertical position - const tolerance = 2; - const lines: TextItem[][] = []; - let currentLine: TextItem[] = []; - let currentY: number | null = null; - - textItems.forEach((item) => { - const y = item.transform[5]; - if (currentY === null) { - currentY = y; - currentLine.push(item); - } else if (Math.abs(y - currentY) < tolerance) { - currentLine.push(item); - } else { - lines.push(currentLine); - currentLine = [item]; - currentY = y; - } - }); - lines.push(currentLine); - - // Process each line to build text - let pageText = ''; - for (const line of lines) { - // Sort items horizontally within the line - line.sort((a, b) => a.transform[4] - b.transform[4]); - - let lineText = ''; - let prevItem: TextItem | null = null; - - for (const item of line) { - if (!prevItem) { - lineText = item.str; - } else { - const prevEndX = prevItem.transform[4] + (prevItem.width ?? 0); - const currentStartX = item.transform[4]; - const space = currentStartX - prevEndX; - - // Add space if gap is significant, otherwise concatenate directly - if (space > ((item.width ?? 0) * 0.3)) { - lineText += ' ' + item.str; - } else { - lineText += item.str; - } - } - prevItem = item; - } - pageText += lineText + ' '; - } - - return pageText.replace(/\s+/g, ' ').trim(); - } catch (error) { - console.error('Error extracting text from PDF:', error); - throw new Error('Failed to extract text from PDF'); + const extractTextFromPDF = useCallback(async (pdfURL: string, currDocPage: number): Promise => { + try { + const base64Data = pdfURL.split(',')[1]; + const binaryData = atob(base64Data); + const bytes = new Uint8Array(binaryData.length); + for (let i = 0; i < binaryData.length; i++) { + bytes[i] = binaryData.charCodeAt(i); } - }, []); + + const loadingTask = pdfjs.getDocument({ data: bytes }); + const pdf = await loadingTask.promise; + + // Get only the specified page + const page = await pdf.getPage(currDocPage); + const textContent = await page.getTextContent(); + + // Filter out non-text items and assert proper type + const textItems = textContent.items.filter((item): item is TextItem => + 'str' in item && 'transform' in item + ); + + // Group text items into lines based on their vertical position + const tolerance = 2; + const lines: TextItem[][] = []; + let currentLine: TextItem[] = []; + let currentY: number | null = null; + + textItems.forEach((item) => { + const y = item.transform[5]; + if (currentY === null) { + currentY = y; + currentLine.push(item); + } else if (Math.abs(y - currentY) < tolerance) { + currentLine.push(item); + } else { + lines.push(currentLine); + currentLine = [item]; + currentY = y; + } + }); + lines.push(currentLine); + + // Process each line to build text + let pageText = ''; + for (const line of lines) { + // Sort items horizontally within the line + line.sort((a, b) => a.transform[4] - b.transform[4]); + + let lineText = ''; + let prevItem: TextItem | null = null; + + for (const item of line) { + if (!prevItem) { + lineText = item.str; + } else { + const prevEndX = prevItem.transform[4] + (prevItem.width ?? 0); + const currentStartX = item.transform[4]; + const space = currentStartX - prevEndX; + + // Add space if gap is significant, otherwise concatenate directly + if (space > ((item.width ?? 0) * 0.3)) { + lineText += ' ' + item.str; + } else { + lineText += item.str; + } + } + prevItem = item; + } + pageText += lineText + ' '; + } + + return pageText.replace(/\s+/g, ' ').trim(); + } catch (error) { + console.error('Error extracting text from PDF:', error); + throw new Error('Failed to extract text from PDF'); + } + }, []); // Load curr doc text const loadCurrDocText = useCallback(async () => { @@ -267,18 +267,18 @@ export function PDFProvider({ children }: { children: ReactNode }) { console.error('Failed to get document URL:', error); setError('Failed to retrieve the document. Please try again.'); } - }, [getDocument, loadCurrDocText]); + }, [getDocument]); const clearCurrDoc = useCallback(() => { setCurrDocName(undefined); setCurrDocURL(undefined); setCurrDocText(undefined); - setCurrDocPages(undefined); // Clear TTS text + setCurrDocPages(undefined); // Goes to TTS context setTTSText(''); - }, []); + }, [setCurrDocPages, setTTSText]); // Clear all highlights in the PDF viewer const clearHighlights = useCallback(() => {