Skip to content

Commit

Permalink
Introduce a viewer constant for document.documentElement.style
Browse files Browse the repository at this point in the history
Over time, as we've been introducing JavaScript code to modify CSS variables, we've been adding shorthand properties to various classes to reduce unnecessary repetition when accessing the document-styles.
Rather than repeating this in multiple places, it seems overall simpler to just introduce a constant and re-use that throughout the viewer instead.
  • Loading branch information
Snuffleupagus committed May 25, 2022
1 parent 5b02c68 commit ca244d9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
DEFAULT_SCALE,
DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE,
docStyle,
getVisibleElements,
isPortraitOrientation,
isValidRotation,
Expand Down Expand Up @@ -290,7 +291,6 @@ class BaseViewer {
} else {
this.renderingQueue = options.renderingQueue;
}
this._doc = document.documentElement;

this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this));
this.presentationModeState = PresentationModeState.UNKNOWN;
Expand Down Expand Up @@ -1016,7 +1016,7 @@ class BaseViewer {
return;
}

this._doc.style.setProperty("--zoom-factor", newScale);
docStyle.setProperty("--zoom-factor", newScale);

const updateArgs = { scale: newScale };
for (const pageView of this._pages) {
Expand Down Expand Up @@ -2067,7 +2067,7 @@ class BaseViewer {
if (height !== this.#previousContainerHeight) {
this.#previousContainerHeight = height;

this._doc.style.setProperty("--viewer-container-height", `${height}px`);
docStyle.setProperty("--viewer-container-height", `${height}px`);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import {
approximateFraction,
DEFAULT_SCALE,
docStyle,
OutputScale,
RendererType,
RenderingStates,
Expand Down Expand Up @@ -339,8 +340,7 @@ class PDFPageView {
});

if (this._isStandalone) {
const { style } = document.documentElement;
style.setProperty("--zoom-factor", this.scale);
docStyle.setProperty("--zoom-factor", this.scale);
}

if (this.svg) {
Expand Down
6 changes: 4 additions & 2 deletions web/pdf_sidebar_resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

import { docStyle } from "./ui_utils.js";

const SIDEBAR_WIDTH_VAR = "--sidebar-width";
const SIDEBAR_MIN_WIDTH = 200; // pixels
const SIDEBAR_RESIZING_CLASS = "sidebarResizing";
Expand All @@ -34,7 +36,6 @@ class PDFSidebarResizer {
constructor(options, eventBus, l10n) {
this.isRTL = false;
this.sidebarOpen = false;
this.doc = document.documentElement;
this._width = null;
this._outerContainerWidth = null;
this._boundEvents = Object.create(null);
Expand Down Expand Up @@ -75,7 +76,8 @@ class PDFSidebarResizer {
return false;
}
this._width = width;
this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);

docStyle.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions web/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
animationStarted,
DEFAULT_SCALE,
DEFAULT_SCALE_VALUE,
docStyle,
MAX_SCALE,
MIN_SCALE,
noContextMenuHandler,
Expand Down Expand Up @@ -274,8 +275,7 @@ class Toolbar {
maxWidth += 2 * scaleSelectOverflow;

if (maxWidth > scaleSelectContainerWidth) {
const doc = document.documentElement;
doc.style.setProperty("--scale-select-container-width", `${maxWidth}px`);
docStyle.setProperty("--scale-select-container-width", `${maxWidth}px`);
}
// Zeroing the width and height cause Firefox to release graphics resources
// immediately, which can greatly reduce memory consumption.
Expand Down
14 changes: 10 additions & 4 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,13 @@ const animationStarted = new Promise(function (resolve) {
window.requestAnimationFrame(resolve);
});

const docStyle =
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("LIB") &&
typeof document === "undefined"
? null
: document.documentElement.style;

function clamp(v, min, max) {
return Math.min(Math.max(v, min), max);
}
Expand Down Expand Up @@ -709,8 +716,7 @@ class ProgressBar {
}
this.div.classList.remove("indeterminate");

const doc = document.documentElement;
doc.style.setProperty("--progressBar-percent", `${this._percent}%`);
docStyle.setProperty("--progressBar-percent", `${this._percent}%`);
}

get percent() {
Expand All @@ -730,8 +736,7 @@ class ProgressBar {
const container = viewer.parentNode;
const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
if (scrollbarWidth > 0) {
const doc = document.documentElement;
doc.style.setProperty("--progressBar-end-offset", `${scrollbarWidth}px`);
docStyle.setProperty("--progressBar-end-offset", `${scrollbarWidth}px`);
}
}

Expand Down Expand Up @@ -843,6 +848,7 @@ export {
DEFAULT_SCALE,
DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE,
docStyle,
getActiveOrFocusedElement,
getPageSizeInches,
getVisibleElements,
Expand Down

0 comments on commit ca244d9

Please sign in to comment.