Allow listening for preference changes in the Firefox PDF viewer (bug 1886586)
This commit is contained in:
parent
1083087eee
commit
44427fa7b2
@ -799,11 +799,7 @@ const PDFViewerApplication = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get supportsCaretBrowsingMode() {
|
get supportsCaretBrowsingMode() {
|
||||||
return shadow(
|
return AppOptions.get("supportsCaretBrowsingMode");
|
||||||
this,
|
|
||||||
"supportsCaretBrowsingMode",
|
|
||||||
AppOptions.get("supportsCaretBrowsingMode")
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
moveCaret(isUp, select) {
|
moveCaret(isUp, select) {
|
||||||
|
|||||||
@ -21,6 +21,12 @@ import { AppOptions, OptionKind } from "./app_options.js";
|
|||||||
* or every time the viewer is loaded.
|
* or every time the viewer is loaded.
|
||||||
*/
|
*/
|
||||||
class BasePreferences {
|
class BasePreferences {
|
||||||
|
#browserDefaults = Object.freeze(
|
||||||
|
typeof PDFJSDev === "undefined"
|
||||||
|
? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true)
|
||||||
|
: PDFJSDev.eval("BROWSER_PREFERENCES")
|
||||||
|
);
|
||||||
|
|
||||||
#defaults = Object.freeze(
|
#defaults = Object.freeze(
|
||||||
typeof PDFJSDev === "undefined"
|
typeof PDFJSDev === "undefined"
|
||||||
? AppOptions.getAll(OptionKind.PREFERENCE, /* defaultOnly = */ true)
|
? AppOptions.getAll(OptionKind.PREFERENCE, /* defaultOnly = */ true)
|
||||||
@ -46,13 +52,11 @@ class BasePreferences {
|
|||||||
|
|
||||||
this.#initializedPromise = this._readFromStorage(this.#defaults).then(
|
this.#initializedPromise = this._readFromStorage(this.#defaults).then(
|
||||||
({ browserPrefs, prefs }) => {
|
({ browserPrefs, prefs }) => {
|
||||||
const BROWSER_PREFS =
|
|
||||||
typeof PDFJSDev === "undefined"
|
|
||||||
? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true)
|
|
||||||
: PDFJSDev.eval("BROWSER_PREFERENCES");
|
|
||||||
const options = Object.create(null);
|
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];
|
const prefVal = browserPrefs?.[name];
|
||||||
options[name] =
|
options[name] =
|
||||||
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
|
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
|
||||||
@ -64,6 +68,12 @@ class BasePreferences {
|
|||||||
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
|
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
|
||||||
}
|
}
|
||||||
AppOptions.setAll(options, /* init = */ true);
|
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");
|
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.
|
* Reset the preferences to their default values and update storage.
|
||||||
* @returns {Promise} A promise that is resolved when the preference values
|
* @returns {Promise} A promise that is resolved when the preference values
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user