Skip to content

Commit 8166903

Browse files
committed
Fix horizontal PDF scale
1 parent a1136be commit 8166903

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

src/components/PDFViewer.tsx

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

3-
import { RefObject, useCallback } from 'react';
3+
import { RefObject, useCallback, useState, useEffect, useRef } from 'react';
44
import { Document, Page } from 'react-pdf';
55
import 'react-pdf/dist/Page/AnnotationLayer.css';
66
import 'react-pdf/dist/Page/TextLayer.css';
7-
import { useState, useEffect, useRef } from 'react';
87
import { PDFSkeleton } from './PDFSkeleton';
98
import { useTTS } from '@/contexts/TTSContext';
109
import { usePDF } from '@/contexts/PDFContext';
@@ -109,13 +108,25 @@ export function PDFViewer({ zoomLevel }: PDFViewerProps) {
109108
};
110109
}, [currDocText, currentSentence, highlightPattern, clearHighlights]);
111110

112-
// Add scale calculation function
113-
const calculateScale = useCallback((pageWidth = 595): number => {
111+
// Add page dimensions state
112+
const [pageWidth, setPageWidth] = useState<number>(595); // default A4 width
113+
const [pageHeight, setPageHeight] = useState<number>(842); // default A4 height
114+
115+
// Modify scale calculation function to handle orientation
116+
const calculateScale = useCallback((width = pageWidth, height = pageHeight): number => {
114117
const margin = 24; // 24px padding on each side
118+
const containerHeight = window.innerHeight - 100; // approximate visible height
115119
const targetWidth = containerWidth - margin;
116-
const baseScale = targetWidth / pageWidth;
120+
const targetHeight = containerHeight - margin;
121+
122+
// Calculate scales based on both dimensions
123+
const scaleByWidth = targetWidth / width;
124+
const scaleByHeight = targetHeight / height;
125+
126+
// Use the smaller scale to ensure the page fits both dimensions
127+
const baseScale = Math.min(scaleByWidth, scaleByHeight);
117128
return baseScale * (zoomLevel / 100);
118-
}, [containerWidth, zoomLevel]);
129+
}, [containerWidth, zoomLevel, pageWidth, pageHeight]);
119130

120131
// Add resize observer effect
121132
useEffect(() => {
@@ -152,6 +163,10 @@ export function PDFViewer({ zoomLevel }: PDFViewerProps) {
152163
renderTextLayer={true}
153164
className="shadow-lg"
154165
scale={calculateScale()}
166+
onLoadSuccess={(page) => {
167+
setPageWidth(page.originalWidth);
168+
setPageHeight(page.originalHeight);
169+
}}
155170
/>
156171
</div>
157172
</div>

src/contexts/PDFContext.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ interface PDFContextType {
3939
getDocument: (id: string) => Promise<PDFDocument | undefined>;
4040
removeDocument: (id: string) => Promise<void>;
4141
isLoading: boolean;
42-
error: string | null;
4342
extractTextFromPDF: (pdfURL: string, currDocPage: number) => Promise<string>;
4443
highlightPattern: (text: string, pattern: string, containerRef: React.RefObject<HTMLDivElement>) => void;
4544
clearHighlights: () => void;
@@ -65,7 +64,6 @@ const PDFContext = createContext<PDFContextType | undefined>(undefined);
6564
export function PDFProvider({ children }: { children: ReactNode }) {
6665
const [documents, setDocuments] = useState<PDFDocument[]>([]);
6766
const [isLoading, setIsLoading] = useState(true);
68-
const [error, setError] = useState<string | null>(null);
6967

7068
const { isDBReady } = useConfig();
7169
const {
@@ -86,12 +84,10 @@ export function PDFProvider({ children }: { children: ReactNode }) {
8684
if (!isDBReady) return;
8785

8886
try {
89-
setError(null);
9087
const docs = await indexedDBService.getAllDocuments();
9188
setDocuments(docs);
9289
} catch (error) {
9390
console.error('Failed to load documents:', error);
94-
setError('Failed to load documents. Please try again.');
9591
} finally {
9692
setIsLoading(false);
9793
}
@@ -102,7 +98,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
10298

10399
// Add a new document to IndexedDB
104100
const addDocument = useCallback(async (file: File): Promise<string> => {
105-
setError(null);
106101
const id = uuidv4();
107102
const newDoc: PDFDocument = {
108103
id,
@@ -118,32 +113,27 @@ export function PDFProvider({ children }: { children: ReactNode }) {
118113
return id;
119114
} catch (error) {
120115
console.error('Failed to add document:', error);
121-
setError('Failed to save the document. Please try again.');
122116
throw error;
123117
}
124118
}, []);
125119

126120
// Get a document by ID from IndexedDB
127121
const getDocument = useCallback(async (id: string): Promise<PDFDocument | undefined> => {
128-
setError(null);
129122
try {
130123
return await indexedDBService.getDocument(id);
131124
} catch (error) {
132125
console.error('Failed to get document:', error);
133-
setError('Failed to retrieve the document. Please try again.');
134126
return undefined;
135127
}
136128
}, []);
137129

138130
// Remove a document by ID from IndexedDB
139131
const removeDocument = useCallback(async (id: string): Promise<void> => {
140-
setError(null);
141132
try {
142133
await indexedDBService.removeDocument(id);
143134
setDocuments((prev) => prev.filter((doc) => doc.id !== id));
144135
} catch (error) {
145136
console.error('Failed to remove document:', error);
146-
setError('Failed to remove the document. Please try again.');
147137
throw error;
148138
}
149139
}, []);
@@ -241,7 +231,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
241231
setTTSText(text);
242232
} catch (error) {
243233
console.error('Error loading PDF text:', error);
244-
setError('Failed to extract PDF text');
245234
}
246235
}, [currDocURL, currDocPage, extractTextFromPDF, setTTSText]);
247236

@@ -254,7 +243,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
254243

255244
// Set curr document
256245
const setCurrentDocument = useCallback(async (id: string): Promise<void> => {
257-
setError(null);
258246
try {
259247
const doc = await getDocument(id);
260248
if (doc) {
@@ -265,7 +253,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
265253
}
266254
} catch (error) {
267255
console.error('Failed to get document URL:', error);
268-
setError('Failed to retrieve the document. Please try again.');
269256
}
270257
}, [getDocument]);
271258

@@ -463,7 +450,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
463450
getDocument,
464451
removeDocument,
465452
isLoading,
466-
error,
467453
extractTextFromPDF,
468454
highlightPattern,
469455
clearHighlights,
@@ -483,7 +469,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
483469
getDocument,
484470
removeDocument,
485471
isLoading,
486-
error,
487472
extractTextFromPDF,
488473
highlightPattern,
489474
clearHighlights,

0 commit comments

Comments
 (0)