/
settingseditor.tsx
217 lines (198 loc) · 6.02 KB
/
settingseditor.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
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import { ILabStatus } from '@jupyterlab/application';
import { showDialog } from '@jupyterlab/apputils';
import { ISettingRegistry, Settings } from '@jupyterlab/settingregistry';
import { IStateDB } from '@jupyterlab/statedb';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { IFormRendererRegistry, ReactWidget } from '@jupyterlab/ui-components';
import { CommandRegistry } from '@lumino/commands';
import { IDisposable } from '@lumino/disposable';
import { Message } from '@lumino/messaging';
import { ISignal, Signal } from '@lumino/signaling';
import { SplitPanel } from '@lumino/widgets';
import React from 'react';
import { PluginList } from './pluginlist';
import { SettingsPanel } from './settingspanel';
/**
* Form based interface for editing settings.
*/
export class SettingsEditor extends SplitPanel {
constructor(options: SettingsEditor.IOptions) {
super({
orientation: 'horizontal',
renderer: SplitPanel.defaultRenderer,
spacing: 1
});
this.translator = options.translator || nullTranslator;
this._status = options.status;
const list = (this._list = new PluginList({
registry: options.registry,
toSkip: options.toSkip,
translator: this.translator,
query: options.query
}));
this.addWidget(list);
this.setDirtyState = this.setDirtyState.bind(this);
/**
* Initializes the settings panel after loading the schema for all plugins.
*/
void Promise.all(
PluginList.sortPlugins(options.registry)
.filter(plugin => {
const { schema } = plugin;
const deprecated = schema['jupyter.lab.setting-deprecated'] === true;
const editable = Object.keys(schema.properties || {}).length > 0;
const extensible = schema.additionalProperties !== false;
return !deprecated && (editable || extensible);
})
.map(async plugin => await options.registry.load(plugin.id))
)
.then(settings => {
const settingsPanel = ReactWidget.create(
<SettingsPanel
settings={
settings.filter(
pluginSettings =>
!(options.toSkip ?? []).includes(pluginSettings.id)
) as Settings[]
}
editorRegistry={options.editorRegistry}
handleSelectSignal={this._list.handleSelectSignal}
onSelect={(id: string) => (this._list.selection = id)}
hasError={this._list.setError}
updateFilterSignal={this._list.updateFilterSignal}
updateDirtyState={this.setDirtyState}
translator={this.translator}
initialFilter={this._list.filter}
/>
);
this.addWidget(settingsPanel);
})
.catch(reason => {
console.error(`Fail to load the setting plugins:\n${reason}`);
});
}
/**
* A signal emitted on the start and end of a saving operation.
*/
get saveStateChanged(): ISignal<this, SettingsEditor.SaveState> {
return this._saveStateChange;
}
/**
* Set the dirty state status
*
* @param dirty New status
*/
setDirtyState(dirty: boolean): void {
this._dirty = dirty;
if (this._dirty && !this._clearDirty) {
this._clearDirty = this._status.setDirty();
} else if (!this._dirty && this._clearDirty) {
this._clearDirty.dispose();
this._clearDirty = null;
}
if (dirty) {
if (!this.title.className.includes('jp-mod-dirty')) {
this.title.className += ' jp-mod-dirty';
}
} else {
this.title.className = this.title.className.replace('jp-mod-dirty', '');
}
this._saveStateChange.emit(dirty ? 'started' : 'completed');
}
/**
* A message handler invoked on a `'close-request'` message.
*
* @param msg Widget message
*/
protected onCloseRequest(msg: Message): void {
const trans = this.translator.load('jupyterlab');
if (this._list.hasErrors) {
void showDialog({
title: trans.__('Warning'),
body: trans.__(
'Unsaved changes due to validation error. Continue without saving?'
)
}).then(value => {
if (value.button.accept) {
this.dispose();
super.onCloseRequest(msg);
}
});
} else if (this._dirty) {
void showDialog({
title: trans.__('Warning'),
body: trans.__(
'Some changes have not been saved. Continue without saving?'
)
}).then(value => {
if (value.button.accept) {
this.dispose();
super.onCloseRequest(msg);
}
});
} else {
this.dispose();
super.onCloseRequest(msg);
}
}
protected translator: ITranslator;
private _clearDirty: IDisposable | null = null;
private _status: ILabStatus;
private _dirty: boolean = false;
private _list: PluginList;
private _saveStateChange = new Signal<this, SettingsEditor.SaveState>(this);
}
export namespace SettingsEditor {
/**
* Settings editor save state
*/
export type SaveState = 'started' | 'failed' | 'completed';
/**
*
*/
export type PluginSearchFilter =
| ((plugin: ISettingRegistry.IPlugin) => string[] | null)
| null;
/**
* Settings editor options
*/
export interface IOptions {
/**
* Form component registry
*/
editorRegistry: IFormRendererRegistry;
/**
* The state database key for the editor's state management.
*/
key: string;
/**
* The setting registry the editor modifies.
*/
registry: ISettingRegistry;
/**
* The state database used to store layout.
*/
state: IStateDB;
/**
* Command registry used to open the JSON settings editor.
*/
commands: CommandRegistry;
/**
* Application status
*/
status: ILabStatus;
/**
* List of plugins to skip
*/
toSkip?: string[];
/**
* The application language translator.
*/
translator?: ITranslator;
query?: string;
}
}