content.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. KernelMessage, Session, nbformat
  5. } from '@jupyterlab/services';
  6. import {
  7. map, toArray
  8. } from 'phosphor/lib/algorithm/iteration';
  9. import {
  10. Message, sendMessage
  11. } from 'phosphor/lib/core/messaging';
  12. import {
  13. clearSignalData, defineSignal, ISignal
  14. } from 'phosphor/lib/core/signaling';
  15. import {
  16. Token
  17. } from 'phosphor/lib/core/token';
  18. import {
  19. Panel, PanelLayout
  20. } from 'phosphor/lib/ui/panel';
  21. import {
  22. Widget, WidgetMessage
  23. } from 'phosphor/lib/ui/widget';
  24. import {
  25. CellCompleterHandler, CompleterModel, CompleterWidget
  26. } from '../completer';
  27. import {
  28. InspectionHandler
  29. } from '../inspector';
  30. import {
  31. IEditorMimeTypeService
  32. } from '../codeeditor';
  33. import {
  34. BaseCellWidget, CodeCellWidget, RawCellWidget,
  35. CodeCellModel, RawCellModel
  36. } from '../notebook/cells';
  37. import {
  38. EdgeLocation, ICellEditorWidget, ITextChange
  39. } from '../notebook/cells/editor';
  40. import {
  41. IRenderMime
  42. } from '../rendermime';
  43. import {
  44. ForeignHandler
  45. } from './foreign';
  46. import {
  47. ConsoleHistory, IConsoleHistory
  48. } from './history';
  49. import {
  50. IObservableVector, ObservableVector
  51. } from '../common/observablevector';
  52. /**
  53. * The class name added to console widgets.
  54. */
  55. const CONSOLE_CLASS = 'jp-ConsoleContent';
  56. /**
  57. * The class name added to the console banner.
  58. */
  59. const BANNER_CLASS = 'jp-ConsoleContent-banner';
  60. /**
  61. * The class name of a cell whose input originated from a foreign session.
  62. */
  63. const FOREIGN_CELL_CLASS = 'jp-ConsoleContent-foreignCell';
  64. /**
  65. * The class name of the active prompt
  66. */
  67. const PROMPT_CLASS = 'jp-ConsoleContent-prompt';
  68. /**
  69. * The class name of the panel that holds cell content.
  70. */
  71. const CONTENT_CLASS = 'jp-ConsoleContent-content';
  72. /**
  73. * The class name of the panel that holds prompts.
  74. */
  75. const INPUT_CLASS = 'jp-ConsoleContent-input';
  76. /**
  77. * The timeout in ms for execution requests to the kernel.
  78. */
  79. const EXECUTION_TIMEOUT = 250;
  80. /**
  81. * A widget containing a Jupyter console's content.
  82. *
  83. * #### Notes
  84. * The ConsoleContent class is intended to be used within a ConsolePanel
  85. * instance. Under most circumstances, it is not instantiated by user code.
  86. */
  87. export
  88. class ConsoleContent extends Widget {
  89. /**
  90. * Construct a console content widget.
  91. */
  92. constructor(options: ConsoleContent.IOptions) {
  93. super();
  94. this.addClass(CONSOLE_CLASS);
  95. // Create the panels that hold the content and input.
  96. let layout = this.layout = new PanelLayout();
  97. this._cells = new ObservableVector<BaseCellWidget>();
  98. this._content = new Panel();
  99. this._input = new Panel();
  100. this._renderer = options.renderer;
  101. this._rendermime = options.rendermime;
  102. this._session = options.session;
  103. this._history = options.history || new ConsoleHistory({
  104. kernel: this._session.kernel
  105. });
  106. // Add top-level CSS classes.
  107. this._content.addClass(CONTENT_CLASS);
  108. this._input.addClass(INPUT_CLASS);
  109. // Insert the content and input panes into the widget.
  110. layout.addWidget(this._content);
  111. layout.addWidget(this._input);
  112. // Create the banner.
  113. let banner = this._renderer.createBanner();
  114. banner.addClass(BANNER_CLASS);
  115. banner.readOnly = true;
  116. banner.model.source = '...';
  117. this._content.addWidget(banner);
  118. // Set the banner text and the mimetype.
  119. this._initialize();
  120. // Set up the inspection handler.
  121. this._inspectionHandler = new InspectionHandler({
  122. kernel: this._session.kernel,
  123. rendermime: this._rendermime
  124. });
  125. // Set up the foreign iopub handler.
  126. this._foreignHandler = new ForeignHandler({
  127. kernel: this._session.kernel,
  128. parent: this,
  129. renderer: { createCell: () => this._newForeignCell() }
  130. });
  131. // Instantiate the completer.
  132. this._newCompleter(options.completer);
  133. }
  134. /**
  135. * The list of content cells in the console.
  136. *
  137. * #### Notes
  138. * This list does not include the banner or the prompt for a console.
  139. */
  140. get cells(): IObservableVector<BaseCellWidget> {
  141. return this._cells;
  142. }
  143. /*
  144. * The console content panel that holds the banner and executed cells.
  145. */
  146. get content(): Panel {
  147. return this._content;
  148. }
  149. /**
  150. * A signal emitted when the console executes its prompt.
  151. */
  152. readonly executed: ISignal<this, Date>;
  153. /**
  154. * Get the inspection handler used by the console.
  155. */
  156. get inspectionHandler(): InspectionHandler {
  157. return this._inspectionHandler;
  158. }
  159. /*
  160. * The console input prompt.
  161. */
  162. get prompt(): CodeCellWidget | null {
  163. let inputLayout = (this._input.layout as PanelLayout);
  164. return inputLayout.widgets.at(0) as CodeCellWidget || null;
  165. }
  166. /**
  167. * Get the session used by the console.
  168. */
  169. get session(): Session.ISession {
  170. return this._session;
  171. }
  172. /**
  173. * Add a new cell to the content panel.
  174. *
  175. * @param cell - The cell widget being added to the content panel.
  176. *
  177. * #### Notes
  178. * This method is meant for use by outside classes that want to inject content
  179. * into a console. It is distinct from the `inject` method in that it requires
  180. * rendered code cell widgets and does not execute them.
  181. */
  182. addCell(cell: BaseCellWidget) {
  183. this._content.addWidget(cell);
  184. this._cells.pushBack(cell);
  185. cell.disposed.connect(this._onCellDisposed, this);
  186. this.update();
  187. }
  188. /**
  189. * Clear the code cells.
  190. */
  191. clear(): void {
  192. // Dispose all the content cells except the first, which is the banner.
  193. let cells = this._content.widgets;
  194. while (cells.length > 1) {
  195. cells.at(1).dispose();
  196. }
  197. }
  198. /**
  199. * Dispose of the resources held by the widget.
  200. */
  201. dispose() {
  202. // Do nothing if already disposed.
  203. if (this.isDisposed) {
  204. return;
  205. }
  206. super.dispose();
  207. this._completerHandler.dispose();
  208. this._completerHandler = null;
  209. this._completer.dispose();
  210. this._completer = null;
  211. this._foreignHandler.dispose();
  212. this._foreignHandler = null;
  213. this._history.dispose();
  214. this._history = null;
  215. this._inspectionHandler.dispose();
  216. this._inspectionHandler = null;
  217. this._session = null;
  218. this._cells.clear();
  219. this._cells = null;
  220. }
  221. /**
  222. * Execute the current prompt.
  223. *
  224. * @param force - Whether to force execution without checking code
  225. * completeness.
  226. *
  227. * @param timeout - The length of time, in milliseconds, that the execution
  228. * should wait for the API to determine whether code being submitted is
  229. * incomplete before attempting submission anyway. The default value is `250`.
  230. */
  231. execute(force = false, timeout = EXECUTION_TIMEOUT): Promise<void> {
  232. this._completer.reset();
  233. if (this._session.status === 'dead') {
  234. return Promise.resolve(void 0);
  235. }
  236. let prompt = this.prompt;
  237. prompt.trusted = true;
  238. if (force) {
  239. // Create a new prompt before kernel execution to allow typeahead.
  240. this.newPrompt();
  241. return this._execute(prompt);
  242. }
  243. // Check whether we should execute.
  244. return this._shouldExecute(timeout).then(should => {
  245. if (should) {
  246. // Create a new prompt before kernel execution to allow typeahead.
  247. this.newPrompt();
  248. return this._execute(prompt);
  249. }
  250. });
  251. }
  252. /**
  253. * Inject arbitrary code for the console to execute immediately.
  254. *
  255. * @param code - The code contents of the cell being injected.
  256. *
  257. * @returns A promise that indicates when the injected cell's execution ends.
  258. */
  259. inject(code: string): Promise<void> {
  260. // Create a new cell using the prompt renderer.
  261. let cell = this._renderer.createPrompt(this._rendermime, this);
  262. cell.model.source = code;
  263. cell.mimetype = this._mimetype;
  264. cell.readOnly = true;
  265. this.addCell(cell);
  266. return this._execute(cell);
  267. }
  268. /**
  269. * Insert a line break in the prompt.
  270. */
  271. insertLinebreak(): void {
  272. let prompt = this.prompt;
  273. let model = prompt.model;
  274. // Insert the line break at the cursor position, and move cursor forward.
  275. let pos = prompt.editor.getCursorPosition();
  276. model.source = model.source.substr(0, pos) + '\n' +
  277. model.source.substr(pos);
  278. prompt.editor.setCursorPosition(pos + 1);
  279. }
  280. /**
  281. * Serialize the output.
  282. */
  283. serialize(): nbformat.ICodeCell[] {
  284. let prompt = this.prompt;
  285. let layout = this._content.layout as PanelLayout;
  286. // Serialize content.
  287. let output = map(layout.widgets, widget => {
  288. return (widget as CodeCellWidget).model.toJSON() as nbformat.ICodeCell;
  289. });
  290. // Serialize prompt and return.
  291. return toArray(output).concat(prompt.model.toJSON() as nbformat.ICodeCell);
  292. }
  293. /**
  294. * Handle the DOM events for the widget.
  295. *
  296. * @param event - The DOM event sent to the widget.
  297. *
  298. * #### Notes
  299. * This method implements the DOM `EventListener` interface and is
  300. * called in response to events on the notebook panel's node. It should
  301. * not be called directly by user code.
  302. */
  303. handleEvent(event: Event): void {
  304. switch (event.type) {
  305. case 'keydown':
  306. this._evtKeyDown(event as KeyboardEvent);
  307. break;
  308. default:
  309. break;
  310. }
  311. }
  312. /**
  313. * Handle `after_attach` messages for the widget.
  314. */
  315. protected onAfterAttach(msg: Message): void {
  316. let node = this.node;
  317. node.addEventListener('keydown', this, true);
  318. // Create a prompt if necessary.
  319. if (!this.prompt) {
  320. this.newPrompt();
  321. }
  322. // Listen for kernel change events.
  323. this._addSessionListeners();
  324. }
  325. /**
  326. * Handle `before_detach` messages for the widget.
  327. */
  328. protected onBeforeDetach(msg: Message): void {
  329. let node = this.node;
  330. node.removeEventListener('keydown', this, true);
  331. }
  332. /**
  333. * Handle `'activate-request'` messages.
  334. */
  335. protected onActivateRequest(msg: Message): void {
  336. sendMessage(this.prompt.editor, WidgetMessage.ActivateRequest);
  337. this.update();
  338. }
  339. /**
  340. * Make a new prompt.
  341. */
  342. protected newPrompt(): void {
  343. let prompt = this.prompt;
  344. let input = this._input;
  345. // Make the last prompt read-only, clear its signals, and move to content.
  346. if (prompt) {
  347. prompt.readOnly = true;
  348. prompt.removeClass(PROMPT_CLASS);
  349. clearSignalData(prompt.editor);
  350. (input.layout as PanelLayout).removeWidgetAt(0);
  351. this.addCell(prompt);
  352. }
  353. // Create the new prompt.
  354. prompt = this._renderer.createPrompt(this._rendermime, this);
  355. prompt.mimetype = this._mimetype;
  356. prompt.addClass(PROMPT_CLASS);
  357. this._input.addWidget(prompt);
  358. // Hook up history handling.
  359. let editor = prompt.editor;
  360. editor.edgeRequested.connect(this.onEdgeRequest, this);
  361. editor.textChanged.connect(this.onTextChange, this);
  362. // Associate the new prompt with the completer and inspection handlers.
  363. this._completerHandler.activeCell = prompt;
  364. this._inspectionHandler.activeCell = prompt;
  365. sendMessage(prompt.editor, WidgetMessage.ActivateRequest);
  366. this.update();
  367. }
  368. /**
  369. * Handle an edge requested signal.
  370. */
  371. protected onEdgeRequest(editor: ICellEditorWidget, location: EdgeLocation): Promise<void> {
  372. let prompt = this.prompt;
  373. if (location === 'top') {
  374. return this._history.back(prompt.model.source).then(value => {
  375. if (!value) {
  376. return;
  377. }
  378. if (prompt.model.source === value) {
  379. return;
  380. }
  381. this._setByHistory = true;
  382. prompt.model.source = value;
  383. prompt.editor.setCursorPosition(0);
  384. });
  385. }
  386. return this._history.forward(prompt.model.source).then(value => {
  387. let text = value || this._history.placeholder;
  388. if (prompt.model.source === text) {
  389. return;
  390. }
  391. this._setByHistory = true;
  392. prompt.model.source = text;
  393. prompt.editor.setCursorPosition(text.length);
  394. });
  395. }
  396. /**
  397. * Handle a text change signal from the editor.
  398. */
  399. protected onTextChange(editor: ICellEditorWidget, args: ITextChange): void {
  400. if (this._setByHistory) {
  401. this._setByHistory = false;
  402. return;
  403. }
  404. this._history.reset();
  405. }
  406. /**
  407. * Handle `update-request` messages.
  408. */
  409. protected onUpdateRequest(msg: Message): void {
  410. Private.scrollToBottom(this._content.node);
  411. }
  412. /**
  413. * Handle the `'keydown'` event for the widget.
  414. */
  415. private _evtKeyDown(event: KeyboardEvent): void {
  416. if (event.keyCode === 13 && !this.prompt.editor.hasFocus()) {
  417. this.prompt.editor.activate();
  418. }
  419. }
  420. /**
  421. * Handle kernel change events on the session.
  422. */
  423. private _addSessionListeners(): void {
  424. if (this._listening) {
  425. return;
  426. }
  427. this._listening = this._session.kernelChanged.connect((sender, kernel) => {
  428. this.clear();
  429. this.newPrompt();
  430. this._initialize();
  431. this._history.kernel = kernel;
  432. this._completerHandler.kernel = kernel;
  433. this._foreignHandler.kernel = kernel;
  434. this._inspectionHandler.kernel = kernel;
  435. });
  436. }
  437. /**
  438. * Initialize the banner and mimetype.
  439. */
  440. private _initialize(): void {
  441. let kernel = this._session.kernel;
  442. kernel.ready.then(() => {
  443. this._handleInfo(kernel.info);
  444. });
  445. }
  446. /**
  447. * Execute the code in the current prompt.
  448. */
  449. private _execute(cell: CodeCellWidget): Promise<void> {
  450. this._history.push(cell.model.source);
  451. cell.model.contentChanged.connect(this.update, this);
  452. let onSuccess = (value: KernelMessage.IExecuteReplyMsg) => {
  453. this.executed.emit(new Date());
  454. if (!value) {
  455. return;
  456. }
  457. if (value.content.status === 'ok') {
  458. let content = value.content as KernelMessage.IExecuteOkReply;
  459. // Use deprecated payloads for backwards compatibility.
  460. if (content.payload && content.payload.length) {
  461. let setNextInput = content.payload.filter(i => {
  462. return (i as any).source === 'set_next_input';
  463. })[0];
  464. if (setNextInput) {
  465. let text = (setNextInput as any).text;
  466. // Ignore the `replace` value and always set the next cell.
  467. cell.model.source = text;
  468. }
  469. }
  470. }
  471. cell.model.contentChanged.disconnect(this.update, this);
  472. this.update();
  473. };
  474. let onFailure = () => {
  475. cell.model.contentChanged.disconnect(this.update, this);
  476. this.update();
  477. };
  478. return cell.execute(this._session.kernel).then(onSuccess, onFailure);
  479. }
  480. /**
  481. * Update the console based on the kernel info.
  482. */
  483. private _handleInfo(info: KernelMessage.IInfoReply): void {
  484. let layout = this._content.layout as PanelLayout;
  485. let banner = layout.widgets.at(0) as RawCellWidget;
  486. banner.model.source = info.banner;
  487. let lang = info.language_info as nbformat.ILanguageInfoMetadata;
  488. this._mimetype = this._renderer.getCodeMimetype(lang);
  489. if (this.prompt) {
  490. this.prompt.mimetype = this._mimetype;
  491. }
  492. }
  493. /**
  494. * Create a new completer widget if necessary and initialize it.
  495. */
  496. private _newCompleter(completer: CompleterWidget): void {
  497. // Instantiate completer widget.
  498. this._completer = completer || new CompleterWidget({
  499. model: new CompleterModel()
  500. });
  501. // Set the completer widget's anchor node to peg its position.
  502. this._completer.anchor = this.node;
  503. // Because a completer widget may be passed in, check if it is attached.
  504. if (!this._completer.isAttached) {
  505. Widget.attach(this._completer, document.body);
  506. }
  507. // Set up the completer handler.
  508. this._completerHandler = new CellCompleterHandler({
  509. completer: this._completer,
  510. kernel: this._session.kernel
  511. });
  512. }
  513. /**
  514. * Create a new foreign cell.
  515. */
  516. private _newForeignCell(): CodeCellWidget {
  517. let cell = this._renderer.createForeignCell(this._rendermime, this);
  518. cell.readOnly = true;
  519. cell.mimetype = this._mimetype;
  520. cell.addClass(FOREIGN_CELL_CLASS);
  521. return cell;
  522. }
  523. /**
  524. * Handle cell disposed signals.
  525. */
  526. private _onCellDisposed(sender: Widget, args: void): void {
  527. if (!this.isDisposed) {
  528. this._cells.remove(sender as CodeCellWidget);
  529. }
  530. }
  531. /**
  532. * Test whether we should execute the prompt.
  533. */
  534. private _shouldExecute(timeout: number): Promise<boolean> {
  535. let prompt = this.prompt;
  536. let code = prompt.model.source + '\n';
  537. return new Promise<boolean>((resolve, reject) => {
  538. let timer = setTimeout(() => { resolve(true); }, timeout);
  539. this._session.kernel.requestIsComplete({ code }).then(isComplete => {
  540. clearTimeout(timer);
  541. if (isComplete.content.status !== 'incomplete') {
  542. resolve(true);
  543. return;
  544. }
  545. prompt.model.source = code + isComplete.content.indent;
  546. prompt.editor.setCursorPosition(prompt.model.source.length);
  547. resolve(false);
  548. }).catch(() => { resolve(true); });
  549. });
  550. }
  551. private _cells: IObservableVector<BaseCellWidget> = null;
  552. private _completer: CompleterWidget = null;
  553. private _completerHandler: CellCompleterHandler = null;
  554. private _content: Panel = null;
  555. private _foreignHandler: ForeignHandler = null;
  556. private _history: IConsoleHistory = null;
  557. private _input: Panel = null;
  558. private _inspectionHandler: InspectionHandler = null;
  559. private _listening = false;
  560. private _mimetype = 'text/x-ipython';
  561. private _renderer: ConsoleContent.IRenderer = null;
  562. private _rendermime: IRenderMime = null;
  563. private _session: Session.ISession = null;
  564. private _setByHistory = false;
  565. }
  566. // Define the signals for the `ConsoleContent` class.
  567. defineSignal(ConsoleContent.prototype, 'executed');
  568. /**
  569. * A namespace for ConsoleContent statics.
  570. */
  571. export
  572. namespace ConsoleContent {
  573. /**
  574. * The initialization options for a console content widget.
  575. */
  576. export
  577. interface IOptions {
  578. /**
  579. * The completer widget for a console content widget.
  580. */
  581. completer?: CompleterWidget;
  582. /**
  583. * The history manager for a console content widget.
  584. */
  585. history?: IConsoleHistory;
  586. /**
  587. * The renderer for a console content widget.
  588. */
  589. renderer: IRenderer;
  590. /**
  591. * The mime renderer for the console content widget.
  592. */
  593. rendermime: IRenderMime;
  594. /**
  595. * The session for the console content widget.
  596. */
  597. session: Session.ISession;
  598. }
  599. /**
  600. * A renderer for completer widget nodes.
  601. */
  602. export
  603. interface IRenderer {
  604. /**
  605. * Create a new banner widget.
  606. */
  607. createBanner(): RawCellWidget;
  608. /**
  609. * Create a new prompt widget.
  610. */
  611. createPrompt(rendermime: IRenderMime, context: ConsoleContent): CodeCellWidget;
  612. /**
  613. * Create a code cell whose input originated from a foreign session.
  614. */
  615. createForeignCell(rendermine: IRenderMime, context: ConsoleContent): CodeCellWidget;
  616. /**
  617. * Get the preferred mimetype given language info.
  618. */
  619. getCodeMimetype(info: nbformat.ILanguageInfoMetadata): string;
  620. }
  621. /**
  622. * Default implementation of `IRenderer`.
  623. */
  624. export
  625. class Renderer implements IRenderer {
  626. /**
  627. * The banner renderer.
  628. */
  629. readonly bannerRenderer: CodeCellWidget.IRenderer;
  630. /**
  631. * The prompt renderer.
  632. */
  633. readonly promptRenderer: CodeCellWidget.IRenderer;
  634. /**
  635. * The foreign cell renderer.
  636. */
  637. readonly foreignCellRenderer: CodeCellWidget.IRenderer;
  638. /**
  639. * The mime type service of a code editor.
  640. */
  641. readonly editorMimeTypeService: IEditorMimeTypeService;
  642. /**
  643. * Create a new renderer.
  644. */
  645. constructor(options: Renderer.IOptions) {
  646. this.bannerRenderer = options.bannerRenderer;
  647. this.promptRenderer = options.promptRenderer;
  648. this.foreignCellRenderer = options.foreignCellRenderer;
  649. }
  650. /**
  651. * Create a new banner widget.
  652. */
  653. createBanner(): RawCellWidget {
  654. let widget = new RawCellWidget({
  655. renderer: this.bannerRenderer
  656. });
  657. widget.model = new RawCellModel();
  658. return widget;
  659. }
  660. /**
  661. * Create a new prompt widget.
  662. */
  663. createPrompt(rendermime: IRenderMime, context: ConsoleContent): CodeCellWidget {
  664. let widget = new CodeCellWidget({
  665. rendermime,
  666. renderer: this.promptRenderer
  667. });
  668. widget.model = new CodeCellModel();
  669. return widget;
  670. }
  671. /**
  672. * Create a new code cell widget for an input from a foreign session.
  673. */
  674. createForeignCell(rendermime: IRenderMime, context: ConsoleContent): CodeCellWidget {
  675. let widget = new CodeCellWidget({
  676. rendermime,
  677. renderer: this.foreignCellRenderer
  678. });
  679. widget.model = new CodeCellModel();
  680. return widget;
  681. }
  682. /**
  683. * Get the preferred mimetype given language info.
  684. */
  685. getCodeMimetype(info: nbformat.ILanguageInfoMetadata): string {
  686. return this.editorMimeTypeService.getMimeTypeByLanguage(info);
  687. }
  688. }
  689. /**
  690. * The namespace for `Renderer`.
  691. */
  692. export
  693. namespace Renderer {
  694. /**
  695. * An initialize options for `Renderer`.
  696. */
  697. export
  698. interface IOptions {
  699. /**
  700. * The banner renderer.
  701. */
  702. readonly bannerRenderer: CodeCellWidget.IRenderer;
  703. /**
  704. * The prompt renderer.
  705. */
  706. readonly promptRenderer: CodeCellWidget.IRenderer;
  707. /**
  708. * The foreign cell renderer.
  709. */
  710. readonly foreignCellRenderer: CodeCellWidget.IRenderer;
  711. /**
  712. * The mime type service of a code editor.
  713. */
  714. readonly editorMimeTypeService: IEditorMimeTypeService;
  715. }
  716. }
  717. /* tslint:disable */
  718. /**
  719. * The console renderer token.
  720. */
  721. export
  722. const IRenderer = new Token<IRenderer>('jupyter.services.console.renderer');
  723. /* tslint:enable */
  724. }
  725. /**
  726. * A namespace for console widget private data.
  727. */
  728. namespace Private {
  729. /**
  730. * Jump to the bottom of a node.
  731. *
  732. * @param node - The scrollable element.
  733. */
  734. export
  735. function scrollToBottom(node: HTMLElement): void {
  736. node.scrollTop = node.scrollHeight - node.clientHeight;
  737. }
  738. }