forked from jupyterlab/jupyterlab
/
widget.ts
342 lines (307 loc) · 8.23 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { MimeData } from '@lumino/coreutils';
import { IDragEvent } from '@lumino/dragdrop';
import { Message } from '@lumino/messaging';
import { Widget } from '@lumino/widgets';
import { CodeEditor } from './';
/**
* The class name added to an editor widget that has a primary selection.
*/
const HAS_SELECTION_CLASS = 'jp-mod-has-primary-selection';
/**
* The class name added to an editor widget that has a cursor/selection
* within the whitespace at the beginning of a line
*/
const HAS_IN_LEADING_WHITESPACE_CLASS = 'jp-mod-in-leading-whitespace';
/**
* A class used to indicate a drop target.
*/
const DROP_TARGET_CLASS = 'jp-mod-dropTarget';
/**
* RegExp to test for leading whitespace
*/
const leadingWhitespaceRe = /^\s+$/;
/**
* A widget which hosts a code editor.
*/
export class CodeEditorWrapper extends Widget {
/**
* Construct a new code editor widget.
*/
constructor(options: CodeEditorWrapper.IOptions) {
super();
const editor = (this.editor = options.factory({
host: this.node,
model: options.model,
uuid: options.uuid,
config: options.config,
selectionStyle: options.selectionStyle
}));
editor.model.selections.changed.connect(this._onSelectionsChanged, this);
this._updateOnShow = options.updateOnShow !== false;
}
/**
* Get the editor wrapped by the widget.
*/
readonly editor: CodeEditor.IEditor;
/**
* Get the model used by the widget.
*/
get model(): CodeEditor.IModel {
return this.editor.model;
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
if (this.isDisposed) {
return;
}
super.dispose();
this.editor.dispose();
}
/**
* 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 notebook panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'p-dragenter':
this._evtDragEnter(event as IDragEvent);
break;
case 'p-dragleave':
this._evtDragLeave(event as IDragEvent);
break;
case 'p-dragover':
this._evtDragOver(event as IDragEvent);
break;
case 'p-drop':
this._evtDrop(event as IDragEvent);
break;
default:
break;
}
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this.editor.focus();
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
let node = this.node;
node.addEventListener('p-dragenter', this);
node.addEventListener('p-dragleave', this);
node.addEventListener('p-dragover', this);
node.addEventListener('p-drop', this);
if (this.isVisible) {
this.update();
}
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
let node = this.node;
node.removeEventListener('p-dragenter', this);
node.removeEventListener('p-dragleave', this);
node.removeEventListener('p-dragover', this);
node.removeEventListener('p-drop', this);
}
/**
* A message handler invoked on an `'after-show'` message.
*/
protected onAfterShow(msg: Message): void {
if (this._updateOnShow) {
this.update();
}
}
/**
* A message handler invoked on a `'resize'` message.
*/
protected onResize(msg: Widget.ResizeMessage): void {
if (msg.width >= 0 && msg.height >= 0) {
this.editor.setSize(msg);
} else if (this.isVisible) {
this.editor.resizeToFit();
}
}
/**
* A message handler invoked on an `'update-request'` message.
*/
protected onUpdateRequest(msg: Message): void {
this.editor.refresh();
}
/**
* Handle a change in model selections.
*/
private _onSelectionsChanged(): void {
const { start, end } = this.editor.getSelection();
if (start.column !== end.column || start.line !== end.line) {
// a selection was made
this.addClass(HAS_SELECTION_CLASS);
this.removeClass(HAS_IN_LEADING_WHITESPACE_CLASS);
} else {
// the cursor was placed
this.removeClass(HAS_SELECTION_CLASS);
if (
this.editor
.getLine(end.line)!
.slice(0, end.column)
.match(leadingWhitespaceRe)
) {
this.addClass(HAS_IN_LEADING_WHITESPACE_CLASS);
} else {
this.removeClass(HAS_IN_LEADING_WHITESPACE_CLASS);
}
}
}
/**
* Handle the `'p-dragenter'` event for the widget.
*/
private _evtDragEnter(event: IDragEvent): void {
if (this.editor.getOption('readOnly') === true) {
return;
}
const data = Private.findTextData(event.mimeData);
if (data === undefined) {
return;
}
event.preventDefault();
event.stopPropagation();
this.addClass('jp-mod-dropTarget');
}
/**
* Handle the `'p-dragleave'` event for the widget.
*/
private _evtDragLeave(event: IDragEvent): void {
this.removeClass(DROP_TARGET_CLASS);
if (this.editor.getOption('readOnly') === true) {
return;
}
const data = Private.findTextData(event.mimeData);
if (data === undefined) {
return;
}
event.preventDefault();
event.stopPropagation();
}
/**
* Handle the `'p-dragover'` event for the widget.
*/
private _evtDragOver(event: IDragEvent): void {
this.removeClass(DROP_TARGET_CLASS);
if (this.editor.getOption('readOnly') === true) {
return;
}
const data = Private.findTextData(event.mimeData);
if (data === undefined) {
return;
}
event.preventDefault();
event.stopPropagation();
event.dropAction = 'copy';
this.addClass(DROP_TARGET_CLASS);
}
/**
* Handle the `'p-drop'` event for the widget.
*/
private _evtDrop(event: IDragEvent): void {
if (this.editor.getOption('readOnly') === true) {
return;
}
const data = Private.findTextData(event.mimeData);
if (data === undefined) {
return;
}
const coordinate = {
top: event.y,
bottom: event.y,
left: event.x,
right: event.x,
x: event.x,
y: event.y,
width: 0,
height: 0
};
const position = this.editor.getPositionForCoordinate(coordinate);
if (position === null) {
return;
}
this.removeClass(DROP_TARGET_CLASS);
event.preventDefault();
event.stopPropagation();
if (event.proposedAction === 'none') {
event.dropAction = 'none';
return;
}
const offset = this.editor.getOffsetAt(position);
this.model.value.insert(offset, data);
}
private _updateOnShow: boolean;
}
/**
* The namespace for the `CodeEditorWrapper` statics.
*/
export namespace CodeEditorWrapper {
/**
* The options used to initialize a code editor widget.
*/
export interface IOptions {
/**
* A code editor factory.
*
* #### Notes
* The widget needs a factory and a model instead of a `CodeEditor.IEditor`
* object because it needs to provide its own node as the host.
*/
factory: CodeEditor.Factory;
/**
* The model used to initialize the code editor.
*/
model: CodeEditor.IModel;
/**
* The desired uuid for the editor.
*/
uuid?: string;
/**
* The configuration options for the editor.
*/
config?: Partial<CodeEditor.IConfig>;
/**
* The default selection style for the editor.
*/
selectionStyle?: CodeEditor.ISelectionStyle;
/**
* Whether to send an update request to the editor when it is shown.
*/
updateOnShow?: boolean;
}
}
/**
* A namespace for private functionality.
*/
namespace Private {
/**
* Given a MimeData instance, extract the first text data, if any.
*/
export function findTextData(mime: MimeData): string | undefined {
const types = mime.types();
const textType = types.find(t => t.indexOf('text') === 0);
if (textType === undefined) {
return undefined;
}
return mime.getData(textType) as string;
}
}