@jupyterlab/codeeditor 4.0.0-alpha.9 → 4.0.0-beta.0

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,358 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { IObservableJSON } from '@jupyterlab/observables';
5
+ import { ISharedText, SourceChange } from '@jupyter/ydoc';
6
+ import {
7
+ ITranslator,
8
+ nullTranslator,
9
+ TranslationBundle
10
+ } from '@jupyterlab/translation';
11
+ import { checkIcon, undoIcon } from '@jupyterlab/ui-components';
12
+ import {
13
+ JSONExt,
14
+ JSONObject,
15
+ ReadonlyPartialJSONObject
16
+ } from '@lumino/coreutils';
17
+ import { Message } from '@lumino/messaging';
18
+ import { Widget } from '@lumino/widgets';
19
+ import { CodeEditor } from './editor';
20
+
21
+ /**
22
+ * The class name added to a JSONEditor instance.
23
+ */
24
+ const JSONEDITOR_CLASS = 'jp-JSONEditor';
25
+
26
+ /**
27
+ * The class name added when the Metadata editor contains invalid JSON.
28
+ */
29
+ const ERROR_CLASS = 'jp-mod-error';
30
+
31
+ /**
32
+ * The class name added to the editor host node.
33
+ */
34
+ const HOST_CLASS = 'jp-JSONEditor-host';
35
+
36
+ /**
37
+ * The class name added to the header area.
38
+ */
39
+ const HEADER_CLASS = 'jp-JSONEditor-header';
40
+
41
+ /**
42
+ * A widget for editing observable JSON.
43
+ */
44
+ export class JSONEditor extends Widget {
45
+ /**
46
+ * Construct a new JSON editor.
47
+ */
48
+ constructor(options: JSONEditor.IOptions) {
49
+ super();
50
+ this.translator = options.translator || nullTranslator;
51
+ this._trans = this.translator.load('jupyterlab');
52
+ this.addClass(JSONEDITOR_CLASS);
53
+
54
+ this.headerNode = document.createElement('div');
55
+ this.headerNode.className = HEADER_CLASS;
56
+
57
+ this.revertButtonNode = undoIcon.element({
58
+ tag: 'span',
59
+ title: this._trans.__('Revert changes to data')
60
+ });
61
+
62
+ this.commitButtonNode = checkIcon.element({
63
+ tag: 'span',
64
+ title: this._trans.__('Commit changes to data'),
65
+ marginLeft: '8px'
66
+ });
67
+
68
+ this.editorHostNode = document.createElement('div');
69
+ this.editorHostNode.className = HOST_CLASS;
70
+
71
+ this.headerNode.appendChild(this.revertButtonNode);
72
+ this.headerNode.appendChild(this.commitButtonNode);
73
+
74
+ this.node.appendChild(this.headerNode);
75
+ this.node.appendChild(this.editorHostNode);
76
+
77
+ const model = new CodeEditor.Model({ mimeType: 'application/json' });
78
+ model.sharedModel.changed.connect(this._onModelChanged, this);
79
+
80
+ this.model = model;
81
+ this.editor = options.editorFactory({
82
+ host: this.editorHostNode,
83
+ model,
84
+ config: {
85
+ readOnly: true
86
+ }
87
+ });
88
+ }
89
+
90
+ /**
91
+ * The code editor used by the editor.
92
+ */
93
+ readonly editor: CodeEditor.IEditor;
94
+
95
+ /**
96
+ * The code editor model used by the editor.
97
+ */
98
+ readonly model: CodeEditor.IModel;
99
+
100
+ /**
101
+ * The editor host node used by the JSON editor.
102
+ */
103
+ readonly headerNode: HTMLDivElement;
104
+
105
+ /**
106
+ * The editor host node used by the JSON editor.
107
+ */
108
+ readonly editorHostNode: HTMLDivElement;
109
+
110
+ /**
111
+ * The revert button used by the JSON editor.
112
+ */
113
+ readonly revertButtonNode: HTMLSpanElement;
114
+
115
+ /**
116
+ * The commit button used by the JSON editor.
117
+ */
118
+ readonly commitButtonNode: HTMLSpanElement;
119
+
120
+ /**
121
+ * The observable source.
122
+ */
123
+ get source(): IObservableJSON | null {
124
+ return this._source;
125
+ }
126
+ set source(value: IObservableJSON | null) {
127
+ if (this._source === value) {
128
+ return;
129
+ }
130
+ if (this._source) {
131
+ this._source.changed.disconnect(this._onSourceChanged, this);
132
+ }
133
+ this._source = value;
134
+ this.editor.setOption('readOnly', value === null);
135
+ if (value) {
136
+ value.changed.connect(this._onSourceChanged, this);
137
+ }
138
+ this._setValue();
139
+ }
140
+
141
+ /**
142
+ * Get whether the editor is dirty.
143
+ */
144
+ get isDirty(): boolean {
145
+ return this._dataDirty || this._inputDirty;
146
+ }
147
+
148
+ /**
149
+ * Dispose of the editor.
150
+ */
151
+ dispose(): void {
152
+ if (this.isDisposed) {
153
+ return;
154
+ }
155
+
156
+ this.source?.dispose();
157
+ this.model.dispose();
158
+ this.editor.dispose();
159
+
160
+ super.dispose();
161
+ }
162
+
163
+ /**
164
+ * Handle the DOM events for the widget.
165
+ *
166
+ * @param event - The DOM event sent to the widget.
167
+ *
168
+ * #### Notes
169
+ * This method implements the DOM `EventListener` interface and is
170
+ * called in response to events on the notebook panel's node. It should
171
+ * not be called directly by user code.
172
+ */
173
+ handleEvent(event: Event): void {
174
+ switch (event.type) {
175
+ case 'blur':
176
+ this._evtBlur(event as FocusEvent);
177
+ break;
178
+ case 'click':
179
+ this._evtClick(event as MouseEvent);
180
+ break;
181
+ default:
182
+ break;
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Handle `after-attach` messages for the widget.
188
+ */
189
+ protected onAfterAttach(msg: Message): void {
190
+ const node = this.editorHostNode;
191
+ node.addEventListener('blur', this, true);
192
+ node.addEventListener('click', this, true);
193
+ this.revertButtonNode.hidden = true;
194
+ this.commitButtonNode.hidden = true;
195
+ this.headerNode.addEventListener('click', this);
196
+ }
197
+
198
+ /**
199
+ * Handle `before-detach` messages for the widget.
200
+ */
201
+ protected onBeforeDetach(msg: Message): void {
202
+ const node = this.editorHostNode;
203
+ node.removeEventListener('blur', this, true);
204
+ node.removeEventListener('click', this, true);
205
+ this.headerNode.removeEventListener('click', this);
206
+ }
207
+
208
+ /**
209
+ * Handle a change to the metadata of the source.
210
+ */
211
+ private _onSourceChanged(
212
+ sender: IObservableJSON,
213
+ args: IObservableJSON.IChangedArgs
214
+ ) {
215
+ if (this._changeGuard) {
216
+ return;
217
+ }
218
+ if (this._inputDirty || this.editor.hasFocus()) {
219
+ this._dataDirty = true;
220
+ return;
221
+ }
222
+ this._setValue();
223
+ }
224
+
225
+ /**
226
+ * Handle change events.
227
+ */
228
+ private _onModelChanged(model: ISharedText, change: SourceChange): void {
229
+ if (change.sourceChange) {
230
+ let valid = true;
231
+ try {
232
+ const value = JSON.parse(this.editor.model.sharedModel.getSource());
233
+ this.removeClass(ERROR_CLASS);
234
+ this._inputDirty =
235
+ !this._changeGuard && !JSONExt.deepEqual(value, this._originalValue);
236
+ } catch (err) {
237
+ this.addClass(ERROR_CLASS);
238
+ this._inputDirty = true;
239
+ valid = false;
240
+ }
241
+ this.revertButtonNode.hidden = !this._inputDirty;
242
+ this.commitButtonNode.hidden = !valid || !this._inputDirty;
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Handle blur events for the text area.
248
+ */
249
+ private _evtBlur(event: FocusEvent): void {
250
+ // Update the metadata if necessary.
251
+ if (!this._inputDirty && this._dataDirty) {
252
+ this._setValue();
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Handle click events for the buttons.
258
+ */
259
+ private _evtClick(event: MouseEvent): void {
260
+ const target = event.target as HTMLElement;
261
+ if (this.revertButtonNode.contains(target)) {
262
+ this._setValue();
263
+ } else if (this.commitButtonNode.contains(target)) {
264
+ if (!this.commitButtonNode.hidden && !this.hasClass(ERROR_CLASS)) {
265
+ this._changeGuard = true;
266
+ this._mergeContent();
267
+ this._changeGuard = false;
268
+ this._setValue();
269
+ }
270
+ } else if (this.editorHostNode.contains(target)) {
271
+ this.editor.focus();
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Merge the user content.
277
+ */
278
+ private _mergeContent(): void {
279
+ const model = this.editor.model;
280
+ const old = this._originalValue;
281
+ const user = JSON.parse(model.sharedModel.getSource()) as JSONObject;
282
+ const source = this.source;
283
+ if (!source) {
284
+ return;
285
+ }
286
+
287
+ // If it is in user and has changed from old, set in new.
288
+ for (const key in user) {
289
+ if (!JSONExt.deepEqual(user[key], old[key] || null)) {
290
+ source.set(key, user[key]);
291
+ }
292
+ }
293
+
294
+ // If it was in old and is not in user, remove from source.
295
+ for (const key in old) {
296
+ if (!(key in user)) {
297
+ source.delete(key);
298
+ }
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Set the value given the owner contents.
304
+ */
305
+ private _setValue(): void {
306
+ this._dataDirty = false;
307
+ this._inputDirty = false;
308
+ this.revertButtonNode.hidden = true;
309
+ this.commitButtonNode.hidden = true;
310
+ this.removeClass(ERROR_CLASS);
311
+ const model = this.editor.model;
312
+ const content = this._source ? this._source.toJSON() : {};
313
+ this._changeGuard = true;
314
+ if (content === void 0) {
315
+ model.sharedModel.setSource(this._trans.__('No data!'));
316
+ this._originalValue = JSONExt.emptyObject;
317
+ } else {
318
+ const value = JSON.stringify(content, null, 4);
319
+ model.sharedModel.setSource(value);
320
+ this._originalValue = content;
321
+ // Move the cursor to within the brace.
322
+ if (value.length > 1 && value[0] === '{') {
323
+ this.editor.setCursorPosition({ line: 0, column: 1 });
324
+ }
325
+ }
326
+ this._changeGuard = false;
327
+ this.commitButtonNode.hidden = true;
328
+ this.revertButtonNode.hidden = true;
329
+ }
330
+
331
+ protected translator: ITranslator;
332
+ private _trans: TranslationBundle;
333
+ private _dataDirty = false;
334
+ private _inputDirty = false;
335
+ private _source: IObservableJSON | null = null;
336
+ private _originalValue: ReadonlyPartialJSONObject = JSONExt.emptyObject;
337
+ private _changeGuard = false;
338
+ }
339
+
340
+ /**
341
+ * The static namespace JSONEditor class statics.
342
+ */
343
+ export namespace JSONEditor {
344
+ /**
345
+ * The options used to initialize a json editor.
346
+ */
347
+ export interface IOptions {
348
+ /**
349
+ * The editor factory used by the editor.
350
+ */
351
+ editorFactory: CodeEditor.Factory;
352
+
353
+ /**
354
+ * The language translator.
355
+ */
356
+ translator?: ITranslator;
357
+ }
358
+ }