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