1 |
|
2 |
|
3 | import { addToolbarButtonClass, Dialog, ReactWidget, sessionContextDialogs, showDialog, Toolbar, ToolbarButton, ToolbarButtonComponent, UseSignal } from '@jupyterlab/apputils';
|
4 | import { nullTranslator } from '@jupyterlab/translation';
|
5 | import { addIcon, copyIcon, cutIcon, fastForwardIcon, HTMLSelect, pasteIcon, runIcon, saveIcon } 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({ 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 |
|
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, 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 |
|
136 |
|
137 |
|
138 |
|
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 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function createCellTypeItem(panel, translator) {
|
170 | return new CellTypeSwitcher(panel.content, translator);
|
171 | }
|
172 | ToolbarItems.createCellTypeItem = createCellTypeItem;
|
173 | |
174 |
|
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 |
|
208 |
|
209 | export class CellTypeSwitcher extends ReactWidget {
|
210 | |
211 |
|
212 |
|
213 | constructor(widget, translator) {
|
214 | super();
|
215 | |
216 |
|
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 |
|
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 |
|
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 |
|
\ | No newline at end of file |