@jupyterlab/settingeditor 4.0.0-alpha.9 → 4.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,482 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ import { CodeEditor } from '@jupyterlab/codeeditor';
7
+ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
8
+ import { ISettingRegistry } from '@jupyterlab/settingregistry';
9
+ import { IStateDB } from '@jupyterlab/statedb';
10
+ import { ITranslator, nullTranslator } from '@jupyterlab/translation';
11
+ import { jupyterIcon, ReactWidget } from '@jupyterlab/ui-components';
12
+ import { CommandRegistry } from '@lumino/commands';
13
+ import { JSONExt, JSONObject, JSONValue } from '@lumino/coreutils';
14
+ import { Message } from '@lumino/messaging';
15
+ import { ISignal } from '@lumino/signaling';
16
+ import { SplitPanel, Widget } from '@lumino/widgets';
17
+ import * as React from 'react';
18
+ import { PluginEditor } from './plugineditor';
19
+ import { PluginList } from './pluginlist';
20
+
21
+ /**
22
+ * The ratio panes in the setting editor.
23
+ */
24
+ const DEFAULT_LAYOUT: JsonSettingEditor.ILayoutState = {
25
+ sizes: [1, 3],
26
+ container: {
27
+ editor: 'raw',
28
+ plugin: '',
29
+ sizes: [1, 1]
30
+ }
31
+ };
32
+
33
+ /**
34
+ * An interface for modifying and saving application settings.
35
+ */
36
+ export class JsonSettingEditor extends SplitPanel {
37
+ /**
38
+ * Create a new setting editor.
39
+ */
40
+ constructor(options: JsonSettingEditor.IOptions) {
41
+ super({
42
+ orientation: 'horizontal',
43
+ renderer: SplitPanel.defaultRenderer,
44
+ spacing: 1
45
+ });
46
+ this.translator = options.translator || nullTranslator;
47
+ const trans = this.translator.load('jupyterlab');
48
+ this.addClass('jp-SettingEditor');
49
+ this.key = options.key;
50
+ this.state = options.state;
51
+
52
+ const { commands, editorFactory, rendermime } = options;
53
+ const registry = (this.registry = options.registry);
54
+ const instructions = (this._instructions = ReactWidget.create(
55
+ <React.Fragment>
56
+ <h2>
57
+ <jupyterIcon.react
58
+ className="jp-SettingEditorInstructions-icon"
59
+ tag="span"
60
+ elementPosition="center"
61
+ height="auto"
62
+ width="60px"
63
+ />
64
+ <span className="jp-SettingEditorInstructions-title">
65
+ {trans.__('Settings')}
66
+ </span>
67
+ </h2>
68
+ <span className="jp-SettingEditorInstructions-text">
69
+ {trans.__(
70
+ 'Select a plugin from the list to view and edit its preferences.'
71
+ )}
72
+ </span>
73
+ </React.Fragment>
74
+ ));
75
+ instructions.addClass('jp-SettingEditorInstructions');
76
+ const editor = (this._editor = new PluginEditor({
77
+ commands,
78
+ editorFactory,
79
+ registry,
80
+ rendermime,
81
+ translator: this.translator
82
+ }));
83
+ const confirm = () => editor.confirm();
84
+ const list = (this._list = new PluginList({
85
+ confirm,
86
+ registry,
87
+ translator: this.translator
88
+ }));
89
+ const when = options.when;
90
+
91
+ if (when) {
92
+ this._when = Array.isArray(when) ? Promise.all(when) : when;
93
+ }
94
+
95
+ this.addWidget(list);
96
+ this.addWidget(instructions);
97
+
98
+ SplitPanel.setStretch(list, 0);
99
+ SplitPanel.setStretch(instructions, 1);
100
+ SplitPanel.setStretch(editor, 1);
101
+
102
+ editor.stateChanged.connect(this._onStateChanged, this);
103
+ list.changed.connect(this._onStateChanged, this);
104
+ this.handleMoved.connect(this._onStateChanged, this);
105
+ }
106
+
107
+ /**
108
+ * The state database key for the editor's state management.
109
+ */
110
+ readonly key: string;
111
+
112
+ /**
113
+ * The setting registry used by the editor.
114
+ */
115
+ readonly registry: ISettingRegistry;
116
+
117
+ /**
118
+ * The state database used to store layout.
119
+ */
120
+ readonly state: IStateDB;
121
+
122
+ /**
123
+ * Whether the raw editor revert functionality is enabled.
124
+ */
125
+ get canRevertRaw(): boolean {
126
+ return this._editor.raw.canRevert;
127
+ }
128
+
129
+ /**
130
+ * Whether the raw editor save functionality is enabled.
131
+ */
132
+ get canSaveRaw(): boolean {
133
+ return this._editor.raw.canSave;
134
+ }
135
+
136
+ /**
137
+ * Emits when the commands passed in at instantiation change.
138
+ */
139
+ get commandsChanged(): ISignal<any, string[]> {
140
+ return this._editor.raw.commandsChanged;
141
+ }
142
+
143
+ /**
144
+ * The currently loaded settings.
145
+ */
146
+ get settings(): ISettingRegistry.ISettings | null {
147
+ return this._editor.settings;
148
+ }
149
+
150
+ /**
151
+ * The inspectable raw user editor source for the currently loaded settings.
152
+ */
153
+ get source(): CodeEditor.IEditor {
154
+ return this._editor.raw.source;
155
+ }
156
+
157
+ /**
158
+ * Dispose of the resources held by the setting editor.
159
+ */
160
+ dispose(): void {
161
+ if (this.isDisposed) {
162
+ return;
163
+ }
164
+
165
+ super.dispose();
166
+ this._editor.dispose();
167
+ this._instructions.dispose();
168
+ this._list.dispose();
169
+ }
170
+
171
+ /**
172
+ * Revert raw editor back to original settings.
173
+ */
174
+ revert(): void {
175
+ this._editor.raw.revert();
176
+ }
177
+
178
+ /**
179
+ * Save the contents of the raw editor.
180
+ */
181
+ save(): Promise<void> {
182
+ return this._editor.raw.save();
183
+ }
184
+
185
+ /**
186
+ * Handle `'after-attach'` messages.
187
+ */
188
+ protected onAfterAttach(msg: Message): void {
189
+ super.onAfterAttach(msg);
190
+ this.hide();
191
+ this._fetchState()
192
+ .then(() => {
193
+ this.show();
194
+ this._setState();
195
+ })
196
+ .catch(reason => {
197
+ console.error('Fetching setting editor state failed', reason);
198
+ this.show();
199
+ this._setState();
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Handle `'close-request'` messages.
205
+ */
206
+ protected onCloseRequest(msg: Message): void {
207
+ this._editor
208
+ .confirm()
209
+ .then(() => {
210
+ super.onCloseRequest(msg);
211
+ this.dispose();
212
+ })
213
+ .catch(() => {
214
+ /* no op */
215
+ });
216
+ }
217
+
218
+ /**
219
+ * Get the state of the panel.
220
+ */
221
+ private _fetchState(): Promise<void> {
222
+ if (this._fetching) {
223
+ return this._fetching;
224
+ }
225
+
226
+ const { key, state } = this;
227
+ const promises = [state.fetch(key), this._when];
228
+
229
+ return (this._fetching = Promise.all(promises).then(([value]) => {
230
+ this._fetching = null;
231
+
232
+ if (this._saving) {
233
+ return;
234
+ }
235
+
236
+ this._state = Private.normalizeState(value, this._state);
237
+ }));
238
+ }
239
+
240
+ /**
241
+ * Handle root level layout state changes.
242
+ */
243
+ private async _onStateChanged(): Promise<void> {
244
+ this._state.sizes = this.relativeSizes();
245
+ this._state.container = this._editor.state;
246
+ this._state.container.plugin = this._list.selection;
247
+ try {
248
+ await this._saveState();
249
+ } catch (error) {
250
+ console.error('Saving setting editor state failed', error);
251
+ }
252
+ this._setState();
253
+ }
254
+
255
+ /**
256
+ * Set the state of the setting editor.
257
+ */
258
+ private async _saveState(): Promise<void> {
259
+ const { key, state } = this;
260
+ const value = this._state;
261
+
262
+ this._saving = true;
263
+ try {
264
+ await state.save(key, value);
265
+ this._saving = false;
266
+ } catch (error) {
267
+ this._saving = false;
268
+ throw error;
269
+ }
270
+ }
271
+
272
+ /**
273
+ * Set the layout sizes.
274
+ */
275
+ private _setLayout(): void {
276
+ const editor = this._editor;
277
+ const state = this._state;
278
+
279
+ editor.state = state.container;
280
+
281
+ // Allow the message queue (which includes fit requests that might disrupt
282
+ // setting relative sizes) to clear before setting sizes.
283
+ requestAnimationFrame(() => {
284
+ this.setRelativeSizes(state.sizes);
285
+ });
286
+ }
287
+
288
+ /**
289
+ * Set the presets of the setting editor.
290
+ */
291
+ private _setState(): void {
292
+ const editor = this._editor;
293
+ const list = this._list;
294
+ const { container } = this._state;
295
+
296
+ if (!container.plugin) {
297
+ editor.settings = null;
298
+ list.selection = '';
299
+ this._setLayout();
300
+ return;
301
+ }
302
+
303
+ if (editor.settings && editor.settings.id === container.plugin) {
304
+ this._setLayout();
305
+ return;
306
+ }
307
+
308
+ const instructions = this._instructions;
309
+
310
+ this.registry
311
+ .load(container.plugin)
312
+ .then(settings => {
313
+ if (instructions.isAttached) {
314
+ instructions.parent = null;
315
+ }
316
+ if (!editor.isAttached) {
317
+ this.addWidget(editor);
318
+ }
319
+ editor.settings = settings;
320
+ list.selection = container.plugin;
321
+ this._setLayout();
322
+ })
323
+ .catch(reason => {
324
+ console.error(`Loading ${container.plugin} settings failed.`, reason);
325
+ list.selection = this._state.container.plugin = '';
326
+ editor.settings = null;
327
+ this._setLayout();
328
+ });
329
+ }
330
+
331
+ protected translator: ITranslator;
332
+ private _editor: PluginEditor;
333
+ private _fetching: Promise<void> | null = null;
334
+ private _instructions: Widget;
335
+ private _list: PluginList;
336
+ private _saving = false;
337
+ private _state: JsonSettingEditor.ILayoutState =
338
+ JSONExt.deepCopy(DEFAULT_LAYOUT);
339
+ private _when: Promise<any>;
340
+ }
341
+
342
+ /**
343
+ * A namespace for `JsonSettingEditor` statics.
344
+ */
345
+ export namespace JsonSettingEditor {
346
+ /**
347
+ * The instantiation options for a setting editor.
348
+ */
349
+ export interface IOptions {
350
+ /**
351
+ * The toolbar commands and registry for the setting editor toolbar.
352
+ */
353
+ commands: {
354
+ /**
355
+ * The command registry.
356
+ */
357
+ registry: CommandRegistry;
358
+
359
+ /**
360
+ * The revert command ID.
361
+ */
362
+ revert: string;
363
+
364
+ /**
365
+ * The save command ID.
366
+ */
367
+ save: string;
368
+ };
369
+
370
+ /**
371
+ * The editor factory used by the setting editor.
372
+ */
373
+ editorFactory: CodeEditor.Factory;
374
+
375
+ /**
376
+ * The state database key for the editor's state management.
377
+ */
378
+ key: string;
379
+
380
+ /**
381
+ * The setting registry the editor modifies.
382
+ */
383
+ registry: ISettingRegistry;
384
+
385
+ /**
386
+ * The optional MIME renderer to use for rendering debug messages.
387
+ */
388
+ rendermime?: IRenderMimeRegistry;
389
+
390
+ /**
391
+ * The state database used to store layout.
392
+ */
393
+ state: IStateDB;
394
+
395
+ /**
396
+ * The point after which the editor should restore its state.
397
+ */
398
+ when?: Promise<any> | Array<Promise<any>>;
399
+
400
+ /**
401
+ * The application language translator.
402
+ */
403
+ translator?: ITranslator;
404
+ }
405
+
406
+ /**
407
+ * The layout state for the setting editor.
408
+ */
409
+ export interface ILayoutState extends JSONObject {
410
+ /**
411
+ * The layout state for a plugin editor container.
412
+ */
413
+ container: IPluginLayout;
414
+
415
+ /**
416
+ * The relative sizes of the plugin list and plugin editor.
417
+ */
418
+ sizes: number[];
419
+ }
420
+
421
+ /**
422
+ * The layout information that is stored and restored from the state database.
423
+ */
424
+ export interface IPluginLayout extends JSONObject {
425
+ /**
426
+ * The current plugin being displayed.
427
+ */
428
+ plugin: string;
429
+ sizes: number[];
430
+ }
431
+ }
432
+
433
+ /**
434
+ * A namespace for private module data.
435
+ */
436
+ namespace Private {
437
+ /**
438
+ * Return a normalized restored layout state that defaults to the presets.
439
+ */
440
+ export function normalizeState(
441
+ saved: JSONObject | null,
442
+ current: JsonSettingEditor.ILayoutState
443
+ ): JsonSettingEditor.ILayoutState {
444
+ if (!saved) {
445
+ return JSONExt.deepCopy(DEFAULT_LAYOUT);
446
+ }
447
+
448
+ if (!('sizes' in saved) || !numberArray(saved.sizes)) {
449
+ saved.sizes = JSONExt.deepCopy(DEFAULT_LAYOUT.sizes);
450
+ }
451
+ if (!('container' in saved)) {
452
+ saved.container = JSONExt.deepCopy(DEFAULT_LAYOUT.container);
453
+ return saved as JsonSettingEditor.ILayoutState;
454
+ }
455
+
456
+ const container =
457
+ 'container' in saved &&
458
+ saved.container &&
459
+ typeof saved.container === 'object'
460
+ ? (saved.container as JSONObject)
461
+ : {};
462
+
463
+ saved.container = {
464
+ plugin:
465
+ typeof container.plugin === 'string'
466
+ ? container.plugin
467
+ : DEFAULT_LAYOUT.container.plugin,
468
+ sizes: numberArray(container.sizes)
469
+ ? container.sizes
470
+ : JSONExt.deepCopy(DEFAULT_LAYOUT.container.sizes)
471
+ };
472
+
473
+ return saved as JsonSettingEditor.ILayoutState;
474
+ }
475
+
476
+ /**
477
+ * Tests whether an array consists exclusively of numbers.
478
+ */
479
+ function numberArray(value: JSONValue): boolean {
480
+ return Array.isArray(value) && value.every(x => typeof x === 'number');
481
+ }
482
+ }