inputarea.ts 7.0 KB

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