UNPKG

10.2 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { addToolbarButtonClass, Dialog, ReactWidget, sessionContextDialogs, showDialog, Toolbar, ToolbarButton, ToolbarButtonComponent, UseSignal } from '@jupyterlab/apputils';
4import { nullTranslator } from '@jupyterlab/translation';
5import { addIcon, copyIcon, cutIcon, fastForwardIcon, HTMLSelect, pasteIcon, runIcon, saveIcon } from '@jupyterlab/ui-components';
6import * as React from 'react';
7import { NotebookActions } from './actions';
8/**
9 * The class name added to toolbar cell type dropdown wrapper.
10 */
11const TOOLBAR_CELLTYPE_CLASS = 'jp-Notebook-toolbarCellType';
12/**
13 * The class name added to toolbar cell type dropdown.
14 */
15const TOOLBAR_CELLTYPE_DROPDOWN_CLASS = 'jp-Notebook-toolbarCellTypeDropdown';
16/**
17 * A namespace for the default toolbar items.
18 */
19export var ToolbarItems;
20(function (ToolbarItems) {
21 /**
22 * Create save button toolbar item.
23 *
24 * @deprecated since v3.2
25 * This is dead code now.
26 */
27 function createSaveButton(panel, translator) {
28 const trans = (translator || nullTranslator).load('jupyterlab');
29 function onClick() {
30 if (panel.context.model.readOnly) {
31 return showDialog({
32 title: trans.__('Cannot Save'),
33 body: trans.__('Document is read-only'),
34 buttons: [Dialog.okButton({ label: trans.__('Ok') })]
35 });
36 }
37 void panel.context.save().then(() => {
38 if (!panel.isDisposed) {
39 return panel.context.createCheckpoint();
40 }
41 });
42 }
43 return addToolbarButtonClass(ReactWidget.create(React.createElement(UseSignal, { signal: panel.context.fileChanged }, () => (React.createElement(ToolbarButtonComponent, { icon: saveIcon, onClick: onClick, tooltip: trans.__('Save the notebook contents and create checkpoint'), enabled: !!(panel &&
44 panel.context &&
45 panel.context.contentsModel &&
46 panel.context.contentsModel.writable) })))));
47 }
48 ToolbarItems.createSaveButton = createSaveButton;
49 /**
50 * Create an insert toolbar item.
51 *
52 * @deprecated since v3.2
53 * This is dead code now.
54 */
55 function createInsertButton(panel, translator) {
56 const trans = (translator || nullTranslator).load('jupyterlab');
57 return new ToolbarButton({
58 icon: addIcon,
59 onClick: () => {
60 NotebookActions.insertBelow(panel.content);
61 },
62 tooltip: trans.__('Insert a cell below')
63 });
64 }
65 ToolbarItems.createInsertButton = createInsertButton;
66 /**
67 * Create a cut toolbar item.
68 *
69 * @deprecated since v3.2
70 * This is dead code now.
71 */
72 function createCutButton(panel, translator) {
73 const trans = (translator || nullTranslator).load('jupyterlab');
74 return new ToolbarButton({
75 icon: cutIcon,
76 onClick: () => {
77 NotebookActions.cut(panel.content);
78 },
79 tooltip: trans.__('Cut the selected cells')
80 });
81 }
82 ToolbarItems.createCutButton = createCutButton;
83 /**
84 * Create a copy toolbar item.
85 *
86 * @deprecated since v3.2
87 * This is dead code now.
88 */
89 function createCopyButton(panel, translator) {
90 const trans = (translator || nullTranslator).load('jupyterlab');
91 return new ToolbarButton({
92 icon: copyIcon,
93 onClick: () => {
94 NotebookActions.copy(panel.content);
95 },
96 tooltip: trans.__('Copy the selected cells')
97 });
98 }
99 ToolbarItems.createCopyButton = createCopyButton;
100 /**
101 * Create a paste toolbar item.
102 *
103 * @deprecated since v3.2
104 * This is dead code now.
105 */
106 function createPasteButton(panel, translator) {
107 const trans = (translator || nullTranslator).load('jupyterlab');
108 return new ToolbarButton({
109 icon: pasteIcon,
110 onClick: () => {
111 NotebookActions.paste(panel.content);
112 },
113 tooltip: trans.__('Paste cells from the clipboard')
114 });
115 }
116 ToolbarItems.createPasteButton = createPasteButton;
117 /**
118 * Create a run toolbar item.
119 *
120 * @deprecated since v3.2
121 * This is dead code now.
122 */
123 function createRunButton(panel, translator) {
124 const trans = (translator || nullTranslator).load('jupyterlab');
125 return new ToolbarButton({
126 icon: runIcon,
127 onClick: () => {
128 void NotebookActions.runAndAdvance(panel.content, panel.sessionContext);
129 },
130 tooltip: trans.__('Run the selected cells and advance')
131 });
132 }
133 ToolbarItems.createRunButton = createRunButton;
134 /**
135 * Create a restart run all toolbar item
136 *
137 * @deprecated since v3.2
138 * This is dead code now.
139 */
140 function createRestartRunAllButton(panel, dialogs, translator) {
141 const trans = (translator || nullTranslator).load('jupyterlab');
142 return new ToolbarButton({
143 icon: fastForwardIcon,
144 onClick: () => {
145 void (dialogs !== null && dialogs !== void 0 ? dialogs : sessionContextDialogs)
146 .restart(panel.sessionContext, translator)
147 .then(restarted => {
148 if (restarted) {
149 void NotebookActions.runAll(panel.content, panel.sessionContext);
150 }
151 return restarted;
152 });
153 },
154 tooltip: trans.__('Restart the kernel, then re-run the whole notebook')
155 });
156 }
157 ToolbarItems.createRestartRunAllButton = createRestartRunAllButton;
158 /**
159 * Create a cell type switcher item.
160 *
161 * #### Notes
162 * It will display the type of the current active cell.
163 * If more than one cell is selected but are of different types,
164 * it will display `'-'`.
165 * When the user changes the cell type, it will change the
166 * cell types of the selected cells.
167 * It can handle a change to the context.
168 */
169 function createCellTypeItem(panel, translator) {
170 return new CellTypeSwitcher(panel.content, translator);
171 }
172 ToolbarItems.createCellTypeItem = createCellTypeItem;
173 /**
174 * Get the default toolbar items for panel
175 */
176 function getDefaultItems(panel, sessionDialogs, translator) {
177 return [
178 { name: 'save', widget: createSaveButton(panel, translator) },
179 { name: 'insert', widget: createInsertButton(panel, translator) },
180 { name: 'cut', widget: createCutButton(panel, translator) },
181 { name: 'copy', widget: createCopyButton(panel, translator) },
182 { name: 'paste', widget: createPasteButton(panel, translator) },
183 { name: 'run', widget: createRunButton(panel, translator) },
184 {
185 name: 'interrupt',
186 widget: Toolbar.createInterruptButton(panel.sessionContext, translator)
187 },
188 {
189 name: 'restart',
190 widget: Toolbar.createRestartButton(panel.sessionContext, sessionDialogs, translator)
191 },
192 {
193 name: 'restart-and-run',
194 widget: createRestartRunAllButton(panel, sessionDialogs, translator)
195 },
196 { name: 'cellType', widget: createCellTypeItem(panel, translator) },
197 { name: 'spacer', widget: Toolbar.createSpacerItem() },
198 {
199 name: 'kernelName',
200 widget: Toolbar.createKernelNameItem(panel.sessionContext, sessionDialogs, translator)
201 }
202 ];
203 }
204 ToolbarItems.getDefaultItems = getDefaultItems;
205})(ToolbarItems || (ToolbarItems = {}));
206/**
207 * A toolbar widget that switches cell types.
208 */
209export class CellTypeSwitcher extends ReactWidget {
210 /**
211 * Construct a new cell type switcher.
212 */
213 constructor(widget, translator) {
214 super();
215 /**
216 * Handle `change` events for the HTMLSelect component.
217 */
218 this.handleChange = (event) => {
219 if (event.target.value !== '-') {
220 NotebookActions.changeCellType(this._notebook, event.target.value);
221 this._notebook.activate();
222 }
223 };
224 /**
225 * Handle `keydown` events for the HTMLSelect component.
226 */
227 this.handleKeyDown = (event) => {
228 if (event.keyCode === 13) {
229 this._notebook.activate();
230 }
231 };
232 this._trans = (translator || nullTranslator).load('jupyterlab');
233 this.addClass(TOOLBAR_CELLTYPE_CLASS);
234 this._notebook = widget;
235 if (widget.model) {
236 this.update();
237 }
238 widget.activeCellChanged.connect(this.update, this);
239 // Follow a change in the selection.
240 widget.selectionChanged.connect(this.update, this);
241 }
242 render() {
243 let value = '-';
244 if (this._notebook.activeCell) {
245 value = this._notebook.activeCell.model.type;
246 }
247 for (const widget of this._notebook.widgets) {
248 if (this._notebook.isSelectedOrActive(widget)) {
249 if (widget.model.type !== value) {
250 value = '-';
251 break;
252 }
253 }
254 }
255 return (React.createElement(HTMLSelect, { className: TOOLBAR_CELLTYPE_DROPDOWN_CLASS, onChange: this.handleChange, onKeyDown: this.handleKeyDown, value: value, "aria-label": this._trans.__('Cell type'), title: this._trans.__('Select the cell type') },
256 React.createElement("option", { value: "-" }, "-"),
257 React.createElement("option", { value: "code" }, this._trans.__('Code')),
258 React.createElement("option", { value: "markdown" }, this._trans.__('Markdown')),
259 React.createElement("option", { value: "raw" }, this._trans.__('Raw'))));
260 }
261}
262//# sourceMappingURL=default-toolbar.js.map
\No newline at end of file