Skip to content

Commit 5de41a9

Browse files
feat: Add initial PDF viewer with PDF rendering, search, outline, and caching capabilities.
1 parent 0f6c337 commit 5de41a9

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

viewer/js/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ function renderPage(pageNumber, zoom, prerender, prerenderTrigger = 0) {
252252
textLayerDiv.replaceWith(cached.textLayerDiv);
253253
textLayerDiv = cached.textLayerDiv;
254254
setLayerTransform(cached.pageWidth, cached.pageHeight, textLayerDiv);
255-
container.style.setProperty("--scale-factor", newZoomRatio.toString());
256255
textLayerDiv.hidden = false;
257256

258257
if (searchController) {
@@ -312,7 +311,6 @@ function renderPage(pageNumber, zoom, prerender, prerenderTrigger = 0) {
312311
}
313312

314313
if (zoom === 2) {
315-
container.style.setProperty("--scale-factor", newZoomRatio.toString());
316314
pageRendering = false;
317315

318316
// zoom focus relative to page origin, rather than screen origin
@@ -401,7 +399,6 @@ function renderPage(pageNumber, zoom, prerender, prerenderTrigger = 0) {
401399
if (useRender) {
402400
textLayerDiv.replaceWith(newTextLayerDiv);
403401
textLayerDiv = newTextLayerDiv;
404-
container.style.setProperty("--scale-factor", newZoomRatio.toString());
405402
textLayerDiv.hidden = false;
406403

407404
searchController.drawPageMatches(pageNumber, textLayerDiv, zoom === 1 || zoom === 2);

viewer/js/search_controller.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export class SearchController {
88
this.currentMatchIndex = -1;
99
this.searchAbortController = null;
1010
this.highlightOverlay = null; // Container for highlights
11+
this.activeTextLayer = null;
12+
this.activePage = 0;
1113
}
1214

1315
async find(query) {
@@ -115,12 +117,20 @@ export class SearchController {
115117
}
116118

117119
showMatch(match) {
118-
// Navigate to the page
120+
if (match.pageNum === this.activePage && this.activeTextLayer) {
121+
// Already on the same page, just redraw highlights to try to avoid full re-render
122+
// Update: calling getPageCallback might be ignored by index.js if page is same,
123+
// so we MUST redraw manually here to update the 'selected' highlight.
124+
this.drawPageMatches(match.pageNum, this.activeTextLayer);
125+
}
126+
// Always request page navigation to ensure we are on the right page
119127
this.getPageCallback(match.pageNum);
120128
}
121129

122130
drawPageMatches(pageNum, textLayerDiv, skipScroll = false) {
123131
this.removeHighlights();
132+
this.activeTextLayer = textLayerDiv;
133+
this.activePage = pageNum;
124134

125135
if (this.matches.length === 0) {
126136
return;
@@ -142,10 +152,6 @@ export class SearchController {
142152
const currentMatch = (this.currentMatchIndex !== -1) ? this.matches[this.currentMatchIndex] : null;
143153
let selectedDiv = null;
144154

145-
// Get the scale factor from CSS custom property
146-
const containerEl = document.getElementById("container");
147-
const scaleFactor = parseFloat(containerEl.style.getPropertyValue("--scale-factor")) || 1;
148-
149155
let currentLen = 0;
150156

151157
spans.forEach((span) => {
@@ -181,12 +187,11 @@ export class SearchController {
181187
}
182188
}
183189

184-
// Calculate positions relative to text layer using current viewport positions
185-
// and then normalize by the scale factor to get unscaled coordinates
186-
const left = (rect.left - textLayerRect.left) / scaleFactor;
187-
const top = (rect.top - textLayerRect.top) / scaleFactor;
188-
const width = rect.width / scaleFactor;
189-
const height = rect.height / scaleFactor;
190+
// Calculate positions relative to text layer
191+
const left = rect.left - textLayerRect.left;
192+
const top = rect.top - textLayerRect.top;
193+
const width = rect.width;
194+
const height = rect.height;
190195

191196
div.style.left = left + "px";
192197
div.style.top = top + "px";

viewer/main.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
height: 100%;
1010
pointer-events: none;
1111
z-index: 2;
12-
transform-origin: 0 0;
13-
transform: scale(var(--scale-factor, 1));
1412
}
1513

1614
.search-highlight {

0 commit comments

Comments
 (0)