123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- 'use strict';
- import {
- loadModeByMIME
- } from 'jupyter-js-ui/lib/codemirror';
- import {
- RenderMime
- } from 'jupyter-js-ui/lib/rendermime';
- import {
- Message
- } from 'phosphor-messaging';
- import {
- PanelLayout
- } from 'phosphor-panel';
- import {
- Widget
- } from 'phosphor-widget';
- import {
- MimeBundle
- } from '../notebook/nbformat';
- import {
- OutputAreaWidget, ObservableOutputs
- } from '../output-area';
- import {
- sanitize
- } from 'sanitizer';
- import {
- IMetadataCursor
- } from '../common/metadata';
- import {
- CellEditorWidget
- } from './editor';
- import {
- ICodeCellModel, ICellModel
- } from './model';
- const CELL_CLASS = 'jp-Cell';
- const INPUT_CLASS = 'jp-InputArea';
- const PROMPT_CLASS = 'jp-InputArea-prompt';
- const EDITOR_CLASS = 'jp-InputArea-editor';
- const COLLAPSED_CLASS = 'jp-mod-collapsed';
- const READONLY_CLASS = 'jp-mod-readOnly';
- const CODE_CELL_CLASS = 'jp-CodeCell';
- const MARKDOWN_CELL_CLASS = 'jp-MarkdownCell';
- const RENDERER_CLASS = 'jp-MarkdownCell-renderer';
- const RAW_CELL_CLASS = 'jp-RawCell';
- const RENDERED_CLASS = 'jp-mod-rendered';
- const DEFAULT_MARKDOWN_TEXT = 'Type Markdown and LaTeX: $ α^2 $';
- export
- class BaseCellWidget extends Widget {
-
- static createCellEditor(model: ICellModel): CellEditorWidget {
- return new CellEditorWidget(model);
- }
-
- constructor(model: ICellModel) {
- super();
- this.addClass(CELL_CLASS);
- this._model = model;
- let ctor = this.constructor as typeof BaseCellWidget;
- this._editor = ctor.createCellEditor(model);
- this._input = new InputAreaWidget(this._editor);
- this.layout = new PanelLayout();
- (this.layout as PanelLayout).addChild(this._input);
- model.contentChanged.connect(this.onModelChanged, this);
- this._trustedCursor = model.getMetadata('trusted');
- this._trusted = this._trustedCursor.getValue();
- }
-
- get model(): ICellModel {
- return this._model;
- }
-
- get editor(): CellEditorWidget {
- return this._editor;
- }
-
- get mimetype(): string {
- return this._mimetype;
- }
- set mimetype(value: string) {
- if (this._mimetype === value) {
- return;
- }
- this._mimetype = value;
- loadModeByMIME(this.editor.editor, value);
- }
-
- get readOnly(): boolean {
- return this._readOnly;
- }
- set readOnly(value: boolean) {
- if (value === this._readOnly) {
- return;
- }
- this._readOnly = value;
- this.update();
- }
-
- get trusted(): boolean {
- return this._trusted;
- }
- set trusted(value: boolean) {
- this._trustedCursor.setValue(value);
- }
-
- focus(): void {
- this.editor.editor.focus();
- }
-
- setPrompt(value: string): void {
- this._input.setPrompt(value);
- }
-
- toggleInput(value: boolean): void {
- if (value) {
- this._input.show();
- this.focus();
- } else {
- this._input.hide();
- }
- }
-
- dispose() {
-
- if (this.isDisposed) {
- return;
- }
- this._model = null;
- this._input = null;
- this._editor = null;
- this._trustedCursor.dispose();
- this._trustedCursor = null;
- super.dispose();
- }
-
- protected onAfterAttach(msg: Message): void {
- this.update();
- }
-
- protected onUpdateRequest(message: Message): void {
-
- let option = this._readOnly ? 'nocursor' : false;
- this.editor.editor.setOption('readOnly', option);
- this.toggleClass(READONLY_CLASS, this._readOnly);
- }
-
- protected onModelChanged(model: ICellModel, change: string): void {
- switch (change) {
- case 'metadata':
- case 'metadata.trusted':
- this._trusted = this._trustedCursor.getValue();
- this.update();
- break;
- default:
- break;
- }
- }
- private _input: InputAreaWidget = null;
- private _editor: CellEditorWidget = null;
- private _model: ICellModel = null;
- private _mimetype = 'text/plain';
- private _readOnly = false;
- private _trustedCursor: IMetadataCursor = null;
- private _trusted = false;
- }
- export
- class CodeCellWidget extends BaseCellWidget {
-
- static createOutput(outputs: ObservableOutputs, rendermime: RenderMime<Widget>): OutputAreaWidget {
- return new OutputAreaWidget(outputs, rendermime);
- }
-
- constructor(model: ICodeCellModel, rendermime: RenderMime<Widget>) {
- super(model);
- this._rendermime = rendermime;
- this.addClass(CODE_CELL_CLASS);
- let constructor = this.constructor as typeof CodeCellWidget;
- this._output = constructor.createOutput(model.outputs, rendermime);
- this._output.trusted = this.trusted;
- (this.layout as PanelLayout).addChild(this._output);
- this._collapsedCursor = model.getMetadata('collapsed');
- this._scrolledCursor = model.getMetadata('scrolled');
- this.setPrompt(String(model.executionCount));
- }
-
- dispose(): void {
- if (this.isDisposed) {
- return;
- }
- this._rendermime = null;
- this._collapsedCursor.dispose();
- this._collapsedCursor = null;
- this._scrolledCursor.dispose();
- this._scrolledCursor = null;
- this._output = null;
- super.dispose();
- }
-
- protected onUpdateRequest(message: Message): void {
- this.toggleClass(COLLAPSED_CLASS, this._collapsedCursor.getValue());
-
- this._output.trusted = this.trusted;
- super.onUpdateRequest(message);
- }
-
- protected onModelChanged(model: ICodeCellModel, change: string): void {
- switch (change) {
- case 'metadata.collapsed':
- case 'metadata.scrolled':
- this.update();
- break;
- case 'executionCount':
- this.setPrompt(String(model.executionCount));
- break;
- default:
- break;
- }
- super.onModelChanged(model, change);
- }
- private _output: OutputAreaWidget = null;
- private _rendermime: RenderMime<Widget> = null;
- private _collapsedCursor: IMetadataCursor = null;
- private _scrolledCursor: IMetadataCursor = null;
- }
- export
- class MarkdownCellWidget extends BaseCellWidget {
-
- constructor(model: ICellModel, rendermime: RenderMime<Widget>) {
- super(model);
- this._rendermime = rendermime;
- this.addClass(MARKDOWN_CELL_CLASS);
-
- this.mimetype = 'text/x-ipythongfm';
- this._renderer = new Widget();
- this._renderer.addClass(RENDERER_CLASS);
- (this.layout as PanelLayout).addChild(this._renderer);
- }
-
- get rendered(): boolean {
- return this._rendered;
- }
- set rendered(value: boolean) {
- if (value === this._rendered) {
- return;
- }
- this._rendered = value;
- this.update();
- }
-
- dispose(): void {
- if (this.isDisposed) {
- return;
- }
- this._renderer = null;
- this._rendermime = null;
- super.dispose();
- }
-
- protected onUpdateRequest(message: Message): void {
- let model = this.model;
- if (this.rendered) {
- let text = model.source || DEFAULT_MARKDOWN_TEXT;
-
- if (text !== this._prev) {
- text = sanitize(text);
- let bundle: MimeBundle = { 'text/markdown': text };
- this._renderer.dispose();
- this._renderer = this._rendermime.render(bundle) || new Widget();
- this._renderer.addClass(RENDERER_CLASS);
- (this.layout as PanelLayout).addChild(this._renderer);
- }
- this._prev = text;
- this._renderer.show();
- this.toggleInput(false);
- this.addClass(RENDERED_CLASS);
- } else {
- this._renderer.hide();
- this.toggleInput(true);
- this.removeClass(RENDERED_CLASS);
- }
- super.onUpdateRequest(message);
- }
- private _rendermime: RenderMime<Widget> = null;
- private _renderer: Widget = null;
- private _rendered = true;
- private _prev = '';
- }
- export
- class RawCellWidget extends BaseCellWidget {
-
- constructor(model: ICellModel) {
- super(model);
- this.addClass(RAW_CELL_CLASS);
- }
- }
- class InputAreaWidget extends Widget {
-
- constructor(editor: CellEditorWidget) {
- super();
- this.addClass(INPUT_CLASS);
- editor.addClass(EDITOR_CLASS);
- this.layout = new PanelLayout();
- let prompt = new Widget();
- prompt.addClass(PROMPT_CLASS);
- let layout = this.layout as PanelLayout;
- layout.addChild(prompt);
- layout.addChild(editor);
- }
-
- setPrompt(value: string): void {
- let prompt = (this.layout as PanelLayout).childAt(0);
- if (value === 'null') {
- value = ' ';
- }
- let text = `In [${value || ' '}]:`;
- prompt.node.textContent = text;
- }
- }
|