@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.
- package/lib/editor.d.ts +48 -216
- package/lib/editor.js +26 -140
- package/lib/editor.js.map +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/jsoneditor.d.ts +5 -9
- package/lib/jsoneditor.js +41 -39
- package/lib/jsoneditor.js.map +1 -1
- package/lib/lineCol.js +4 -3
- package/lib/lineCol.js.map +1 -1
- package/lib/tokens.d.ts +1 -1
- package/lib/viewer.d.ts +48 -0
- package/lib/viewer.js +45 -0
- package/lib/viewer.js.map +1 -0
- package/lib/widget.d.ts +5 -31
- package/lib/widget.js +6 -43
- package/lib/widget.js.map +1 -1
- package/package.json +23 -22
- package/src/editor.ts +560 -0
- package/src/factory.ts +19 -0
- package/src/index.ts +15 -0
- package/src/jsoneditor.ts +358 -0
- package/src/lineCol.tsx +381 -0
- package/src/mimetype.ts +43 -0
- package/src/tokens.ts +57 -0
- package/src/viewer.ts +93 -0
- package/src/widget.ts +289 -0
- package/style/linecol.css +5 -0
package/src/widget.ts
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { MimeData } from '@lumino/coreutils';
|
|
5
|
+
import { Drag } from '@lumino/dragdrop';
|
|
6
|
+
import { Message } from '@lumino/messaging';
|
|
7
|
+
import { Widget } from '@lumino/widgets';
|
|
8
|
+
import { CodeEditor } from './';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The class name added to an editor widget that has a primary selection.
|
|
12
|
+
*/
|
|
13
|
+
const HAS_SELECTION_CLASS = 'jp-mod-has-primary-selection';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The class name added to an editor widget that has a cursor/selection
|
|
17
|
+
* within the whitespace at the beginning of a line
|
|
18
|
+
*/
|
|
19
|
+
const HAS_IN_LEADING_WHITESPACE_CLASS = 'jp-mod-in-leading-whitespace';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A class used to indicate a drop target.
|
|
23
|
+
*/
|
|
24
|
+
const DROP_TARGET_CLASS = 'jp-mod-dropTarget';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* RegExp to test for leading whitespace
|
|
28
|
+
*/
|
|
29
|
+
const leadingWhitespaceRe = /^\s+$/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A widget which hosts a code editor.
|
|
33
|
+
*/
|
|
34
|
+
export class CodeEditorWrapper extends Widget {
|
|
35
|
+
/**
|
|
36
|
+
* Construct a new code editor widget.
|
|
37
|
+
*/
|
|
38
|
+
constructor(options: CodeEditorWrapper.IOptions) {
|
|
39
|
+
super();
|
|
40
|
+
const { factory, model, editorOptions } = options;
|
|
41
|
+
const editor = (this.editor = factory({
|
|
42
|
+
host: this.node,
|
|
43
|
+
model,
|
|
44
|
+
...editorOptions
|
|
45
|
+
}));
|
|
46
|
+
editor.model.selections.changed.connect(this._onSelectionsChanged, this);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the editor wrapped by the widget.
|
|
51
|
+
*/
|
|
52
|
+
readonly editor: CodeEditor.IEditor;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get the model used by the widget.
|
|
56
|
+
*/
|
|
57
|
+
get model(): CodeEditor.IModel {
|
|
58
|
+
return this.editor.model;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Dispose of the resources held by the widget.
|
|
63
|
+
*/
|
|
64
|
+
dispose(): void {
|
|
65
|
+
if (this.isDisposed) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.editor.dispose();
|
|
69
|
+
super.dispose();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Handle the DOM events for the widget.
|
|
74
|
+
*
|
|
75
|
+
* @param event - The DOM event sent to the widget.
|
|
76
|
+
*
|
|
77
|
+
* #### Notes
|
|
78
|
+
* This method implements the DOM `EventListener` interface and is
|
|
79
|
+
* called in response to events on the notebook panel's node. It should
|
|
80
|
+
* not be called directly by user code.
|
|
81
|
+
*/
|
|
82
|
+
handleEvent(event: Event): void {
|
|
83
|
+
switch (event.type) {
|
|
84
|
+
case 'lm-dragenter':
|
|
85
|
+
this._evtDragEnter(event as Drag.Event);
|
|
86
|
+
break;
|
|
87
|
+
case 'lm-dragleave':
|
|
88
|
+
this._evtDragLeave(event as Drag.Event);
|
|
89
|
+
break;
|
|
90
|
+
case 'lm-dragover':
|
|
91
|
+
this._evtDragOver(event as Drag.Event);
|
|
92
|
+
break;
|
|
93
|
+
case 'lm-drop':
|
|
94
|
+
this._evtDrop(event as Drag.Event);
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Handle `'activate-request'` messages.
|
|
103
|
+
*/
|
|
104
|
+
protected onActivateRequest(msg: Message): void {
|
|
105
|
+
this.editor.focus();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A message handler invoked on an `'after-attach'` message.
|
|
110
|
+
*/
|
|
111
|
+
protected onAfterAttach(msg: Message): void {
|
|
112
|
+
super.onAfterAttach(msg);
|
|
113
|
+
const node = this.node;
|
|
114
|
+
node.addEventListener('lm-dragenter', this);
|
|
115
|
+
node.addEventListener('lm-dragleave', this);
|
|
116
|
+
node.addEventListener('lm-dragover', this);
|
|
117
|
+
node.addEventListener('lm-drop', this);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Handle `before-detach` messages for the widget.
|
|
122
|
+
*/
|
|
123
|
+
protected onBeforeDetach(msg: Message): void {
|
|
124
|
+
const node = this.node;
|
|
125
|
+
node.removeEventListener('lm-dragenter', this);
|
|
126
|
+
node.removeEventListener('lm-dragleave', this);
|
|
127
|
+
node.removeEventListener('lm-dragover', this);
|
|
128
|
+
node.removeEventListener('lm-drop', this);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Handle a change in model selections.
|
|
133
|
+
*/
|
|
134
|
+
private _onSelectionsChanged(): void {
|
|
135
|
+
const { start, end } = this.editor.getSelection();
|
|
136
|
+
|
|
137
|
+
if (start.column !== end.column || start.line !== end.line) {
|
|
138
|
+
// a selection was made
|
|
139
|
+
this.addClass(HAS_SELECTION_CLASS);
|
|
140
|
+
this.removeClass(HAS_IN_LEADING_WHITESPACE_CLASS);
|
|
141
|
+
} else {
|
|
142
|
+
// the cursor was placed
|
|
143
|
+
this.removeClass(HAS_SELECTION_CLASS);
|
|
144
|
+
|
|
145
|
+
if (
|
|
146
|
+
this.editor
|
|
147
|
+
.getLine(end.line)!
|
|
148
|
+
.slice(0, end.column)
|
|
149
|
+
.match(leadingWhitespaceRe)
|
|
150
|
+
) {
|
|
151
|
+
this.addClass(HAS_IN_LEADING_WHITESPACE_CLASS);
|
|
152
|
+
} else {
|
|
153
|
+
this.removeClass(HAS_IN_LEADING_WHITESPACE_CLASS);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Handle the `'lm-dragenter'` event for the widget.
|
|
160
|
+
*/
|
|
161
|
+
private _evtDragEnter(event: Drag.Event): void {
|
|
162
|
+
if (this.editor.getOption('readOnly') === true) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const data = Private.findTextData(event.mimeData);
|
|
166
|
+
if (data === undefined) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
event.stopPropagation();
|
|
171
|
+
this.addClass('jp-mod-dropTarget');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Handle the `'lm-dragleave'` event for the widget.
|
|
176
|
+
*/
|
|
177
|
+
private _evtDragLeave(event: Drag.Event): void {
|
|
178
|
+
this.removeClass(DROP_TARGET_CLASS);
|
|
179
|
+
if (this.editor.getOption('readOnly') === true) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const data = Private.findTextData(event.mimeData);
|
|
183
|
+
if (data === undefined) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
event.stopPropagation();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Handle the `'lm-dragover'` event for the widget.
|
|
192
|
+
*/
|
|
193
|
+
private _evtDragOver(event: Drag.Event): void {
|
|
194
|
+
this.removeClass(DROP_TARGET_CLASS);
|
|
195
|
+
if (this.editor.getOption('readOnly') === true) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const data = Private.findTextData(event.mimeData);
|
|
199
|
+
if (data === undefined) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
event.stopPropagation();
|
|
204
|
+
event.dropAction = 'copy';
|
|
205
|
+
this.addClass(DROP_TARGET_CLASS);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Handle the `'lm-drop'` event for the widget.
|
|
210
|
+
*/
|
|
211
|
+
private _evtDrop(event: Drag.Event): void {
|
|
212
|
+
if (this.editor.getOption('readOnly') === true) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const data = Private.findTextData(event.mimeData);
|
|
216
|
+
if (data === undefined) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const coordinate = {
|
|
220
|
+
top: event.y,
|
|
221
|
+
bottom: event.y,
|
|
222
|
+
left: event.x,
|
|
223
|
+
right: event.x,
|
|
224
|
+
x: event.x,
|
|
225
|
+
y: event.y,
|
|
226
|
+
width: 0,
|
|
227
|
+
height: 0
|
|
228
|
+
} as CodeEditor.ICoordinate;
|
|
229
|
+
const position = this.editor.getPositionForCoordinate(coordinate);
|
|
230
|
+
if (position === null) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.removeClass(DROP_TARGET_CLASS);
|
|
234
|
+
event.preventDefault();
|
|
235
|
+
event.stopPropagation();
|
|
236
|
+
if (event.proposedAction === 'none') {
|
|
237
|
+
event.dropAction = 'none';
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const offset = this.editor.getOffsetAt(position);
|
|
241
|
+
this.model.sharedModel.updateSource(offset, offset, data);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The namespace for the `CodeEditorWrapper` statics.
|
|
247
|
+
*/
|
|
248
|
+
export namespace CodeEditorWrapper {
|
|
249
|
+
/**
|
|
250
|
+
* The options used to initialize a code editor widget.
|
|
251
|
+
*/
|
|
252
|
+
export interface IOptions {
|
|
253
|
+
/**
|
|
254
|
+
* A code editor factory.
|
|
255
|
+
*
|
|
256
|
+
* #### Notes
|
|
257
|
+
* The widget needs a factory and a the editor options
|
|
258
|
+
* because it needs to provide its own node as the host.
|
|
259
|
+
*/
|
|
260
|
+
factory: CodeEditor.Factory;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The content model for the wrapper.
|
|
264
|
+
*/
|
|
265
|
+
model: CodeEditor.IModel;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Code editor options
|
|
269
|
+
*/
|
|
270
|
+
editorOptions?: Omit<CodeEditor.IOptions, 'host' | 'model'>;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* A namespace for private functionality.
|
|
276
|
+
*/
|
|
277
|
+
namespace Private {
|
|
278
|
+
/**
|
|
279
|
+
* Given a MimeData instance, extract the first text data, if any.
|
|
280
|
+
*/
|
|
281
|
+
export function findTextData(mime: MimeData): string | undefined {
|
|
282
|
+
const types = mime.types();
|
|
283
|
+
const textType = types.find(t => t.indexOf('text') === 0);
|
|
284
|
+
if (textType === undefined) {
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
return mime.getData(textType) as string;
|
|
288
|
+
}
|
|
289
|
+
}
|