UNPKG

3.92 kBJavaScriptView Raw
1import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
2import { TextItem } from '@jupyterlab/statusbar';
3import { nullTranslator } from '@jupyterlab/translation';
4import * as React from 'react';
5/**
6 * A pure function for rendering a Command/Edit mode component.
7 *
8 * @param props: the props for rendering the component.
9 *
10 * @returns a tsx component for command/edit mode.
11 */
12function CommandEditComponent(props) {
13 const trans = (props.translator || nullTranslator).load('jupyterlab');
14 return (React.createElement(TextItem, { source: trans.__('Mode: %1', props.modeNames[props.notebookMode]) }));
15}
16/**
17 * StatusBar item to display which notebook mode user is in.
18 */
19export class CommandEditStatus extends VDomRenderer {
20 /**
21 * Construct a new CommandEdit status item.
22 */
23 constructor(translator) {
24 super(new CommandEditStatus.Model());
25 this.translator = translator || nullTranslator;
26 this._trans = this.translator.load('jupyterlab');
27 this._modeNames = {
28 command: this._trans.__('Command'),
29 edit: this._trans.__('Edit')
30 };
31 }
32 /**
33 * Render the CommandEdit status item.
34 */
35 render() {
36 if (!this.model) {
37 return null;
38 }
39 this.node.title = this._trans.__('Notebook is in %1 mode', this._modeNames[this.model.notebookMode]);
40 return (React.createElement(CommandEditComponent, { notebookMode: this.model.notebookMode, translator: this.translator, modeNames: this._modeNames }));
41 }
42}
43/**
44 * A namespace for CommandEdit statics.
45 */
46(function (CommandEditStatus) {
47 /**
48 * A VDomModel for the CommandEdit renderer.
49 */
50 class Model extends VDomModel {
51 constructor() {
52 super(...arguments);
53 /**
54 * On a change to the notebook, update the mode.
55 */
56 this._onChanged = (_notebook) => {
57 const oldMode = this._notebookMode;
58 if (this._notebook) {
59 this._notebookMode = _notebook.mode;
60 }
61 else {
62 this._notebookMode = 'command';
63 }
64 this._triggerChange(oldMode, this._notebookMode);
65 };
66 this._notebookMode = 'command';
67 this._notebook = null;
68 }
69 /**
70 * The current mode of the current notebook.
71 */
72 get notebookMode() {
73 return this._notebookMode;
74 }
75 /**
76 * Set the current notebook for the model.
77 */
78 set notebook(notebook) {
79 const oldNotebook = this._notebook;
80 if (oldNotebook !== null) {
81 oldNotebook.stateChanged.disconnect(this._onChanged, this);
82 oldNotebook.activeCellChanged.disconnect(this._onChanged, this);
83 oldNotebook.modelContentChanged.disconnect(this._onChanged, this);
84 }
85 const oldMode = this._notebookMode;
86 this._notebook = notebook;
87 if (this._notebook === null) {
88 this._notebookMode = 'command';
89 }
90 else {
91 this._notebookMode = this._notebook.mode;
92 this._notebook.stateChanged.connect(this._onChanged, this);
93 this._notebook.activeCellChanged.connect(this._onChanged, this);
94 this._notebook.modelContentChanged.connect(this._onChanged, this);
95 }
96 this._triggerChange(oldMode, this._notebookMode);
97 }
98 /**
99 * Trigger a state change for the renderer.
100 */
101 _triggerChange(oldState, newState) {
102 if (oldState !== newState) {
103 this.stateChanged.emit(void 0);
104 }
105 }
106 }
107 CommandEditStatus.Model = Model;
108})(CommandEditStatus || (CommandEditStatus = {}));
109//# sourceMappingURL=modestatus.js.map
\No newline at end of file