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 {
|
|
|
|
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
|
|
|
}
|
2020-11-16 21:56:58 +09:00
|
|
|
Object.defineProperty(this, "defaults", {
|
|
|
|
value: Object.freeze(
|
|
|
|
typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")
|
|
|
|
? AppOptions.getAll(OptionKind.PREFERENCE)
|
Re-factor the default preferences generation to support build targets (PR 10548)
Originally the default preferences where simply placed in a JSON-file, checked into the repository, which over time became impractical, annoying, and error-prone to maintain; please see PR 10548.
While that improved the overall situation a fair bit, it however inherited one quite unfortunate property of the old JSON-based solution[1]: It's still not possible for *different* build targets to specify their *own* default preference values.
With some preferences, such as e.g. `enableScripting`, it's not inconceivable that you'd want to (at least) support build-specific default preference values. Currently that's not really possible, which is why this PR re-factors the default preferences generation to support this.
---
[1] This fact isn't really clear from the `AppOptions` implementation, unless you're familiar with the `gulpfile.js` code, which could lead to some confusion for those new to this part of the code-base.
2021-03-18 22:33:54 +09:00
|
|
|
: PDFJSDev.eval("DEFAULT_PREFERENCES")
|
2020-11-16 21:56:58 +09:00
|
|
|
),
|
|
|
|
writable: false,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: false,
|
|
|
|
});
|
2021-03-22 22:19:50 +09:00
|
|
|
this.prefs = Object.create(null);
|
2016-05-12 07:58:17 +09:00
|
|
|
|
2020-11-16 21:56:58 +09:00
|
|
|
this._initializedPromise = this._readFromStorage(this.defaults).then(
|
|
|
|
prefs => {
|
2021-03-22 22:19:50 +09:00
|
|
|
for (const name in this.defaults) {
|
|
|
|
const prefValue = prefs?.[name];
|
|
|
|
// Ignore preferences whose types don't match the default values.
|
|
|
|
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() {
|
|
|
|
await this._initializedPromise;
|
2021-03-22 22:19:50 +09:00
|
|
|
this.prefs = Object.create(null);
|
2018-07-30 23:48:16 +09:00
|
|
|
return this._writeToStorage(this.defaults);
|
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) {
|
|
|
|
await this._initializedPromise;
|
2019-12-23 02:21:04 +09:00
|
|
|
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(`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
|
|
|
}
|
2019-12-23 02:21:04 +09:00
|
|
|
const valueType = typeof value;
|
|
|
|
const 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
|
|
|
}
|
2018-07-30 23:48:16 +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
|
|
|
if (valueType === "number" && !Number.isInteger(value)) {
|
2018-07-30 23:48:16 +09:00
|
|
|
throw new Error(`Set preference: "${value}" must be an integer.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.prefs[name] = value;
|
|
|
|
return this._writeToStorage(this.prefs);
|
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) {
|
|
|
|
await this._initializedPromise;
|
2021-03-22 22:30:03 +09:00
|
|
|
const defaultValue = this.defaults[name],
|
|
|
|
prefValue = this.prefs[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.`);
|
|
|
|
}
|
2021-03-22 22:30:03 +09:00
|
|
|
return prefValue !== undefined ? prefValue : 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() {
|
|
|
|
await this._initializedPromise;
|
2021-03-22 22:30:03 +09:00
|
|
|
const obj = Object.create(null);
|
|
|
|
|
|
|
|
for (const name in this.defaults) {
|
|
|
|
const prefValue = this.prefs[name];
|
|
|
|
obj[name] = prefValue !== undefined ? prefValue : this.defaults[name];
|
|
|
|
}
|
|
|
|
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 };
|