1 |
|
2 |
|
3 | import { Toolbar as AppToolbar, Dialog, SessionContextDialogs, showDialog } from '@jupyterlab/apputils';
|
4 | import { nullTranslator } from '@jupyterlab/translation';
|
5 | import { addIcon, addToolbarButtonClass, copyIcon, cutIcon, fastForwardIcon, HTMLSelect, pasteIcon, ReactWidget, runIcon, saveIcon, Toolbar, ToolbarButton, ToolbarButtonComponent, UseSignal } from '@jupyterlab/ui-components';
|
6 | import * as React from 'react';
|
7 | import { NotebookActions } from './actions';
|
8 |
|
9 |
|
10 |
|
11 | const TOOLBAR_CELLTYPE_CLASS = 'jp-Notebook-toolbarCellType';
|
12 |
|
13 |
|
14 |
|
15 | const TOOLBAR_CELLTYPE_DROPDOWN_CLASS = 'jp-Notebook-toolbarCellTypeDropdown';
|
16 |
|
17 |
|
18 |
|
19 | export var ToolbarItems;
|
20 | (function (ToolbarItems) {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
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 |
|
51 |
|
52 |
|
53 |
|
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 |
|
68 |
|
69 |
|
70 |
|
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 |
|
85 |
|
86 |
|
87 |
|
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 |
|
102 |
|
103 |
|
104 |
|
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 |
|
119 |
|
120 |
|
121 |
|
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 |
|
136 |
|
137 |
|
138 |
|
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 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function createCellTypeItem(panel, translator) {
|
169 | return new CellTypeSwitcher(panel.content, translator);
|
170 | }
|
171 | ToolbarItems.createCellTypeItem = createCellTypeItem;
|
172 | |
173 |
|
174 |
|
175 |
|
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 |
|
212 |
|
213 | export class CellTypeSwitcher extends ReactWidget {
|
214 | |
215 |
|
216 |
|
217 | constructor(widget, translator) {
|
218 | super();
|
219 | |
220 |
|
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 |
|
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 |
|
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 |
|
\ | No newline at end of file |