Introduce a viewer constant for document.documentElement.style

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.
This commit is contained in:
Jonas Jenwald 2022-05-22 18:00:57 +02:00
parent 5b02c685d6
commit ca244d9bca
5 changed files with 21 additions and 13 deletions

View File

@ -40,6 +40,7 @@ import {
DEFAULT_SCALE, DEFAULT_SCALE,
DEFAULT_SCALE_DELTA, DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
docStyle,
getVisibleElements, getVisibleElements,
isPortraitOrientation, isPortraitOrientation,
isValidRotation, isValidRotation,
@ -290,7 +291,6 @@ class BaseViewer {
} else { } else {
this.renderingQueue = options.renderingQueue; this.renderingQueue = options.renderingQueue;
} }
this._doc = document.documentElement;
this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this)); this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this));
this.presentationModeState = PresentationModeState.UNKNOWN; this.presentationModeState = PresentationModeState.UNKNOWN;
@ -1016,7 +1016,7 @@ class BaseViewer {
return; return;
} }
this._doc.style.setProperty("--zoom-factor", newScale); docStyle.setProperty("--zoom-factor", newScale);
const updateArgs = { scale: newScale }; const updateArgs = { scale: newScale };
for (const pageView of this._pages) { for (const pageView of this._pages) {
@ -2067,7 +2067,7 @@ class BaseViewer {
if (height !== this.#previousContainerHeight) { if (height !== this.#previousContainerHeight) {
this.#previousContainerHeight = height; this.#previousContainerHeight = height;
this._doc.style.setProperty("--viewer-container-height", `${height}px`); docStyle.setProperty("--viewer-container-height", `${height}px`);
} }
} }
} }

View File

@ -40,6 +40,7 @@ import {
import { import {
approximateFraction, approximateFraction,
DEFAULT_SCALE, DEFAULT_SCALE,
docStyle,
OutputScale, OutputScale,
RendererType, RendererType,
RenderingStates, RenderingStates,
@ -339,8 +340,7 @@ class PDFPageView {
}); });
if (this._isStandalone) { if (this._isStandalone) {
const { style } = document.documentElement; docStyle.setProperty("--zoom-factor", this.scale);
style.setProperty("--zoom-factor", this.scale);
} }
if (this.svg) { if (this.svg) {

View File

@ -13,6 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
import { docStyle } from "./ui_utils.js";
const SIDEBAR_WIDTH_VAR = "--sidebar-width"; const SIDEBAR_WIDTH_VAR = "--sidebar-width";
const SIDEBAR_MIN_WIDTH = 200; // pixels const SIDEBAR_MIN_WIDTH = 200; // pixels
const SIDEBAR_RESIZING_CLASS = "sidebarResizing"; const SIDEBAR_RESIZING_CLASS = "sidebarResizing";
@ -34,7 +36,6 @@ class PDFSidebarResizer {
constructor(options, eventBus, l10n) { constructor(options, eventBus, l10n) {
this.isRTL = false; this.isRTL = false;
this.sidebarOpen = false; this.sidebarOpen = false;
this.doc = document.documentElement;
this._width = null; this._width = null;
this._outerContainerWidth = null; this._outerContainerWidth = null;
this._boundEvents = Object.create(null); this._boundEvents = Object.create(null);
@ -75,7 +76,8 @@ class PDFSidebarResizer {
return false; return false;
} }
this._width = width; this._width = width;
this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
docStyle.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
return true; return true;
} }

View File

@ -17,6 +17,7 @@ import {
animationStarted, animationStarted,
DEFAULT_SCALE, DEFAULT_SCALE,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
docStyle,
MAX_SCALE, MAX_SCALE,
MIN_SCALE, MIN_SCALE,
noContextMenuHandler, noContextMenuHandler,
@ -274,8 +275,7 @@ class Toolbar {
maxWidth += 2 * scaleSelectOverflow; maxWidth += 2 * scaleSelectOverflow;
if (maxWidth > scaleSelectContainerWidth) { if (maxWidth > scaleSelectContainerWidth) {
const doc = document.documentElement; docStyle.setProperty("--scale-select-container-width", `${maxWidth}px`);
doc.style.setProperty("--scale-select-container-width", `${maxWidth}px`);
} }
// Zeroing the width and height cause Firefox to release graphics resources // Zeroing the width and height cause Firefox to release graphics resources
// immediately, which can greatly reduce memory consumption. // immediately, which can greatly reduce memory consumption.

View File

@ -677,6 +677,13 @@ const animationStarted = new Promise(function (resolve) {
window.requestAnimationFrame(resolve); window.requestAnimationFrame(resolve);
}); });
const docStyle =
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("LIB") &&
typeof document === "undefined"
? null
: document.documentElement.style;
function clamp(v, min, max) { function clamp(v, min, max) {
return Math.min(Math.max(v, min), max); return Math.min(Math.max(v, min), max);
} }
@ -709,8 +716,7 @@ class ProgressBar {
} }
this.div.classList.remove("indeterminate"); this.div.classList.remove("indeterminate");
const doc = document.documentElement; docStyle.setProperty("--progressBar-percent", `${this._percent}%`);
doc.style.setProperty("--progressBar-percent", `${this._percent}%`);
} }
get percent() { get percent() {
@ -730,8 +736,7 @@ class ProgressBar {
const container = viewer.parentNode; const container = viewer.parentNode;
const scrollbarWidth = container.offsetWidth - viewer.offsetWidth; const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
if (scrollbarWidth > 0) { if (scrollbarWidth > 0) {
const doc = document.documentElement; docStyle.setProperty("--progressBar-end-offset", `${scrollbarWidth}px`);
doc.style.setProperty("--progressBar-end-offset", `${scrollbarWidth}px`);
} }
} }
@ -843,6 +848,7 @@ export {
DEFAULT_SCALE, DEFAULT_SCALE,
DEFAULT_SCALE_DELTA, DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
docStyle,
getActiveOrFocusedElement, getActiveOrFocusedElement,
getPageSizeInches, getPageSizeInches,
getVisibleElements, getVisibleElements,