2013-11-19 07:51:06 +09:00
|
|
|
/* Copyright 2013 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-16 23:00:19 +09:00
|
|
|
import { AppOptions, OptionKind } from "./app_options.js";
|
|
|
|
|
2014-03-17 06:33:39 +09:00
|
|
|
/**
|
2017-04-17 20:21:11 +09:00
|
|
|
* BasePreferences - Abstract base class for storing persistent settings.
|
2014-03-17 06:33:39 +09:00
|
|
|
* Used for settings that should be applied to all opened documents,
|
|
|
|
* or every time the viewer is loaded.
|
|
|
|
*/
|
2017-04-17 20:21:11 +09:00
|
|
|
class BasePreferences {
|
2022-03-12 21:46:59 +09:00
|
|
|
#defaults = Object.freeze(
|
2023-03-18 20:09:25 +09:00
|
|
|
typeof PDFJSDev === "undefined"
|
2022-03-12 21:46:59 +09:00
|
|
|
? AppOptions.getAll(OptionKind.PREFERENCE)
|
|
|
|
: PDFJSDev.eval("DEFAULT_PREFERENCES")
|
|
|
|
);
|
|
|
|
|
|
|
|
#prefs = Object.create(null);
|
|
|
|
|
|
|
|
#initializedPromise = null;
|
|
|
|
|
2017-04-17 20:21:11 +09:00
|
|
|
constructor() {
|
|
|
|
if (this.constructor === BasePreferences) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
throw new Error("Cannot initialize BasePreferences.");
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2022-03-12 21:46:59 +09:00
|
|
|
|
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
|
|
|
|
Object.defineProperty(this, "defaults", {
|
|
|
|
get() {
|
|
|
|
return this.#defaults;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#initializedPromise = this._readFromStorage(this.#defaults).then(
|
2020-11-16 21:56:58 +09:00
|
|
|
prefs => {
|
2022-03-12 21:46:59 +09:00
|
|
|
for (const name in this.#defaults) {
|
2021-03-22 22:19:50 +09:00
|
|
|
const prefValue = prefs?.[name];
|
|
|
|
// Ignore preferences whose types don't match the default values.
|
2022-03-12 21:46:59 +09:00
|
|
|
if (typeof prefValue === typeof this.#defaults[name]) {
|
|
|
|
this.#prefs[name] = prefValue;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 21:56:58 +09:00
|
|
|
}
|
|
|
|
);
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2014-03-17 06:33:39 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub function for writing preferences to storage.
|
|
|
|
* @param {Object} prefObj The preferences that should be written to storage.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved when the preference values
|
|
|
|
* have been written.
|
2014-03-17 06:33:39 +09:00
|
|
|
*/
|
2018-07-30 23:48:16 +09:00
|
|
|
async _writeToStorage(prefObj) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
throw new Error("Not implemented: _writeToStorage");
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2014-03-17 06:33:39 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub function for reading preferences from storage.
|
|
|
|
* @param {Object} prefObj The preferences that should be read from storage.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved with an {Object} containing
|
|
|
|
* the preferences that have been read.
|
2014-03-17 06:33:39 +09:00
|
|
|
*/
|
2018-07-30 23:48:16 +09:00
|
|
|
async _readFromStorage(prefObj) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
throw new Error("Not implemented: _readFromStorage");
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2014-03-17 06:33:39 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the preferences to their default values and update storage.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved when the preference values
|
|
|
|
* have been reset.
|
2014-03-17 06:33:39 +09:00
|
|
|
*/
|
2018-07-30 23:48:16 +09:00
|
|
|
async reset() {
|
2023-06-22 01:28:35 +09:00
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|
|
|
throw new Error("Please use `about:config` to change preferences.");
|
|
|
|
}
|
2022-03-12 21:46:59 +09:00
|
|
|
await this.#initializedPromise;
|
2022-03-12 22:40:18 +09:00
|
|
|
const prefs = this.#prefs;
|
|
|
|
|
2022-03-12 21:46:59 +09:00
|
|
|
this.#prefs = Object.create(null);
|
2022-03-12 22:40:18 +09:00
|
|
|
return this._writeToStorage(this.#defaults).catch(reason => {
|
|
|
|
// Revert all preference values, since writing to storage failed.
|
|
|
|
this.#prefs = prefs;
|
|
|
|
throw reason;
|
|
|
|
});
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2014-03-17 06:33:39 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a preference.
|
|
|
|
* @param {string} name The name of the preference that should be changed.
|
|
|
|
* @param {boolean|number|string} value The new value of the preference.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved when the value has been set,
|
|
|
|
* provided that the preference exists and the types match.
|
2014-03-17 06:33:39 +09:00
|
|
|
*/
|
2018-07-30 23:48:16 +09:00
|
|
|
async set(name, value) {
|
2023-06-22 01:28:35 +09:00
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|
|
|
throw new Error("Please use `about:config` to change preferences.");
|
|
|
|
}
|
2022-03-12 21:46:59 +09:00
|
|
|
await this.#initializedPromise;
|
2022-03-12 22:40:18 +09:00
|
|
|
const defaultValue = this.#defaults[name],
|
|
|
|
prefs = this.#prefs;
|
2013-11-19 07:51:06 +09:00
|
|
|
|
2018-07-30 23:48:16 +09:00
|
|
|
if (defaultValue === undefined) {
|
|
|
|
throw new Error(`Set preference: "${name}" is undefined.`);
|
|
|
|
} else if (value === undefined) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
throw new Error("Set preference: no value is specified.");
|
2018-07-30 23:48:16 +09:00
|
|
|
}
|
2022-03-12 22:40:18 +09:00
|
|
|
const valueType = typeof value,
|
|
|
|
defaultType = typeof defaultValue;
|
2018-07-30 23:48:16 +09:00
|
|
|
|
|
|
|
if (valueType !== defaultType) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (valueType === "number" && defaultType === "string") {
|
2018-07-30 23:48:16 +09:00
|
|
|
value = value.toString();
|
2014-01-31 02:09:31 +09:00
|
|
|
} else {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
throw new Error(
|
2021-03-22 22:19:50 +09:00
|
|
|
`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
);
|
2013-11-19 07:51:06 +09:00
|
|
|
}
|
2023-07-20 17:57:30 +09:00
|
|
|
} else if (valueType === "number" && !Number.isInteger(value)) {
|
|
|
|
throw new Error(`Set preference: "${value}" must be an integer.`);
|
2018-07-30 23:48:16 +09:00
|
|
|
}
|
2022-03-12 22:40:18 +09:00
|
|
|
|
2022-03-12 21:46:59 +09:00
|
|
|
this.#prefs[name] = value;
|
2022-03-12 22:40:18 +09:00
|
|
|
return this._writeToStorage(this.#prefs).catch(reason => {
|
|
|
|
// Revert all preference values, since writing to storage failed.
|
|
|
|
this.#prefs = prefs;
|
|
|
|
throw reason;
|
|
|
|
});
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2013-11-19 07:51:06 +09:00
|
|
|
|
2014-03-17 06:33:39 +09:00
|
|
|
/**
|
|
|
|
* Get the value of a preference.
|
|
|
|
* @param {string} name The name of the preference whose value is requested.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {Promise} A promise resolved with a {boolean|number|string}
|
|
|
|
* containing the value of the preference.
|
2014-03-17 06:33:39 +09:00
|
|
|
*/
|
2018-07-30 23:48:16 +09:00
|
|
|
async get(name) {
|
2022-03-12 21:46:59 +09:00
|
|
|
await this.#initializedPromise;
|
|
|
|
const defaultValue = this.#defaults[name];
|
2013-11-19 07:51:06 +09:00
|
|
|
|
2018-07-30 23:48:16 +09:00
|
|
|
if (defaultValue === undefined) {
|
|
|
|
throw new Error(`Get preference: "${name}" is undefined.`);
|
|
|
|
}
|
2022-03-12 21:46:59 +09:00
|
|
|
return this.#prefs[name] ?? defaultValue;
|
2017-04-17 20:21:11 +09:00
|
|
|
}
|
2018-07-24 03:12:30 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the values of all preferences.
|
2019-10-13 01:14:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved with an {Object} containing
|
|
|
|
* the values of all preferences.
|
2018-07-24 03:12:30 +09:00
|
|
|
*/
|
2018-07-30 23:48:16 +09:00
|
|
|
async getAll() {
|
2022-03-12 21:46:59 +09:00
|
|
|
await this.#initializedPromise;
|
2021-03-22 22:30:03 +09:00
|
|
|
const obj = Object.create(null);
|
|
|
|
|
2022-03-12 21:46:59 +09:00
|
|
|
for (const name in this.#defaults) {
|
|
|
|
obj[name] = this.#prefs[name] ?? this.#defaults[name];
|
2021-03-22 22:30:03 +09:00
|
|
|
}
|
|
|
|
return obj;
|
2018-07-24 03:12:30 +09:00
|
|
|
}
|
2016-10-15 00:57:53 +09:00
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
export { BasePreferences };
|