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

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,257 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ import { Dialog, showDialog, showErrorMessage } from '@jupyterlab/apputils';
7
+ import { CodeEditor } from '@jupyterlab/codeeditor';
8
+ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
9
+ import { ISettingRegistry } from '@jupyterlab/settingregistry';
10
+ import {
11
+ ITranslator,
12
+ nullTranslator,
13
+ TranslationBundle
14
+ } from '@jupyterlab/translation';
15
+ import { CommandRegistry } from '@lumino/commands';
16
+ import { JSONExt } from '@lumino/coreutils';
17
+ import { Message } from '@lumino/messaging';
18
+ import { ISignal, Signal } from '@lumino/signaling';
19
+ import { StackedLayout, Widget } from '@lumino/widgets';
20
+ import { RawEditor } from './raweditor';
21
+ import { JsonSettingEditor } from './jsonsettingeditor';
22
+
23
+ /**
24
+ * The class name added to all plugin editors.
25
+ */
26
+ const PLUGIN_EDITOR_CLASS = 'jp-PluginEditor';
27
+
28
+ /**
29
+ * An individual plugin settings editor.
30
+ */
31
+ export class PluginEditor extends Widget {
32
+ /**
33
+ * Create a new plugin editor.
34
+ *
35
+ * @param options - The plugin editor instantiation options.
36
+ */
37
+ constructor(options: PluginEditor.IOptions) {
38
+ super();
39
+ this.addClass(PLUGIN_EDITOR_CLASS);
40
+
41
+ const { commands, editorFactory, registry, rendermime, translator } =
42
+ options;
43
+ this.translator = translator || nullTranslator;
44
+ this._trans = this.translator.load('jupyterlab');
45
+
46
+ // TODO: Remove this layout. We were using this before when we
47
+ // when we had a way to switch between the raw and table editor
48
+ // Now, the raw editor is the only child and probably could merged into
49
+ // this class directly in the future.
50
+ const layout = (this.layout = new StackedLayout());
51
+ const { onSaveError } = Private;
52
+
53
+ this.raw = this._rawEditor = new RawEditor({
54
+ commands,
55
+ editorFactory,
56
+ onSaveError,
57
+ registry,
58
+ rendermime,
59
+ translator
60
+ });
61
+ this._rawEditor.handleMoved.connect(this._onStateChanged, this);
62
+
63
+ layout.addWidget(this._rawEditor);
64
+ }
65
+
66
+ /**
67
+ * The plugin editor's raw editor.
68
+ */
69
+ readonly raw: RawEditor;
70
+
71
+ /**
72
+ * Tests whether the settings have been modified and need saving.
73
+ */
74
+ get isDirty(): boolean {
75
+ return this._rawEditor.isDirty;
76
+ }
77
+
78
+ /**
79
+ * The plugin settings being edited.
80
+ */
81
+ get settings(): ISettingRegistry.ISettings | null {
82
+ return this._settings;
83
+ }
84
+ set settings(settings: ISettingRegistry.ISettings | null) {
85
+ if (this._settings === settings) {
86
+ return;
87
+ }
88
+
89
+ const raw = this._rawEditor;
90
+
91
+ this._settings = raw.settings = settings;
92
+ this.update();
93
+ }
94
+
95
+ /**
96
+ * The plugin editor layout state.
97
+ */
98
+ get state(): JsonSettingEditor.IPluginLayout {
99
+ const plugin = this._settings ? this._settings.id : '';
100
+ const { sizes } = this._rawEditor;
101
+
102
+ return { plugin, sizes };
103
+ }
104
+ set state(state: JsonSettingEditor.IPluginLayout) {
105
+ if (JSONExt.deepEqual(this.state, state)) {
106
+ return;
107
+ }
108
+
109
+ this._rawEditor.sizes = state.sizes;
110
+ this.update();
111
+ }
112
+
113
+ /**
114
+ * A signal that emits when editor layout state changes and needs to be saved.
115
+ */
116
+ get stateChanged(): ISignal<this, void> {
117
+ return this._stateChanged;
118
+ }
119
+
120
+ /**
121
+ * If the editor is in a dirty state, confirm that the user wants to leave.
122
+ */
123
+ confirm(): Promise<void> {
124
+ if (this.isHidden || !this.isAttached || !this.isDirty) {
125
+ return Promise.resolve(undefined);
126
+ }
127
+
128
+ return showDialog({
129
+ title: this._trans.__('You have unsaved changes.'),
130
+ body: this._trans.__('Do you want to leave without saving?'),
131
+ buttons: [
132
+ Dialog.cancelButton({ label: this._trans.__('Cancel') }),
133
+ Dialog.okButton({ label: this._trans.__('Ok') })
134
+ ]
135
+ }).then(result => {
136
+ if (!result.button.accept) {
137
+ throw new Error('User canceled.');
138
+ }
139
+ });
140
+ }
141
+
142
+ /**
143
+ * Dispose of the resources held by the plugin editor.
144
+ */
145
+ dispose(): void {
146
+ if (this.isDisposed) {
147
+ return;
148
+ }
149
+
150
+ super.dispose();
151
+ this._rawEditor.dispose();
152
+ }
153
+
154
+ /**
155
+ * Handle `after-attach` messages.
156
+ */
157
+ protected onAfterAttach(msg: Message): void {
158
+ this.update();
159
+ }
160
+
161
+ /**
162
+ * Handle `'update-request'` messages.
163
+ */
164
+ protected onUpdateRequest(msg: Message): void {
165
+ const raw = this._rawEditor;
166
+ const settings = this._settings;
167
+
168
+ if (!settings) {
169
+ this.hide();
170
+ return;
171
+ }
172
+
173
+ this.show();
174
+ raw.show();
175
+ }
176
+
177
+ /**
178
+ * Handle layout state changes that need to be saved.
179
+ */
180
+ private _onStateChanged(): void {
181
+ (this.stateChanged as Signal<any, void>).emit(undefined);
182
+ }
183
+
184
+ protected translator: ITranslator;
185
+ private _trans: TranslationBundle;
186
+ private _rawEditor: RawEditor;
187
+ private _settings: ISettingRegistry.ISettings | null = null;
188
+ private _stateChanged = new Signal<this, void>(this);
189
+ }
190
+
191
+ /**
192
+ * A namespace for `PluginEditor` statics.
193
+ */
194
+ export namespace PluginEditor {
195
+ /**
196
+ * The instantiation options for a plugin editor.
197
+ */
198
+ export interface IOptions {
199
+ /**
200
+ * The toolbar commands and registry for the setting editor toolbar.
201
+ */
202
+ commands: {
203
+ /**
204
+ * The command registry.
205
+ */
206
+ registry: CommandRegistry;
207
+
208
+ /**
209
+ * The revert command ID.
210
+ */
211
+ revert: string;
212
+
213
+ /**
214
+ * The save command ID.
215
+ */
216
+ save: string;
217
+ };
218
+
219
+ /**
220
+ * The editor factory used by the plugin editor.
221
+ */
222
+ editorFactory: CodeEditor.Factory;
223
+
224
+ /**
225
+ * The setting registry used by the editor.
226
+ */
227
+ registry: ISettingRegistry;
228
+
229
+ /**
230
+ * The optional MIME renderer to use for rendering debug messages.
231
+ */
232
+ rendermime?: IRenderMimeRegistry;
233
+
234
+ /**
235
+ * The application language translator.
236
+ */
237
+ translator?: ITranslator;
238
+ }
239
+ }
240
+
241
+ /**
242
+ * A namespace for private module data.
243
+ */
244
+ namespace Private {
245
+ /**
246
+ * Handle save errors.
247
+ */
248
+ export function onSaveError(
249
+ reason: Dialog.IError,
250
+ translator?: ITranslator
251
+ ): void {
252
+ translator = translator || nullTranslator;
253
+ const trans = translator.load('jupyterlab');
254
+ console.error(`Saving setting editor value failed: ${reason.message}`);
255
+ void showErrorMessage(trans.__('Your changes were not saved.'), reason);
256
+ }
257
+ }