Re-factor how the compatibilityParams, in the viewer, are handled

Previously we'd simply export this directly from `web/app_options.js`, which meant that it'd be technically possible to *accidentally* modify the `compatibilityParams` Object when accessing it.
To avoid this we instead introduce a new `AppOptions`-method that is used to lookup data in `compatibilityParams`, which means that we no longer need to export this Object.

Based on these changes, it's now possible to simplify some existing code in `AppOptions` by taking full advantage of the nullish coalescing (`??`) operator.
This commit is contained in:
Jonas Jenwald 2024-02-20 10:50:28 +01:00
parent 90b2664622
commit 38004b65b1
2 changed files with 18 additions and 24 deletions

View File

@ -438,16 +438,17 @@ class AppOptions {
throw new Error("Cannot initialize AppOptions."); throw new Error("Cannot initialize AppOptions.");
} }
static getCompat(name) {
return compatibilityParams[name] ?? undefined;
}
static get(name) { static get(name) {
const userOption = userOptions[name]; return (
if (userOption !== undefined) { userOptions[name] ??
return userOption; compatibilityParams[name] ??
} defaultOptions[name]?.value ??
const defaultOption = defaultOptions[name]; undefined
if (defaultOption !== undefined) { );
return compatibilityParams[name] ?? defaultOption.value;
}
return undefined;
} }
static getAll(kind = null, defaultOnly = false) { static getAll(kind = null, defaultOnly = false) {
@ -458,16 +459,9 @@ class AppOptions {
if (kind && !(kind & defaultOption.kind)) { if (kind && !(kind & defaultOption.kind)) {
continue; continue;
} }
if (defaultOnly) { options[name] = defaultOnly
options[name] = defaultOption.value; ? defaultOption.value
continue; : userOptions[name] ?? compatibilityParams[name] ?? defaultOption.value;
}
const userOption = userOptions[name];
options[name] =
userOption !== undefined
? userOption
: compatibilityParams[name] ?? defaultOption.value;
} }
return options; return options;
} }
@ -501,4 +495,4 @@ class AppOptions {
} }
} }
export { AppOptions, compatibilityParams, OptionKind }; export { AppOptions, OptionKind };

View File

@ -41,7 +41,7 @@ import {
} from "./ui_utils.js"; } from "./ui_utils.js";
import { AnnotationEditorLayerBuilder } from "./annotation_editor_layer_builder.js"; import { AnnotationEditorLayerBuilder } from "./annotation_editor_layer_builder.js";
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js"; import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
import { compatibilityParams } from "./app_options.js"; import { AppOptions } from "./app_options.js";
import { DrawLayerBuilder } from "./draw_layer_builder.js"; import { DrawLayerBuilder } from "./draw_layer_builder.js";
import { GenericL10n } from "web-null_l10n"; import { GenericL10n } from "web-null_l10n";
import { SimpleLinkService } from "./pdf_link_service.js"; import { SimpleLinkService } from "./pdf_link_service.js";
@ -83,8 +83,6 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
* the necessary layer-properties. * the necessary layer-properties.
*/ */
const MAX_CANVAS_PIXELS = compatibilityParams.maxCanvasPixels || 16777216;
const DEFAULT_LAYER_PROPERTIES = const DEFAULT_LAYER_PROPERTIES =
typeof PDFJSDev === "undefined" || !PDFJSDev.test("COMPONENTS") typeof PDFJSDev === "undefined" || !PDFJSDev.test("COMPONENTS")
? null ? null
@ -152,7 +150,9 @@ class PDFPageView {
this.#annotationMode = this.#annotationMode =
options.annotationMode ?? AnnotationMode.ENABLE_FORMS; options.annotationMode ?? AnnotationMode.ENABLE_FORMS;
this.imageResourcesPath = options.imageResourcesPath || ""; this.imageResourcesPath = options.imageResourcesPath || "";
this.maxCanvasPixels = options.maxCanvasPixels ?? MAX_CANVAS_PIXELS; this.maxCanvasPixels =
options.maxCanvasPixels ??
(AppOptions.getCompat("maxCanvasPixels") || 16777216);
this.pageColors = options.pageColors || null; this.pageColors = options.pageColors || null;
this.eventBus = options.eventBus; this.eventBus = options.eventBus;