Skip to content

Commit

Permalink
Merge pull request #15830 from calixteman/dont_compute_rect
Browse files Browse the repository at this point in the history
Avoid to compute the client rect of the viewer
  • Loading branch information
calixteman authored Dec 17, 2022
2 parents 65a476a + c550953 commit 1ab711e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
7 changes: 3 additions & 4 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,6 @@ function webViewerResize() {
// Work-around issue 15324 by ignoring "resize" events during printing.
return;
}
pdfViewer.updateContainerHeightCss();

if (!pdfDocument) {
return;
Expand Down Expand Up @@ -2649,9 +2648,9 @@ function webViewerWheel(evt) {
// left corner is restored. When the mouse wheel is used, the position
// under the cursor should be restored instead.
const scaleCorrectionFactor = currentScale / previousScale - 1;
const rect = pdfViewer.container.getBoundingClientRect();
const dx = evt.clientX - rect.left;
const dy = evt.clientY - rect.top;
const [top, left] = pdfViewer.containerTopLeft;
const dx = evt.clientX - left;
const dy = evt.clientY - top;
pdfViewer.container.scrollLeft += dx * scaleCorrectionFactor;
pdfViewer.container.scrollTop += dy * scaleCorrectionFactor;
}
Expand Down
34 changes: 28 additions & 6 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,14 @@ class PDFViewer {

#annotationMode = AnnotationMode.ENABLE_FORMS;

#containerTopLeft = null;

#enablePermissions = false;

#previousContainerHeight = 0;

#resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this));

#scrollModePageState = null;

#onVisibilityChange = null;
Expand All @@ -247,6 +251,8 @@ class PDFViewer {
);
}
this.container = options.container;
this.#resizeObserver.observe(this.container);

this.viewer = options.viewer || options.container.firstElementChild;

if (
Expand Down Expand Up @@ -330,7 +336,8 @@ class PDFViewer {
if (this.removePageBorders) {
this.viewer.classList.add("removePageBorders");
}
this.updateContainerHeightCss();

this.#updateContainerHeightCss();
}

get pagesCount() {
Expand Down Expand Up @@ -1137,7 +1144,6 @@ class PDFViewer {
if (this.defaultRenderingQueue) {
this.update();
}
this.updateContainerHeightCss();
}

/**
Expand Down Expand Up @@ -2186,16 +2192,32 @@ class PDFViewer {
this.currentScaleValue = newScale;
}

updateContainerHeightCss() {
const height = this.container.clientHeight;

#updateContainerHeightCss(height = this.container.clientHeight) {
if (height !== this.#previousContainerHeight) {
this.#previousContainerHeight = height;

docStyle.setProperty("--viewer-container-height", `${height}px`);
}
}

#resizeObserverCallback(entries) {
for (const entry of entries) {
if (entry.target === this.container) {
this.#updateContainerHeightCss(
Math.floor(entry.borderBoxSize[0].blockSize)
);
this.#containerTopLeft = null;
break;
}
}
}

get containerTopLeft() {
return (this.#containerTopLeft ||= [
this.container.offsetTop,
this.container.offsetLeft,
]);
}

/**
* @type {number}
*/
Expand Down

0 comments on commit 1ab711e

Please sign in to comment.