forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raweditor.ts
462 lines (383 loc) · 10.6 KB
/
raweditor.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
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Toolbar, CommandToolbarButton } from '@jupyterlab/apputils';
import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
import { ISettingRegistry } from '@jupyterlab/coreutils';
import { RenderMimeRegistry } from '@jupyterlab/rendermime';
import { CommandRegistry } from '@phosphor/commands';
import { Message } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { BoxLayout, Widget } from '@phosphor/widgets';
import { createInspector } from './inspector';
import { SplitPanel } from './splitpanel';
/**
* A class name added to all raw editors.
*/
const RAW_EDITOR_CLASS = 'jp-SettingsRawEditor';
/**
* A class name added to the user settings editor.
*/
const USER_CLASS = 'jp-SettingsRawEditor-user';
/**
* A class name added to the user editor when there are validation errors.
*/
const ERROR_CLASS = 'jp-mod-error';
/**
* The banner text for the default editor.
*/
const DEFAULT_TITLE = 'System Defaults';
/**
* The banner text for the user settings editor.
*/
const USER_TITLE = 'User Preferences';
/**
* A raw JSON settings editor.
*/
export class RawEditor extends SplitPanel {
/**
* Create a new plugin editor.
*/
constructor(options: RawEditor.IOptions) {
super({
orientation: 'horizontal',
renderer: SplitPanel.defaultRenderer,
spacing: 1
});
const { commands, editorFactory, registry } = options;
this.registry = registry;
this._commands = commands;
// Create read-only defaults editor.
const defaults = (this._defaults = new CodeEditorWrapper({
model: new CodeEditor.Model(),
factory: editorFactory
}));
defaults.editor.model.value.text = '';
defaults.editor.model.mimeType = 'text/javascript';
defaults.editor.setOption('readOnly', true);
// Create read-write user settings editor.
const user = (this._user = new CodeEditorWrapper({
model: new CodeEditor.Model(),
factory: editorFactory,
config: { lineNumbers: true }
}));
user.addClass(USER_CLASS);
user.editor.model.mimeType = 'text/javascript';
user.editor.model.value.changed.connect(
this._onTextChanged,
this
);
// Create and set up an inspector.
this._inspector = createInspector(this, options.rendermime);
this.addClass(RAW_EDITOR_CLASS);
this._onSaveError = options.onSaveError;
this.addWidget(Private.defaultsEditor(defaults));
this.addWidget(Private.userEditor(user, this._toolbar, this._inspector));
}
/**
* The setting registry used by the editor.
*/
readonly registry: ISettingRegistry;
/**
* Whether the raw editor revert functionality is enabled.
*/
get canRevert(): boolean {
return this._canRevert;
}
/**
* Whether the raw editor save functionality is enabled.
*/
get canSave(): boolean {
return this._canSave;
}
/**
* Emits when the commands passed in at instantiation change.
*/
get commandsChanged(): ISignal<any, string[]> {
return this._commandsChanged;
}
/**
* Whether the debug panel is visible.
*/
get isDebugVisible(): boolean {
return this._inspector.isVisible;
}
/**
* Tests whether the settings have been modified and need saving.
*/
get isDirty(): boolean {
return this._user.editor.model.value.text !== this._settings.raw;
}
/**
* The plugin settings being edited.
*/
get settings(): ISettingRegistry.ISettings | null {
return this._settings;
}
set settings(settings: ISettingRegistry.ISettings | null) {
if (!settings && !this._settings) {
return;
}
const samePlugin =
settings && this._settings && settings.plugin === this._settings.plugin;
if (samePlugin) {
return;
}
const defaults = this._defaults;
const user = this._user;
// Disconnect old settings change handler.
if (this._settings) {
this._settings.changed.disconnect(this._onSettingsChanged, this);
}
if (settings) {
this._settings = settings;
this._settings.changed.connect(
this._onSettingsChanged,
this
);
this._onSettingsChanged();
} else {
this._settings = null;
defaults.editor.model.value.text = '';
user.editor.model.value.text = '';
}
this.update();
}
/**
* Get the relative sizes of the two editor panels.
*/
get sizes(): number[] {
return this.relativeSizes();
}
set sizes(sizes: number[]) {
this.setRelativeSizes(sizes);
}
/**
* The inspectable source editor for user input.
*/
get source(): CodeEditor.IEditor {
return this._user.editor;
}
/**
* Dispose of the resources held by the raw editor.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
super.dispose();
this._defaults.dispose();
this._user.dispose();
}
/**
* Revert the editor back to original settings.
*/
revert(): void {
this._user.editor.model.value.text = this.settings.raw;
this._updateToolbar(false, false);
}
/**
* Save the contents of the raw editor.
*/
save(): Promise<void> {
if (!this.isDirty) {
return Promise.resolve(undefined);
}
const settings = this._settings;
const source = this._user.editor.model.value.text;
return settings
.save(source)
.then(() => {
this._updateToolbar(false, false);
})
.catch(reason => {
this._updateToolbar(true, false);
this._onSaveError(reason);
});
}
/**
* Toggle the debug functionality.
*/
toggleDebug(): void {
const inspector = this._inspector;
if (inspector.isHidden) {
inspector.show();
} else {
inspector.hide();
}
this._updateToolbar();
}
/**
* Handle `after-attach` messages.
*/
protected onAfterAttach(msg: Message): void {
Private.populateToolbar(this._commands, this._toolbar);
this.update();
}
/**
* Handle `'update-request'` messages.
*/
protected onUpdateRequest(msg: Message): void {
const settings = this._settings;
const defaults = this._defaults;
const user = this._user;
if (settings) {
defaults.editor.refresh();
user.editor.refresh();
}
}
/**
* Handle text changes in the underlying editor.
*/
private _onTextChanged(): void {
const raw = this._user.editor.model.value.text;
const settings = this._settings;
this.removeClass(ERROR_CLASS);
// If there are no settings loaded or there are no changes, bail.
if (!settings || settings.raw === raw) {
this._updateToolbar(false, false);
return;
}
const errors = settings.validate(raw);
if (errors) {
this.addClass(ERROR_CLASS);
this._updateToolbar(true, false);
return;
}
this._updateToolbar(true, true);
}
/**
* Handle updates to the settings.
*/
private _onSettingsChanged(): void {
const settings = this._settings;
const defaults = this._defaults;
const user = this._user;
defaults.editor.model.value.text = settings.annotatedDefaults();
user.editor.model.value.text = settings.raw;
}
private _updateToolbar(revert = this._canRevert, save = this._canSave): void {
const commands = this._commands;
this._canRevert = revert;
this._canSave = save;
this._commandsChanged.emit([
commands.debug,
commands.revert,
commands.save
]);
}
private _canRevert = false;
private _canSave = false;
private _commands: RawEditor.ICommandBundle;
private _commandsChanged = new Signal<this, string[]>(this);
private _defaults: CodeEditorWrapper;
private _inspector: Widget;
private _onSaveError: (reason: any) => void;
private _settings: ISettingRegistry.ISettings | null = null;
private _toolbar = new Toolbar<Widget>();
private _user: CodeEditorWrapper;
}
/**
* A namespace for `RawEditor` statics.
*/
export namespace RawEditor {
/**
* The toolbar commands and registry for the setting editor toolbar.
*/
export interface ICommandBundle {
/**
* The command registry.
*/
registry: CommandRegistry;
/**
* The debug command ID.
*/
debug: string;
/**
* The revert command ID.
*/
revert: string;
/**
* The save command ID.
*/
save: string;
}
/**
* The instantiation options for a raw editor.
*/
export interface IOptions {
/**
* The toolbar commands and registry for the setting editor toolbar.
*/
commands: ICommandBundle;
/**
* The editor factory used by the raw editor.
*/
editorFactory: CodeEditor.Factory;
/**
* A function the raw editor calls on save errors.
*/
onSaveError: (reason: any) => void;
/**
* The setting registry used by the editor.
*/
registry: ISettingRegistry;
/**
* The optional MIME renderer to use for rendering debug messages.
*/
rendermime?: RenderMimeRegistry;
}
}
/**
* A namespace for private module data.
*/
namespace Private {
/**
* Returns the wrapped setting defaults editor.
*/
export function defaultsEditor(editor: Widget): Widget {
const widget = new Widget();
const layout = (widget.layout = new BoxLayout({ spacing: 0 }));
const banner = new Widget();
const bar = new Toolbar();
banner.node.innerText = DEFAULT_TITLE;
bar.insertItem(0, 'banner', banner);
layout.addWidget(bar);
layout.addWidget(editor);
return widget;
}
/**
* Populate the raw editor toolbar.
*/
export function populateToolbar(
commands: RawEditor.ICommandBundle,
toolbar: Toolbar<Widget>
): void {
const { debug, registry, revert, save } = commands;
toolbar.addItem('spacer', Toolbar.createSpacerItem());
// Note the button order. The rationale here is that no matter what state
// the toolbar is in, the relative location of the revert button in the
// toolbar remains the same.
[revert, debug, save].forEach(name => {
const item = new CommandToolbarButton({ commands: registry, id: name });
toolbar.addItem(name, item);
});
}
/**
* Returns the wrapped user overrides editor.
*/
export function userEditor(
editor: Widget,
toolbar: Toolbar<Widget>,
inspector: Widget
): Widget {
const widget = new Widget();
const layout = (widget.layout = new BoxLayout({ spacing: 0 }));
const banner = new Widget();
banner.node.innerText = USER_TITLE;
toolbar.insertItem(0, 'banner', banner);
layout.addWidget(toolbar);
layout.addWidget(editor);
layout.addWidget(inspector);
return widget;
}
}