UNPKG

14.6 kBTypeScriptView Raw
1import { IDataConnector } from '@jupyterlab/statedb';
2import { JSONValue, PartialJSONValue, ReadonlyJSONObject, ReadonlyPartialJSONObject, ReadonlyPartialJSONValue } from '@lumino/coreutils';
3import { IDisposable } from '@lumino/disposable';
4import { ISignal } from '@lumino/signaling';
5import { ISettingRegistry } from './tokens';
6/**
7 * An implementation of a schema validator.
8 */
9export interface ISchemaValidator {
10 /**
11 * Validate a plugin's schema and user data; populate the `composite` data.
12 *
13 * @param plugin - The plugin being validated. Its `composite` data will be
14 * populated by reference.
15 *
16 * @param populate - Whether plugin data should be populated, defaults to
17 * `true`.
18 *
19 * @return A list of errors if either the schema or data fail to validate or
20 * `null` if there are no errors.
21 */
22 validateData(plugin: ISettingRegistry.IPlugin, populate?: boolean): ISchemaValidator.IError[] | null;
23}
24/**
25 * A namespace for schema validator interfaces.
26 */
27export declare namespace ISchemaValidator {
28 /**
29 * A schema validation error definition.
30 */
31 interface IError {
32 /**
33 * The path in the data where the error occurred.
34 */
35 dataPath: string;
36 /**
37 * The keyword whose validation failed.
38 */
39 keyword: string;
40 /**
41 * The error message.
42 */
43 message: string;
44 /**
45 * Optional parameter metadata that might be included in an error.
46 */
47 params?: ReadonlyJSONObject;
48 /**
49 * The path in the schema where the error occurred.
50 */
51 schemaPath: string;
52 }
53}
54/**
55 * The default implementation of a schema validator.
56 */
57export declare class DefaultSchemaValidator implements ISchemaValidator {
58 /**
59 * Instantiate a schema validator.
60 */
61 constructor();
62 /**
63 * Validate a plugin's schema and user data; populate the `composite` data.
64 *
65 * @param plugin - The plugin being validated. Its `composite` data will be
66 * populated by reference.
67 *
68 * @param populate - Whether plugin data should be populated, defaults to
69 * `true`.
70 *
71 * @return A list of errors if either the schema or data fail to validate or
72 * `null` if there are no errors.
73 */
74 validateData(plugin: ISettingRegistry.IPlugin, populate?: boolean): ISchemaValidator.IError[] | null;
75 /**
76 * Add a schema to the validator.
77 *
78 * @param plugin - The plugin ID.
79 *
80 * @param schema - The schema being added.
81 *
82 * @return A list of errors if the schema fails to validate or `null` if there
83 * are no errors.
84 *
85 * #### Notes
86 * It is safe to call this function multiple times with the same plugin name.
87 */
88 private _addSchema;
89 private _composer;
90 private _validator;
91}
92/**
93 * The default concrete implementation of a setting registry.
94 */
95export declare class SettingRegistry implements ISettingRegistry {
96 /**
97 * Create a new setting registry.
98 */
99 constructor(options: SettingRegistry.IOptions);
100 /**
101 * The data connector used by the setting registry.
102 */
103 readonly connector: IDataConnector<ISettingRegistry.IPlugin, string, string>;
104 /**
105 * The schema of the setting registry.
106 */
107 readonly schema: ISettingRegistry.ISchema;
108 /**
109 * The schema validator used by the setting registry.
110 */
111 readonly validator: ISchemaValidator;
112 /**
113 * A signal that emits the name of a plugin when its settings change.
114 */
115 get pluginChanged(): ISignal<this, string>;
116 /**
117 * The collection of setting registry plugins.
118 */
119 readonly plugins: {
120 [name: string]: ISettingRegistry.IPlugin;
121 };
122 /**
123 * Get an individual setting.
124 *
125 * @param plugin - The name of the plugin whose settings are being retrieved.
126 *
127 * @param key - The name of the setting being retrieved.
128 *
129 * @returns A promise that resolves when the setting is retrieved.
130 */
131 get(plugin: string, key: string): Promise<{
132 composite: PartialJSONValue | undefined;
133 user: PartialJSONValue | undefined;
134 }>;
135 /**
136 * Load a plugin's settings into the setting registry.
137 *
138 * @param plugin - The name of the plugin whose settings are being loaded.
139 *
140 * @returns A promise that resolves with a plugin settings object or rejects
141 * if the plugin is not found.
142 */
143 load(plugin: string): Promise<ISettingRegistry.ISettings>;
144 /**
145 * Reload a plugin's settings into the registry even if they already exist.
146 *
147 * @param plugin - The name of the plugin whose settings are being reloaded.
148 *
149 * @returns A promise that resolves with a plugin settings object or rejects
150 * with a list of `ISchemaValidator.IError` objects if it fails.
151 */
152 reload(plugin: string): Promise<ISettingRegistry.ISettings>;
153 /**
154 * Remove a single setting in the registry.
155 *
156 * @param plugin - The name of the plugin whose setting is being removed.
157 *
158 * @param key - The name of the setting being removed.
159 *
160 * @returns A promise that resolves when the setting is removed.
161 */
162 remove(plugin: string, key: string): Promise<void>;
163 /**
164 * Set a single setting in the registry.
165 *
166 * @param plugin - The name of the plugin whose setting is being set.
167 *
168 * @param key - The name of the setting being set.
169 *
170 * @param value - The value of the setting being set.
171 *
172 * @returns A promise that resolves when the setting has been saved.
173 *
174 */
175 set(plugin: string, key: string, value: JSONValue): Promise<void>;
176 /**
177 * Register a plugin transform function to act on a specific plugin.
178 *
179 * @param plugin - The name of the plugin whose settings are transformed.
180 *
181 * @param transforms - The transform functions applied to the plugin.
182 *
183 * @returns A disposable that removes the transforms from the registry.
184 *
185 * #### Notes
186 * - `compose` transformations: The registry automatically overwrites a
187 * plugin's default values with user overrides, but a plugin may instead wish
188 * to merge values. This behavior can be accomplished in a `compose`
189 * transformation.
190 * - `fetch` transformations: The registry uses the plugin data that is
191 * fetched from its connector. If a plugin wants to override, e.g. to update
192 * its schema with dynamic defaults, a `fetch` transformation can be applied.
193 */
194 transform(plugin: string, transforms: {
195 [phase in ISettingRegistry.IPlugin.Phase]?: ISettingRegistry.IPlugin.Transform;
196 }): IDisposable;
197 /**
198 * Upload a plugin's settings.
199 *
200 * @param plugin - The name of the plugin whose settings are being set.
201 *
202 * @param raw - The raw plugin settings being uploaded.
203 *
204 * @returns A promise that resolves when the settings have been saved.
205 */
206 upload(plugin: string, raw: string): Promise<void>;
207 /**
208 * Load a plugin into the registry.
209 */
210 private _load;
211 /**
212 * Preload a list of plugins and fail gracefully.
213 */
214 private _preload;
215 /**
216 * Save a plugin in the registry.
217 */
218 private _save;
219 /**
220 * Transform the plugin if necessary.
221 */
222 private _transform;
223 /**
224 * Validate and preload a plugin, compose the `composite` data.
225 */
226 private _validate;
227 private _pluginChanged;
228 private _ready;
229 private _timeout;
230 private _transformers;
231}
232/**
233 * A manager for a specific plugin's settings.
234 */
235export declare class Settings implements ISettingRegistry.ISettings {
236 /**
237 * Instantiate a new plugin settings manager.
238 */
239 constructor(options: Settings.IOptions);
240 /**
241 * The plugin name.
242 */
243 readonly id: string;
244 /**
245 * The setting registry instance used as a back-end for these settings.
246 */
247 readonly registry: ISettingRegistry;
248 /**
249 * A signal that emits when the plugin's settings have changed.
250 */
251 get changed(): ISignal<this, void>;
252 /**
253 * The composite of user settings and extension defaults.
254 */
255 get composite(): ReadonlyPartialJSONObject;
256 /**
257 * Test whether the plugin settings manager disposed.
258 */
259 get isDisposed(): boolean;
260 get plugin(): ISettingRegistry.IPlugin;
261 /**
262 * The plugin's schema.
263 */
264 get schema(): ISettingRegistry.ISchema;
265 /**
266 * The plugin settings raw text value.
267 */
268 get raw(): string;
269 /**
270 * Checks if any fields are different from the default value.
271 */
272 isDefault(user: ReadonlyPartialJSONObject): boolean;
273 get isModified(): boolean;
274 /**
275 * The user settings.
276 */
277 get user(): ReadonlyPartialJSONObject;
278 /**
279 * The published version of the NPM package containing these settings.
280 */
281 get version(): string;
282 /**
283 * Return the defaults in a commented JSON format.
284 */
285 annotatedDefaults(): string;
286 /**
287 * Calculate the default value of a setting by iterating through the schema.
288 *
289 * @param key - The name of the setting whose default value is calculated.
290 *
291 * @returns A calculated default JSON value for a specific setting.
292 */
293 default(key?: string): PartialJSONValue | undefined;
294 /**
295 * Dispose of the plugin settings resources.
296 */
297 dispose(): void;
298 /**
299 * Get an individual setting.
300 *
301 * @param key - The name of the setting being retrieved.
302 *
303 * @returns The setting value.
304 *
305 * #### Notes
306 * This method returns synchronously because it uses a cached copy of the
307 * plugin settings that is synchronized with the registry.
308 */
309 get(key: string): {
310 composite: ReadonlyPartialJSONValue | undefined;
311 user: ReadonlyPartialJSONValue | undefined;
312 };
313 /**
314 * Remove a single setting.
315 *
316 * @param key - The name of the setting being removed.
317 *
318 * @returns A promise that resolves when the setting is removed.
319 *
320 * #### Notes
321 * This function is asynchronous because it writes to the setting registry.
322 */
323 remove(key: string): Promise<void>;
324 /**
325 * Save all of the plugin's user settings at once.
326 */
327 save(raw: string): Promise<void>;
328 /**
329 * Set a single setting.
330 *
331 * @param key - The name of the setting being set.
332 *
333 * @param value - The value of the setting.
334 *
335 * @returns A promise that resolves when the setting has been saved.
336 *
337 * #### Notes
338 * This function is asynchronous because it writes to the setting registry.
339 */
340 set(key: string, value: JSONValue): Promise<void>;
341 /**
342 * Validates raw settings with comments.
343 *
344 * @param raw - The JSON with comments string being validated.
345 *
346 * @returns A list of errors or `null` if valid.
347 */
348 validate(raw: string): ISchemaValidator.IError[] | null;
349 /**
350 * Handle plugin changes in the setting registry.
351 */
352 private _onPluginChanged;
353 private _changed;
354 private _isDisposed;
355}
356/**
357 * A namespace for `SettingRegistry` statics.
358 */
359export declare namespace SettingRegistry {
360 /**
361 * The instantiation options for a setting registry
362 */
363 interface IOptions {
364 /**
365 * The data connector used by the setting registry.
366 */
367 connector: IDataConnector<ISettingRegistry.IPlugin, string>;
368 /**
369 * Preloaded plugin data to populate the setting registry.
370 */
371 plugins?: ISettingRegistry.IPlugin[];
372 /**
373 * The number of milliseconds before a `load()` call to the registry waits
374 * before timing out if it requires a transformation that has not been
375 * registered.
376 *
377 * #### Notes
378 * The default value is 7000.
379 */
380 timeout?: number;
381 /**
382 * The validator used to enforce the settings JSON schema.
383 */
384 validator?: ISchemaValidator;
385 }
386 /**
387 * Reconcile the menus.
388 *
389 * @param reference The reference list of menus.
390 * @param addition The list of menus to add.
391 * @param warn Warn if the command items are duplicated within the same menu.
392 * @returns The reconciled list of menus.
393 */
394 function reconcileMenus(reference: ISettingRegistry.IMenu[] | null, addition: ISettingRegistry.IMenu[] | null, warn?: boolean, addNewItems?: boolean): ISettingRegistry.IMenu[];
395 /**
396 * Merge two set of menu items.
397 *
398 * @param reference Reference set of menu items
399 * @param addition New items to add
400 * @param warn Whether to warn if item is duplicated; default to false
401 * @returns The merged set of items
402 */
403 function reconcileItems<T extends ISettingRegistry.IMenuItem>(reference?: T[], addition?: T[], warn?: boolean, addNewItems?: boolean): T[] | undefined;
404 /**
405 * Remove disabled entries from menu items
406 *
407 * @param items Menu items
408 * @returns Filtered menu items
409 */
410 function filterDisabledItems<T extends ISettingRegistry.IMenuItem>(items: T[]): T[];
411 /**
412 * Reconcile default and user shortcuts and return the composite list.
413 *
414 * @param defaults - The list of default shortcuts.
415 *
416 * @param user - The list of user shortcut overrides and additions.
417 *
418 * @returns A loadable list of shortcuts (omitting disabled and overridden).
419 */
420 function reconcileShortcuts(defaults: ISettingRegistry.IShortcut[], user: ISettingRegistry.IShortcut[]): ISettingRegistry.IShortcut[];
421 /**
422 * Merge two set of toolbar items.
423 *
424 * @param reference Reference set of toolbar items
425 * @param addition New items to add
426 * @param warn Whether to warn if item is duplicated; default to false
427 * @returns The merged set of items
428 */
429 function reconcileToolbarItems(reference?: ISettingRegistry.IToolbarItem[], addition?: ISettingRegistry.IToolbarItem[], warn?: boolean): ISettingRegistry.IToolbarItem[] | undefined;
430}
431/**
432 * A namespace for `Settings` statics.
433 */
434export declare namespace Settings {
435 /**
436 * The instantiation options for a `Settings` object.
437 */
438 interface IOptions {
439 /**
440 * The setting values for a plugin.
441 */
442 plugin: ISettingRegistry.IPlugin;
443 /**
444 * The system registry instance used by the settings manager.
445 */
446 registry: ISettingRegistry;
447 }
448}