UNPKG

sophon-notebook-notebook

Version:
103 lines 3.51 kB
import * as React from 'react'; import { VDomRenderer, VDomModel } from 'sophon-notebook-apputils'; import { Text } from 'sophon-notebook-coreutils'; import { TextItem } from 'sophon-notebook-statusbar'; /** * A pure function for rendering a Command/Edit mode component. * * @param props: the props for rendering the component. * * @returns a tsx component for command/edit mode. */ function CommandEditComponent(props) { return React.createElement(TextItem, { source: `Mode: ${Text.titleCase(props.notebookMode)}` }); } /** * StatusBar item to display which notebook mode user is in. */ export class CommandEditStatus extends VDomRenderer { /** * Construct a new CommandEdit status item. */ constructor() { super(); this.model = new CommandEditStatus.Model(); } /** * Render the CommandEdit status item. */ render() { if (!this.model) { return null; } this.node.title = `Notebook is in ${this.model.notebookMode} mode`; return React.createElement(CommandEditComponent, { notebookMode: this.model.notebookMode }); } } /** * A namespace for CommandEdit statics. */ (function (CommandEditStatus) { /** * A VDomModle for the CommandEdit renderer. */ class Model extends VDomModel { constructor() { super(...arguments); /** * On a change to the notebook, update the mode. */ this._onChanged = (_notebook) => { const oldMode = this._notebookMode; if (this._notebook) { this._notebookMode = _notebook.mode; } else { this._notebookMode = 'command'; } this._triggerChange(oldMode, this._notebookMode); }; this._notebookMode = 'command'; this._notebook = null; } /** * The current mode of the current notebook. */ get notebookMode() { return this._notebookMode; } /** * Set the current notebook for the model. */ set notebook(notebook) { const oldNotebook = this._notebook; if (oldNotebook !== null) { oldNotebook.stateChanged.disconnect(this._onChanged, this); oldNotebook.activeCellChanged.disconnect(this._onChanged, this); oldNotebook.modelContentChanged.disconnect(this._onChanged, this); } const oldMode = this._notebookMode; this._notebook = notebook; if (this._notebook === null) { this._notebookMode = 'command'; } else { this._notebookMode = this._notebook.mode; this._notebook.stateChanged.connect(this._onChanged, this); this._notebook.activeCellChanged.connect(this._onChanged, this); this._notebook.modelContentChanged.connect(this._onChanged, this); } this._triggerChange(oldMode, this._notebookMode); } /** * Trigger a state change for the renderer. */ _triggerChange(oldState, newState) { if (oldState !== newState) { this.stateChanged.emit(void 0); } } } CommandEditStatus.Model = Model; })(CommandEditStatus || (CommandEditStatus = {})); //# sourceMappingURL=modestatus.js.map