@jupyterlab/codeeditor 4.0.0-alpha.8 → 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.
@@ -0,0 +1,381 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { Popup, showPopup, TextItem } from '@jupyterlab/statusbar';
5
+ import {
6
+ ITranslator,
7
+ nullTranslator,
8
+ TranslationBundle
9
+ } from '@jupyterlab/translation';
10
+ import {
11
+ classes,
12
+ lineFormIcon,
13
+ ReactWidget,
14
+ VDomModel,
15
+ VDomRenderer
16
+ } from '@jupyterlab/ui-components';
17
+ import React from 'react';
18
+ import { CodeEditor } from './editor';
19
+
20
+ /**
21
+ * A namespace for LineFormComponent statics.
22
+ */
23
+ namespace LineFormComponent {
24
+ /**
25
+ * The props for LineFormComponent.
26
+ */
27
+ export interface IProps {
28
+ /**
29
+ * A callback for when the form is submitted.
30
+ */
31
+ handleSubmit: (value: number) => void;
32
+
33
+ /**
34
+ * The current line of the form.
35
+ */
36
+ currentLine: number;
37
+
38
+ /**
39
+ * The maximum line the form can take (typically the
40
+ * maximum line of the relevant editor).
41
+ */
42
+ maxLine: number;
43
+
44
+ /**
45
+ * The application language translator.
46
+ */
47
+ translator?: ITranslator;
48
+ }
49
+
50
+ /**
51
+ * The props for LineFormComponent.
52
+ */
53
+ export interface IState {
54
+ /**
55
+ * The current value of the form.
56
+ */
57
+ value: string;
58
+
59
+ /**
60
+ * Whether the form has focus.
61
+ */
62
+ hasFocus: boolean;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * A component for rendering a "go-to-line" form.
68
+ */
69
+ class LineFormComponent extends React.Component<
70
+ LineFormComponent.IProps,
71
+ LineFormComponent.IState
72
+ > {
73
+ /**
74
+ * Construct a new LineFormComponent.
75
+ */
76
+ constructor(props: LineFormComponent.IProps) {
77
+ super(props);
78
+ this.translator = props.translator || nullTranslator;
79
+ this._trans = this.translator.load('jupyterlab');
80
+ this.state = {
81
+ value: '',
82
+ hasFocus: false
83
+ };
84
+ }
85
+
86
+ /**
87
+ * Focus the element on mount.
88
+ */
89
+ componentDidMount() {
90
+ this._textInput!.focus();
91
+ }
92
+
93
+ /**
94
+ * Render the LineFormComponent.
95
+ */
96
+ render() {
97
+ return (
98
+ <div className="jp-lineFormSearch">
99
+ <form name="lineColumnForm" onSubmit={this._handleSubmit} noValidate>
100
+ <div
101
+ className={classes(
102
+ 'jp-lineFormWrapper',
103
+ 'lm-lineForm-wrapper',
104
+ this.state.hasFocus ? 'jp-lineFormWrapperFocusWithin' : undefined
105
+ )}
106
+ >
107
+ <input
108
+ type="text"
109
+ className="jp-lineFormInput"
110
+ onChange={this._handleChange}
111
+ onFocus={this._handleFocus}
112
+ onBlur={this._handleBlur}
113
+ value={this.state.value}
114
+ ref={input => {
115
+ this._textInput = input;
116
+ }}
117
+ />
118
+ <div className="jp-baseLineForm jp-lineFormButtonContainer">
119
+ <lineFormIcon.react
120
+ className="jp-baseLineForm jp-lineFormButtonIcon"
121
+ elementPosition="center"
122
+ />
123
+ <input
124
+ type="submit"
125
+ className="jp-baseLineForm jp-lineFormButton"
126
+ value=""
127
+ />
128
+ </div>
129
+ </div>
130
+ <label className="jp-lineFormCaption">
131
+ {this._trans.__(
132
+ 'Go to line number between 1 and %1',
133
+ this.props.maxLine
134
+ )}
135
+ </label>
136
+ </form>
137
+ </div>
138
+ );
139
+ }
140
+
141
+ /**
142
+ * Handle a change to the value in the input field.
143
+ */
144
+ private _handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
145
+ this.setState({ value: event.currentTarget.value });
146
+ };
147
+
148
+ /**
149
+ * Handle submission of the input field.
150
+ */
151
+ private _handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
152
+ event.preventDefault();
153
+
154
+ const value = parseInt(this._textInput!.value, 10);
155
+ if (
156
+ !isNaN(value) &&
157
+ isFinite(value) &&
158
+ 1 <= value &&
159
+ value <= this.props.maxLine
160
+ ) {
161
+ this.props.handleSubmit(value);
162
+ }
163
+
164
+ return false;
165
+ };
166
+
167
+ /**
168
+ * Handle focusing of the input field.
169
+ */
170
+ private _handleFocus = () => {
171
+ this.setState({ hasFocus: true });
172
+ };
173
+
174
+ /**
175
+ * Handle blurring of the input field.
176
+ */
177
+ private _handleBlur = () => {
178
+ this.setState({ hasFocus: false });
179
+ };
180
+
181
+ protected translator: ITranslator;
182
+ private _trans: TranslationBundle;
183
+ private _textInput: HTMLInputElement | null = null;
184
+ }
185
+
186
+ /**
187
+ * A namespace for LineColComponent.
188
+ */
189
+ namespace LineColComponent {
190
+ /**
191
+ * Props for LineColComponent.
192
+ */
193
+ export interface IProps {
194
+ /**
195
+ * The current line number.
196
+ */
197
+ line: number;
198
+
199
+ /**
200
+ * The current column number.
201
+ */
202
+ column: number;
203
+
204
+ /**
205
+ * The application language translator.
206
+ */
207
+ translator?: ITranslator;
208
+
209
+ /**
210
+ * A click handler for the LineColComponent, which
211
+ * we use to launch the LineFormComponent.
212
+ */
213
+ handleClick: () => void;
214
+ }
215
+ }
216
+
217
+ /**
218
+ * A pure functional component for rendering a line/column
219
+ * status item.
220
+ */
221
+ function LineColComponent(
222
+ props: LineColComponent.IProps
223
+ ): React.ReactElement<LineColComponent.IProps> {
224
+ const translator = props.translator || nullTranslator;
225
+ const trans = translator.load('jupyterlab');
226
+ return (
227
+ <TextItem
228
+ onClick={props.handleClick}
229
+ source={trans.__('Ln %1, Col %2', props.line, props.column)}
230
+ title={trans.__('Go to line number…')}
231
+ />
232
+ );
233
+ }
234
+
235
+ /**
236
+ * A widget implementing a line/column status item.
237
+ */
238
+ export class LineCol extends VDomRenderer<LineCol.Model> {
239
+ /**
240
+ * Construct a new LineCol status item.
241
+ */
242
+ constructor(translator?: ITranslator) {
243
+ super(new LineCol.Model());
244
+ this.addClass('jp-mod-highlighted');
245
+ this.translator = translator || nullTranslator;
246
+ }
247
+
248
+ /**
249
+ * Render the status item.
250
+ */
251
+ render(): React.ReactElement<LineColComponent.IProps> | null {
252
+ if (this.model === null) {
253
+ return null;
254
+ } else {
255
+ return (
256
+ <LineColComponent
257
+ line={this.model.line}
258
+ column={this.model.column}
259
+ translator={this.translator}
260
+ handleClick={() => this._handleClick()}
261
+ />
262
+ );
263
+ }
264
+ }
265
+
266
+ /**
267
+ * A click handler for the widget.
268
+ */
269
+ private _handleClick(): void {
270
+ if (this._popup) {
271
+ this._popup.dispose();
272
+ }
273
+ const body = ReactWidget.create(
274
+ <LineFormComponent
275
+ handleSubmit={val => this._handleSubmit(val)}
276
+ currentLine={this.model!.line}
277
+ maxLine={this.model!.editor!.lineCount}
278
+ translator={this.translator}
279
+ />
280
+ );
281
+
282
+ this._popup = showPopup({
283
+ body: body,
284
+ anchor: this,
285
+ align: 'right'
286
+ });
287
+ }
288
+
289
+ /**
290
+ * Handle submission for the widget.
291
+ */
292
+ private _handleSubmit(value: number): void {
293
+ this.model!.editor!.setCursorPosition({ line: value - 1, column: 0 });
294
+ this._popup!.dispose();
295
+ this.model!.editor!.focus();
296
+ }
297
+
298
+ protected translator: ITranslator;
299
+ private _popup: Popup | null = null;
300
+ }
301
+
302
+ /**
303
+ * A namespace for LineCol statics.
304
+ */
305
+ export namespace LineCol {
306
+ /**
307
+ * A VDom model for a status item tracking the line/column of an editor.
308
+ */
309
+ export class Model extends VDomModel {
310
+ /**
311
+ * The current editor of the model.
312
+ */
313
+ get editor(): CodeEditor.IEditor | null {
314
+ return this._editor;
315
+ }
316
+ set editor(editor: CodeEditor.IEditor | null) {
317
+ const oldEditor = this._editor;
318
+ if (oldEditor?.model?.selections) {
319
+ oldEditor.model.selections.changed.disconnect(this._onSelectionChanged);
320
+ }
321
+
322
+ const oldState = this._getAllState();
323
+ this._editor = editor;
324
+ if (!this._editor) {
325
+ this._column = 1;
326
+ this._line = 1;
327
+ } else {
328
+ this._editor.model.selections.changed.connect(this._onSelectionChanged);
329
+
330
+ const pos = this._editor.getCursorPosition();
331
+ this._column = pos.column + 1;
332
+ this._line = pos.line + 1;
333
+ }
334
+
335
+ this._triggerChange(oldState, this._getAllState());
336
+ }
337
+
338
+ /**
339
+ * The current line of the model.
340
+ */
341
+ get line(): number {
342
+ return this._line;
343
+ }
344
+
345
+ /**
346
+ * The current column of the model.
347
+ */
348
+ get column(): number {
349
+ return this._column;
350
+ }
351
+
352
+ /**
353
+ * React to a change in the cursors of the current editor.
354
+ */
355
+ private _onSelectionChanged = () => {
356
+ const oldState = this._getAllState();
357
+ const pos = this.editor!.getCursorPosition();
358
+ this._line = pos.line + 1;
359
+ this._column = pos.column + 1;
360
+
361
+ this._triggerChange(oldState, this._getAllState());
362
+ };
363
+
364
+ private _getAllState(): [number, number] {
365
+ return [this._line, this._column];
366
+ }
367
+
368
+ private _triggerChange(
369
+ oldState: [number, number],
370
+ newState: [number, number]
371
+ ) {
372
+ if (oldState[0] !== newState[0] || oldState[1] !== newState[1]) {
373
+ this.stateChanged.emit(void 0);
374
+ }
375
+ }
376
+
377
+ private _line: number = 1;
378
+ private _column: number = 1;
379
+ private _editor: CodeEditor.IEditor | null = null;
380
+ }
381
+ }
@@ -0,0 +1,43 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import * as nbformat from '@jupyterlab/nbformat';
5
+
6
+ /**
7
+ * The mime type service of a code editor.
8
+ */
9
+ export interface IEditorMimeTypeService {
10
+ /**
11
+ * Get a mime type for the given language info.
12
+ *
13
+ * @param info - The language information.
14
+ *
15
+ * @returns A valid mimetype.
16
+ *
17
+ * #### Notes
18
+ * If a mime type cannot be found returns the default mime type `text/plain`, never `null`.
19
+ */
20
+ getMimeTypeByLanguage(info: nbformat.ILanguageInfoMetadata): string;
21
+
22
+ /**
23
+ * Get a mime type for the given file path.
24
+ *
25
+ * @param filePath - The full path to the file.
26
+ *
27
+ * @returns A valid mimetype.
28
+ *
29
+ * #### Notes
30
+ * If a mime type cannot be found returns the default mime type `text/plain`, never `null`.
31
+ */
32
+ getMimeTypeByFilePath(filePath: string): string;
33
+ }
34
+
35
+ /**
36
+ * A namespace for `IEditorMimeTypeService`.
37
+ */
38
+ export namespace IEditorMimeTypeService {
39
+ /**
40
+ * The default mime type.
41
+ */
42
+ export const defaultMimeType: string = 'text/plain';
43
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,57 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { Token } from '@lumino/coreutils';
5
+ import { Widget } from '@lumino/widgets';
6
+ import { CodeEditor } from './editor';
7
+ import { IEditorFactoryService } from './factory';
8
+ import { IEditorMimeTypeService } from './mimetype';
9
+
10
+ /**
11
+ * Code editor services token.
12
+ */
13
+ export const IEditorServices = new Token<IEditorServices>(
14
+ '@jupyterlab/codeeditor:IEditorServices'
15
+ );
16
+
17
+ /**
18
+ * Code editor services.
19
+ */
20
+ export interface IEditorServices {
21
+ /**
22
+ * The code editor factory.
23
+ */
24
+ readonly factoryService: IEditorFactoryService;
25
+
26
+ /**
27
+ * The editor mime type service.
28
+ */
29
+ readonly mimeTypeService: IEditorMimeTypeService;
30
+ }
31
+
32
+ /**
33
+ * Code editor cursor position token.
34
+ */
35
+ export const IPositionModel = new Token<IPositionModel>(
36
+ '@jupyterlab/codeeditor:IPositionModel'
37
+ );
38
+
39
+ /**
40
+ * Code editor cursor position model.
41
+ */
42
+ export interface IPositionModel {
43
+ /**
44
+ * Add a editor provider.
45
+ *
46
+ * A provider will receive the currently active widget and must return the
47
+ * associated editor if it can or null otherwise.
48
+ */
49
+ addEditorProvider: (
50
+ provider: (widget: Widget | null) => Promise<CodeEditor.IEditor | null>
51
+ ) => void;
52
+
53
+ /**
54
+ * Callback to force updating the provider
55
+ */
56
+ update(): void;
57
+ }
package/src/viewer.ts ADDED
@@ -0,0 +1,93 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { StackedLayout, Widget } from '@lumino/widgets';
5
+ import { CodeEditor } from './editor';
6
+ import { CodeEditorWrapper } from './widget';
7
+
8
+ export class CodeViewerWidget extends Widget {
9
+ /**
10
+ * Construct a new code viewer widget.
11
+ */
12
+ constructor(options: CodeViewerWidget.IOptions) {
13
+ super();
14
+ this.model = options.model;
15
+
16
+ const editorWidget = new CodeEditorWrapper({
17
+ factory: options.factory,
18
+ model: this.model,
19
+ editorOptions: {
20
+ ...options.editorOptions,
21
+ config: { ...options.editorOptions?.config, readOnly: true }
22
+ }
23
+ });
24
+ this.editor = editorWidget.editor;
25
+
26
+ const layout = (this.layout = new StackedLayout());
27
+ layout.addWidget(editorWidget);
28
+ }
29
+
30
+ static createCodeViewer(
31
+ options: CodeViewerWidget.INoModelOptions
32
+ ): CodeViewerWidget {
33
+ const { content, mimeType, ...others } = options;
34
+ const model = new CodeEditor.Model({
35
+ mimeType
36
+ });
37
+ model.sharedModel.setSource(content);
38
+ const widget = new CodeViewerWidget({ ...others, model });
39
+ widget.disposed.connect(() => {
40
+ model.dispose();
41
+ });
42
+ return widget;
43
+ }
44
+
45
+ get content(): string {
46
+ return this.model.sharedModel.getSource();
47
+ }
48
+
49
+ get mimeType(): string {
50
+ return this.model.mimeType;
51
+ }
52
+
53
+ readonly model: CodeEditor.IModel;
54
+ readonly editor: CodeEditor.IEditor;
55
+ }
56
+
57
+ /**
58
+ * The namespace for code viewer widget.
59
+ */
60
+ export namespace CodeViewerWidget {
61
+ /**
62
+ * The options used to create an code viewer widget.
63
+ */
64
+ export interface IOptions {
65
+ /**
66
+ * A code editor factory.
67
+ */
68
+ factory: CodeEditor.Factory;
69
+ /**
70
+ * The content model for the viewer.
71
+ */
72
+ model: CodeEditor.IModel;
73
+ /**
74
+ * Code editor options
75
+ */
76
+ editorOptions?: Omit<CodeEditor.IOptions, 'host' | 'model'>;
77
+ }
78
+
79
+ /**
80
+ * The options used to create an code viewer widget without a model.
81
+ */
82
+ export interface INoModelOptions extends Omit<IOptions, 'model'> {
83
+ /**
84
+ * The content to display in the viewer.
85
+ */
86
+ content: string;
87
+
88
+ /**
89
+ * The mime type for the content.
90
+ */
91
+ mimeType?: string;
92
+ }
93
+ }