/
widget.ts
285 lines (261 loc) · 7.09 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { MimeData } from '@lumino/coreutils';
import { Drag } 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 { factory, model, editorOptions } = options;
const editor = (this.editor = factory({
host: this.node,
model,
...editorOptions
}));
editor.model.selections.changed.connect(this._onSelectionsChanged, this);
}
/**
* 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(): void {
if (this.isDisposed) {
return;
}
this.editor.dispose();
super.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 'lm-dragenter':
this._evtDragEnter(event as Drag.Event);
break;
case 'lm-dragleave':
this._evtDragLeave(event as Drag.Event);
break;
case 'lm-dragover':
this._evtDragOver(event as Drag.Event);
break;
case 'lm-drop':
this._evtDrop(event as Drag.Event);
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);
const node = this.node;
node.addEventListener('lm-dragenter', this);
node.addEventListener('lm-dragleave', this);
node.addEventListener('lm-dragover', this);
node.addEventListener('lm-drop', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
const node = this.node;
node.removeEventListener('lm-dragenter', this);
node.removeEventListener('lm-dragleave', this);
node.removeEventListener('lm-dragover', this);
node.removeEventListener('lm-drop', this);
}
/**
* 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 `'lm-dragenter'` event for the widget.
*/
private _evtDragEnter(event: Drag.Event): 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 `'lm-dragleave'` event for the widget.
*/
private _evtDragLeave(event: Drag.Event): 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 `'lm-dragover'` event for the widget.
*/
private _evtDragOver(event: Drag.Event): 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 `'lm-drop'` event for the widget.
*/
private _evtDrop(event: Drag.Event): void {
if (this.editor.getOption('readOnly') === true) {
return;
}
const data = Private.findTextData(event.mimeData);
if (data === undefined) {
return;
}
const coordinate: CodeEditor.ICoordinate = {
top: event.y,
bottom: event.y,
left: event.x,
right: event.x
};
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.sharedModel.updateSource(offset, offset, data);
}
}
/**
* 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 the editor options
* because it needs to provide its own node as the host.
*/
factory: CodeEditor.Factory;
/**
* The content model for the wrapper.
*/
model: CodeEditor.IModel;
/**
* Code editor options
*/
editorOptions?: Omit<CodeEditor.IOptions, 'host' | 'model'>;
}
}
/**
* 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;
}
}