@jupyterlab/codeeditor 4.0.0-alpha.2 → 4.0.0-alpha.21

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/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
+ }
package/style/base.css CHANGED
@@ -3,6 +3,7 @@
3
3
  |
4
4
  | Distributed under the terms of the Modified BSD License.
5
5
  |----------------------------------------------------------------------------*/
6
+ @import url('./linecol.css');
6
7
 
7
8
  .jp-JSONEditor {
8
9
  display: flex;
@@ -13,7 +14,7 @@
13
14
  .jp-JSONEditor-host {
14
15
  flex: 1 1 auto;
15
16
  border: var(--jp-border-width) solid var(--jp-input-border-color);
16
- border-radius: 0px;
17
+ border-radius: 0;
17
18
  background: var(--jp-layout-color0);
18
19
  min-height: 50px;
19
20
  padding: 1px;
package/style/index.css CHANGED
@@ -6,6 +6,7 @@
6
6
  /* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
7
7
  @import url('~@lumino/widgets/style/index.css');
8
8
  @import url('~@jupyterlab/ui-components/style/index.css');
9
+ @import url('~@jupyterlab/statusbar/style/index.css');
9
10
  @import url('~@lumino/dragdrop/style/index.css');
10
11
 
11
12
  @import url('./base.css');
package/style/index.js CHANGED
@@ -6,6 +6,7 @@
6
6
  /* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
7
7
  import '@lumino/widgets/style/index.js';
8
8
  import '@jupyterlab/ui-components/style/index.js';
9
+ import '@jupyterlab/statusbar/style/index.js';
9
10
  import '@lumino/dragdrop/style/index.js';
10
11
 
11
12
  import './base.css';
@@ -0,0 +1,79 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+
6
+ .jp-lineFormSearch {
7
+ padding: 4px 12px;
8
+ background-color: var(--jp-layout-color2);
9
+ box-shadow: var(--jp-toolbar-box-shadow);
10
+ z-index: 2;
11
+ font-size: var(--jp-ui-font-size1);
12
+ }
13
+
14
+ .jp-lineFormCaption {
15
+ font-size: var(--jp-ui-font-size0);
16
+ line-height: var(--jp-ui-font-size1);
17
+ margin-top: 4px;
18
+ color: var(--jp-ui-font-color0);
19
+ }
20
+
21
+ .jp-baseLineForm {
22
+ border: none;
23
+ border-radius: 0;
24
+ position: absolute;
25
+ background-size: 16px;
26
+ background-repeat: no-repeat;
27
+ background-position: center;
28
+ outline: none;
29
+ }
30
+
31
+ .jp-lineFormButtonContainer {
32
+ top: 4px;
33
+ right: 8px;
34
+ height: 24px;
35
+ padding: 0 12px;
36
+ width: 12px;
37
+ }
38
+
39
+ .jp-lineFormButtonIcon {
40
+ top: 0;
41
+ right: 0;
42
+ background-color: var(--jp-brand-color1);
43
+ height: 100%;
44
+ width: 100%;
45
+ box-sizing: border-box;
46
+ padding: 4px 6px;
47
+ }
48
+
49
+ .jp-lineFormButton {
50
+ top: 0;
51
+ right: 0;
52
+ background-color: transparent;
53
+ height: 100%;
54
+ width: 100%;
55
+ box-sizing: border-box;
56
+ }
57
+
58
+ .jp-lineFormWrapper {
59
+ overflow: hidden;
60
+ padding: 0 8px;
61
+ border: 1px solid var(--jp-border-color0);
62
+ background-color: var(--jp-input-active-background);
63
+ height: 22px;
64
+ }
65
+
66
+ .jp-lineFormWrapperFocusWithin {
67
+ border: var(--jp-border-width) solid var(--md-blue-500);
68
+ box-shadow: inset 0 0 4px var(--md-blue-300);
69
+ }
70
+
71
+ .jp-lineFormInput {
72
+ background: transparent;
73
+ width: 200px;
74
+ height: 100%;
75
+ border: none;
76
+ outline: none;
77
+ color: var(--jp-ui-font-color0);
78
+ line-height: 28px;
79
+ }