Skip to content

Commit

Permalink
Merge pull request #17818 from Snuffleupagus/updatePrefs
Browse files Browse the repository at this point in the history
Allow listening for preference changes in the Firefox PDF viewer (bug 1886586)
  • Loading branch information
Snuffleupagus authored Mar 21, 2024
2 parents 1083087 + 44427fa commit 067c49d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
6 changes: 1 addition & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,7 @@ const PDFViewerApplication = {
},

get supportsCaretBrowsingMode() {
return shadow(
this,
"supportsCaretBrowsingMode",
AppOptions.get("supportsCaretBrowsingMode")
);
return AppOptions.get("supportsCaretBrowsingMode");
},

moveCaret(isUp, select) {
Expand Down
40 changes: 35 additions & 5 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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);
});
}
}
);
}
Expand All @@ -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
Expand Down

0 comments on commit 067c49d

Please sign in to comment.