/
default-toolbar.tsx
304 lines (274 loc) · 7.7 KB
/
default-toolbar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import * as React from 'react';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { Widget } from '@phosphor/widgets';
import { NotebookActions } from './actions';
import {
showDialog,
Dialog,
Toolbar,
ToolbarButtonComponent,
UseSignal,
addToolbarButtonClass,
ReactWidget,
ToolbarButton
} from '@jupyterlab/apputils';
import { nbformat } from '@jupyterlab/coreutils';
import { HTMLSelect } from '@jupyterlab/ui-components';
import { NotebookPanel } from './panel';
import { Notebook } from './widget';
/**
* The class name added to toolbar save button.
*/
const TOOLBAR_SAVE_CLASS = 'jp-SaveIcon';
/**
* The class name added to toolbar insert button.
*/
const TOOLBAR_INSERT_CLASS = 'jp-AddIcon';
/**
* The class name added to toolbar cut button.
*/
const TOOLBAR_CUT_CLASS = 'jp-CutIcon';
/**
* The class name added to toolbar copy button.
*/
const TOOLBAR_COPY_CLASS = 'jp-CopyIcon';
/**
* The class name added to toolbar paste button.
*/
const TOOLBAR_PASTE_CLASS = 'jp-PasteIcon';
/**
* The class name added to toolbar run button.
*/
const TOOLBAR_RUN_CLASS = 'jp-RunIcon';
/**
* The class name added to toolbar cell type dropdown wrapper.
*/
const TOOLBAR_CELLTYPE_CLASS = 'jp-Notebook-toolbarCellType';
/**
* The class name added to toolbar cell type dropdown.
*/
const TOOLBAR_CELLTYPE_DROPDOWN_CLASS = 'jp-Notebook-toolbarCellTypeDropdown';
/**
* A namespace for the default toolbar items.
*/
export namespace ToolbarItems {
/**
* Create save button toolbar item.
*/
export function createSaveButton(panel: NotebookPanel): Widget {
function onClick() {
if (panel.context.model.readOnly) {
return showDialog({
title: 'Cannot Save',
body: 'Document is read-only',
buttons: [Dialog.okButton()]
});
}
void panel.context.save().then(() => {
if (!panel.isDisposed) {
return panel.context.createCheckpoint();
}
});
}
return addToolbarButtonClass(
ReactWidget.create(
<UseSignal signal={panel.context.fileChanged}>
{() => (
<ToolbarButtonComponent
iconClassName={TOOLBAR_SAVE_CLASS}
onClick={onClick}
tooltip="Save the notebook contents and create checkpoint"
enabled={
!!(
panel &&
panel.context &&
panel.context.contentsModel &&
panel.context.contentsModel.writable
)
}
/>
)}
</UseSignal>
)
);
}
/**
* Create an insert toolbar item.
*/
export function createInsertButton(panel: NotebookPanel): Widget {
return new ToolbarButton({
iconClassName: TOOLBAR_INSERT_CLASS,
onClick: () => {
NotebookActions.insertBelow(panel.content);
},
tooltip: 'Insert a cell below'
});
}
/**
* Create a cut toolbar item.
*/
export function createCutButton(panel: NotebookPanel): Widget {
return new ToolbarButton({
iconClassName: TOOLBAR_CUT_CLASS,
onClick: () => {
NotebookActions.cut(panel.content);
},
tooltip: 'Cut the selected cells'
});
}
/**
* Create a copy toolbar item.
*/
export function createCopyButton(panel: NotebookPanel): Widget {
return new ToolbarButton({
iconClassName: TOOLBAR_COPY_CLASS,
onClick: () => {
NotebookActions.copy(panel.content);
},
tooltip: 'Copy the selected cells'
});
}
/**
* Create a paste toolbar item.
*/
export function createPasteButton(panel: NotebookPanel): Widget {
return new ToolbarButton({
iconClassName: TOOLBAR_PASTE_CLASS,
onClick: () => {
NotebookActions.paste(panel.content);
},
tooltip: 'Paste cells from the clipboard'
});
}
/**
* Create a run toolbar item.
*/
export function createRunButton(panel: NotebookPanel): Widget {
return new ToolbarButton({
iconClassName: TOOLBAR_RUN_CLASS,
onClick: () => {
void NotebookActions.runAndAdvance(panel.content, panel.session);
},
tooltip: 'Run the selected cells and advance'
});
}
/**
* Create a cell type switcher item.
*
* #### Notes
* It will display the type of the current active cell.
* If more than one cell is selected but are of different types,
* it will display `'-'`.
* When the user changes the cell type, it will change the
* cell types of the selected cells.
* It can handle a change to the context.
*/
export function createCellTypeItem(panel: NotebookPanel): Widget {
return new CellTypeSwitcher(panel.content);
}
/**
* Get the default toolbar items for panel
*/
export function getDefaultItems(
panel: NotebookPanel
): DocumentRegistry.IToolbarItem[] {
return [
{ name: 'save', widget: createSaveButton(panel) },
{ name: 'insert', widget: createInsertButton(panel) },
{ name: 'cut', widget: createCutButton(panel) },
{ name: 'copy', widget: createCopyButton(panel) },
{ name: 'paste', widget: createPasteButton(panel) },
{ name: 'run', widget: createRunButton(panel) },
{
name: 'interrupt',
widget: Toolbar.createInterruptButton(panel.session)
},
{
name: 'restart',
widget: Toolbar.createRestartButton(panel.session)
},
{ name: 'cellType', widget: createCellTypeItem(panel) },
{ name: 'spacer', widget: Toolbar.createSpacerItem() },
{
name: 'kernelName',
widget: Toolbar.createKernelNameItem(panel.session)
},
{
name: 'kernelStatus',
widget: Toolbar.createKernelStatusItem(panel.session)
}
];
}
}
/**
* A toolbar widget that switches cell types.
*/
export class CellTypeSwitcher extends ReactWidget {
/**
* Construct a new cell type switcher.
*/
constructor(widget: Notebook) {
super();
this.addClass(TOOLBAR_CELLTYPE_CLASS);
this._notebook = widget;
if (widget.model) {
this.update();
}
widget.activeCellChanged.connect(this.update, this);
// Follow a change in the selection.
widget.selectionChanged.connect(this.update, this);
}
/**
* Handle `change` events for the HTMLSelect component.
*/
handleChange = (event: React.ChangeEvent<HTMLSelectElement>): void => {
if (event.target.value !== '-') {
NotebookActions.changeCellType(this._notebook, event.target
.value as nbformat.CellType);
this._notebook.activate();
}
};
/**
* Handle `keydown` events for the HTMLSelect component.
*/
handleKeyDown = (event: React.KeyboardEvent): void => {
if (event.keyCode === 13) {
this._notebook.activate();
}
};
render() {
let value = '-';
if (this._notebook.activeCell) {
value = this._notebook.activeCell.model.type;
}
for (let widget of this._notebook.widgets) {
if (this._notebook.isSelectedOrActive(widget)) {
if (widget.model.type !== value) {
value = '-';
break;
}
}
}
return (
<HTMLSelect
className={TOOLBAR_CELLTYPE_DROPDOWN_CLASS}
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
value={value}
iconProps={{
icon: <span className="jp-MaterialIcon jp-DownCaretIcon bp3-icon" />
}}
aria-label="Cell type"
minimal
>
<option value="-">-</option>
<option value="code">Code</option>
<option value="markdown">Markdown</option>
<option value="raw">Raw</option>
</HTMLSelect>
);
}
private _notebook: Notebook = null;
}