Skip to content

Commit bcc755a

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.#previousContainerHeight`-parameter, used as part of the code that will center pages *vertically* in PresentationMode, can be removed. 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 the code to update the new `--viewer-container-height` CSS variable was purposely placed in the `web/app.js` file, since it's only used in the default viewer. In particular, neither PresentationMode nor Toolbar-functionality is included in the "viewer components".
1 parent cfac6fa commit bcc755a

File tree

6 files changed

+28
-54
lines changed

6 files changed

+28
-54
lines changed

web/app.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ const PDFViewerApplication = {
253253
_docStats: null,
254254
_wheelUnusedTicks: 0,
255255
_idleCallbacks: new Set(),
256+
_doc: document.documentElement,
256257
_PDFBug: null,
257258

258259
// Called once when the document is loaded.
@@ -275,13 +276,14 @@ const PDFViewerApplication = {
275276
}
276277
await this._initializeViewerComponents();
277278

279+
this._updateViewerContainerHeight();
278280
// Bind the various event handlers *after* the viewer has been
279281
// initialized, to prevent errors if an event arrives too soon.
280282
this.bindEvents();
281283
this.bindWindowEvents();
282284

283285
// We can start UI localization now.
284-
const appContainer = appConfig.appContainer || document.documentElement;
286+
const appContainer = appConfig.appContainer || this._doc;
285287
this.l10n.translate(appContainer).then(() => {
286288
// Dispatch the 'localized' event on the `eventBus` once the viewer
287289
// has been fully initialized and translated.
@@ -574,7 +576,6 @@ const PDFViewerApplication = {
574576

575577
this.secondaryToolbar = new SecondaryToolbar(
576578
appConfig.secondaryToolbar,
577-
container,
578579
eventBus
579580
);
580581

@@ -2077,6 +2078,23 @@ const PDFViewerApplication = {
20772078
}
20782079
},
20792080

2081+
/**
2082+
* @ignore
2083+
*/
2084+
_updateViewerContainerHeight() {
2085+
const containerHeight = this.appConfig.mainContainer.clientHeight;
2086+
2087+
if (containerHeight === this._previousContainerHeight) {
2088+
return;
2089+
}
2090+
this._previousContainerHeight = containerHeight;
2091+
2092+
this._doc.style.setProperty(
2093+
"--viewer-container-height",
2094+
`${containerHeight}px`
2095+
);
2096+
},
2097+
20802098
/**
20812099
* Used together with the integration-tests, to enable awaiting full
20822100
* initialization of the scripting/sandbox.
@@ -2400,6 +2418,8 @@ function webViewerSpreadModeChanged(evt) {
24002418
}
24012419

24022420
function webViewerResize() {
2421+
PDFViewerApplication._updateViewerContainerHeight();
2422+
24032423
const { pdfDocument, pdfViewer } = PDFViewerApplication;
24042424
if (!pdfDocument) {
24052425
return;

web/base_viewer.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ class BaseViewer {
206206

207207
#enablePermissions = false;
208208

209-
#previousContainerHeight = 0;
210-
211209
#scrollModePageState = null;
212210

213211
#onVisibilityChange = null;
@@ -936,7 +934,6 @@ class BaseViewer {
936934
if (this.isInPresentationMode) {
937935
const dummyPage = document.createElement("div");
938936
dummyPage.className = "dummyPage";
939-
dummyPage.style.height = `${this.container.clientHeight}px`;
940937
spread.appendChild(dummyPage);
941938
}
942939

@@ -994,14 +991,6 @@ class BaseViewer {
994991
* only because of limited numerical precision.
995992
*/
996993
#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-
}
1005994
return (
1006995
newScale === this._currentScale ||
1007996
Math.abs(newScale - this._currentScale) < 1e-15
@@ -1062,8 +1051,6 @@ class BaseViewer {
10621051
if (this.defaultRenderingQueue) {
10631052
this.update();
10641053
}
1065-
1066-
this.#previousContainerHeight = this.container.clientHeight;
10671054
}
10681055

10691056
/**
@@ -1096,8 +1083,7 @@ class BaseViewer {
10961083
hPadding = vPadding = 4;
10971084
} else if (this.removePageBorders) {
10981085
hPadding = vPadding = 0;
1099-
}
1100-
if (this._scrollMode === ScrollMode.HORIZONTAL) {
1086+
} else if (this._scrollMode === ScrollMode.HORIZONTAL) {
11011087
[hPadding, vPadding] = [vPadding, hPadding]; // Swap the padding values.
11021088
}
11031089
const pageWidthScale =

web/pdf_viewer.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
.pdfViewer .dummyPage {
5858
position: relative;
5959
width: 0;
60-
/* The height is set via JS, see `BaseViewer.#ensurePageViewVisible`. */
60+
height: var(--viewer-container-height);
6161
}
6262

6363
.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
@@ -17,6 +17,7 @@
1717

1818
:root {
1919
--dir-factor: 1;
20+
--viewer-container-height: 440px;
2021
--sidebar-width: 200px;
2122
--sidebar-transition-duration: 200ms;
2223
--sidebar-transition-timing-function: ease;
@@ -482,7 +483,7 @@ select {
482483

483484
#secondaryToolbarButtonContainer {
484485
max-width: 220px;
485-
max-height: 400px;
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)