UNPKG

10.5 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { Toolbar as AppToolbar, Dialog, SessionContextDialogs, showDialog } from '@jupyterlab/apputils';
4import { nullTranslator } from '@jupyterlab/translation';
5import { addIcon, addToolbarButtonClass, copyIcon, cutIcon, fastForwardIcon, HTMLSelect, pasteIcon, ReactWidget, runIcon, saveIcon, Toolbar, ToolbarButton, ToolbarButtonComponent, UseSignal } 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()]
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, sessionDialogs, translator) {
124 const trans = (translator !== null && translator !== void 0 ? translator : nullTranslator).load('jupyterlab');
125 return new ToolbarButton({
126 icon: runIcon,
127 onClick: () => {
128 void NotebookActions.runAndAdvance(panel.content, panel.sessionContext, sessionDialogs, translator);
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 !== null && translator !== void 0 ? translator : nullTranslator).load('jupyterlab');
142 return new ToolbarButton({
143 icon: fastForwardIcon,
144 onClick: () => {
145 const dialogs_ = dialogs !== null && dialogs !== void 0 ? dialogs : new SessionContextDialogs({ translator });
146 void dialogs_.restart(panel.sessionContext).then(restarted => {
147 if (restarted) {
148 void NotebookActions.runAll(panel.content, panel.sessionContext, dialogs_, translator);
149 }
150 return restarted;
151 });
152 },
153 tooltip: trans.__('Restart the kernel, then re-run the whole notebook')
154 });
155 }
156 ToolbarItems.createRestartRunAllButton = createRestartRunAllButton;
157 /**
158 * Create a cell type switcher item.
159 *
160 * #### Notes
161 * It will display the type of the current active cell.
162 * If more than one cell is selected but are of different types,
163 * it will display `'-'`.
164 * When the user changes the cell type, it will change the
165 * cell types of the selected cells.
166 * It can handle a change to the context.
167 */
168 function createCellTypeItem(panel, translator) {
169 return new CellTypeSwitcher(panel.content, translator);
170 }
171 ToolbarItems.createCellTypeItem = createCellTypeItem;
172 /**
173 * Get the default toolbar items for panel
174 *
175 * @deprecated since v4
176 */
177 function getDefaultItems(panel, sessionDialogs, translator) {
178 return [
179 { name: 'save', widget: createSaveButton(panel, translator) },
180 { name: 'insert', widget: createInsertButton(panel, translator) },
181 { name: 'cut', widget: createCutButton(panel, translator) },
182 { name: 'copy', widget: createCopyButton(panel, translator) },
183 { name: 'paste', widget: createPasteButton(panel, translator) },
184 {
185 name: 'run',
186 widget: createRunButton(panel, sessionDialogs, translator)
187 },
188 {
189 name: 'interrupt',
190 widget: AppToolbar.createInterruptButton(panel.sessionContext, translator)
191 },
192 {
193 name: 'restart',
194 widget: AppToolbar.createRestartButton(panel.sessionContext, sessionDialogs, translator)
195 },
196 {
197 name: 'restart-and-run',
198 widget: createRestartRunAllButton(panel, sessionDialogs, translator)
199 },
200 { name: 'cellType', widget: createCellTypeItem(panel, translator) },
201 { name: 'spacer', widget: Toolbar.createSpacerItem() },
202 {
203 name: 'kernelName',
204 widget: AppToolbar.createKernelNameItem(panel.sessionContext, sessionDialogs, translator)
205 }
206 ];
207 }
208 ToolbarItems.getDefaultItems = getDefaultItems;
209})(ToolbarItems || (ToolbarItems = {}));
210/**
211 * A toolbar widget that switches cell types.
212 */
213export class CellTypeSwitcher extends ReactWidget {
214 /**
215 * Construct a new cell type switcher.
216 */
217 constructor(widget, translator) {
218 super();
219 /**
220 * Handle `change` events for the HTMLSelect component.
221 */
222 this.handleChange = (event) => {
223 if (event.target.value !== '-') {
224 NotebookActions.changeCellType(this._notebook, event.target.value);
225 this._notebook.activate();
226 }
227 };
228 /**
229 * Handle `keydown` events for the HTMLSelect component.
230 */
231 this.handleKeyDown = (event) => {
232 if (event.keyCode === 13) {
233 this._notebook.activate();
234 }
235 };
236 this._trans = (translator || nullTranslator).load('jupyterlab');
237 this.addClass(TOOLBAR_CELLTYPE_CLASS);
238 this._notebook = widget;
239 if (widget.model) {
240 this.update();
241 }
242 widget.activeCellChanged.connect(this.update, this);
243 // Follow a change in the selection.
244 widget.selectionChanged.connect(this.update, this);
245 }
246 render() {
247 let value = '-';
248 if (this._notebook.activeCell) {
249 value = this._notebook.activeCell.model.type;
250 }
251 for (const widget of this._notebook.widgets) {
252 if (this._notebook.isSelectedOrActive(widget)) {
253 if (widget.model.type !== value) {
254 value = '-';
255 break;
256 }
257 }
258 }
259 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') },
260 React.createElement("option", { value: "-" }, "-"),
261 React.createElement("option", { value: "code" }, this._trans.__('Code')),
262 React.createElement("option", { value: "markdown" }, this._trans.__('Markdown')),
263 React.createElement("option", { value: "raw" }, this._trans.__('Raw'))));
264 }
265}
266//# sourceMappingURL=default-toolbar.js.map
\No newline at end of file