@jupyterlab/cells 4.0.0-alpha.19 → 4.0.0-alpha.20

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