-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
inputarea.ts
292 lines (253 loc) · 6.53 KB
/
inputarea.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { PanelLayout } from '@lumino/widgets';
import { Widget } from '@lumino/widgets';
import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
import { ICellModel } from './model';
/**
* The class name added to input area widgets.
*/
const INPUT_AREA_CLASS = 'jp-InputArea';
/**
* The class name added to the prompt area of cell.
*/
const INPUT_AREA_PROMPT_CLASS = 'jp-InputArea-prompt';
/**
* The class name added to OutputPrompt.
*/
const INPUT_PROMPT_CLASS = 'jp-InputPrompt';
/**
* The class name added to the editor area of the cell.
*/
const INPUT_AREA_EDITOR_CLASS = 'jp-InputArea-editor';
/** ****************************************************************************
* InputArea
******************************************************************************/
/**
* An input area widget, which hosts a prompt and an editor widget.
*/
export class InputArea extends Widget {
/**
* Construct an input area widget.
*/
constructor(options: InputArea.IOptions) {
super();
this.addClass(INPUT_AREA_CLASS);
const { contentFactory, editorOptions, model } = options;
this.model = model;
this.contentFactory = contentFactory;
// Prompt
const prompt = (this._prompt = contentFactory.createInputPrompt());
prompt.addClass(INPUT_AREA_PROMPT_CLASS);
// Editor
const editor = (this._editor = new CodeEditorWrapper({
factory: contentFactory.editorFactory,
model,
editorOptions
}));
editor.addClass(INPUT_AREA_EDITOR_CLASS);
const layout = (this.layout = new PanelLayout());
layout.addWidget(prompt);
layout.addWidget(editor);
}
/**
* The model used by the widget.
*/
readonly model: ICellModel;
/**
* The content factory used by the widget.
*/
readonly contentFactory: InputArea.IContentFactory;
/**
* Get the CodeEditorWrapper used by the cell.
*/
get editorWidget(): CodeEditorWrapper {
return this._editor;
}
/**
* Get the CodeEditor used by the cell.
*/
get editor(): CodeEditor.IEditor {
return this._editor.editor;
}
/**
* Get the prompt node used by the cell.
*/
get promptNode(): HTMLElement {
return this._prompt.node;
}
/**
* Get the rendered input area widget, if any.
*/
get renderedInput(): Widget {
return this._rendered;
}
/**
* Render an input instead of the text editor.
*/
renderInput(widget: Widget): void {
const layout = this.layout as PanelLayout;
if (this._rendered) {
this._rendered.parent = null;
}
this._editor.hide();
this._rendered = widget;
layout.addWidget(widget);
}
/**
* Show the text editor.
*/
showEditor(): void {
if (this._rendered) {
this._rendered.parent = null;
}
this._editor.show();
}
/**
* Set the prompt of the input area.
*/
setPrompt(value: string): void {
this._prompt.executionCount = value;
}
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
this._prompt = null!;
this._editor = null!;
this._rendered = null!;
super.dispose();
}
private _prompt: IInputPrompt;
private _editor: CodeEditorWrapper;
private _rendered: Widget;
}
/**
* A namespace for `InputArea` statics.
*/
export namespace InputArea {
/**
* The options used to create an `InputArea`.
*/
export interface IOptions {
/**
* The model used by the widget.
*/
model: ICellModel;
/**
* The content factory used by the widget to create children.
*/
contentFactory: IContentFactory;
/**
* Editor options
*/
editorOptions?: Omit<CodeEditor.IOptions, 'host' | 'model'>;
}
/**
* An input area widget content factory.
*
* The content factory is used to create children in a way
* that can be customized.
*/
export interface IContentFactory {
/**
* The editor factory we need to include in `CodeEditorWrapper.IOptions`.
*
* This is a separate readonly attribute rather than a factory method as we need
* to pass it around.
*/
readonly editorFactory: CodeEditor.Factory;
/**
* Create an input prompt.
*/
createInputPrompt(): IInputPrompt;
}
/**
* Default implementation of `IContentFactory`.
*
* This defaults to using an `editorFactory` based on CodeMirror.
*/
export class ContentFactory implements IContentFactory {
/**
* Construct a `ContentFactory`.
*/
constructor(options: ContentFactory.IOptions) {
this._editor = options.editorFactory;
}
/**
* Return the `CodeEditor.Factory` being used.
*/
get editorFactory(): CodeEditor.Factory {
return this._editor;
}
/**
* Create an input prompt.
*/
createInputPrompt(): IInputPrompt {
return new InputPrompt();
}
private _editor: CodeEditor.Factory;
}
/**
* A namespace for the input area content factory.
*/
export namespace ContentFactory {
/**
* Options for the content factory.
*/
export interface IOptions {
/**
* The editor factory used by the content factory.
*
* If this is not passed, a default CodeMirror editor factory
* will be used.
*/
editorFactory: CodeEditor.Factory;
}
}
}
/** ****************************************************************************
* InputPrompt
******************************************************************************/
/**
* The interface for the input prompt.
*/
export interface IInputPrompt extends Widget {
/**
* The execution count of the prompt.
*/
executionCount: string | null;
}
/**
* The default input prompt implementation.
*/
export class InputPrompt extends Widget implements IInputPrompt {
/*
* Create an output prompt widget.
*/
constructor() {
super();
this.addClass(INPUT_PROMPT_CLASS);
}
/**
* The execution count for the prompt.
*/
get executionCount(): string | null {
return this._executionCount;
}
set executionCount(value: string | null) {
this._executionCount = value;
if (value === null) {
this.node.textContent = ' ';
} else {
this.node.textContent = `[${value || ' '}]:`;
}
}
private _executionCount: string | null = null;
}