@jupyterlab/codemirror 0.19.1-alpha.0 → 0.19.1
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/LICENSE +34 -0
- package/lib/codemirror-ipython.d.ts +2 -2
- package/lib/codemirror-ipython.js +34 -34
- package/lib/codemirror-ipythongfm.d.ts +3 -3
- package/lib/codemirror-ipythongfm.js +61 -61
- package/lib/editor.d.ts +442 -442
- package/lib/editor.js +1021 -1021
- package/lib/factory.d.ts +21 -21
- package/lib/factory.js +48 -48
- package/lib/index.d.ts +10 -10
- package/lib/index.js +21 -21
- package/lib/mimetype.d.ts +21 -21
- package/lib/mimetype.js +43 -43
- package/lib/mode.d.ts +71 -71
- package/lib/mode.js +118 -118
- package/package.json +11 -8
package/lib/editor.js
CHANGED
|
@@ -1,1021 +1,1021 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copyright (c) Jupyter Development Team.
|
|
3
|
-
// Distributed under the terms of the Modified BSD License.
|
|
4
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
5
|
-
var t = {};
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
9
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
|
10
|
-
t[p[i]] = s[p[i]];
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
const codemirror_1 = __importDefault(require("codemirror"));
|
|
18
|
-
const coreutils_1 = require("@phosphor/coreutils");
|
|
19
|
-
const algorithm_1 = require("@phosphor/algorithm");
|
|
20
|
-
const disposable_1 = require("@phosphor/disposable");
|
|
21
|
-
const signaling_1 = require("@phosphor/signaling");
|
|
22
|
-
const apputils_1 = require("@jupyterlab/apputils");
|
|
23
|
-
const codeeditor_1 = require("@jupyterlab/codeeditor");
|
|
24
|
-
const coreutils_2 = require("@phosphor/coreutils");
|
|
25
|
-
const mode_1 = require("./mode");
|
|
26
|
-
require("codemirror/addon/comment/comment.js");
|
|
27
|
-
require("codemirror/addon/edit/matchbrackets.js");
|
|
28
|
-
require("codemirror/addon/edit/closebrackets.js");
|
|
29
|
-
require("codemirror/addon/scroll/scrollpastend.js");
|
|
30
|
-
require("codemirror/addon/search/searchcursor");
|
|
31
|
-
require("codemirror/addon/search/search");
|
|
32
|
-
require("codemirror/addon/search/jump-to-line");
|
|
33
|
-
require("codemirror/keymap/emacs.js");
|
|
34
|
-
require("codemirror/keymap/sublime.js");
|
|
35
|
-
require("codemirror/keymap/vim.js");
|
|
36
|
-
/**
|
|
37
|
-
* The class name added to CodeMirrorWidget instances.
|
|
38
|
-
*/
|
|
39
|
-
const EDITOR_CLASS = 'jp-CodeMirrorEditor';
|
|
40
|
-
/**
|
|
41
|
-
* The class name added to read only cell editor widgets.
|
|
42
|
-
*/
|
|
43
|
-
const READ_ONLY_CLASS = 'jp-mod-readOnly';
|
|
44
|
-
/**
|
|
45
|
-
* The class name for the hover box for collaborator cursors.
|
|
46
|
-
*/
|
|
47
|
-
const COLLABORATOR_CURSOR_CLASS = 'jp-CollaboratorCursor';
|
|
48
|
-
/**
|
|
49
|
-
* The class name for the hover box for collaborator cursors.
|
|
50
|
-
*/
|
|
51
|
-
const COLLABORATOR_HOVER_CLASS = 'jp-CollaboratorCursor-hover';
|
|
52
|
-
/**
|
|
53
|
-
* The key code for the up arrow key.
|
|
54
|
-
*/
|
|
55
|
-
const UP_ARROW = 38;
|
|
56
|
-
/**
|
|
57
|
-
* The key code for the down arrow key.
|
|
58
|
-
*/
|
|
59
|
-
const DOWN_ARROW = 40;
|
|
60
|
-
/**
|
|
61
|
-
* The time that a collaborator name hover persists.
|
|
62
|
-
*/
|
|
63
|
-
const HOVER_TIMEOUT = 1000;
|
|
64
|
-
/**
|
|
65
|
-
* CodeMirror editor.
|
|
66
|
-
*/
|
|
67
|
-
class CodeMirrorEditor {
|
|
68
|
-
/**
|
|
69
|
-
* Construct a CodeMirror editor.
|
|
70
|
-
*/
|
|
71
|
-
constructor(options) {
|
|
72
|
-
/**
|
|
73
|
-
* A signal emitted when either the top or bottom edge is requested.
|
|
74
|
-
*/
|
|
75
|
-
this.edgeRequested = new signaling_1.Signal(this);
|
|
76
|
-
this.selectionMarkers = {};
|
|
77
|
-
this._keydownHandlers = new Array();
|
|
78
|
-
this._changeGuard = false;
|
|
79
|
-
this._uuid = '';
|
|
80
|
-
this._needsRefresh = false;
|
|
81
|
-
this._isDisposed = false;
|
|
82
|
-
this._lastChange = null;
|
|
83
|
-
this._timer = -1;
|
|
84
|
-
let host = (this.host = options.host);
|
|
85
|
-
host.classList.add(EDITOR_CLASS);
|
|
86
|
-
host.classList.add('jp-Editor');
|
|
87
|
-
host.addEventListener('focus', this, true);
|
|
88
|
-
host.addEventListener('blur', this, true);
|
|
89
|
-
host.addEventListener('scroll', this, true);
|
|
90
|
-
this._uuid = options.uuid || coreutils_2.UUID.uuid4();
|
|
91
|
-
// Handle selection style.
|
|
92
|
-
let style = options.selectionStyle || {};
|
|
93
|
-
this._selectionStyle = Object.assign({}, codeeditor_1.CodeEditor.defaultSelectionStyle, style);
|
|
94
|
-
let model = (this._model = options.model);
|
|
95
|
-
let config = options.config || {};
|
|
96
|
-
let fullConfig = (this._config = Object.assign({}, CodeMirrorEditor.defaultConfig, config));
|
|
97
|
-
let editor = (this._editor = Private.createEditor(host, fullConfig));
|
|
98
|
-
let doc = editor.getDoc();
|
|
99
|
-
// Handle initial values for text, mimetype, and selections.
|
|
100
|
-
doc.setValue(model.value.text);
|
|
101
|
-
this.clearHistory();
|
|
102
|
-
this._onMimeTypeChanged();
|
|
103
|
-
this._onCursorActivity();
|
|
104
|
-
this._timer = window.setInterval(() => {
|
|
105
|
-
this._checkSync();
|
|
106
|
-
}, 3000);
|
|
107
|
-
// Connect to changes.
|
|
108
|
-
model.value.changed.connect(this._onValueChanged, this);
|
|
109
|
-
model.mimeTypeChanged.connect(this._onMimeTypeChanged, this);
|
|
110
|
-
model.selections.changed.connect(this._onSelectionsChanged, this);
|
|
111
|
-
codemirror_1.default.on(editor, 'keydown', (editor, event) => {
|
|
112
|
-
let index = algorithm_1.ArrayExt.findFirstIndex(this._keydownHandlers, handler => {
|
|
113
|
-
if (handler(this, event) === true) {
|
|
114
|
-
event.preventDefault();
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
});
|
|
119
|
-
if (index === -1) {
|
|
120
|
-
this.onKeydown(event);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
codemirror_1.default.on(editor, 'cursorActivity', () => this._onCursorActivity());
|
|
124
|
-
codemirror_1.default.on(editor.getDoc(), 'beforeChange', (instance, change) => {
|
|
125
|
-
this._beforeDocChanged(instance, change);
|
|
126
|
-
});
|
|
127
|
-
codemirror_1.default.on(editor.getDoc(), 'change', (instance, change) => {
|
|
128
|
-
// Manually refresh after setValue to make sure editor is properly sized.
|
|
129
|
-
if (change.origin === 'setValue' && this.hasFocus()) {
|
|
130
|
-
this.refresh();
|
|
131
|
-
}
|
|
132
|
-
this._lastChange = change;
|
|
133
|
-
});
|
|
134
|
-
// Manually refresh on paste to make sure editor is properly sized.
|
|
135
|
-
editor.getWrapperElement().addEventListener('paste', () => {
|
|
136
|
-
if (this.hasFocus()) {
|
|
137
|
-
this.refresh();
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* The uuid of this editor;
|
|
143
|
-
*/
|
|
144
|
-
get uuid() {
|
|
145
|
-
return this._uuid;
|
|
146
|
-
}
|
|
147
|
-
set uuid(value) {
|
|
148
|
-
this._uuid = value;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* The selection style of this editor.
|
|
152
|
-
*/
|
|
153
|
-
get selectionStyle() {
|
|
154
|
-
return this._selectionStyle;
|
|
155
|
-
}
|
|
156
|
-
set selectionStyle(value) {
|
|
157
|
-
this._selectionStyle = value;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Get the codemirror editor wrapped by the editor.
|
|
161
|
-
*/
|
|
162
|
-
get editor() {
|
|
163
|
-
return this._editor;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Get the codemirror doc wrapped by the widget.
|
|
167
|
-
*/
|
|
168
|
-
get doc() {
|
|
169
|
-
return this._editor.getDoc();
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Get the number of lines in the editor.
|
|
173
|
-
*/
|
|
174
|
-
get lineCount() {
|
|
175
|
-
return this.doc.lineCount();
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Returns a model for this editor.
|
|
179
|
-
*/
|
|
180
|
-
get model() {
|
|
181
|
-
return this._model;
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* The height of a line in the editor in pixels.
|
|
185
|
-
*/
|
|
186
|
-
get lineHeight() {
|
|
187
|
-
return this._editor.defaultTextHeight();
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* The widget of a character in the editor in pixels.
|
|
191
|
-
*/
|
|
192
|
-
get charWidth() {
|
|
193
|
-
return this._editor.defaultCharWidth();
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Tests whether the editor is disposed.
|
|
197
|
-
*/
|
|
198
|
-
get isDisposed() {
|
|
199
|
-
return this._isDisposed;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Dispose of the resources held by the widget.
|
|
203
|
-
*/
|
|
204
|
-
dispose() {
|
|
205
|
-
if (this.isDisposed) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
this._isDisposed = true;
|
|
209
|
-
this.host.removeEventListener('focus', this, true);
|
|
210
|
-
this.host.removeEventListener('blur', this, true);
|
|
211
|
-
this.host.removeEventListener('scroll', this, true);
|
|
212
|
-
this._keydownHandlers.length = 0;
|
|
213
|
-
window.clearInterval(this._timer);
|
|
214
|
-
signaling_1.Signal.clearData(this);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Get a config option for the editor.
|
|
218
|
-
*/
|
|
219
|
-
getOption(option) {
|
|
220
|
-
return this._config[option];
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Set a config option for the editor.
|
|
224
|
-
*/
|
|
225
|
-
setOption(option, value) {
|
|
226
|
-
// Don't bother setting the option if it is already the same.
|
|
227
|
-
if (this._config[option] !== value) {
|
|
228
|
-
this._config[option] = value;
|
|
229
|
-
Private.setOption(this.editor, option, value, this._config);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Returns the content for the given line number.
|
|
234
|
-
*/
|
|
235
|
-
getLine(line) {
|
|
236
|
-
return this.doc.getLine(line);
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Find an offset for the given position.
|
|
240
|
-
*/
|
|
241
|
-
getOffsetAt(position) {
|
|
242
|
-
return this.doc.indexFromPos({
|
|
243
|
-
ch: position.column,
|
|
244
|
-
line: position.line
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Find a position for the given offset.
|
|
249
|
-
*/
|
|
250
|
-
getPositionAt(offset) {
|
|
251
|
-
const { ch, line } = this.doc.posFromIndex(offset);
|
|
252
|
-
return { line, column: ch };
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Undo one edit (if any undo events are stored).
|
|
256
|
-
*/
|
|
257
|
-
undo() {
|
|
258
|
-
this.doc.undo();
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Redo one undone edit.
|
|
262
|
-
*/
|
|
263
|
-
redo() {
|
|
264
|
-
this.doc.redo();
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Clear the undo history.
|
|
268
|
-
*/
|
|
269
|
-
clearHistory() {
|
|
270
|
-
this.doc.clearHistory();
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Brings browser focus to this editor text.
|
|
274
|
-
*/
|
|
275
|
-
focus() {
|
|
276
|
-
this._editor.focus();
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Test whether the editor has keyboard focus.
|
|
280
|
-
*/
|
|
281
|
-
hasFocus() {
|
|
282
|
-
return this._editor.getWrapperElement().contains(document.activeElement);
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Explicitly blur the editor.
|
|
286
|
-
*/
|
|
287
|
-
blur() {
|
|
288
|
-
this._editor.getInputField().blur();
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Repaint editor.
|
|
292
|
-
*/
|
|
293
|
-
refresh() {
|
|
294
|
-
this._editor.refresh();
|
|
295
|
-
this._needsRefresh = false;
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Refresh the editor if it is focused;
|
|
299
|
-
* otherwise postpone refreshing till focusing.
|
|
300
|
-
*/
|
|
301
|
-
resizeToFit() {
|
|
302
|
-
if (this.hasFocus()) {
|
|
303
|
-
this.refresh();
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
this._needsRefresh = true;
|
|
307
|
-
}
|
|
308
|
-
this._clearHover();
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Add a keydown handler to the editor.
|
|
312
|
-
*
|
|
313
|
-
* @param handler - A keydown handler.
|
|
314
|
-
*
|
|
315
|
-
* @returns A disposable that can be used to remove the handler.
|
|
316
|
-
*/
|
|
317
|
-
addKeydownHandler(handler) {
|
|
318
|
-
this._keydownHandlers.push(handler);
|
|
319
|
-
return new disposable_1.DisposableDelegate(() => {
|
|
320
|
-
algorithm_1.ArrayExt.removeAllWhere(this._keydownHandlers, val => val === handler);
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Set the size of the editor in pixels.
|
|
325
|
-
*/
|
|
326
|
-
setSize(dimension) {
|
|
327
|
-
if (dimension) {
|
|
328
|
-
this._editor.setSize(dimension.width, dimension.height);
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
this._editor.setSize(null, null);
|
|
332
|
-
}
|
|
333
|
-
this._needsRefresh = false;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Reveal the given position in the editor.
|
|
337
|
-
*/
|
|
338
|
-
revealPosition(position) {
|
|
339
|
-
const cmPosition = this._toCodeMirrorPosition(position);
|
|
340
|
-
this._editor.scrollIntoView(cmPosition);
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Reveal the given selection in the editor.
|
|
344
|
-
*/
|
|
345
|
-
revealSelection(selection) {
|
|
346
|
-
const range = this._toCodeMirrorRange(selection);
|
|
347
|
-
this._editor.scrollIntoView(range);
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Get the window coordinates given a cursor position.
|
|
351
|
-
*/
|
|
352
|
-
getCoordinateForPosition(position) {
|
|
353
|
-
const pos = this._toCodeMirrorPosition(position);
|
|
354
|
-
const rect = this.editor.charCoords(pos, 'page');
|
|
355
|
-
return rect;
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Get the cursor position given window coordinates.
|
|
359
|
-
*
|
|
360
|
-
* @param coordinate - The desired coordinate.
|
|
361
|
-
*
|
|
362
|
-
* @returns The position of the coordinates, or null if not
|
|
363
|
-
* contained in the editor.
|
|
364
|
-
*/
|
|
365
|
-
getPositionForCoordinate(coordinate) {
|
|
366
|
-
return this._toPosition(this.editor.coordsChar(coordinate)) || null;
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Returns the primary position of the cursor, never `null`.
|
|
370
|
-
*/
|
|
371
|
-
getCursorPosition() {
|
|
372
|
-
const cursor = this.doc.getCursor();
|
|
373
|
-
return this._toPosition(cursor);
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Set the primary position of the cursor.
|
|
377
|
-
*
|
|
378
|
-
* #### Notes
|
|
379
|
-
* This will remove any secondary cursors.
|
|
380
|
-
*/
|
|
381
|
-
setCursorPosition(position) {
|
|
382
|
-
const cursor = this._toCodeMirrorPosition(position);
|
|
383
|
-
this.doc.setCursor(cursor);
|
|
384
|
-
// If the editor does not have focus, this cursor change
|
|
385
|
-
// will get screened out in _onCursorsChanged(). Make an
|
|
386
|
-
// exception for this method.
|
|
387
|
-
if (!this.editor.hasFocus()) {
|
|
388
|
-
this.model.selections.set(this.uuid, this.getSelections());
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Returns the primary selection, never `null`.
|
|
393
|
-
*/
|
|
394
|
-
getSelection() {
|
|
395
|
-
return this.getSelections()[0];
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Set the primary selection. This will remove any secondary cursors.
|
|
399
|
-
*/
|
|
400
|
-
setSelection(selection) {
|
|
401
|
-
this.setSelections([selection]);
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* Gets the selections for all the cursors, never `null` or empty.
|
|
405
|
-
*/
|
|
406
|
-
getSelections() {
|
|
407
|
-
const selections = this.doc.listSelections();
|
|
408
|
-
if (selections.length > 0) {
|
|
409
|
-
return selections.map(selection => this._toSelection(selection));
|
|
410
|
-
}
|
|
411
|
-
const cursor = this.doc.getCursor();
|
|
412
|
-
const selection = this._toSelection({ anchor: cursor, head: cursor });
|
|
413
|
-
return [selection];
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Sets the selections for all the cursors, should not be empty.
|
|
417
|
-
* Cursors will be removed or added, as necessary.
|
|
418
|
-
* Passing an empty array resets a cursor position to the start of a document.
|
|
419
|
-
*/
|
|
420
|
-
setSelections(selections) {
|
|
421
|
-
const cmSelections = this._toCodeMirrorSelections(selections);
|
|
422
|
-
this.doc.setSelections(cmSelections, 0);
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Get a list of tokens for the current editor text content.
|
|
426
|
-
*/
|
|
427
|
-
getTokens() {
|
|
428
|
-
let tokens = [];
|
|
429
|
-
for (let i = 0; i < this.lineCount; ++i) {
|
|
430
|
-
const lineTokens = this.editor.getLineTokens(i).map(t => ({
|
|
431
|
-
offset: this.getOffsetAt({ column: t.start, line: i }),
|
|
432
|
-
value: t.string,
|
|
433
|
-
type: t.type || ''
|
|
434
|
-
}));
|
|
435
|
-
tokens = tokens.concat(lineTokens);
|
|
436
|
-
}
|
|
437
|
-
return tokens;
|
|
438
|
-
}
|
|
439
|
-
/**
|
|
440
|
-
* Get the token at a given editor position.
|
|
441
|
-
*/
|
|
442
|
-
getTokenForPosition(position) {
|
|
443
|
-
const cursor = this._toCodeMirrorPosition(position);
|
|
444
|
-
const token = this.editor.getTokenAt(cursor);
|
|
445
|
-
return {
|
|
446
|
-
offset: this.getOffsetAt({ column: token.start, line: cursor.line }),
|
|
447
|
-
value: token.string,
|
|
448
|
-
type: token.type
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Insert a new indented line at the current cursor position.
|
|
453
|
-
*/
|
|
454
|
-
newIndentedLine() {
|
|
455
|
-
this.execCommand('newlineAndIndent');
|
|
456
|
-
}
|
|
457
|
-
/**
|
|
458
|
-
* Execute a codemirror command on the editor.
|
|
459
|
-
*
|
|
460
|
-
* @param command - The name of the command to execute.
|
|
461
|
-
*/
|
|
462
|
-
execCommand(command) {
|
|
463
|
-
this._editor.execCommand(command);
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Handle keydown events from the editor.
|
|
467
|
-
*/
|
|
468
|
-
onKeydown(event) {
|
|
469
|
-
let position = this.getCursorPosition();
|
|
470
|
-
let { line, column } = position;
|
|
471
|
-
if (line === 0 && column === 0 && event.keyCode === UP_ARROW) {
|
|
472
|
-
if (!event.shiftKey) {
|
|
473
|
-
this.edgeRequested.emit('top');
|
|
474
|
-
}
|
|
475
|
-
return false;
|
|
476
|
-
}
|
|
477
|
-
let lastLine = this.lineCount - 1;
|
|
478
|
-
let lastCh = this.getLine(lastLine).length;
|
|
479
|
-
if (line === lastLine &&
|
|
480
|
-
column === lastCh &&
|
|
481
|
-
event.keyCode === DOWN_ARROW) {
|
|
482
|
-
if (!event.shiftKey) {
|
|
483
|
-
this.edgeRequested.emit('bottom');
|
|
484
|
-
}
|
|
485
|
-
return false;
|
|
486
|
-
}
|
|
487
|
-
return false;
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Converts selections to code mirror selections.
|
|
491
|
-
*/
|
|
492
|
-
_toCodeMirrorSelections(selections) {
|
|
493
|
-
if (selections.length > 0) {
|
|
494
|
-
return selections.map(selection => this._toCodeMirrorSelection(selection));
|
|
495
|
-
}
|
|
496
|
-
const position = { line: 0, ch: 0 };
|
|
497
|
-
return [{ anchor: position, head: position }];
|
|
498
|
-
}
|
|
499
|
-
/**
|
|
500
|
-
* Handles a mime type change.
|
|
501
|
-
*/
|
|
502
|
-
_onMimeTypeChanged() {
|
|
503
|
-
const mime = this._model.mimeType;
|
|
504
|
-
let editor = this._editor;
|
|
505
|
-
mode_1.Mode.ensure(mime).then(spec => {
|
|
506
|
-
editor.setOption('mode', spec.mime);
|
|
507
|
-
});
|
|
508
|
-
let extraKeys = editor.getOption('extraKeys') || {};
|
|
509
|
-
const isCode = mime !== 'text/plain' && mime !== 'text/x-ipythongfm';
|
|
510
|
-
if (isCode) {
|
|
511
|
-
extraKeys['Backspace'] = 'delSpaceToPrevTabStop';
|
|
512
|
-
}
|
|
513
|
-
else {
|
|
514
|
-
delete extraKeys['Backspace'];
|
|
515
|
-
}
|
|
516
|
-
editor.setOption('extraKeys', extraKeys);
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Handles a selections change.
|
|
520
|
-
*/
|
|
521
|
-
_onSelectionsChanged(selections, args) {
|
|
522
|
-
const uuid = args.key;
|
|
523
|
-
if (uuid !== this.uuid) {
|
|
524
|
-
this._cleanSelections(uuid);
|
|
525
|
-
if (args.type !== 'remove' && args.newValue) {
|
|
526
|
-
this._markSelections(uuid, args.newValue);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* Clean selections for the given uuid.
|
|
532
|
-
*/
|
|
533
|
-
_cleanSelections(uuid) {
|
|
534
|
-
const markers = this.selectionMarkers[uuid];
|
|
535
|
-
if (markers) {
|
|
536
|
-
markers.forEach(marker => {
|
|
537
|
-
marker.clear();
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
delete this.selectionMarkers[uuid];
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Marks selections.
|
|
544
|
-
*/
|
|
545
|
-
_markSelections(uuid, selections) {
|
|
546
|
-
const markers = [];
|
|
547
|
-
// If we are marking selections corresponding to an active hover,
|
|
548
|
-
// remove it.
|
|
549
|
-
if (uuid === this._hoverId) {
|
|
550
|
-
this._clearHover();
|
|
551
|
-
}
|
|
552
|
-
// If we can id the selection to a specific collaborator,
|
|
553
|
-
// use that information.
|
|
554
|
-
let collaborator;
|
|
555
|
-
if (this._model.modelDB.collaborators) {
|
|
556
|
-
collaborator = this._model.modelDB.collaborators.get(uuid);
|
|
557
|
-
}
|
|
558
|
-
// Style each selection for the uuid.
|
|
559
|
-
selections.forEach(selection => {
|
|
560
|
-
// Only render selections if the start is not equal to the end.
|
|
561
|
-
// In that case, we don't need to render the cursor.
|
|
562
|
-
if (!coreutils_1.JSONExt.deepEqual(selection.start, selection.end)) {
|
|
563
|
-
const { anchor, head } = this._toCodeMirrorSelection(selection);
|
|
564
|
-
let markerOptions;
|
|
565
|
-
if (collaborator) {
|
|
566
|
-
markerOptions = this._toTextMarkerOptions(Object.assign({}, selection.style, { color: collaborator.color }));
|
|
567
|
-
}
|
|
568
|
-
else {
|
|
569
|
-
markerOptions = this._toTextMarkerOptions(selection.style);
|
|
570
|
-
}
|
|
571
|
-
markers.push(this.doc.markText(anchor, head, markerOptions));
|
|
572
|
-
}
|
|
573
|
-
else if (collaborator) {
|
|
574
|
-
let caret = this._getCaret(collaborator);
|
|
575
|
-
markers.push(this.doc.setBookmark(this._toCodeMirrorPosition(selection.end), {
|
|
576
|
-
widget: caret
|
|
577
|
-
}));
|
|
578
|
-
}
|
|
579
|
-
});
|
|
580
|
-
this.selectionMarkers[uuid] = markers;
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Handles a cursor activity event.
|
|
584
|
-
*/
|
|
585
|
-
_onCursorActivity() {
|
|
586
|
-
// Only add selections if the editor has focus. This avoids unwanted
|
|
587
|
-
// triggering of cursor activity due to collaborator actions.
|
|
588
|
-
if (this._editor.hasFocus()) {
|
|
589
|
-
const selections = this.getSelections();
|
|
590
|
-
this.model.selections.set(this.uuid, selections);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* Converts a code mirror selection to an editor selection.
|
|
595
|
-
*/
|
|
596
|
-
_toSelection(selection) {
|
|
597
|
-
return {
|
|
598
|
-
uuid: this.uuid,
|
|
599
|
-
start: this._toPosition(selection.anchor),
|
|
600
|
-
end: this._toPosition(selection.head),
|
|
601
|
-
style: this.selectionStyle
|
|
602
|
-
};
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* Converts the selection style to a text marker options.
|
|
606
|
-
*/
|
|
607
|
-
_toTextMarkerOptions(style) {
|
|
608
|
-
let r = parseInt(style.color.slice(1, 3), 16);
|
|
609
|
-
let g = parseInt(style.color.slice(3, 5), 16);
|
|
610
|
-
let b = parseInt(style.color.slice(5, 7), 16);
|
|
611
|
-
let css = `background-color: rgba( ${r}, ${g}, ${b}, 0.15)`;
|
|
612
|
-
return {
|
|
613
|
-
className: style.className,
|
|
614
|
-
title: style.displayName,
|
|
615
|
-
css
|
|
616
|
-
};
|
|
617
|
-
}
|
|
618
|
-
/**
|
|
619
|
-
* Converts an editor selection to a code mirror selection.
|
|
620
|
-
*/
|
|
621
|
-
_toCodeMirrorSelection(selection) {
|
|
622
|
-
// Selections only appear to render correctly if the anchor
|
|
623
|
-
// is before the head in the document. That is, reverse selections
|
|
624
|
-
// do not appear as intended.
|
|
625
|
-
let forward = selection.start.line < selection.end.line ||
|
|
626
|
-
(selection.start.line === selection.end.line &&
|
|
627
|
-
selection.start.column <= selection.end.column);
|
|
628
|
-
let anchor = forward ? selection.start : selection.end;
|
|
629
|
-
let head = forward ? selection.end : selection.start;
|
|
630
|
-
return {
|
|
631
|
-
anchor: this._toCodeMirrorPosition(anchor),
|
|
632
|
-
head: this._toCodeMirrorPosition(head)
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Converts an editor selection to a code mirror selection.
|
|
637
|
-
*/
|
|
638
|
-
_toCodeMirrorRange(range) {
|
|
639
|
-
return {
|
|
640
|
-
from: this._toCodeMirrorPosition(range.start),
|
|
641
|
-
to: this._toCodeMirrorPosition(range.end)
|
|
642
|
-
};
|
|
643
|
-
}
|
|
644
|
-
/**
|
|
645
|
-
* Convert a code mirror position to an editor position.
|
|
646
|
-
*/
|
|
647
|
-
_toPosition(position) {
|
|
648
|
-
return {
|
|
649
|
-
line: position.line,
|
|
650
|
-
column: position.ch
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
/**
|
|
654
|
-
* Convert an editor position to a code mirror position.
|
|
655
|
-
*/
|
|
656
|
-
_toCodeMirrorPosition(position) {
|
|
657
|
-
return {
|
|
658
|
-
line: position.line,
|
|
659
|
-
ch: position.column
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
/**
|
|
663
|
-
* Handle model value changes.
|
|
664
|
-
*/
|
|
665
|
-
_onValueChanged(value, args) {
|
|
666
|
-
if (this._changeGuard) {
|
|
667
|
-
return;
|
|
668
|
-
}
|
|
669
|
-
this._changeGuard = true;
|
|
670
|
-
let doc = this.doc;
|
|
671
|
-
switch (args.type) {
|
|
672
|
-
case 'insert':
|
|
673
|
-
let pos = doc.posFromIndex(args.start);
|
|
674
|
-
doc.replaceRange(args.value, pos, pos);
|
|
675
|
-
break;
|
|
676
|
-
case 'remove':
|
|
677
|
-
let from = doc.posFromIndex(args.start);
|
|
678
|
-
let to = doc.posFromIndex(args.end);
|
|
679
|
-
doc.replaceRange('', from, to);
|
|
680
|
-
break;
|
|
681
|
-
case 'set':
|
|
682
|
-
doc.setValue(args.value);
|
|
683
|
-
break;
|
|
684
|
-
default:
|
|
685
|
-
break;
|
|
686
|
-
}
|
|
687
|
-
this._changeGuard = false;
|
|
688
|
-
}
|
|
689
|
-
/**
|
|
690
|
-
* Handles document changes.
|
|
691
|
-
*/
|
|
692
|
-
_beforeDocChanged(doc, change) {
|
|
693
|
-
if (this._changeGuard) {
|
|
694
|
-
return;
|
|
695
|
-
}
|
|
696
|
-
this._changeGuard = true;
|
|
697
|
-
let value = this._model.value;
|
|
698
|
-
let start = doc.indexFromPos(change.from);
|
|
699
|
-
let end = doc.indexFromPos(change.to);
|
|
700
|
-
let inserted = change.text.join('\n');
|
|
701
|
-
if (end !== start) {
|
|
702
|
-
value.remove(start, end);
|
|
703
|
-
}
|
|
704
|
-
if (inserted) {
|
|
705
|
-
value.insert(start, inserted);
|
|
706
|
-
}
|
|
707
|
-
this._changeGuard = false;
|
|
708
|
-
}
|
|
709
|
-
/**
|
|
710
|
-
* Handle the DOM events for the editor.
|
|
711
|
-
*
|
|
712
|
-
* @param event - The DOM event sent to the editor.
|
|
713
|
-
*
|
|
714
|
-
* #### Notes
|
|
715
|
-
* This method implements the DOM `EventListener` interface and is
|
|
716
|
-
* called in response to events on the editor's DOM node. It should
|
|
717
|
-
* not be called directly by user code.
|
|
718
|
-
*/
|
|
719
|
-
handleEvent(event) {
|
|
720
|
-
switch (event.type) {
|
|
721
|
-
case 'focus':
|
|
722
|
-
this._evtFocus(event);
|
|
723
|
-
break;
|
|
724
|
-
case 'blur':
|
|
725
|
-
this._evtBlur(event);
|
|
726
|
-
break;
|
|
727
|
-
case 'scroll':
|
|
728
|
-
this._evtScroll();
|
|
729
|
-
break;
|
|
730
|
-
default:
|
|
731
|
-
break;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
/**
|
|
735
|
-
* Handle `focus` events for the editor.
|
|
736
|
-
*/
|
|
737
|
-
_evtFocus(event) {
|
|
738
|
-
if (this._needsRefresh) {
|
|
739
|
-
this.refresh();
|
|
740
|
-
}
|
|
741
|
-
this.host.classList.add('jp-mod-focused');
|
|
742
|
-
// Update the selections on editor gaining focus because
|
|
743
|
-
// the onCursorActivity function filters usual cursor events
|
|
744
|
-
// based on the editor's focus.
|
|
745
|
-
this._onCursorActivity();
|
|
746
|
-
}
|
|
747
|
-
/**
|
|
748
|
-
* Handle `blur` events for the editor.
|
|
749
|
-
*/
|
|
750
|
-
_evtBlur(event) {
|
|
751
|
-
this.host.classList.remove('jp-mod-focused');
|
|
752
|
-
}
|
|
753
|
-
/**
|
|
754
|
-
* Handle `scroll` events for the editor.
|
|
755
|
-
*/
|
|
756
|
-
_evtScroll() {
|
|
757
|
-
// Remove any active hover.
|
|
758
|
-
this._clearHover();
|
|
759
|
-
}
|
|
760
|
-
/**
|
|
761
|
-
* Clear the hover for a caret, due to things like
|
|
762
|
-
* scrolling, resizing, deactivation, etc, where
|
|
763
|
-
* the position is no longer valid.
|
|
764
|
-
*/
|
|
765
|
-
_clearHover() {
|
|
766
|
-
if (this._caretHover) {
|
|
767
|
-
window.clearTimeout(this._hoverTimeout);
|
|
768
|
-
document.body.removeChild(this._caretHover);
|
|
769
|
-
this._caretHover = null;
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
/**
|
|
773
|
-
* Construct a caret element representing the position
|
|
774
|
-
* of a collaborator's cursor.
|
|
775
|
-
*/
|
|
776
|
-
_getCaret(collaborator) {
|
|
777
|
-
let name = collaborator ? collaborator.displayName : 'Anonymous';
|
|
778
|
-
let color = collaborator ? collaborator.color : this._selectionStyle.color;
|
|
779
|
-
let caret = document.createElement('span');
|
|
780
|
-
caret.className = COLLABORATOR_CURSOR_CLASS;
|
|
781
|
-
caret.style.borderBottomColor = color;
|
|
782
|
-
caret.onmouseenter = () => {
|
|
783
|
-
this._clearHover();
|
|
784
|
-
this._hoverId = collaborator.sessionId;
|
|
785
|
-
let rect = caret.getBoundingClientRect();
|
|
786
|
-
// Construct and place the hover box.
|
|
787
|
-
let hover = document.createElement('div');
|
|
788
|
-
hover.className = COLLABORATOR_HOVER_CLASS;
|
|
789
|
-
hover.style.left = String(rect.left) + 'px';
|
|
790
|
-
hover.style.top = String(rect.bottom) + 'px';
|
|
791
|
-
hover.textContent = name;
|
|
792
|
-
hover.style.backgroundColor = color;
|
|
793
|
-
// If the user mouses over the hover, take over the timer.
|
|
794
|
-
hover.onmouseenter = () => {
|
|
795
|
-
window.clearTimeout(this._hoverTimeout);
|
|
796
|
-
};
|
|
797
|
-
hover.onmouseleave = () => {
|
|
798
|
-
this._hoverTimeout = window.setTimeout(() => {
|
|
799
|
-
this._clearHover();
|
|
800
|
-
}, HOVER_TIMEOUT);
|
|
801
|
-
};
|
|
802
|
-
this._caretHover = hover;
|
|
803
|
-
document.body.appendChild(hover);
|
|
804
|
-
};
|
|
805
|
-
caret.onmouseleave = () => {
|
|
806
|
-
this._hoverTimeout = window.setTimeout(() => {
|
|
807
|
-
this._clearHover();
|
|
808
|
-
}, HOVER_TIMEOUT);
|
|
809
|
-
};
|
|
810
|
-
return caret;
|
|
811
|
-
}
|
|
812
|
-
/**
|
|
813
|
-
* Check for an out of sync editor.
|
|
814
|
-
*/
|
|
815
|
-
_checkSync() {
|
|
816
|
-
let change = this._lastChange;
|
|
817
|
-
if (!change) {
|
|
818
|
-
return;
|
|
819
|
-
}
|
|
820
|
-
this._lastChange = null;
|
|
821
|
-
let editor = this._editor;
|
|
822
|
-
let doc = editor.getDoc();
|
|
823
|
-
if (doc.getValue() === this._model.value.text) {
|
|
824
|
-
return;
|
|
825
|
-
}
|
|
826
|
-
apputils_1.showDialog({
|
|
827
|
-
title: 'Code Editor out of Sync',
|
|
828
|
-
body: 'Please open your browser JavaScript console for bug report instructions'
|
|
829
|
-
});
|
|
830
|
-
console.log('Please paste the following to https://github.com/jupyterlab/jupyterlab/issues/2951');
|
|
831
|
-
console.log(JSON.stringify({
|
|
832
|
-
model: this._model.value.text,
|
|
833
|
-
view: doc.getValue(),
|
|
834
|
-
selections: this.getSelections(),
|
|
835
|
-
cursor: this.getCursorPosition(),
|
|
836
|
-
lineSep: editor.getOption('lineSeparator'),
|
|
837
|
-
mode: editor.getOption('mode'),
|
|
838
|
-
change
|
|
839
|
-
}));
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
exports.CodeMirrorEditor = CodeMirrorEditor;
|
|
843
|
-
/**
|
|
844
|
-
* The namespace for `CodeMirrorEditor` statics.
|
|
845
|
-
*/
|
|
846
|
-
(function (CodeMirrorEditor) {
|
|
847
|
-
/**
|
|
848
|
-
* The default configuration options for an editor.
|
|
849
|
-
*/
|
|
850
|
-
CodeMirrorEditor.defaultConfig = Object.assign({}, codeeditor_1.CodeEditor.defaultConfig, { mode: 'null', theme: 'jupyter', smartIndent: true, electricChars: true, keyMap: 'default', extraKeys: null, gutters: [], fixedGutter: true, showCursorWhenSelecting: false, coverGutterNextToScrollbar: false, dragDrop: true, lineSeparator: null, scrollbarStyle: 'native', lineWiseCopyCut: true, scrollPastEnd: false });
|
|
851
|
-
/**
|
|
852
|
-
* Add a command to CodeMirror.
|
|
853
|
-
*
|
|
854
|
-
* @param name - The name of the command to add.
|
|
855
|
-
*
|
|
856
|
-
* @param command - The command function.
|
|
857
|
-
*/
|
|
858
|
-
function addCommand(name, command) {
|
|
859
|
-
codemirror_1.default.commands[name] = command;
|
|
860
|
-
}
|
|
861
|
-
CodeMirrorEditor.addCommand = addCommand;
|
|
862
|
-
})(CodeMirrorEditor = exports.CodeMirrorEditor || (exports.CodeMirrorEditor = {}));
|
|
863
|
-
/**
|
|
864
|
-
* The namespace for module private data.
|
|
865
|
-
*/
|
|
866
|
-
var Private;
|
|
867
|
-
(function (Private) {
|
|
868
|
-
function createEditor(host, config) {
|
|
869
|
-
let { autoClosingBrackets, fontFamily, fontSize, insertSpaces, lineHeight, lineWrap, wordWrapColumn, tabSize, readOnly } = config, otherOptions = __rest(config, ["autoClosingBrackets", "fontFamily", "fontSize", "insertSpaces", "lineHeight", "lineWrap", "wordWrapColumn", "tabSize", "readOnly"]);
|
|
870
|
-
let bareConfig = Object.assign({ autoCloseBrackets: autoClosingBrackets, indentUnit: tabSize, indentWithTabs: !insertSpaces, lineWrapping: lineWrap === 'off' ? false : true, readOnly }, otherOptions);
|
|
871
|
-
return codemirror_1.default(el => {
|
|
872
|
-
if (fontFamily) {
|
|
873
|
-
el.style.fontFamily = fontFamily;
|
|
874
|
-
}
|
|
875
|
-
if (fontSize) {
|
|
876
|
-
el.style.fontSize = fontSize + 'px';
|
|
877
|
-
}
|
|
878
|
-
if (lineHeight) {
|
|
879
|
-
el.style.lineHeight = lineHeight.toString();
|
|
880
|
-
}
|
|
881
|
-
if (readOnly) {
|
|
882
|
-
el.classList.add(READ_ONLY_CLASS);
|
|
883
|
-
}
|
|
884
|
-
if (lineWrap === 'wordWrapColumn') {
|
|
885
|
-
const lines = el.querySelector('.CodeMirror-lines');
|
|
886
|
-
lines.style.width = `${wordWrapColumn}ch`;
|
|
887
|
-
}
|
|
888
|
-
if (lineWrap === 'bounded') {
|
|
889
|
-
const lines = el.querySelector('.CodeMirror-lines');
|
|
890
|
-
lines.style.maxWidth = `${wordWrapColumn}ch`;
|
|
891
|
-
}
|
|
892
|
-
host.appendChild(el);
|
|
893
|
-
}, bareConfig);
|
|
894
|
-
}
|
|
895
|
-
Private.createEditor = createEditor;
|
|
896
|
-
/**
|
|
897
|
-
* Indent or insert a tab as appropriate.
|
|
898
|
-
*/
|
|
899
|
-
function indentMoreOrinsertTab(cm) {
|
|
900
|
-
let doc = cm.getDoc();
|
|
901
|
-
let from = doc.getCursor('from');
|
|
902
|
-
let to = doc.getCursor('to');
|
|
903
|
-
let sel = !posEq(from, to);
|
|
904
|
-
if (sel) {
|
|
905
|
-
codemirror_1.default.commands['indentMore'](cm);
|
|
906
|
-
return;
|
|
907
|
-
}
|
|
908
|
-
// Check for start of line.
|
|
909
|
-
let line = doc.getLine(from.line);
|
|
910
|
-
let before = line.slice(0, from.ch);
|
|
911
|
-
if (/^\s*$/.test(before)) {
|
|
912
|
-
codemirror_1.default.commands['indentMore'](cm);
|
|
913
|
-
}
|
|
914
|
-
else {
|
|
915
|
-
if (cm.getOption('indentWithTabs')) {
|
|
916
|
-
codemirror_1.default.commands['insertTab'](cm);
|
|
917
|
-
}
|
|
918
|
-
else {
|
|
919
|
-
codemirror_1.default.commands['insertSoftTab'](cm);
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
Private.indentMoreOrinsertTab = indentMoreOrinsertTab;
|
|
924
|
-
/**
|
|
925
|
-
* Delete spaces to the previous tab stob in a codemirror editor.
|
|
926
|
-
*/
|
|
927
|
-
function delSpaceToPrevTabStop(cm) {
|
|
928
|
-
let doc = cm.getDoc();
|
|
929
|
-
let from = doc.getCursor('from');
|
|
930
|
-
let to = doc.getCursor('to');
|
|
931
|
-
let sel = !posEq(from, to);
|
|
932
|
-
if (sel) {
|
|
933
|
-
let ranges = doc.listSelections();
|
|
934
|
-
for (let i = ranges.length - 1; i >= 0; i--) {
|
|
935
|
-
let head = ranges[i].head;
|
|
936
|
-
let anchor = ranges[i].anchor;
|
|
937
|
-
doc.replaceRange('', codemirror_1.default.Pos(head.line, head.ch), codemirror_1.default.Pos(anchor.line, anchor.ch));
|
|
938
|
-
}
|
|
939
|
-
return;
|
|
940
|
-
}
|
|
941
|
-
let cur = doc.getCursor();
|
|
942
|
-
let tabsize = cm.getOption('tabSize');
|
|
943
|
-
let chToPrevTabStop = cur.ch - (Math.ceil(cur.ch / tabsize) - 1) * tabsize;
|
|
944
|
-
from = { ch: cur.ch - chToPrevTabStop, line: cur.line };
|
|
945
|
-
let select = doc.getRange(from, cur);
|
|
946
|
-
if (select.match(/^\ +$/) !== null) {
|
|
947
|
-
doc.replaceRange('', from, cur);
|
|
948
|
-
}
|
|
949
|
-
else {
|
|
950
|
-
codemirror_1.default.commands['delCharBefore'](cm);
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
Private.delSpaceToPrevTabStop = delSpaceToPrevTabStop;
|
|
954
|
-
/**
|
|
955
|
-
* Test whether two CodeMirror positions are equal.
|
|
956
|
-
*/
|
|
957
|
-
function posEq(a, b) {
|
|
958
|
-
return a.line === b.line && a.ch === b.ch;
|
|
959
|
-
}
|
|
960
|
-
Private.posEq = posEq;
|
|
961
|
-
/**
|
|
962
|
-
* Set a config option for the editor.
|
|
963
|
-
*/
|
|
964
|
-
function setOption(editor, option, value, config) {
|
|
965
|
-
let el = editor.getWrapperElement();
|
|
966
|
-
switch (option) {
|
|
967
|
-
case 'lineWrap':
|
|
968
|
-
const lineWrapping = value === 'off' ? false : true;
|
|
969
|
-
const lines = el.querySelector('.CodeMirror-lines');
|
|
970
|
-
const maxWidth = value === 'bounded' ? `${config.wordWrapColumn}ch` : null;
|
|
971
|
-
const width = value === 'wordWrapColumn' ? `${config.wordWrapColumn}ch` : null;
|
|
972
|
-
lines.style.maxWidth = maxWidth;
|
|
973
|
-
lines.style.width = width;
|
|
974
|
-
editor.setOption('lineWrapping', lineWrapping);
|
|
975
|
-
break;
|
|
976
|
-
case 'wordWrapColumn':
|
|
977
|
-
const { lineWrap } = config;
|
|
978
|
-
if (lineWrap === 'wordWrapColumn' || lineWrap === 'bounded') {
|
|
979
|
-
const lines = el.querySelector('.CodeMirror-lines');
|
|
980
|
-
const prop = lineWrap === 'wordWrapColumn' ? 'width' : 'maxWidth';
|
|
981
|
-
lines.style[prop] = `${value}ch`;
|
|
982
|
-
}
|
|
983
|
-
break;
|
|
984
|
-
case 'tabSize':
|
|
985
|
-
editor.setOption('indentUnit', value);
|
|
986
|
-
break;
|
|
987
|
-
case 'insertSpaces':
|
|
988
|
-
editor.setOption('indentWithTabs', !value);
|
|
989
|
-
break;
|
|
990
|
-
case 'autoClosingBrackets':
|
|
991
|
-
editor.setOption('autoCloseBrackets', value);
|
|
992
|
-
break;
|
|
993
|
-
case 'readOnly':
|
|
994
|
-
el.classList.toggle(READ_ONLY_CLASS, value);
|
|
995
|
-
editor.setOption(option, value);
|
|
996
|
-
break;
|
|
997
|
-
case 'fontFamily':
|
|
998
|
-
el.style.fontFamily = value;
|
|
999
|
-
break;
|
|
1000
|
-
case 'fontSize':
|
|
1001
|
-
el.style.fontSize = value ? value + 'px' : null;
|
|
1002
|
-
break;
|
|
1003
|
-
case 'lineHeight':
|
|
1004
|
-
el.style.lineHeight = value ? value.toString() : null;
|
|
1005
|
-
break;
|
|
1006
|
-
default:
|
|
1007
|
-
editor.setOption(option, value);
|
|
1008
|
-
break;
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
Private.setOption = setOption;
|
|
1012
|
-
})(Private || (Private = {}));
|
|
1013
|
-
/**
|
|
1014
|
-
* Add a CodeMirror command to delete until previous non blanking space
|
|
1015
|
-
* character or first multiple of tabsize tabstop.
|
|
1016
|
-
*/
|
|
1017
|
-
CodeMirrorEditor.addCommand('delSpaceToPrevTabStop', Private.delSpaceToPrevTabStop);
|
|
1018
|
-
/**
|
|
1019
|
-
* Add a CodeMirror command to indent or insert a tab as appropriate.
|
|
1020
|
-
*/
|
|
1021
|
-
CodeMirrorEditor.addCommand('indentMoreOrinsertTab', Private.indentMoreOrinsertTab);
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Jupyter Development Team.
|
|
3
|
+
// Distributed under the terms of the Modified BSD License.
|
|
4
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
5
|
+
var t = {};
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
9
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
|
10
|
+
t[p[i]] = s[p[i]];
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const codemirror_1 = __importDefault(require("codemirror"));
|
|
18
|
+
const coreutils_1 = require("@phosphor/coreutils");
|
|
19
|
+
const algorithm_1 = require("@phosphor/algorithm");
|
|
20
|
+
const disposable_1 = require("@phosphor/disposable");
|
|
21
|
+
const signaling_1 = require("@phosphor/signaling");
|
|
22
|
+
const apputils_1 = require("@jupyterlab/apputils");
|
|
23
|
+
const codeeditor_1 = require("@jupyterlab/codeeditor");
|
|
24
|
+
const coreutils_2 = require("@phosphor/coreutils");
|
|
25
|
+
const mode_1 = require("./mode");
|
|
26
|
+
require("codemirror/addon/comment/comment.js");
|
|
27
|
+
require("codemirror/addon/edit/matchbrackets.js");
|
|
28
|
+
require("codemirror/addon/edit/closebrackets.js");
|
|
29
|
+
require("codemirror/addon/scroll/scrollpastend.js");
|
|
30
|
+
require("codemirror/addon/search/searchcursor");
|
|
31
|
+
require("codemirror/addon/search/search");
|
|
32
|
+
require("codemirror/addon/search/jump-to-line");
|
|
33
|
+
require("codemirror/keymap/emacs.js");
|
|
34
|
+
require("codemirror/keymap/sublime.js");
|
|
35
|
+
require("codemirror/keymap/vim.js");
|
|
36
|
+
/**
|
|
37
|
+
* The class name added to CodeMirrorWidget instances.
|
|
38
|
+
*/
|
|
39
|
+
const EDITOR_CLASS = 'jp-CodeMirrorEditor';
|
|
40
|
+
/**
|
|
41
|
+
* The class name added to read only cell editor widgets.
|
|
42
|
+
*/
|
|
43
|
+
const READ_ONLY_CLASS = 'jp-mod-readOnly';
|
|
44
|
+
/**
|
|
45
|
+
* The class name for the hover box for collaborator cursors.
|
|
46
|
+
*/
|
|
47
|
+
const COLLABORATOR_CURSOR_CLASS = 'jp-CollaboratorCursor';
|
|
48
|
+
/**
|
|
49
|
+
* The class name for the hover box for collaborator cursors.
|
|
50
|
+
*/
|
|
51
|
+
const COLLABORATOR_HOVER_CLASS = 'jp-CollaboratorCursor-hover';
|
|
52
|
+
/**
|
|
53
|
+
* The key code for the up arrow key.
|
|
54
|
+
*/
|
|
55
|
+
const UP_ARROW = 38;
|
|
56
|
+
/**
|
|
57
|
+
* The key code for the down arrow key.
|
|
58
|
+
*/
|
|
59
|
+
const DOWN_ARROW = 40;
|
|
60
|
+
/**
|
|
61
|
+
* The time that a collaborator name hover persists.
|
|
62
|
+
*/
|
|
63
|
+
const HOVER_TIMEOUT = 1000;
|
|
64
|
+
/**
|
|
65
|
+
* CodeMirror editor.
|
|
66
|
+
*/
|
|
67
|
+
class CodeMirrorEditor {
|
|
68
|
+
/**
|
|
69
|
+
* Construct a CodeMirror editor.
|
|
70
|
+
*/
|
|
71
|
+
constructor(options) {
|
|
72
|
+
/**
|
|
73
|
+
* A signal emitted when either the top or bottom edge is requested.
|
|
74
|
+
*/
|
|
75
|
+
this.edgeRequested = new signaling_1.Signal(this);
|
|
76
|
+
this.selectionMarkers = {};
|
|
77
|
+
this._keydownHandlers = new Array();
|
|
78
|
+
this._changeGuard = false;
|
|
79
|
+
this._uuid = '';
|
|
80
|
+
this._needsRefresh = false;
|
|
81
|
+
this._isDisposed = false;
|
|
82
|
+
this._lastChange = null;
|
|
83
|
+
this._timer = -1;
|
|
84
|
+
let host = (this.host = options.host);
|
|
85
|
+
host.classList.add(EDITOR_CLASS);
|
|
86
|
+
host.classList.add('jp-Editor');
|
|
87
|
+
host.addEventListener('focus', this, true);
|
|
88
|
+
host.addEventListener('blur', this, true);
|
|
89
|
+
host.addEventListener('scroll', this, true);
|
|
90
|
+
this._uuid = options.uuid || coreutils_2.UUID.uuid4();
|
|
91
|
+
// Handle selection style.
|
|
92
|
+
let style = options.selectionStyle || {};
|
|
93
|
+
this._selectionStyle = Object.assign({}, codeeditor_1.CodeEditor.defaultSelectionStyle, style);
|
|
94
|
+
let model = (this._model = options.model);
|
|
95
|
+
let config = options.config || {};
|
|
96
|
+
let fullConfig = (this._config = Object.assign({}, CodeMirrorEditor.defaultConfig, config));
|
|
97
|
+
let editor = (this._editor = Private.createEditor(host, fullConfig));
|
|
98
|
+
let doc = editor.getDoc();
|
|
99
|
+
// Handle initial values for text, mimetype, and selections.
|
|
100
|
+
doc.setValue(model.value.text);
|
|
101
|
+
this.clearHistory();
|
|
102
|
+
this._onMimeTypeChanged();
|
|
103
|
+
this._onCursorActivity();
|
|
104
|
+
this._timer = window.setInterval(() => {
|
|
105
|
+
this._checkSync();
|
|
106
|
+
}, 3000);
|
|
107
|
+
// Connect to changes.
|
|
108
|
+
model.value.changed.connect(this._onValueChanged, this);
|
|
109
|
+
model.mimeTypeChanged.connect(this._onMimeTypeChanged, this);
|
|
110
|
+
model.selections.changed.connect(this._onSelectionsChanged, this);
|
|
111
|
+
codemirror_1.default.on(editor, 'keydown', (editor, event) => {
|
|
112
|
+
let index = algorithm_1.ArrayExt.findFirstIndex(this._keydownHandlers, handler => {
|
|
113
|
+
if (handler(this, event) === true) {
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
});
|
|
119
|
+
if (index === -1) {
|
|
120
|
+
this.onKeydown(event);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
codemirror_1.default.on(editor, 'cursorActivity', () => this._onCursorActivity());
|
|
124
|
+
codemirror_1.default.on(editor.getDoc(), 'beforeChange', (instance, change) => {
|
|
125
|
+
this._beforeDocChanged(instance, change);
|
|
126
|
+
});
|
|
127
|
+
codemirror_1.default.on(editor.getDoc(), 'change', (instance, change) => {
|
|
128
|
+
// Manually refresh after setValue to make sure editor is properly sized.
|
|
129
|
+
if (change.origin === 'setValue' && this.hasFocus()) {
|
|
130
|
+
this.refresh();
|
|
131
|
+
}
|
|
132
|
+
this._lastChange = change;
|
|
133
|
+
});
|
|
134
|
+
// Manually refresh on paste to make sure editor is properly sized.
|
|
135
|
+
editor.getWrapperElement().addEventListener('paste', () => {
|
|
136
|
+
if (this.hasFocus()) {
|
|
137
|
+
this.refresh();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The uuid of this editor;
|
|
143
|
+
*/
|
|
144
|
+
get uuid() {
|
|
145
|
+
return this._uuid;
|
|
146
|
+
}
|
|
147
|
+
set uuid(value) {
|
|
148
|
+
this._uuid = value;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The selection style of this editor.
|
|
152
|
+
*/
|
|
153
|
+
get selectionStyle() {
|
|
154
|
+
return this._selectionStyle;
|
|
155
|
+
}
|
|
156
|
+
set selectionStyle(value) {
|
|
157
|
+
this._selectionStyle = value;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get the codemirror editor wrapped by the editor.
|
|
161
|
+
*/
|
|
162
|
+
get editor() {
|
|
163
|
+
return this._editor;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get the codemirror doc wrapped by the widget.
|
|
167
|
+
*/
|
|
168
|
+
get doc() {
|
|
169
|
+
return this._editor.getDoc();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get the number of lines in the editor.
|
|
173
|
+
*/
|
|
174
|
+
get lineCount() {
|
|
175
|
+
return this.doc.lineCount();
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Returns a model for this editor.
|
|
179
|
+
*/
|
|
180
|
+
get model() {
|
|
181
|
+
return this._model;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The height of a line in the editor in pixels.
|
|
185
|
+
*/
|
|
186
|
+
get lineHeight() {
|
|
187
|
+
return this._editor.defaultTextHeight();
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The widget of a character in the editor in pixels.
|
|
191
|
+
*/
|
|
192
|
+
get charWidth() {
|
|
193
|
+
return this._editor.defaultCharWidth();
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Tests whether the editor is disposed.
|
|
197
|
+
*/
|
|
198
|
+
get isDisposed() {
|
|
199
|
+
return this._isDisposed;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Dispose of the resources held by the widget.
|
|
203
|
+
*/
|
|
204
|
+
dispose() {
|
|
205
|
+
if (this.isDisposed) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
this._isDisposed = true;
|
|
209
|
+
this.host.removeEventListener('focus', this, true);
|
|
210
|
+
this.host.removeEventListener('blur', this, true);
|
|
211
|
+
this.host.removeEventListener('scroll', this, true);
|
|
212
|
+
this._keydownHandlers.length = 0;
|
|
213
|
+
window.clearInterval(this._timer);
|
|
214
|
+
signaling_1.Signal.clearData(this);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Get a config option for the editor.
|
|
218
|
+
*/
|
|
219
|
+
getOption(option) {
|
|
220
|
+
return this._config[option];
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Set a config option for the editor.
|
|
224
|
+
*/
|
|
225
|
+
setOption(option, value) {
|
|
226
|
+
// Don't bother setting the option if it is already the same.
|
|
227
|
+
if (this._config[option] !== value) {
|
|
228
|
+
this._config[option] = value;
|
|
229
|
+
Private.setOption(this.editor, option, value, this._config);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Returns the content for the given line number.
|
|
234
|
+
*/
|
|
235
|
+
getLine(line) {
|
|
236
|
+
return this.doc.getLine(line);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Find an offset for the given position.
|
|
240
|
+
*/
|
|
241
|
+
getOffsetAt(position) {
|
|
242
|
+
return this.doc.indexFromPos({
|
|
243
|
+
ch: position.column,
|
|
244
|
+
line: position.line
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Find a position for the given offset.
|
|
249
|
+
*/
|
|
250
|
+
getPositionAt(offset) {
|
|
251
|
+
const { ch, line } = this.doc.posFromIndex(offset);
|
|
252
|
+
return { line, column: ch };
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Undo one edit (if any undo events are stored).
|
|
256
|
+
*/
|
|
257
|
+
undo() {
|
|
258
|
+
this.doc.undo();
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Redo one undone edit.
|
|
262
|
+
*/
|
|
263
|
+
redo() {
|
|
264
|
+
this.doc.redo();
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Clear the undo history.
|
|
268
|
+
*/
|
|
269
|
+
clearHistory() {
|
|
270
|
+
this.doc.clearHistory();
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Brings browser focus to this editor text.
|
|
274
|
+
*/
|
|
275
|
+
focus() {
|
|
276
|
+
this._editor.focus();
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Test whether the editor has keyboard focus.
|
|
280
|
+
*/
|
|
281
|
+
hasFocus() {
|
|
282
|
+
return this._editor.getWrapperElement().contains(document.activeElement);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Explicitly blur the editor.
|
|
286
|
+
*/
|
|
287
|
+
blur() {
|
|
288
|
+
this._editor.getInputField().blur();
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Repaint editor.
|
|
292
|
+
*/
|
|
293
|
+
refresh() {
|
|
294
|
+
this._editor.refresh();
|
|
295
|
+
this._needsRefresh = false;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Refresh the editor if it is focused;
|
|
299
|
+
* otherwise postpone refreshing till focusing.
|
|
300
|
+
*/
|
|
301
|
+
resizeToFit() {
|
|
302
|
+
if (this.hasFocus()) {
|
|
303
|
+
this.refresh();
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
this._needsRefresh = true;
|
|
307
|
+
}
|
|
308
|
+
this._clearHover();
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Add a keydown handler to the editor.
|
|
312
|
+
*
|
|
313
|
+
* @param handler - A keydown handler.
|
|
314
|
+
*
|
|
315
|
+
* @returns A disposable that can be used to remove the handler.
|
|
316
|
+
*/
|
|
317
|
+
addKeydownHandler(handler) {
|
|
318
|
+
this._keydownHandlers.push(handler);
|
|
319
|
+
return new disposable_1.DisposableDelegate(() => {
|
|
320
|
+
algorithm_1.ArrayExt.removeAllWhere(this._keydownHandlers, val => val === handler);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Set the size of the editor in pixels.
|
|
325
|
+
*/
|
|
326
|
+
setSize(dimension) {
|
|
327
|
+
if (dimension) {
|
|
328
|
+
this._editor.setSize(dimension.width, dimension.height);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
this._editor.setSize(null, null);
|
|
332
|
+
}
|
|
333
|
+
this._needsRefresh = false;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Reveal the given position in the editor.
|
|
337
|
+
*/
|
|
338
|
+
revealPosition(position) {
|
|
339
|
+
const cmPosition = this._toCodeMirrorPosition(position);
|
|
340
|
+
this._editor.scrollIntoView(cmPosition);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Reveal the given selection in the editor.
|
|
344
|
+
*/
|
|
345
|
+
revealSelection(selection) {
|
|
346
|
+
const range = this._toCodeMirrorRange(selection);
|
|
347
|
+
this._editor.scrollIntoView(range);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Get the window coordinates given a cursor position.
|
|
351
|
+
*/
|
|
352
|
+
getCoordinateForPosition(position) {
|
|
353
|
+
const pos = this._toCodeMirrorPosition(position);
|
|
354
|
+
const rect = this.editor.charCoords(pos, 'page');
|
|
355
|
+
return rect;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Get the cursor position given window coordinates.
|
|
359
|
+
*
|
|
360
|
+
* @param coordinate - The desired coordinate.
|
|
361
|
+
*
|
|
362
|
+
* @returns The position of the coordinates, or null if not
|
|
363
|
+
* contained in the editor.
|
|
364
|
+
*/
|
|
365
|
+
getPositionForCoordinate(coordinate) {
|
|
366
|
+
return this._toPosition(this.editor.coordsChar(coordinate)) || null;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Returns the primary position of the cursor, never `null`.
|
|
370
|
+
*/
|
|
371
|
+
getCursorPosition() {
|
|
372
|
+
const cursor = this.doc.getCursor();
|
|
373
|
+
return this._toPosition(cursor);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Set the primary position of the cursor.
|
|
377
|
+
*
|
|
378
|
+
* #### Notes
|
|
379
|
+
* This will remove any secondary cursors.
|
|
380
|
+
*/
|
|
381
|
+
setCursorPosition(position) {
|
|
382
|
+
const cursor = this._toCodeMirrorPosition(position);
|
|
383
|
+
this.doc.setCursor(cursor);
|
|
384
|
+
// If the editor does not have focus, this cursor change
|
|
385
|
+
// will get screened out in _onCursorsChanged(). Make an
|
|
386
|
+
// exception for this method.
|
|
387
|
+
if (!this.editor.hasFocus()) {
|
|
388
|
+
this.model.selections.set(this.uuid, this.getSelections());
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Returns the primary selection, never `null`.
|
|
393
|
+
*/
|
|
394
|
+
getSelection() {
|
|
395
|
+
return this.getSelections()[0];
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Set the primary selection. This will remove any secondary cursors.
|
|
399
|
+
*/
|
|
400
|
+
setSelection(selection) {
|
|
401
|
+
this.setSelections([selection]);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Gets the selections for all the cursors, never `null` or empty.
|
|
405
|
+
*/
|
|
406
|
+
getSelections() {
|
|
407
|
+
const selections = this.doc.listSelections();
|
|
408
|
+
if (selections.length > 0) {
|
|
409
|
+
return selections.map(selection => this._toSelection(selection));
|
|
410
|
+
}
|
|
411
|
+
const cursor = this.doc.getCursor();
|
|
412
|
+
const selection = this._toSelection({ anchor: cursor, head: cursor });
|
|
413
|
+
return [selection];
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Sets the selections for all the cursors, should not be empty.
|
|
417
|
+
* Cursors will be removed or added, as necessary.
|
|
418
|
+
* Passing an empty array resets a cursor position to the start of a document.
|
|
419
|
+
*/
|
|
420
|
+
setSelections(selections) {
|
|
421
|
+
const cmSelections = this._toCodeMirrorSelections(selections);
|
|
422
|
+
this.doc.setSelections(cmSelections, 0);
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Get a list of tokens for the current editor text content.
|
|
426
|
+
*/
|
|
427
|
+
getTokens() {
|
|
428
|
+
let tokens = [];
|
|
429
|
+
for (let i = 0; i < this.lineCount; ++i) {
|
|
430
|
+
const lineTokens = this.editor.getLineTokens(i).map(t => ({
|
|
431
|
+
offset: this.getOffsetAt({ column: t.start, line: i }),
|
|
432
|
+
value: t.string,
|
|
433
|
+
type: t.type || ''
|
|
434
|
+
}));
|
|
435
|
+
tokens = tokens.concat(lineTokens);
|
|
436
|
+
}
|
|
437
|
+
return tokens;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Get the token at a given editor position.
|
|
441
|
+
*/
|
|
442
|
+
getTokenForPosition(position) {
|
|
443
|
+
const cursor = this._toCodeMirrorPosition(position);
|
|
444
|
+
const token = this.editor.getTokenAt(cursor);
|
|
445
|
+
return {
|
|
446
|
+
offset: this.getOffsetAt({ column: token.start, line: cursor.line }),
|
|
447
|
+
value: token.string,
|
|
448
|
+
type: token.type
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Insert a new indented line at the current cursor position.
|
|
453
|
+
*/
|
|
454
|
+
newIndentedLine() {
|
|
455
|
+
this.execCommand('newlineAndIndent');
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Execute a codemirror command on the editor.
|
|
459
|
+
*
|
|
460
|
+
* @param command - The name of the command to execute.
|
|
461
|
+
*/
|
|
462
|
+
execCommand(command) {
|
|
463
|
+
this._editor.execCommand(command);
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Handle keydown events from the editor.
|
|
467
|
+
*/
|
|
468
|
+
onKeydown(event) {
|
|
469
|
+
let position = this.getCursorPosition();
|
|
470
|
+
let { line, column } = position;
|
|
471
|
+
if (line === 0 && column === 0 && event.keyCode === UP_ARROW) {
|
|
472
|
+
if (!event.shiftKey) {
|
|
473
|
+
this.edgeRequested.emit('top');
|
|
474
|
+
}
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
let lastLine = this.lineCount - 1;
|
|
478
|
+
let lastCh = this.getLine(lastLine).length;
|
|
479
|
+
if (line === lastLine &&
|
|
480
|
+
column === lastCh &&
|
|
481
|
+
event.keyCode === DOWN_ARROW) {
|
|
482
|
+
if (!event.shiftKey) {
|
|
483
|
+
this.edgeRequested.emit('bottom');
|
|
484
|
+
}
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Converts selections to code mirror selections.
|
|
491
|
+
*/
|
|
492
|
+
_toCodeMirrorSelections(selections) {
|
|
493
|
+
if (selections.length > 0) {
|
|
494
|
+
return selections.map(selection => this._toCodeMirrorSelection(selection));
|
|
495
|
+
}
|
|
496
|
+
const position = { line: 0, ch: 0 };
|
|
497
|
+
return [{ anchor: position, head: position }];
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Handles a mime type change.
|
|
501
|
+
*/
|
|
502
|
+
_onMimeTypeChanged() {
|
|
503
|
+
const mime = this._model.mimeType;
|
|
504
|
+
let editor = this._editor;
|
|
505
|
+
mode_1.Mode.ensure(mime).then(spec => {
|
|
506
|
+
editor.setOption('mode', spec.mime);
|
|
507
|
+
});
|
|
508
|
+
let extraKeys = editor.getOption('extraKeys') || {};
|
|
509
|
+
const isCode = mime !== 'text/plain' && mime !== 'text/x-ipythongfm';
|
|
510
|
+
if (isCode) {
|
|
511
|
+
extraKeys['Backspace'] = 'delSpaceToPrevTabStop';
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
delete extraKeys['Backspace'];
|
|
515
|
+
}
|
|
516
|
+
editor.setOption('extraKeys', extraKeys);
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Handles a selections change.
|
|
520
|
+
*/
|
|
521
|
+
_onSelectionsChanged(selections, args) {
|
|
522
|
+
const uuid = args.key;
|
|
523
|
+
if (uuid !== this.uuid) {
|
|
524
|
+
this._cleanSelections(uuid);
|
|
525
|
+
if (args.type !== 'remove' && args.newValue) {
|
|
526
|
+
this._markSelections(uuid, args.newValue);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Clean selections for the given uuid.
|
|
532
|
+
*/
|
|
533
|
+
_cleanSelections(uuid) {
|
|
534
|
+
const markers = this.selectionMarkers[uuid];
|
|
535
|
+
if (markers) {
|
|
536
|
+
markers.forEach(marker => {
|
|
537
|
+
marker.clear();
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
delete this.selectionMarkers[uuid];
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Marks selections.
|
|
544
|
+
*/
|
|
545
|
+
_markSelections(uuid, selections) {
|
|
546
|
+
const markers = [];
|
|
547
|
+
// If we are marking selections corresponding to an active hover,
|
|
548
|
+
// remove it.
|
|
549
|
+
if (uuid === this._hoverId) {
|
|
550
|
+
this._clearHover();
|
|
551
|
+
}
|
|
552
|
+
// If we can id the selection to a specific collaborator,
|
|
553
|
+
// use that information.
|
|
554
|
+
let collaborator;
|
|
555
|
+
if (this._model.modelDB.collaborators) {
|
|
556
|
+
collaborator = this._model.modelDB.collaborators.get(uuid);
|
|
557
|
+
}
|
|
558
|
+
// Style each selection for the uuid.
|
|
559
|
+
selections.forEach(selection => {
|
|
560
|
+
// Only render selections if the start is not equal to the end.
|
|
561
|
+
// In that case, we don't need to render the cursor.
|
|
562
|
+
if (!coreutils_1.JSONExt.deepEqual(selection.start, selection.end)) {
|
|
563
|
+
const { anchor, head } = this._toCodeMirrorSelection(selection);
|
|
564
|
+
let markerOptions;
|
|
565
|
+
if (collaborator) {
|
|
566
|
+
markerOptions = this._toTextMarkerOptions(Object.assign({}, selection.style, { color: collaborator.color }));
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
markerOptions = this._toTextMarkerOptions(selection.style);
|
|
570
|
+
}
|
|
571
|
+
markers.push(this.doc.markText(anchor, head, markerOptions));
|
|
572
|
+
}
|
|
573
|
+
else if (collaborator) {
|
|
574
|
+
let caret = this._getCaret(collaborator);
|
|
575
|
+
markers.push(this.doc.setBookmark(this._toCodeMirrorPosition(selection.end), {
|
|
576
|
+
widget: caret
|
|
577
|
+
}));
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
this.selectionMarkers[uuid] = markers;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Handles a cursor activity event.
|
|
584
|
+
*/
|
|
585
|
+
_onCursorActivity() {
|
|
586
|
+
// Only add selections if the editor has focus. This avoids unwanted
|
|
587
|
+
// triggering of cursor activity due to collaborator actions.
|
|
588
|
+
if (this._editor.hasFocus()) {
|
|
589
|
+
const selections = this.getSelections();
|
|
590
|
+
this.model.selections.set(this.uuid, selections);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Converts a code mirror selection to an editor selection.
|
|
595
|
+
*/
|
|
596
|
+
_toSelection(selection) {
|
|
597
|
+
return {
|
|
598
|
+
uuid: this.uuid,
|
|
599
|
+
start: this._toPosition(selection.anchor),
|
|
600
|
+
end: this._toPosition(selection.head),
|
|
601
|
+
style: this.selectionStyle
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Converts the selection style to a text marker options.
|
|
606
|
+
*/
|
|
607
|
+
_toTextMarkerOptions(style) {
|
|
608
|
+
let r = parseInt(style.color.slice(1, 3), 16);
|
|
609
|
+
let g = parseInt(style.color.slice(3, 5), 16);
|
|
610
|
+
let b = parseInt(style.color.slice(5, 7), 16);
|
|
611
|
+
let css = `background-color: rgba( ${r}, ${g}, ${b}, 0.15)`;
|
|
612
|
+
return {
|
|
613
|
+
className: style.className,
|
|
614
|
+
title: style.displayName,
|
|
615
|
+
css
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Converts an editor selection to a code mirror selection.
|
|
620
|
+
*/
|
|
621
|
+
_toCodeMirrorSelection(selection) {
|
|
622
|
+
// Selections only appear to render correctly if the anchor
|
|
623
|
+
// is before the head in the document. That is, reverse selections
|
|
624
|
+
// do not appear as intended.
|
|
625
|
+
let forward = selection.start.line < selection.end.line ||
|
|
626
|
+
(selection.start.line === selection.end.line &&
|
|
627
|
+
selection.start.column <= selection.end.column);
|
|
628
|
+
let anchor = forward ? selection.start : selection.end;
|
|
629
|
+
let head = forward ? selection.end : selection.start;
|
|
630
|
+
return {
|
|
631
|
+
anchor: this._toCodeMirrorPosition(anchor),
|
|
632
|
+
head: this._toCodeMirrorPosition(head)
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Converts an editor selection to a code mirror selection.
|
|
637
|
+
*/
|
|
638
|
+
_toCodeMirrorRange(range) {
|
|
639
|
+
return {
|
|
640
|
+
from: this._toCodeMirrorPosition(range.start),
|
|
641
|
+
to: this._toCodeMirrorPosition(range.end)
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Convert a code mirror position to an editor position.
|
|
646
|
+
*/
|
|
647
|
+
_toPosition(position) {
|
|
648
|
+
return {
|
|
649
|
+
line: position.line,
|
|
650
|
+
column: position.ch
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Convert an editor position to a code mirror position.
|
|
655
|
+
*/
|
|
656
|
+
_toCodeMirrorPosition(position) {
|
|
657
|
+
return {
|
|
658
|
+
line: position.line,
|
|
659
|
+
ch: position.column
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Handle model value changes.
|
|
664
|
+
*/
|
|
665
|
+
_onValueChanged(value, args) {
|
|
666
|
+
if (this._changeGuard) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
this._changeGuard = true;
|
|
670
|
+
let doc = this.doc;
|
|
671
|
+
switch (args.type) {
|
|
672
|
+
case 'insert':
|
|
673
|
+
let pos = doc.posFromIndex(args.start);
|
|
674
|
+
doc.replaceRange(args.value, pos, pos);
|
|
675
|
+
break;
|
|
676
|
+
case 'remove':
|
|
677
|
+
let from = doc.posFromIndex(args.start);
|
|
678
|
+
let to = doc.posFromIndex(args.end);
|
|
679
|
+
doc.replaceRange('', from, to);
|
|
680
|
+
break;
|
|
681
|
+
case 'set':
|
|
682
|
+
doc.setValue(args.value);
|
|
683
|
+
break;
|
|
684
|
+
default:
|
|
685
|
+
break;
|
|
686
|
+
}
|
|
687
|
+
this._changeGuard = false;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Handles document changes.
|
|
691
|
+
*/
|
|
692
|
+
_beforeDocChanged(doc, change) {
|
|
693
|
+
if (this._changeGuard) {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
this._changeGuard = true;
|
|
697
|
+
let value = this._model.value;
|
|
698
|
+
let start = doc.indexFromPos(change.from);
|
|
699
|
+
let end = doc.indexFromPos(change.to);
|
|
700
|
+
let inserted = change.text.join('\n');
|
|
701
|
+
if (end !== start) {
|
|
702
|
+
value.remove(start, end);
|
|
703
|
+
}
|
|
704
|
+
if (inserted) {
|
|
705
|
+
value.insert(start, inserted);
|
|
706
|
+
}
|
|
707
|
+
this._changeGuard = false;
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Handle the DOM events for the editor.
|
|
711
|
+
*
|
|
712
|
+
* @param event - The DOM event sent to the editor.
|
|
713
|
+
*
|
|
714
|
+
* #### Notes
|
|
715
|
+
* This method implements the DOM `EventListener` interface and is
|
|
716
|
+
* called in response to events on the editor's DOM node. It should
|
|
717
|
+
* not be called directly by user code.
|
|
718
|
+
*/
|
|
719
|
+
handleEvent(event) {
|
|
720
|
+
switch (event.type) {
|
|
721
|
+
case 'focus':
|
|
722
|
+
this._evtFocus(event);
|
|
723
|
+
break;
|
|
724
|
+
case 'blur':
|
|
725
|
+
this._evtBlur(event);
|
|
726
|
+
break;
|
|
727
|
+
case 'scroll':
|
|
728
|
+
this._evtScroll();
|
|
729
|
+
break;
|
|
730
|
+
default:
|
|
731
|
+
break;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Handle `focus` events for the editor.
|
|
736
|
+
*/
|
|
737
|
+
_evtFocus(event) {
|
|
738
|
+
if (this._needsRefresh) {
|
|
739
|
+
this.refresh();
|
|
740
|
+
}
|
|
741
|
+
this.host.classList.add('jp-mod-focused');
|
|
742
|
+
// Update the selections on editor gaining focus because
|
|
743
|
+
// the onCursorActivity function filters usual cursor events
|
|
744
|
+
// based on the editor's focus.
|
|
745
|
+
this._onCursorActivity();
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Handle `blur` events for the editor.
|
|
749
|
+
*/
|
|
750
|
+
_evtBlur(event) {
|
|
751
|
+
this.host.classList.remove('jp-mod-focused');
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Handle `scroll` events for the editor.
|
|
755
|
+
*/
|
|
756
|
+
_evtScroll() {
|
|
757
|
+
// Remove any active hover.
|
|
758
|
+
this._clearHover();
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Clear the hover for a caret, due to things like
|
|
762
|
+
* scrolling, resizing, deactivation, etc, where
|
|
763
|
+
* the position is no longer valid.
|
|
764
|
+
*/
|
|
765
|
+
_clearHover() {
|
|
766
|
+
if (this._caretHover) {
|
|
767
|
+
window.clearTimeout(this._hoverTimeout);
|
|
768
|
+
document.body.removeChild(this._caretHover);
|
|
769
|
+
this._caretHover = null;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Construct a caret element representing the position
|
|
774
|
+
* of a collaborator's cursor.
|
|
775
|
+
*/
|
|
776
|
+
_getCaret(collaborator) {
|
|
777
|
+
let name = collaborator ? collaborator.displayName : 'Anonymous';
|
|
778
|
+
let color = collaborator ? collaborator.color : this._selectionStyle.color;
|
|
779
|
+
let caret = document.createElement('span');
|
|
780
|
+
caret.className = COLLABORATOR_CURSOR_CLASS;
|
|
781
|
+
caret.style.borderBottomColor = color;
|
|
782
|
+
caret.onmouseenter = () => {
|
|
783
|
+
this._clearHover();
|
|
784
|
+
this._hoverId = collaborator.sessionId;
|
|
785
|
+
let rect = caret.getBoundingClientRect();
|
|
786
|
+
// Construct and place the hover box.
|
|
787
|
+
let hover = document.createElement('div');
|
|
788
|
+
hover.className = COLLABORATOR_HOVER_CLASS;
|
|
789
|
+
hover.style.left = String(rect.left) + 'px';
|
|
790
|
+
hover.style.top = String(rect.bottom) + 'px';
|
|
791
|
+
hover.textContent = name;
|
|
792
|
+
hover.style.backgroundColor = color;
|
|
793
|
+
// If the user mouses over the hover, take over the timer.
|
|
794
|
+
hover.onmouseenter = () => {
|
|
795
|
+
window.clearTimeout(this._hoverTimeout);
|
|
796
|
+
};
|
|
797
|
+
hover.onmouseleave = () => {
|
|
798
|
+
this._hoverTimeout = window.setTimeout(() => {
|
|
799
|
+
this._clearHover();
|
|
800
|
+
}, HOVER_TIMEOUT);
|
|
801
|
+
};
|
|
802
|
+
this._caretHover = hover;
|
|
803
|
+
document.body.appendChild(hover);
|
|
804
|
+
};
|
|
805
|
+
caret.onmouseleave = () => {
|
|
806
|
+
this._hoverTimeout = window.setTimeout(() => {
|
|
807
|
+
this._clearHover();
|
|
808
|
+
}, HOVER_TIMEOUT);
|
|
809
|
+
};
|
|
810
|
+
return caret;
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Check for an out of sync editor.
|
|
814
|
+
*/
|
|
815
|
+
_checkSync() {
|
|
816
|
+
let change = this._lastChange;
|
|
817
|
+
if (!change) {
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
this._lastChange = null;
|
|
821
|
+
let editor = this._editor;
|
|
822
|
+
let doc = editor.getDoc();
|
|
823
|
+
if (doc.getValue() === this._model.value.text) {
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
apputils_1.showDialog({
|
|
827
|
+
title: 'Code Editor out of Sync',
|
|
828
|
+
body: 'Please open your browser JavaScript console for bug report instructions'
|
|
829
|
+
});
|
|
830
|
+
console.log('Please paste the following to https://github.com/jupyterlab/jupyterlab/issues/2951');
|
|
831
|
+
console.log(JSON.stringify({
|
|
832
|
+
model: this._model.value.text,
|
|
833
|
+
view: doc.getValue(),
|
|
834
|
+
selections: this.getSelections(),
|
|
835
|
+
cursor: this.getCursorPosition(),
|
|
836
|
+
lineSep: editor.getOption('lineSeparator'),
|
|
837
|
+
mode: editor.getOption('mode'),
|
|
838
|
+
change
|
|
839
|
+
}));
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
exports.CodeMirrorEditor = CodeMirrorEditor;
|
|
843
|
+
/**
|
|
844
|
+
* The namespace for `CodeMirrorEditor` statics.
|
|
845
|
+
*/
|
|
846
|
+
(function (CodeMirrorEditor) {
|
|
847
|
+
/**
|
|
848
|
+
* The default configuration options for an editor.
|
|
849
|
+
*/
|
|
850
|
+
CodeMirrorEditor.defaultConfig = Object.assign({}, codeeditor_1.CodeEditor.defaultConfig, { mode: 'null', theme: 'jupyter', smartIndent: true, electricChars: true, keyMap: 'default', extraKeys: null, gutters: [], fixedGutter: true, showCursorWhenSelecting: false, coverGutterNextToScrollbar: false, dragDrop: true, lineSeparator: null, scrollbarStyle: 'native', lineWiseCopyCut: true, scrollPastEnd: false });
|
|
851
|
+
/**
|
|
852
|
+
* Add a command to CodeMirror.
|
|
853
|
+
*
|
|
854
|
+
* @param name - The name of the command to add.
|
|
855
|
+
*
|
|
856
|
+
* @param command - The command function.
|
|
857
|
+
*/
|
|
858
|
+
function addCommand(name, command) {
|
|
859
|
+
codemirror_1.default.commands[name] = command;
|
|
860
|
+
}
|
|
861
|
+
CodeMirrorEditor.addCommand = addCommand;
|
|
862
|
+
})(CodeMirrorEditor = exports.CodeMirrorEditor || (exports.CodeMirrorEditor = {}));
|
|
863
|
+
/**
|
|
864
|
+
* The namespace for module private data.
|
|
865
|
+
*/
|
|
866
|
+
var Private;
|
|
867
|
+
(function (Private) {
|
|
868
|
+
function createEditor(host, config) {
|
|
869
|
+
let { autoClosingBrackets, fontFamily, fontSize, insertSpaces, lineHeight, lineWrap, wordWrapColumn, tabSize, readOnly } = config, otherOptions = __rest(config, ["autoClosingBrackets", "fontFamily", "fontSize", "insertSpaces", "lineHeight", "lineWrap", "wordWrapColumn", "tabSize", "readOnly"]);
|
|
870
|
+
let bareConfig = Object.assign({ autoCloseBrackets: autoClosingBrackets, indentUnit: tabSize, indentWithTabs: !insertSpaces, lineWrapping: lineWrap === 'off' ? false : true, readOnly }, otherOptions);
|
|
871
|
+
return codemirror_1.default(el => {
|
|
872
|
+
if (fontFamily) {
|
|
873
|
+
el.style.fontFamily = fontFamily;
|
|
874
|
+
}
|
|
875
|
+
if (fontSize) {
|
|
876
|
+
el.style.fontSize = fontSize + 'px';
|
|
877
|
+
}
|
|
878
|
+
if (lineHeight) {
|
|
879
|
+
el.style.lineHeight = lineHeight.toString();
|
|
880
|
+
}
|
|
881
|
+
if (readOnly) {
|
|
882
|
+
el.classList.add(READ_ONLY_CLASS);
|
|
883
|
+
}
|
|
884
|
+
if (lineWrap === 'wordWrapColumn') {
|
|
885
|
+
const lines = el.querySelector('.CodeMirror-lines');
|
|
886
|
+
lines.style.width = `${wordWrapColumn}ch`;
|
|
887
|
+
}
|
|
888
|
+
if (lineWrap === 'bounded') {
|
|
889
|
+
const lines = el.querySelector('.CodeMirror-lines');
|
|
890
|
+
lines.style.maxWidth = `${wordWrapColumn}ch`;
|
|
891
|
+
}
|
|
892
|
+
host.appendChild(el);
|
|
893
|
+
}, bareConfig);
|
|
894
|
+
}
|
|
895
|
+
Private.createEditor = createEditor;
|
|
896
|
+
/**
|
|
897
|
+
* Indent or insert a tab as appropriate.
|
|
898
|
+
*/
|
|
899
|
+
function indentMoreOrinsertTab(cm) {
|
|
900
|
+
let doc = cm.getDoc();
|
|
901
|
+
let from = doc.getCursor('from');
|
|
902
|
+
let to = doc.getCursor('to');
|
|
903
|
+
let sel = !posEq(from, to);
|
|
904
|
+
if (sel) {
|
|
905
|
+
codemirror_1.default.commands['indentMore'](cm);
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
// Check for start of line.
|
|
909
|
+
let line = doc.getLine(from.line);
|
|
910
|
+
let before = line.slice(0, from.ch);
|
|
911
|
+
if (/^\s*$/.test(before)) {
|
|
912
|
+
codemirror_1.default.commands['indentMore'](cm);
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
if (cm.getOption('indentWithTabs')) {
|
|
916
|
+
codemirror_1.default.commands['insertTab'](cm);
|
|
917
|
+
}
|
|
918
|
+
else {
|
|
919
|
+
codemirror_1.default.commands['insertSoftTab'](cm);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
Private.indentMoreOrinsertTab = indentMoreOrinsertTab;
|
|
924
|
+
/**
|
|
925
|
+
* Delete spaces to the previous tab stob in a codemirror editor.
|
|
926
|
+
*/
|
|
927
|
+
function delSpaceToPrevTabStop(cm) {
|
|
928
|
+
let doc = cm.getDoc();
|
|
929
|
+
let from = doc.getCursor('from');
|
|
930
|
+
let to = doc.getCursor('to');
|
|
931
|
+
let sel = !posEq(from, to);
|
|
932
|
+
if (sel) {
|
|
933
|
+
let ranges = doc.listSelections();
|
|
934
|
+
for (let i = ranges.length - 1; i >= 0; i--) {
|
|
935
|
+
let head = ranges[i].head;
|
|
936
|
+
let anchor = ranges[i].anchor;
|
|
937
|
+
doc.replaceRange('', codemirror_1.default.Pos(head.line, head.ch), codemirror_1.default.Pos(anchor.line, anchor.ch));
|
|
938
|
+
}
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
let cur = doc.getCursor();
|
|
942
|
+
let tabsize = cm.getOption('tabSize');
|
|
943
|
+
let chToPrevTabStop = cur.ch - (Math.ceil(cur.ch / tabsize) - 1) * tabsize;
|
|
944
|
+
from = { ch: cur.ch - chToPrevTabStop, line: cur.line };
|
|
945
|
+
let select = doc.getRange(from, cur);
|
|
946
|
+
if (select.match(/^\ +$/) !== null) {
|
|
947
|
+
doc.replaceRange('', from, cur);
|
|
948
|
+
}
|
|
949
|
+
else {
|
|
950
|
+
codemirror_1.default.commands['delCharBefore'](cm);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
Private.delSpaceToPrevTabStop = delSpaceToPrevTabStop;
|
|
954
|
+
/**
|
|
955
|
+
* Test whether two CodeMirror positions are equal.
|
|
956
|
+
*/
|
|
957
|
+
function posEq(a, b) {
|
|
958
|
+
return a.line === b.line && a.ch === b.ch;
|
|
959
|
+
}
|
|
960
|
+
Private.posEq = posEq;
|
|
961
|
+
/**
|
|
962
|
+
* Set a config option for the editor.
|
|
963
|
+
*/
|
|
964
|
+
function setOption(editor, option, value, config) {
|
|
965
|
+
let el = editor.getWrapperElement();
|
|
966
|
+
switch (option) {
|
|
967
|
+
case 'lineWrap':
|
|
968
|
+
const lineWrapping = value === 'off' ? false : true;
|
|
969
|
+
const lines = el.querySelector('.CodeMirror-lines');
|
|
970
|
+
const maxWidth = value === 'bounded' ? `${config.wordWrapColumn}ch` : null;
|
|
971
|
+
const width = value === 'wordWrapColumn' ? `${config.wordWrapColumn}ch` : null;
|
|
972
|
+
lines.style.maxWidth = maxWidth;
|
|
973
|
+
lines.style.width = width;
|
|
974
|
+
editor.setOption('lineWrapping', lineWrapping);
|
|
975
|
+
break;
|
|
976
|
+
case 'wordWrapColumn':
|
|
977
|
+
const { lineWrap } = config;
|
|
978
|
+
if (lineWrap === 'wordWrapColumn' || lineWrap === 'bounded') {
|
|
979
|
+
const lines = el.querySelector('.CodeMirror-lines');
|
|
980
|
+
const prop = lineWrap === 'wordWrapColumn' ? 'width' : 'maxWidth';
|
|
981
|
+
lines.style[prop] = `${value}ch`;
|
|
982
|
+
}
|
|
983
|
+
break;
|
|
984
|
+
case 'tabSize':
|
|
985
|
+
editor.setOption('indentUnit', value);
|
|
986
|
+
break;
|
|
987
|
+
case 'insertSpaces':
|
|
988
|
+
editor.setOption('indentWithTabs', !value);
|
|
989
|
+
break;
|
|
990
|
+
case 'autoClosingBrackets':
|
|
991
|
+
editor.setOption('autoCloseBrackets', value);
|
|
992
|
+
break;
|
|
993
|
+
case 'readOnly':
|
|
994
|
+
el.classList.toggle(READ_ONLY_CLASS, value);
|
|
995
|
+
editor.setOption(option, value);
|
|
996
|
+
break;
|
|
997
|
+
case 'fontFamily':
|
|
998
|
+
el.style.fontFamily = value;
|
|
999
|
+
break;
|
|
1000
|
+
case 'fontSize':
|
|
1001
|
+
el.style.fontSize = value ? value + 'px' : null;
|
|
1002
|
+
break;
|
|
1003
|
+
case 'lineHeight':
|
|
1004
|
+
el.style.lineHeight = value ? value.toString() : null;
|
|
1005
|
+
break;
|
|
1006
|
+
default:
|
|
1007
|
+
editor.setOption(option, value);
|
|
1008
|
+
break;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
Private.setOption = setOption;
|
|
1012
|
+
})(Private || (Private = {}));
|
|
1013
|
+
/**
|
|
1014
|
+
* Add a CodeMirror command to delete until previous non blanking space
|
|
1015
|
+
* character or first multiple of tabsize tabstop.
|
|
1016
|
+
*/
|
|
1017
|
+
CodeMirrorEditor.addCommand('delSpaceToPrevTabStop', Private.delSpaceToPrevTabStop);
|
|
1018
|
+
/**
|
|
1019
|
+
* Add a CodeMirror command to indent or insert a tab as appropriate.
|
|
1020
|
+
*/
|
|
1021
|
+
CodeMirrorEditor.addCommand('indentMoreOrinsertTab', Private.indentMoreOrinsertTab);
|