UNPKG

5.59 kBJavaScriptView Raw
1/* -----------------------------------------------------------------------------
2| Copyright (c) Jupyter Development Team.
3| Distributed under the terms of the Modified BSD License.
4|----------------------------------------------------------------------------*/
5import { PanelLayout } from '@lumino/widgets';
6import { Widget } from '@lumino/widgets';
7import { CodeEditorWrapper } from '@jupyterlab/codeeditor';
8import { CodeMirrorEditorFactory } from '@jupyterlab/codemirror';
9/**
10 * The class name added to input area widgets.
11 */
12const INPUT_AREA_CLASS = 'jp-InputArea';
13/**
14 * The class name added to the prompt area of cell.
15 */
16const INPUT_AREA_PROMPT_CLASS = 'jp-InputArea-prompt';
17/**
18 * The class name added to OutputPrompt.
19 */
20const INPUT_PROMPT_CLASS = 'jp-InputPrompt';
21/**
22 * The class name added to the editor area of the cell.
23 */
24const INPUT_AREA_EDITOR_CLASS = 'jp-InputArea-editor';
25/** ****************************************************************************
26 * InputArea
27 ******************************************************************************/
28/**
29 * An input area widget, which hosts a prompt and an editor widget.
30 */
31export class InputArea extends Widget {
32 /**
33 * Construct an input area widget.
34 */
35 constructor(options) {
36 super();
37 this.addClass(INPUT_AREA_CLASS);
38 const model = (this.model = options.model);
39 const contentFactory = (this.contentFactory =
40 options.contentFactory || InputArea.defaultContentFactory);
41 // Prompt
42 const prompt = (this._prompt = contentFactory.createInputPrompt());
43 prompt.addClass(INPUT_AREA_PROMPT_CLASS);
44 // Editor
45 const editorOptions = {
46 model,
47 factory: contentFactory.editorFactory,
48 updateOnShow: options.updateOnShow
49 };
50 const editor = (this._editor = new CodeEditorWrapper(editorOptions));
51 editor.addClass(INPUT_AREA_EDITOR_CLASS);
52 const layout = (this.layout = new PanelLayout());
53 layout.addWidget(prompt);
54 if (!options.placeholder) {
55 layout.addWidget(editor);
56 }
57 }
58 /**
59 * Get the CodeEditorWrapper used by the cell.
60 */
61 get editorWidget() {
62 return this._editor;
63 }
64 /**
65 * Get the CodeEditor used by the cell.
66 */
67 get editor() {
68 return this._editor.editor;
69 }
70 /**
71 * Get the prompt node used by the cell.
72 */
73 get promptNode() {
74 return this._prompt.node;
75 }
76 /**
77 * Get the rendered input area widget, if any.
78 */
79 get renderedInput() {
80 return this._rendered;
81 }
82 /**
83 * Render an input instead of the text editor.
84 */
85 renderInput(widget) {
86 const layout = this.layout;
87 if (this._rendered) {
88 this._rendered.parent = null;
89 }
90 this._editor.hide();
91 this._rendered = widget;
92 layout.addWidget(widget);
93 }
94 /**
95 * Show the text editor.
96 */
97 showEditor() {
98 if (this._rendered) {
99 this._rendered.parent = null;
100 }
101 this._editor.show();
102 }
103 /**
104 * Set the prompt of the input area.
105 */
106 setPrompt(value) {
107 this._prompt.executionCount = value;
108 }
109 /**
110 * Dispose of the resources held by the widget.
111 */
112 dispose() {
113 // Do nothing if already disposed.
114 if (this.isDisposed) {
115 return;
116 }
117 this._prompt = null;
118 this._editor = null;
119 this._rendered = null;
120 super.dispose();
121 }
122}
123/**
124 * A namespace for `InputArea` statics.
125 */
126(function (InputArea) {
127 /**
128 * Default implementation of `IContentFactory`.
129 *
130 * This defaults to using an `editorFactory` based on CodeMirror.
131 */
132 class ContentFactory {
133 /**
134 * Construct a `ContentFactory`.
135 */
136 constructor(options = {}) {
137 this._editor = options.editorFactory || InputArea.defaultEditorFactory;
138 }
139 /**
140 * Return the `CodeEditor.Factory` being used.
141 */
142 get editorFactory() {
143 return this._editor;
144 }
145 /**
146 * Create an input prompt.
147 */
148 createInputPrompt() {
149 return new InputPrompt();
150 }
151 }
152 InputArea.ContentFactory = ContentFactory;
153 /**
154 * A function to create the default CodeMirror editor factory.
155 */
156 function _createDefaultEditorFactory() {
157 const editorServices = new CodeMirrorEditorFactory();
158 return editorServices.newInlineEditor;
159 }
160 /**
161 * The default editor factory singleton based on CodeMirror.
162 */
163 InputArea.defaultEditorFactory = _createDefaultEditorFactory();
164 /**
165 * The default `ContentFactory` instance.
166 */
167 InputArea.defaultContentFactory = new ContentFactory({});
168})(InputArea || (InputArea = {}));
169/**
170 * The default input prompt implementation.
171 */
172export class InputPrompt extends Widget {
173 /*
174 * Create an output prompt widget.
175 */
176 constructor() {
177 super();
178 this._executionCount = null;
179 this.addClass(INPUT_PROMPT_CLASS);
180 }
181 /**
182 * The execution count for the prompt.
183 */
184 get executionCount() {
185 return this._executionCount;
186 }
187 set executionCount(value) {
188 this._executionCount = value;
189 if (value === null) {
190 this.node.textContent = ' ';
191 }
192 else {
193 this.node.textContent = `[${value || ' '}]:`;
194 }
195 }
196}
197//# sourceMappingURL=inputarea.js.map
\No newline at end of file