forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugineditor.ts
248 lines (204 loc) · 5.66 KB
/
plugineditor.ts
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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { Dialog, showDialog } from '@jupyterlab/apputils';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { ISettingRegistry } from '@jupyterlab/coreutils';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { CommandRegistry } from '@phosphor/commands';
import { JSONExt } from '@phosphor/coreutils';
import { Message } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget, StackedLayout } from '@phosphor/widgets';
import { RawEditor } from './raweditor';
import { SettingEditor } from './settingeditor';
/**
* The class name added to all plugin editors.
*/
const PLUGIN_EDITOR_CLASS = 'jp-PluginEditor';
/**
* An individual plugin settings editor.
*/
export class PluginEditor extends Widget {
/**
* Create a new plugin editor.
*
* @param options - The plugin editor instantiation options.
*/
constructor(options: PluginEditor.IOptions) {
super();
this.addClass(PLUGIN_EDITOR_CLASS);
const { commands, editorFactory, registry, rendermime } = options;
// TODO: Remove this layout. We were using this before when we
// when we had a way to switch between the raw and table editor
// Now, the raw editor is the only child and probably could merged into
// this class directly in the future.
const layout = (this.layout = new StackedLayout());
const { onSaveError } = Private;
this.raw = this._rawEditor = new RawEditor({
commands,
editorFactory,
onSaveError,
registry,
rendermime
});
this._rawEditor.handleMoved.connect(this._onStateChanged, this);
layout.addWidget(this._rawEditor);
}
/**
* The plugin editor's raw editor.
*/
readonly raw: RawEditor;
/**
* Tests whether the settings have been modified and need saving.
*/
get isDirty(): boolean {
return this._rawEditor.isDirty;
}
/**
* The plugin settings being edited.
*/
get settings(): ISettingRegistry.ISettings | null {
return this._settings;
}
set settings(settings: ISettingRegistry.ISettings | null) {
if (this._settings === settings) {
return;
}
const raw = this._rawEditor;
this._settings = raw.settings = settings;
this.update();
}
/**
* The plugin editor layout state.
*/
get state(): SettingEditor.IPluginLayout {
const plugin = this._settings ? this._settings.id : '';
const { sizes } = this._rawEditor;
return { plugin, sizes };
}
set state(state: SettingEditor.IPluginLayout) {
if (JSONExt.deepEqual(this.state, state)) {
return;
}
this._rawEditor.sizes = state.sizes;
this.update();
}
/**
* A signal that emits when editor layout state changes and needs to be saved.
*/
get stateChanged(): ISignal<this, void> {
return this._stateChanged;
}
/**
* If the editor is in a dirty state, confirm that the user wants to leave.
*/
confirm(): Promise<void> {
if (this.isHidden || !this.isAttached || !this.isDirty) {
return Promise.resolve(undefined);
}
return showDialog({
title: 'You have unsaved changes.',
body: 'Do you want to leave without saving?',
buttons: [Dialog.cancelButton(), Dialog.okButton()]
}).then(result => {
if (!result.button.accept) {
throw new Error('User canceled.');
}
});
}
/**
* Dispose of the resources held by the plugin editor.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
super.dispose();
this._rawEditor.dispose();
}
/**
* Handle `after-attach` messages.
*/
protected onAfterAttach(msg: Message): void {
this.update();
}
/**
* Handle `'update-request'` messages.
*/
protected onUpdateRequest(msg: Message): void {
const raw = this._rawEditor;
const settings = this._settings;
if (!settings) {
this.hide();
return;
}
this.show();
raw.show();
}
/**
* Handle layout state changes that need to be saved.
*/
private _onStateChanged(): void {
(this.stateChanged as Signal<any, void>).emit(undefined);
}
private _rawEditor: RawEditor;
private _settings: ISettingRegistry.ISettings | null = null;
private _stateChanged = new Signal<this, void>(this);
}
/**
* A namespace for `PluginEditor` statics.
*/
export namespace PluginEditor {
/**
* The instantiation options for a plugin 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 plugin editor.
*/
editorFactory: CodeEditor.Factory;
/**
* The setting registry used by the editor.
*/
registry: ISettingRegistry;
/**
* The optional MIME renderer to use for rendering debug messages.
*/
rendermime?: IRenderMimeRegistry;
}
}
/**
* A namespace for private module data.
*/
namespace Private {
/**
* Handle save errors.
*/
export function onSaveError(reason: any): void {
console.error(`Saving setting editor value failed: ${reason.message}`);
void showDialog({
title: 'Your changes were not saved.',
body: reason.message,
buttons: [Dialog.okButton()]
});
}
}