Skip to content

Commit bab7131

Browse files
Merge pull request #2 from ahmetkursatd/pdf-viewer-zoom-highlight-improvement
fix: Keep highlight when page is zoomed in/out
2 parents 8da5c8d + e43a7d5 commit bab7131

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

src/components/document-viewer.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ const DocumentViewer: React.FC<DocumentViewerProps> = ({
418418
pdfDocumentInstance.destroy();
419419
}
420420
};
421-
}, [pdfDocument?.originalFileUrl, pdfJsLoaded, zoomLevel, isBrowser, effectivePageNumber]);
421+
}, [pdfDocument?.originalFileUrl, pdfJsLoaded, isBrowser, effectivePageNumber]);
422422

423423
// Handle text highlighting when citationSnippet changes
424424
useEffect(() => {
@@ -440,12 +440,12 @@ const DocumentViewer: React.FC<DocumentViewerProps> = ({
440440
targetPage = effectivePageNumber;
441441
}
442442
(async () => {
443-
const newHighlight = await createRectangleHighlight(
444-
citationSnippet,
445-
viewer,
446-
targetPage,
447-
currentHighlight
448-
);
443+
const newHighlight = await createRectangleHighlight({
444+
searchText: citationSnippet,
445+
pdfViewerInstance: viewer,
446+
targetPage: targetPage,
447+
currentHighlight: currentHighlight,
448+
});
449449
if (newHighlight) {
450450
removeCurrentHighlight();
451451
setCurrentHighlight(newHighlight);
@@ -516,12 +516,13 @@ const DocumentViewer: React.FC<DocumentViewerProps> = ({
516516

517517
// Recreate highlight without changing page navigation
518518
const recreateHighlight = async () => {
519-
const newHighlight = await createRectangleHighlight(
520-
citationSnippet,
521-
viewer,
522-
undefined, // Don't specify target page to avoid navigation
523-
null
524-
);
519+
const newHighlight = await createRectangleHighlight({
520+
searchText: citationSnippet,
521+
pdfViewerInstance: viewer,
522+
targetPage: currentHighlight?.page,
523+
currentHighlight,
524+
shouldNavigateOnMatch: false,
525+
});
525526
if (newHighlight) {
526527
removeCurrentHighlight();
527528
setCurrentHighlight(newHighlight);

src/utils/pdf-highlighting.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,21 @@ export const findTextInPDF = async (
146146
/**
147147
* Create a rectangle highlight overlay on a PDF page
148148
*/
149-
export const createRectangleHighlight = async (
149+
export const createRectangleHighlight = async ({
150+
searchText,
151+
pdfViewerInstance,
152+
targetPage,
153+
currentHighlight = null,
154+
forceRecreate = false,
155+
shouldNavigateOnMatch = true
156+
}: {
150157
searchText: string,
151-
pdfViewerInstance: any,
152-
targetPage?: number,
153-
currentHighlight?: CurrentHighlight | null,
154-
forceRecreate?: boolean
155-
): Promise<CurrentHighlight | null> => {
158+
pdfViewerInstance: any;
159+
targetPage?: number;
160+
currentHighlight?: CurrentHighlight | null;
161+
forceRecreate?: boolean;
162+
shouldNavigateOnMatch?: boolean;
163+
}): Promise<CurrentHighlight | null> => {
156164
if (!searchText || !pdfViewerInstance || !pdfViewerInstance.pagesCount) return null;
157165

158166
// Check if we already have a highlight for the same text and page
@@ -178,9 +186,8 @@ export const createRectangleHighlight = async (
178186
}
179187
if (!result) return null;
180188

181-
// Only navigate to the page if we're explicitly targeting a specific page
182189
// Don't auto-navigate when user is scrolling around
183-
if (targetPage && result.page !== pdfViewerInstance.currentPageNumber) {
190+
if (shouldNavigateOnMatch && targetPage && result.page !== pdfViewerInstance.currentPageNumber) {
184191
try {
185192
const pageNumber = Number(result.page);
186193
if (pageNumber >= 1 && pageNumber <= pdfViewerInstance.pagesCount) {

0 commit comments

Comments
 (0)