/
widget.ts
353 lines (304 loc) · 8.4 KB
/
widget.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { StackedLayout, Widget } from '@lumino/widgets';
import {
ABCWidgetFactory,
DocumentRegistry,
DocumentWidget,
IDocumentWidget
} from '@jupyterlab/docregistry';
import {
CodeEditor,
IEditorServices,
IEditorMimeTypeService,
CodeEditorWrapper
} from '@jupyterlab/codeeditor';
import { PromiseDelegate } from '@lumino/coreutils';
import { Message } from '@lumino/messaging';
/**
* The data attribute added to a widget that can run code.
*/
const CODE_RUNNER = 'jpCodeRunner';
/**
* The data attribute added to a widget that can undo.
*/
const UNDOER = 'jpUndoer';
/**
* A code editor wrapper for the file editor.
*/
export class FileEditorCodeWrapper extends CodeEditorWrapper {
/**
* Construct a new editor widget.
*/
constructor(options: FileEditor.IOptions) {
super({
factory: options.factory,
model: options.context.model
});
const context = (this._context = options.context);
const editor = this.editor;
this.addClass('jp-FileEditorCodeWrapper');
this.node.dataset[CODE_RUNNER] = 'true';
this.node.dataset[UNDOER] = 'true';
editor.model.value.text = context.model.toString();
void context.ready.then(() => {
this._onContextReady();
});
if (context.model.modelDB.isCollaborative) {
let modelDB = context.model.modelDB;
void modelDB.connected.then(() => {
let collaborators = modelDB.collaborators;
if (!collaborators) {
return;
}
// Setup the selection style for collaborators
let localCollaborator = collaborators.localCollaborator;
this.editor.uuid = localCollaborator.sessionId;
this.editor.selectionStyle = {
...CodeEditor.defaultSelectionStyle,
color: localCollaborator.color
};
collaborators.changed.connect(this._onCollaboratorsChanged, this);
// Trigger an initial onCollaboratorsChanged event.
this._onCollaboratorsChanged();
});
}
}
/**
* Get the context for the editor widget.
*/
get context(): DocumentRegistry.Context {
return this._context;
}
/**
* A promise that resolves when the file editor is ready.
*/
get ready(): Promise<void> {
return this._ready.promise;
}
/**
* Handle actions that should be taken when the context is ready.
*/
private _onContextReady(): void {
if (this.isDisposed) {
return;
}
const contextModel = this._context.model;
const editor = this.editor;
const editorModel = editor.model;
// Set the editor model value.
editorModel.value.text = contextModel.toString();
// Prevent the initial loading from disk from being in the editor history.
editor.clearHistory();
// Wire signal connections.
contextModel.contentChanged.connect(this._onContentChanged, this);
// Resolve the ready promise.
this._ready.resolve(undefined);
}
/**
* Handle a change in context model content.
*/
private _onContentChanged(): void {
const editorModel = this.editor.model;
const oldValue = editorModel.value.text;
const newValue = this._context.model.toString();
if (oldValue !== newValue) {
editorModel.value.text = newValue;
}
}
/**
* Handle a change to the collaborators on the model
* by updating UI elements associated with them.
*/
private _onCollaboratorsChanged(): void {
// If there are selections corresponding to non-collaborators,
// they are stale and should be removed.
let collaborators = this._context.model.modelDB.collaborators;
if (!collaborators) {
return;
}
for (let key of this.editor.model.selections.keys()) {
if (!collaborators.has(key)) {
this.editor.model.selections.delete(key);
}
}
}
protected _context: DocumentRegistry.Context;
private _ready = new PromiseDelegate<void>();
}
/**
* A widget for editors.
*/
export class FileEditor extends Widget {
/**
* Construct a new editor widget.
*/
constructor(options: FileEditor.IOptions) {
super();
this.addClass('jp-FileEditor');
const context = (this._context = options.context);
this._mimeTypeService = options.mimeTypeService;
let editorWidget = (this.editorWidget = new FileEditorCodeWrapper(options));
this.editor = editorWidget.editor;
this.model = editorWidget.model;
// Listen for changes to the path.
context.pathChanged.connect(this._onPathChanged, this);
this._onPathChanged();
let layout = (this.layout = new StackedLayout());
layout.addWidget(editorWidget);
}
/**
* Get the context for the editor widget.
*/
get context(): DocumentRegistry.Context {
return this.editorWidget.context;
}
/**
* A promise that resolves when the file editor is ready.
*/
get ready(): Promise<void> {
return this.editorWidget.ready;
}
/**
* Handle the DOM events for the widget.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the widget's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
if (!this.model) {
return;
}
switch (event.type) {
case 'mousedown':
this._ensureFocus();
break;
default:
break;
}
}
/**
* Handle `after-attach` messages for the widget.
*/
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
let node = this.node;
node.addEventListener('mousedown', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
let node = this.node;
node.removeEventListener('mousedown', this);
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this._ensureFocus();
}
/**
* Ensure that the widget has focus.
*/
private _ensureFocus(): void {
if (!this.editor.hasFocus()) {
this.editor.focus();
}
}
/**
* Handle a change to the path.
*/
private _onPathChanged(): void {
const editor = this.editor;
const localPath = this._context.localPath;
editor.model.mimeType = this._mimeTypeService.getMimeTypeByFilePath(
localPath
);
}
private editorWidget: FileEditorCodeWrapper;
public model: CodeEditor.IModel;
public editor: CodeEditor.IEditor;
protected _context: DocumentRegistry.Context;
private _mimeTypeService: IEditorMimeTypeService;
}
/**
* The namespace for editor widget statics.
*/
export namespace FileEditor {
/**
* The options used to create an editor widget.
*/
export interface IOptions {
/**
* A code editor factory.
*/
factory: CodeEditor.Factory;
/**
* The mime type service for the editor.
*/
mimeTypeService: IEditorMimeTypeService;
/**
* The document context associated with the editor.
*/
context: DocumentRegistry.CodeContext;
}
}
/**
* A widget factory for editors.
*/
export class FileEditorFactory extends ABCWidgetFactory<
IDocumentWidget<FileEditor>,
DocumentRegistry.ICodeModel
> {
/**
* Construct a new editor widget factory.
*/
constructor(options: FileEditorFactory.IOptions) {
super(options.factoryOptions);
this._services = options.editorServices;
}
/**
* Create a new widget given a context.
*/
protected createNewWidget(
context: DocumentRegistry.CodeContext
): IDocumentWidget<FileEditor> {
let func = this._services.factoryService.newDocumentEditor;
let factory: CodeEditor.Factory = options => {
return func(options);
};
const content = new FileEditor({
factory,
context,
mimeTypeService: this._services.mimeTypeService
});
const widget = new DocumentWidget({ content, context });
return widget;
}
private _services: IEditorServices;
}
/**
* The namespace for `FileEditorFactory` class statics.
*/
export namespace FileEditorFactory {
/**
* The options used to create an editor widget factory.
*/
export interface IOptions {
/**
* The editor services used by the factory.
*/
editorServices: IEditorServices;
/**
* The factory options associated with the factory.
*/
factoryOptions: DocumentRegistry.IWidgetFactoryOptions<
IDocumentWidget<FileEditor>
>;
}
}