UNPKG

4.34 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.BasePreferences = void 0;
28
29var _app_options = require("./app_options.js");
30
31class BasePreferences {
32 #defaults = Object.freeze({
33 "annotationEditorMode": -1,
34 "annotationMode": 2,
35 "cursorToolOnLoad": 0,
36 "defaultZoomValue": "",
37 "disablePageLabels": false,
38 "enablePermissions": false,
39 "enablePrintAutoRotate": true,
40 "enableScripting": true,
41 "externalLinkTarget": 0,
42 "historyUpdateUrl": false,
43 "ignoreDestinationZoom": false,
44 "forcePageColors": false,
45 "pageColorsBackground": "Canvas",
46 "pageColorsForeground": "CanvasText",
47 "pdfBugEnabled": false,
48 "sidebarViewOnLoad": -1,
49 "scrollModeOnLoad": -1,
50 "spreadModeOnLoad": -1,
51 "textLayerMode": 1,
52 "useOnlyCssZoom": false,
53 "viewerCssTheme": 0,
54 "viewOnLoad": 0,
55 "disableAutoFetch": false,
56 "disableFontFace": false,
57 "disableRange": false,
58 "disableStream": false,
59 "enableXfa": true,
60 "renderer": "canvas"
61 });
62 #prefs = Object.create(null);
63 #initializedPromise = null;
64
65 constructor() {
66 if (this.constructor === BasePreferences) {
67 throw new Error("Cannot initialize BasePreferences.");
68 }
69
70 this.#initializedPromise = this._readFromStorage(this.#defaults).then(prefs => {
71 for (const name in this.#defaults) {
72 const prefValue = prefs?.[name];
73
74 if (typeof prefValue === typeof this.#defaults[name]) {
75 this.#prefs[name] = prefValue;
76 }
77 }
78 });
79 }
80
81 async _writeToStorage(prefObj) {
82 throw new Error("Not implemented: _writeToStorage");
83 }
84
85 async _readFromStorage(prefObj) {
86 throw new Error("Not implemented: _readFromStorage");
87 }
88
89 async reset() {
90 await this.#initializedPromise;
91 const prefs = this.#prefs;
92 this.#prefs = Object.create(null);
93 return this._writeToStorage(this.#defaults).catch(reason => {
94 this.#prefs = prefs;
95 throw reason;
96 });
97 }
98
99 async set(name, value) {
100 await this.#initializedPromise;
101 const defaultValue = this.#defaults[name],
102 prefs = this.#prefs;
103
104 if (defaultValue === undefined) {
105 throw new Error(`Set preference: "${name}" is undefined.`);
106 } else if (value === undefined) {
107 throw new Error("Set preference: no value is specified.");
108 }
109
110 const valueType = typeof value,
111 defaultType = typeof defaultValue;
112
113 if (valueType !== defaultType) {
114 if (valueType === "number" && defaultType === "string") {
115 value = value.toString();
116 } else {
117 throw new Error(`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`);
118 }
119 } else {
120 if (valueType === "number" && !Number.isInteger(value)) {
121 throw new Error(`Set preference: "${value}" must be an integer.`);
122 }
123 }
124
125 this.#prefs[name] = value;
126 return this._writeToStorage(this.#prefs).catch(reason => {
127 this.#prefs = prefs;
128 throw reason;
129 });
130 }
131
132 async get(name) {
133 await this.#initializedPromise;
134 const defaultValue = this.#defaults[name];
135
136 if (defaultValue === undefined) {
137 throw new Error(`Get preference: "${name}" is undefined.`);
138 }
139
140 return this.#prefs[name] ?? defaultValue;
141 }
142
143 async getAll() {
144 await this.#initializedPromise;
145 const obj = Object.create(null);
146
147 for (const name in this.#defaults) {
148 obj[name] = this.#prefs[name] ?? this.#defaults[name];
149 }
150
151 return obj;
152 }
153
154}
155
156exports.BasePreferences = BasePreferences;
\No newline at end of file