Skip to content

Commit 9817230

Browse files
committed
Introduce a --viewer-container-height CSS variable to simplify the code
This new CSS variable will allow us to simplify a couple of different viewer components, since we no longer need to use JavaScript-based hacks and can directly set the CSS rules instead. In particular: - The `BaseViewer`-handling, used as part of the code that will center pages *vertically* in PresentationMode, can be simplified. By using CSS to control the height of the "dummy"-page we avoid unnecessarily invalidating the scale-value, which can reduce *some* unneeded re-rendering while PresentationMode is active. - The `SecondaryToolbar.#setMaxHeight`-method, and its associated parameters, are no longer necessary and can be completely removed. Note that in order for things to work correctly in general, the new `--viewer-container-height` CSS variable must potentially be updated on any window-based "resize"-event (even when there's no zooming). While this is currently only done in the default viewer, that shouldn't be an issue since neither PresentationMode nor Toolbar-functionality is included in the "viewer components".
1 parent cfac6fa commit 9817230

File tree

6 files changed

+21
-51
lines changed

6 files changed

+21
-51
lines changed

web/app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ const PDFViewerApplication = {
574574

575575
this.secondaryToolbar = new SecondaryToolbar(
576576
appConfig.secondaryToolbar,
577-
container,
578577
eventBus
579578
);
580579

@@ -2401,6 +2400,8 @@ function webViewerSpreadModeChanged(evt) {
24012400

24022401
function webViewerResize() {
24032402
const { pdfDocument, pdfViewer } = PDFViewerApplication;
2403+
pdfViewer.updateContainerHeightCss();
2404+
24042405
if (!pdfDocument) {
24052406
return;
24062407
}

web/base_viewer.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ class BaseViewer {
300300
if (this.removePageBorders) {
301301
this.viewer.classList.add("removePageBorders");
302302
}
303+
this.updateContainerHeightCss();
303304
// Defer the dispatching of this event, to give other viewer components
304305
// time to initialize *and* register 'baseviewerinit' event listeners.
305306
Promise.resolve().then(() => {
@@ -936,7 +937,6 @@ class BaseViewer {
936937
if (this.isInPresentationMode) {
937938
const dummyPage = document.createElement("div");
938939
dummyPage.className = "dummyPage";
939-
dummyPage.style.height = `${this.container.clientHeight}px`;
940940
spread.appendChild(dummyPage);
941941
}
942942

@@ -994,14 +994,6 @@ class BaseViewer {
994994
* only because of limited numerical precision.
995995
*/
996996
#isSameScale(newScale) {
997-
if (
998-
this.isInPresentationMode &&
999-
this.container.clientHeight !== this.#previousContainerHeight
1000-
) {
1001-
// Ensure that the current page remains centered vertically if/when
1002-
// the window is resized while PresentationMode is active.
1003-
return false;
1004-
}
1005997
return (
1006998
newScale === this._currentScale ||
1007999
Math.abs(newScale - this._currentScale) < 1e-15
@@ -1062,8 +1054,7 @@ class BaseViewer {
10621054
if (this.defaultRenderingQueue) {
10631055
this.update();
10641056
}
1065-
1066-
this.#previousContainerHeight = this.container.clientHeight;
1057+
this.updateContainerHeightCss();
10671058
}
10681059

10691060
/**
@@ -1096,8 +1087,7 @@ class BaseViewer {
10961087
hPadding = vPadding = 4;
10971088
} else if (this.removePageBorders) {
10981089
hPadding = vPadding = 0;
1099-
}
1100-
if (this._scrollMode === ScrollMode.HORIZONTAL) {
1090+
} else if (this._scrollMode === ScrollMode.HORIZONTAL) {
11011091
[hPadding, vPadding] = [vPadding, hPadding]; // Swap the padding values.
11021092
}
11031093
const pageWidthScale =
@@ -2077,6 +2067,16 @@ class BaseViewer {
20772067
} while (--steps > 0 && newScale > MIN_SCALE);
20782068
this.currentScaleValue = newScale;
20792069
}
2070+
2071+
updateContainerHeightCss() {
2072+
const height = this.container.clientHeight;
2073+
2074+
if (height !== this.#previousContainerHeight) {
2075+
this.#previousContainerHeight = height;
2076+
2077+
this._doc.style.setProperty("--viewer-container-height", `${height}px`);
2078+
}
2079+
}
20802080
}
20812081

20822082
export { BaseViewer, PagesCountLimit, PDFPageViewBuffer };

web/pdf_viewer.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@import url(xfa_layer_builder.css);
1818

1919
:root {
20+
--viewer-container-height: 0;
2021
--pdfViewer-padding-bottom: 0;
2122
--page-margin: 1px auto -8px;
2223
--page-border: 9px solid transparent;
@@ -57,7 +58,7 @@
5758
.pdfViewer .dummyPage {
5859
position: relative;
5960
width: 0;
60-
/* The height is set via JS, see `BaseViewer.#ensurePageViewVisible`. */
61+
height: var(--viewer-container-height);
6162
}
6263

6364
.pdfViewer.removePageBorders .page {

web/secondary_toolbar.js

+2-32
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { SCROLLBAR_PADDING, ScrollMode, SpreadMode } from "./ui_utils.js";
16+
import { ScrollMode, SpreadMode } from "./ui_utils.js";
1717
import { CursorTool } from "./pdf_cursor_tools.js";
1818
import { PagesCountLimit } from "./base_viewer.js";
1919

@@ -22,9 +22,6 @@ import { PagesCountLimit } from "./base_viewer.js";
2222
* @property {HTMLDivElement} toolbar - Container for the secondary toolbar.
2323
* @property {HTMLButtonElement} toggleButton - Button to toggle the visibility
2424
* of the secondary toolbar.
25-
* @property {HTMLDivElement} toolbarButtonContainer - Container where all the
26-
* toolbar buttons are placed. The maximum height of the toolbar is controlled
27-
* dynamically by adjusting the 'max-height' CSS property of this DOM element.
2825
* @property {HTMLButtonElement} presentationModeButton - Button for entering
2926
* presentation mode.
3027
* @property {HTMLButtonElement} openFileButton - Button to open a file.
@@ -52,13 +49,11 @@ import { PagesCountLimit } from "./base_viewer.js";
5249
class SecondaryToolbar {
5350
/**
5451
* @param {SecondaryToolbarOptions} options
55-
* @param {HTMLDivElement} mainContainer
5652
* @param {EventBus} eventBus
5753
*/
58-
constructor(options, mainContainer, eventBus) {
54+
constructor(options, eventBus) {
5955
this.toolbar = options.toolbar;
6056
this.toggleButton = options.toggleButton;
61-
this.toolbarButtonContainer = options.toolbarButtonContainer;
6257
this.buttons = [
6358
{
6459
element: options.presentationModeButton,
@@ -154,12 +149,8 @@ class SecondaryToolbar {
154149
pageRotateCcw: options.pageRotateCcwButton,
155150
};
156151

157-
this.mainContainer = mainContainer;
158152
this.eventBus = eventBus;
159-
160153
this.opened = false;
161-
this.containerHeight = null;
162-
this.previousContainerHeight = null;
163154

164155
this.reset();
165156

@@ -169,9 +160,6 @@ class SecondaryToolbar {
169160
this.#bindCursorToolsListener(options);
170161
this.#bindScrollModeListener(options);
171162
this.#bindSpreadModeListener(options);
172-
173-
// Bind the event listener for adjusting the 'max-height' of the toolbar.
174-
this.eventBus._on("resize", this.#setMaxHeight.bind(this));
175163
}
176164

177165
/**
@@ -322,8 +310,6 @@ class SecondaryToolbar {
322310
return;
323311
}
324312
this.opened = true;
325-
this.#setMaxHeight();
326-
327313
this.toggleButton.classList.add("toggled");
328314
this.toggleButton.setAttribute("aria-expanded", "true");
329315
this.toolbar.classList.remove("hidden");
@@ -346,22 +332,6 @@ class SecondaryToolbar {
346332
this.open();
347333
}
348334
}
349-
350-
#setMaxHeight() {
351-
if (!this.opened) {
352-
return; // Only adjust the 'max-height' if the toolbar is visible.
353-
}
354-
this.containerHeight = this.mainContainer.clientHeight;
355-
356-
if (this.containerHeight === this.previousContainerHeight) {
357-
return;
358-
}
359-
this.toolbarButtonContainer.style.maxHeight = `${
360-
this.containerHeight - SCROLLBAR_PADDING
361-
}px`;
362-
363-
this.previousContainerHeight = this.containerHeight;
364-
}
365335
}
366336

367337
export { SecondaryToolbar };

web/viewer.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ select {
482482

483483
#secondaryToolbarButtonContainer {
484484
max-width: 220px;
485-
max-height: 400px;
485+
min-height: 26px;
486+
max-height: calc(var(--viewer-container-height) - 40px);
486487
overflow-y: auto;
487488
margin-bottom: -4px;
488489
}

web/viewer.js

-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ function getViewerConfiguration() {
100100
secondaryToolbar: {
101101
toolbar: document.getElementById("secondaryToolbar"),
102102
toggleButton: document.getElementById("secondaryToolbarToggle"),
103-
toolbarButtonContainer: document.getElementById(
104-
"secondaryToolbarButtonContainer"
105-
),
106103
presentationModeButton: document.getElementById(
107104
"secondaryPresentationMode"
108105
),

0 commit comments

Comments
 (0)