1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Merge pull request #17818 from Snuffleupagus/updatePrefs

Allow listening for preference changes in the Firefox PDF viewer (bug 1886586)
This commit is contained in:
Jonas Jenwald 2024-03-21 19:02:25 +01:00 committed by GitHub
commit 067c49dc49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 10 deletions

View file

@ -799,11 +799,7 @@ const PDFViewerApplication = {
},
get supportsCaretBrowsingMode() {
return shadow(
this,
"supportsCaretBrowsingMode",
AppOptions.get("supportsCaretBrowsingMode")
);
return AppOptions.get("supportsCaretBrowsingMode");
},
moveCaret(isUp, select) {

View file

@ -21,6 +21,12 @@ import { AppOptions, OptionKind } from "./app_options.js";
* or every time the viewer is loaded.
*/
class BasePreferences {
#browserDefaults = Object.freeze(
typeof PDFJSDev === "undefined"
? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true)
: PDFJSDev.eval("BROWSER_PREFERENCES")
);
#defaults = Object.freeze(
typeof PDFJSDev === "undefined"
? AppOptions.getAll(OptionKind.PREFERENCE, /* defaultOnly = */ true)
@ -46,13 +52,11 @@ class BasePreferences {
this.#initializedPromise = this._readFromStorage(this.#defaults).then(
({ browserPrefs, prefs }) => {
const BROWSER_PREFS =
typeof PDFJSDev === "undefined"
? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true)
: PDFJSDev.eval("BROWSER_PREFERENCES");
const options = Object.create(null);
for (const [name, defaultVal] of Object.entries(BROWSER_PREFS)) {
for (const [name, defaultVal] of Object.entries(
this.#browserDefaults
)) {
const prefVal = browserPrefs?.[name];
options[name] =
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
@ -64,6 +68,12 @@ class BasePreferences {
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
}
AppOptions.setAll(options, /* init = */ true);
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
window.addEventListener("updatedPreference", evt => {
this.#updatePref(evt.detail);
});
}
}
);
}
@ -88,6 +98,26 @@ class BasePreferences {
throw new Error("Not implemented: _readFromStorage");
}
#updatePref({ name, value }) {
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: #updatePref");
}
if (name in this.#browserDefaults) {
if (typeof value !== typeof this.#browserDefaults[name]) {
return; // Invalid preference value.
}
} else if (name in this.#defaults) {
if (typeof value !== typeof this.#defaults[name]) {
return; // Invalid preference value.
}
this.#prefs[name] = value;
} else {
return; // Invalid preference.
}
AppOptions.set(name, value);
}
/**
* Reset the preferences to their default values and update storage.
* @returns {Promise} A promise that is resolved when the preference values