@jupyterlab/cell-toolbar 4.0.0-alpha.6 → 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/celltoolbartracker.d.ts +15 -29
- package/lib/celltoolbartracker.js +199 -109
- package/lib/celltoolbartracker.js.map +1 -1
- package/lib/index.d.ts +1 -2
- package/lib/index.js +1 -2
- package/lib/index.js.map +1 -1
- package/package.json +19 -20
- package/src/celltoolbartracker.ts +479 -0
- package/src/index.ts +9 -0
- package/style/base.css +14 -8
- package/lib/cellmenu.d.ts +0 -20
- package/lib/cellmenu.js +0 -68
- package/lib/cellmenu.js.map +0 -1
- package/lib/celltoolbarwidget.d.ts +0 -9
- package/lib/celltoolbarwidget.js +0 -14
- package/lib/celltoolbarwidget.js.map +0 -1
- package/lib/tokens.d.ts +0 -25
- package/lib/tokens.js +0 -2
- package/lib/tokens.js.map +0 -1
- package/lib/toolbarbutton.d.ts +0 -56
- package/lib/toolbarbutton.js +0 -54
- package/lib/toolbarbutton.js.map +0 -1
- package/lib/utils.d.ts +0 -7
- package/lib/utils.js +0 -16
- package/lib/utils.js.map +0 -1
- package/style/icons/addabove.svg +0 -11
- package/style/icons/addbelow.svg +0 -11
- package/style/icons/delete.svg +0 -4
- package/style/icons/duplicate.svg +0 -4
- package/style/icons/movedown.svg +0 -3
- package/style/icons/moveup.svg +0 -3
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
/* -----------------------------------------------------------------------------
|
|
2
|
+
| Copyright (c) Jupyter Development Team.
|
|
3
|
+
| Distributed under the terms of the Modified BSD License.
|
|
4
|
+
|----------------------------------------------------------------------------*/
|
|
5
|
+
import { createDefaultFactory, ToolbarRegistry } from '@jupyterlab/apputils';
|
|
6
|
+
import {
|
|
7
|
+
Cell,
|
|
8
|
+
CellModel,
|
|
9
|
+
CodeCell,
|
|
10
|
+
ICellModel,
|
|
11
|
+
MarkdownCell
|
|
12
|
+
} from '@jupyterlab/cells';
|
|
13
|
+
import { DocumentRegistry } from '@jupyterlab/docregistry';
|
|
14
|
+
import { Notebook, NotebookPanel } from '@jupyterlab/notebook';
|
|
15
|
+
import { IObservableList, ObservableList } from '@jupyterlab/observables';
|
|
16
|
+
import { ReactWidget, Toolbar } from '@jupyterlab/ui-components';
|
|
17
|
+
import { some } from '@lumino/algorithm';
|
|
18
|
+
import { CommandRegistry } from '@lumino/commands';
|
|
19
|
+
import { IDisposable } from '@lumino/disposable';
|
|
20
|
+
import { Signal } from '@lumino/signaling';
|
|
21
|
+
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
22
|
+
import { IMapChange } from '@jupyter/ydoc';
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* Text mime types
|
|
26
|
+
*/
|
|
27
|
+
const TEXT_MIME_TYPES = [
|
|
28
|
+
'text/plain',
|
|
29
|
+
'application/vnd.jupyter.stdout',
|
|
30
|
+
'application/vnd.jupyter.stderr'
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Widget cell toolbar classes
|
|
35
|
+
*/
|
|
36
|
+
const CELL_TOOLBAR_CLASS = 'jp-cell-toolbar';
|
|
37
|
+
const CELL_MENU_CLASS = 'jp-cell-menu';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Class for a cell whose contents overlap with the cell toolbar
|
|
41
|
+
*/
|
|
42
|
+
const TOOLBAR_OVERLAP_CLASS = 'jp-toolbar-overlap';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Watch a notebook so that a cell toolbar appears on the active cell
|
|
46
|
+
*/
|
|
47
|
+
export class CellToolbarTracker implements IDisposable {
|
|
48
|
+
constructor(
|
|
49
|
+
panel: NotebookPanel,
|
|
50
|
+
toolbar: IObservableList<ToolbarRegistry.IToolbarItem>
|
|
51
|
+
) {
|
|
52
|
+
this._panel = panel;
|
|
53
|
+
this._previousActiveCell = this._panel.content.activeCell;
|
|
54
|
+
this._toolbar = toolbar;
|
|
55
|
+
|
|
56
|
+
this._onToolbarChanged();
|
|
57
|
+
this._toolbar.changed.connect(this._onToolbarChanged, this);
|
|
58
|
+
|
|
59
|
+
// Only add the toolbar to the notebook's active cell (if any) once it has fully rendered and been revealed.
|
|
60
|
+
void panel.revealed.then(() => {
|
|
61
|
+
// Wait one frame (at 60 fps) for the panel to render the first cell, then display the cell toolbar on it if possible.
|
|
62
|
+
setTimeout(() => {
|
|
63
|
+
this._onActiveCellChanged(panel.content);
|
|
64
|
+
}, 1000 / 60);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Check whether the toolbar should be rendered upon a layout change
|
|
68
|
+
panel.content.renderingLayoutChanged.connect(
|
|
69
|
+
this._onActiveCellChanged,
|
|
70
|
+
this
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// Handle subsequent changes of active cell.
|
|
74
|
+
panel.content.activeCellChanged.connect(this._onActiveCellChanged, this);
|
|
75
|
+
panel.content.activeCell?.model.metadataChanged.connect(
|
|
76
|
+
this._onMetadataChanged,
|
|
77
|
+
this
|
|
78
|
+
);
|
|
79
|
+
panel.disposed.connect(() => {
|
|
80
|
+
panel.content.activeCellChanged.disconnect(this._onActiveCellChanged);
|
|
81
|
+
panel.content.activeCell?.model.metadataChanged.disconnect(
|
|
82
|
+
this._onMetadataChanged
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_onMetadataChanged(model: CellModel, args: IMapChange) {
|
|
88
|
+
if (args.key === 'jupyter') {
|
|
89
|
+
if (
|
|
90
|
+
typeof args.newValue === 'object' &&
|
|
91
|
+
args.newValue.source_hidden === true &&
|
|
92
|
+
(args.type === 'add' || args.type === 'change')
|
|
93
|
+
) {
|
|
94
|
+
// Cell just became hidden; remove toolbar
|
|
95
|
+
this._removeToolbar(model);
|
|
96
|
+
}
|
|
97
|
+
// Check whether input visibility changed
|
|
98
|
+
else if (
|
|
99
|
+
typeof args.oldValue === 'object' &&
|
|
100
|
+
args.oldValue.source_hidden === true
|
|
101
|
+
) {
|
|
102
|
+
// Cell just became visible; add toolbar
|
|
103
|
+
this._addToolbar(model);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
_onActiveCellChanged(notebook: Notebook): void {
|
|
108
|
+
if (this._previousActiveCell && !this._previousActiveCell.isDisposed) {
|
|
109
|
+
// Disposed cells do not have a model anymore.
|
|
110
|
+
this._removeToolbar(this._previousActiveCell.model);
|
|
111
|
+
this._previousActiveCell.model.metadataChanged.disconnect(
|
|
112
|
+
this._onMetadataChanged
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const activeCell = notebook.activeCell;
|
|
117
|
+
if (activeCell === null || activeCell.inputHidden) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
activeCell.model.metadataChanged.connect(this._onMetadataChanged, this);
|
|
122
|
+
|
|
123
|
+
this._addToolbar(activeCell.model);
|
|
124
|
+
this._previousActiveCell = activeCell;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get isDisposed(): boolean {
|
|
128
|
+
return this._isDisposed;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
dispose(): void {
|
|
132
|
+
if (this.isDisposed) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this._isDisposed = true;
|
|
136
|
+
|
|
137
|
+
this._toolbar.changed.disconnect(this._onToolbarChanged, this);
|
|
138
|
+
|
|
139
|
+
const cells = this._panel?.context.model.cells;
|
|
140
|
+
if (cells) {
|
|
141
|
+
for (const model of cells) {
|
|
142
|
+
this._removeToolbar(model);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this._panel = null;
|
|
147
|
+
|
|
148
|
+
Signal.clearData(this);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private _addToolbar(model: ICellModel): void {
|
|
152
|
+
const cell = this._getCell(model);
|
|
153
|
+
|
|
154
|
+
if (cell) {
|
|
155
|
+
const toolbarWidget = new Toolbar();
|
|
156
|
+
toolbarWidget.addClass(CELL_MENU_CLASS);
|
|
157
|
+
|
|
158
|
+
const promises: Promise<void>[] = [];
|
|
159
|
+
for (const { name, widget } of this._toolbar) {
|
|
160
|
+
toolbarWidget.addItem(name, widget);
|
|
161
|
+
if (
|
|
162
|
+
widget instanceof ReactWidget &&
|
|
163
|
+
(widget as ReactWidget).renderPromise !== undefined
|
|
164
|
+
) {
|
|
165
|
+
(widget as ReactWidget).update();
|
|
166
|
+
promises.push((widget as ReactWidget).renderPromise!);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Wait for all the buttons to be rendered before attaching the toolbar.
|
|
171
|
+
Promise.all(promises)
|
|
172
|
+
.then(() => {
|
|
173
|
+
toolbarWidget.addClass(CELL_TOOLBAR_CLASS);
|
|
174
|
+
(cell.layout as PanelLayout).insertWidget(0, toolbarWidget);
|
|
175
|
+
|
|
176
|
+
// For rendered markdown, watch for resize events.
|
|
177
|
+
cell.displayChanged.connect(this._resizeEventCallback, this);
|
|
178
|
+
|
|
179
|
+
// Watch for changes in the cell's contents.
|
|
180
|
+
cell.model.contentChanged.connect(this._changedEventCallback, this);
|
|
181
|
+
|
|
182
|
+
// Hide the cell toolbar if it overlaps with cell contents
|
|
183
|
+
this._updateCellForToolbarOverlap(cell);
|
|
184
|
+
})
|
|
185
|
+
.catch(e => {
|
|
186
|
+
console.error('Error rendering buttons of the cell toolbar: ', e);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private _getCell(model: ICellModel): Cell | undefined {
|
|
192
|
+
return this._panel?.content.widgets.find(widget => widget.model === model);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private _findToolbarWidgets(cell: Cell): Widget[] {
|
|
196
|
+
const widgets = (cell.layout as PanelLayout).widgets;
|
|
197
|
+
|
|
198
|
+
// Search for header using the CSS class or use the first one if not found.
|
|
199
|
+
return widgets.filter(widget => widget.hasClass(CELL_TOOLBAR_CLASS)) || [];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private _removeToolbar(model: ICellModel): void {
|
|
203
|
+
const cell = this._getCell(model);
|
|
204
|
+
if (cell) {
|
|
205
|
+
this._findToolbarWidgets(cell).forEach(widget => {
|
|
206
|
+
widget.dispose();
|
|
207
|
+
});
|
|
208
|
+
// Attempt to remove the resize and changed event handlers.
|
|
209
|
+
cell.displayChanged.disconnect(this._resizeEventCallback, this);
|
|
210
|
+
}
|
|
211
|
+
model.contentChanged.disconnect(this._changedEventCallback, this);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Call back on settings changes
|
|
216
|
+
*/
|
|
217
|
+
private _onToolbarChanged(): void {
|
|
218
|
+
// Reset toolbar when settings changes
|
|
219
|
+
const activeCell: Cell<ICellModel> | null | undefined =
|
|
220
|
+
this._panel?.content.activeCell;
|
|
221
|
+
if (activeCell) {
|
|
222
|
+
this._removeToolbar(activeCell.model);
|
|
223
|
+
this._addToolbar(activeCell.model);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private _changedEventCallback(): void {
|
|
228
|
+
const activeCell = this._panel?.content.activeCell;
|
|
229
|
+
if (activeCell === null || activeCell === undefined) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
this._updateCellForToolbarOverlap(activeCell);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private _resizeEventCallback(): void {
|
|
237
|
+
const activeCell = this._panel?.content.activeCell;
|
|
238
|
+
if (activeCell === null || activeCell === undefined) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
this._updateCellForToolbarOverlap(activeCell);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private _updateCellForToolbarOverlap(activeCell: Cell<ICellModel>) {
|
|
246
|
+
// Remove the "toolbar overlap" class from the cell, rendering the cell's toolbar
|
|
247
|
+
const activeCellElement = activeCell.node;
|
|
248
|
+
activeCellElement.classList.remove(TOOLBAR_OVERLAP_CLASS);
|
|
249
|
+
|
|
250
|
+
if (this._cellToolbarOverlapsContents(activeCell)) {
|
|
251
|
+
// Add the "toolbar overlap" class to the cell, completely concealing the toolbar,
|
|
252
|
+
// if the first line of the content overlaps with it at all
|
|
253
|
+
activeCellElement.classList.add(TOOLBAR_OVERLAP_CLASS);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private _cellToolbarOverlapsContents(activeCell: Cell<ICellModel>): boolean {
|
|
258
|
+
const cellType = activeCell.model.type;
|
|
259
|
+
|
|
260
|
+
// If the toolbar is too large for the current cell, hide it.
|
|
261
|
+
const cellLeft = this._cellEditorWidgetLeft(activeCell);
|
|
262
|
+
const cellRight = this._cellEditorWidgetRight(activeCell);
|
|
263
|
+
const toolbarLeft = this._cellToolbarLeft(activeCell);
|
|
264
|
+
|
|
265
|
+
if (toolbarLeft === null) {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// The toolbar should not take up more than 50% of the cell.
|
|
270
|
+
if ((cellLeft + cellRight) / 2 > toolbarLeft) {
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (cellType === 'markdown' && (activeCell as MarkdownCell).rendered) {
|
|
275
|
+
// Check for overlap in rendered markdown content
|
|
276
|
+
return this._markdownOverlapsToolbar(activeCell as MarkdownCell);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Check for overlap in code content
|
|
280
|
+
if (this._panel?.content.renderingLayout === 'default') {
|
|
281
|
+
return this._codeOverlapsToolbar(activeCell);
|
|
282
|
+
} else {
|
|
283
|
+
return this._outputOverlapsToolbar(activeCell);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Check for overlap between rendered Markdown and the cell toolbar
|
|
289
|
+
*
|
|
290
|
+
* @param activeCell A rendered MarkdownCell
|
|
291
|
+
* @returns `true` if the first line of the output overlaps with the cell toolbar, `false` otherwise
|
|
292
|
+
*/
|
|
293
|
+
private _markdownOverlapsToolbar(activeCell: MarkdownCell): boolean {
|
|
294
|
+
const markdownOutput = activeCell.inputArea; // Rendered markdown appears in the input area
|
|
295
|
+
if (!markdownOutput) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Get the rendered markdown as a widget.
|
|
300
|
+
const markdownOutputWidget = markdownOutput.renderedInput;
|
|
301
|
+
const markdownOutputElement = markdownOutputWidget.node;
|
|
302
|
+
|
|
303
|
+
const firstOutputElementChild =
|
|
304
|
+
markdownOutputElement.firstElementChild as HTMLElement;
|
|
305
|
+
if (firstOutputElementChild === null) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Temporarily set the element's max width so that the bounding client rectangle only encompasses the content.
|
|
310
|
+
const oldMaxWidth = firstOutputElementChild.style.maxWidth;
|
|
311
|
+
firstOutputElementChild.style.maxWidth = 'max-content';
|
|
312
|
+
|
|
313
|
+
const lineRight = firstOutputElementChild.getBoundingClientRect().right;
|
|
314
|
+
|
|
315
|
+
// Reinstate the old max width.
|
|
316
|
+
firstOutputElementChild.style.maxWidth = oldMaxWidth;
|
|
317
|
+
|
|
318
|
+
const toolbarLeft = this._cellToolbarLeft(activeCell);
|
|
319
|
+
|
|
320
|
+
return toolbarLeft === null ? false : lineRight > toolbarLeft;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private _outputOverlapsToolbar(activeCell: Cell<ICellModel>): boolean {
|
|
324
|
+
const outputArea = (activeCell as CodeCell).outputArea.node;
|
|
325
|
+
if (outputArea) {
|
|
326
|
+
const outputs = outputArea.querySelectorAll('[data-mime-type]');
|
|
327
|
+
const toolbarRect = this._cellToolbarRect(activeCell);
|
|
328
|
+
if (toolbarRect) {
|
|
329
|
+
const { left: toolbarLeft, bottom: toolbarBottom } = toolbarRect;
|
|
330
|
+
return some(outputs, output => {
|
|
331
|
+
const node = output.firstElementChild;
|
|
332
|
+
if (node) {
|
|
333
|
+
const range = new Range();
|
|
334
|
+
if (
|
|
335
|
+
TEXT_MIME_TYPES.includes(
|
|
336
|
+
output.getAttribute('data-mime-type') || ''
|
|
337
|
+
)
|
|
338
|
+
) {
|
|
339
|
+
// If the node is plain text, it's in a <pre>. To get the true bounding box of the
|
|
340
|
+
// text, the node contents need to be selected.
|
|
341
|
+
range.selectNodeContents(node);
|
|
342
|
+
} else {
|
|
343
|
+
range.selectNode(node);
|
|
344
|
+
}
|
|
345
|
+
const { right: nodeRight, top: nodeTop } =
|
|
346
|
+
range.getBoundingClientRect();
|
|
347
|
+
|
|
348
|
+
// Note: y-coordinate increases toward the bottom of page
|
|
349
|
+
return nodeRight > toolbarLeft && nodeTop < toolbarBottom;
|
|
350
|
+
}
|
|
351
|
+
return false;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
private _codeOverlapsToolbar(activeCell: Cell<ICellModel>): boolean {
|
|
359
|
+
const editorWidget = activeCell.editorWidget;
|
|
360
|
+
const editor = activeCell.editor;
|
|
361
|
+
if (!editorWidget || !editor) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (editor.lineCount < 1) {
|
|
366
|
+
return false; // Nothing in the editor
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const codeMirrorLines = editorWidget.node.getElementsByClassName('cm-line');
|
|
370
|
+
if (codeMirrorLines.length < 1) {
|
|
371
|
+
return false; // No lines present
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
let lineRight = codeMirrorLines[0].getBoundingClientRect().left;
|
|
375
|
+
const range = document.createRange();
|
|
376
|
+
range.selectNodeContents(codeMirrorLines[0]);
|
|
377
|
+
lineRight += range.getBoundingClientRect().width;
|
|
378
|
+
|
|
379
|
+
const toolbarLeft = this._cellToolbarLeft(activeCell);
|
|
380
|
+
|
|
381
|
+
return toolbarLeft === null ? false : lineRight > toolbarLeft;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private _cellEditorWidgetLeft(activeCell: Cell<ICellModel>): number {
|
|
385
|
+
return activeCell.editorWidget?.node.getBoundingClientRect().left ?? 0;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private _cellEditorWidgetRight(activeCell: Cell<ICellModel>): number {
|
|
389
|
+
return activeCell.editorWidget?.node.getBoundingClientRect().right ?? 0;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private _cellToolbarRect(activeCell: Cell<ICellModel>): DOMRect | null {
|
|
393
|
+
const toolbarWidgets = this._findToolbarWidgets(activeCell);
|
|
394
|
+
if (toolbarWidgets.length < 1) {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
const activeCellToolbar = toolbarWidgets[0].node;
|
|
398
|
+
|
|
399
|
+
return activeCellToolbar.getBoundingClientRect();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private _cellToolbarLeft(activeCell: Cell<ICellModel>): number | null {
|
|
403
|
+
return this._cellToolbarRect(activeCell)?.left || null;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private _isDisposed = false;
|
|
407
|
+
private _panel: NotebookPanel | null;
|
|
408
|
+
private _previousActiveCell: Cell<ICellModel> | null;
|
|
409
|
+
private _toolbar: IObservableList<ToolbarRegistry.IToolbarItem>;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const defaultToolbarItems: ToolbarRegistry.IWidget[] = [
|
|
413
|
+
{
|
|
414
|
+
command: 'notebook:duplicate-below',
|
|
415
|
+
name: 'duplicate-cell'
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
command: 'notebook:move-cell-up',
|
|
419
|
+
name: 'move-cell-up'
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
command: 'notebook:move-cell-down',
|
|
423
|
+
name: 'move-cell-down'
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
command: 'notebook:insert-cell-above',
|
|
427
|
+
name: 'insert-cell-above'
|
|
428
|
+
},
|
|
429
|
+
{
|
|
430
|
+
command: 'notebook:insert-cell-below',
|
|
431
|
+
name: 'insert-cell-below'
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
command: 'notebook:delete-cell',
|
|
435
|
+
name: 'delete-cell'
|
|
436
|
+
}
|
|
437
|
+
];
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Widget extension that creates a CellToolbarTracker each time a notebook is
|
|
441
|
+
* created.
|
|
442
|
+
*/
|
|
443
|
+
export class CellBarExtension implements DocumentRegistry.WidgetExtension {
|
|
444
|
+
static FACTORY_NAME = 'Cell';
|
|
445
|
+
|
|
446
|
+
constructor(
|
|
447
|
+
commands: CommandRegistry,
|
|
448
|
+
toolbarFactory?: (
|
|
449
|
+
widget: Widget
|
|
450
|
+
) => IObservableList<ToolbarRegistry.IToolbarItem>
|
|
451
|
+
) {
|
|
452
|
+
this._commands = commands;
|
|
453
|
+
this._toolbarFactory = toolbarFactory ?? this.defaultToolbarFactory;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
protected get defaultToolbarFactory(): (
|
|
457
|
+
widget: Widget
|
|
458
|
+
) => IObservableList<ToolbarRegistry.IToolbarItem> {
|
|
459
|
+
const itemFactory = createDefaultFactory(this._commands);
|
|
460
|
+
return (widget: Widget) =>
|
|
461
|
+
new ObservableList({
|
|
462
|
+
values: defaultToolbarItems.map(item => {
|
|
463
|
+
return {
|
|
464
|
+
name: item.name,
|
|
465
|
+
widget: itemFactory(CellBarExtension.FACTORY_NAME, widget, item)
|
|
466
|
+
};
|
|
467
|
+
})
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
createNew(panel: NotebookPanel): IDisposable {
|
|
472
|
+
return new CellToolbarTracker(panel, this._toolbarFactory(panel));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
private _commands: CommandRegistry;
|
|
476
|
+
private _toolbarFactory: (
|
|
477
|
+
widget: Widget
|
|
478
|
+
) => IObservableList<ToolbarRegistry.IToolbarItem>;
|
|
479
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/* -----------------------------------------------------------------------------
|
|
2
|
+
| Copyright (c) Jupyter Development Team.
|
|
3
|
+
| Distributed under the terms of the Modified BSD License.
|
|
4
|
+
|----------------------------------------------------------------------------*/
|
|
5
|
+
/**
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module cell-toolbar
|
|
8
|
+
*/
|
|
9
|
+
export * from './celltoolbartracker';
|
package/style/base.css
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Jupyter Development Team.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
.jp-cell-button .jp-icon3[fill] {
|
|
2
7
|
fill: var(--jp-inverse-layout-color4);
|
|
3
8
|
}
|
|
@@ -15,20 +20,21 @@
|
|
|
15
20
|
flex-direction: row;
|
|
16
21
|
padding: 2px 0;
|
|
17
22
|
min-height: 25px;
|
|
18
|
-
z-index:
|
|
23
|
+
z-index: 6;
|
|
19
24
|
position: absolute;
|
|
20
25
|
top: 5px;
|
|
21
26
|
right: 8px;
|
|
27
|
+
|
|
28
|
+
/* Override .jp-Toolbar */
|
|
29
|
+
background-color: inherit;
|
|
30
|
+
border-bottom: inherit;
|
|
31
|
+
box-shadow: inherit;
|
|
22
32
|
}
|
|
23
33
|
|
|
24
|
-
/* Overrides for mobile view
|
|
34
|
+
/* Overrides for mobile view hiding cell toolbar */
|
|
25
35
|
@media only screen and (max-width: 760px) {
|
|
26
|
-
.jp-
|
|
27
|
-
display:
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
.jp-cell-toolbar {
|
|
31
|
-
top: -5px;
|
|
36
|
+
.jp-cell-menu.jp-cell-toolbar {
|
|
37
|
+
display: none;
|
|
32
38
|
}
|
|
33
39
|
}
|
|
34
40
|
|
package/lib/cellmenu.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { CommandRegistry } from '@lumino/commands';
|
|
2
|
-
import { Widget } from '@lumino/widgets';
|
|
3
|
-
import { ICellMenuItem } from './tokens';
|
|
4
|
-
/**
|
|
5
|
-
* Toolbar icon menu container
|
|
6
|
-
*/
|
|
7
|
-
export declare class CellMenu extends Widget {
|
|
8
|
-
constructor(commands: CommandRegistry, items: ICellMenuItem[]);
|
|
9
|
-
handleEvent(event: Event): void;
|
|
10
|
-
/**
|
|
11
|
-
* Handle `after-attach` messages for the widget.
|
|
12
|
-
*/
|
|
13
|
-
onAfterAttach(): void;
|
|
14
|
-
/**
|
|
15
|
-
* Handle `before-detach` messages for the widget.
|
|
16
|
-
*/
|
|
17
|
-
onBeforeDetach(): void;
|
|
18
|
-
protected _addButtons(items: ICellMenuItem[]): void;
|
|
19
|
-
private _commands;
|
|
20
|
-
}
|
package/lib/cellmenu.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/* -----------------------------------------------------------------------------
|
|
2
|
-
| Copyright (c) Jupyter Development Team.
|
|
3
|
-
| Distributed under the terms of the Modified BSD License.
|
|
4
|
-
|----------------------------------------------------------------------------*/
|
|
5
|
-
import { ToolbarButton } from '@jupyterlab/apputils';
|
|
6
|
-
import { LabIcon } from '@jupyterlab/ui-components';
|
|
7
|
-
import { each } from '@lumino/algorithm';
|
|
8
|
-
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
9
|
-
const CELL_MENU_CLASS = 'jp-cell-menu';
|
|
10
|
-
/**
|
|
11
|
-
* Toolbar icon menu container
|
|
12
|
-
*/
|
|
13
|
-
export class CellMenu extends Widget {
|
|
14
|
-
constructor(commands, items) {
|
|
15
|
-
super();
|
|
16
|
-
this._commands = commands;
|
|
17
|
-
this.layout = new PanelLayout();
|
|
18
|
-
this.addClass(CELL_MENU_CLASS);
|
|
19
|
-
this._addButtons(items);
|
|
20
|
-
}
|
|
21
|
-
handleEvent(event) {
|
|
22
|
-
switch (event.type) {
|
|
23
|
-
case 'mousedown':
|
|
24
|
-
case 'click':
|
|
25
|
-
// Ensure the mouse event is not propagated on the cell.
|
|
26
|
-
event.stopPropagation();
|
|
27
|
-
break;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Handle `after-attach` messages for the widget.
|
|
32
|
-
*/
|
|
33
|
-
onAfterAttach() {
|
|
34
|
-
this.node.addEventListener('mousedown', this);
|
|
35
|
-
this.node.addEventListener('click', this);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Handle `before-detach` messages for the widget.
|
|
39
|
-
*/
|
|
40
|
-
onBeforeDetach() {
|
|
41
|
-
this.node.removeEventListener('mousedown', this);
|
|
42
|
-
this.node.removeEventListener('click', this);
|
|
43
|
-
}
|
|
44
|
-
_addButtons(items) {
|
|
45
|
-
each(this.children(), widget => {
|
|
46
|
-
widget.dispose();
|
|
47
|
-
});
|
|
48
|
-
const layout = this.layout;
|
|
49
|
-
items.forEach(entry => {
|
|
50
|
-
var _a, _b;
|
|
51
|
-
if (this._commands.hasCommand(entry.command)) {
|
|
52
|
-
layout.addWidget(new ToolbarButton({
|
|
53
|
-
icon: LabIcon.resolve({ icon: entry.icon }),
|
|
54
|
-
className: `jp-cell-${(_a = entry.cellType) !== null && _a !== void 0 ? _a : 'all'}`,
|
|
55
|
-
onClickWithEvent: (e) => {
|
|
56
|
-
// Prevent propagation of this event in case the command causes the
|
|
57
|
-
// current cell to move. If the cell moves and the event propagates to the
|
|
58
|
-
// new cell, this may have an effect on the new cell.
|
|
59
|
-
void this._commands.execute(entry.command);
|
|
60
|
-
e.stopPropagation();
|
|
61
|
-
},
|
|
62
|
-
tooltip: (_b = entry.tooltip) !== null && _b !== void 0 ? _b : this._commands.label(entry.command)
|
|
63
|
-
}));
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
//# sourceMappingURL=cellmenu.js.map
|
package/lib/cellmenu.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cellmenu.js","sourceRoot":"","sources":["../src/cellmenu.ts"],"names":[],"mappings":"AAAA;;;+EAG+E;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGtD,MAAM,eAAe,GAAG,cAAc,CAAC;AAEvC;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,MAAM;IAClC,YAAY,QAAyB,EAAE,KAAsB;QAC3D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QAChC,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;QAC/B,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED,WAAW,CAAC,KAAY;QACtB,QAAQ,KAAK,CAAC,IAAI,EAAE;YAClB,KAAK,WAAW,CAAC;YACjB,KAAK,OAAO;gBACV,wDAAwD;gBACxD,KAAK,CAAC,eAAe,EAAE,CAAC;gBACxB,MAAM;SACT;IACH,CAAC;IAED;;OAEG;IACH,aAAa;QACX,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACH,cAAc;QACZ,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC;IAES,WAAW,CAAC,KAAsB;QAC1C,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,MAAM,CAAC,EAAE;YAC7B,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,CAAC,MAAqB,CAAC;QAC1C,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;;YACpB,IAAI,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;gBAC5C,MAAM,CAAC,SAAS,CACd,IAAI,aAAa,CAAC;oBAChB,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;oBAC3C,SAAS,EAAE,WAAW,MAAA,KAAK,CAAC,QAAQ,mCAAI,KAAK,EAAE;oBAC/C,gBAAgB,EAAE,CAAC,CAAQ,EAAQ,EAAE;wBACnC,mEAAmE;wBACnE,0EAA0E;wBAC1E,qDAAqD;wBACrD,KAAK,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;wBAC3C,CAAC,CAAC,eAAe,EAAE,CAAC;oBACtB,CAAC;oBACD,OAAO,EAAE,MAAA,KAAK,CAAC,OAAO,mCAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC;iBAC9D,CAAC,CACH,CAAC;aACH;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CAGF"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { CommandRegistry } from '@lumino/commands';
|
|
2
|
-
import { Widget } from '@lumino/widgets';
|
|
3
|
-
import { ICellMenuItem } from './tokens';
|
|
4
|
-
/**
|
|
5
|
-
* Cell Toolbar Widget
|
|
6
|
-
*/
|
|
7
|
-
export declare class CellToolbarWidget extends Widget {
|
|
8
|
-
constructor(commands: CommandRegistry, menuItems: ICellMenuItem[]);
|
|
9
|
-
}
|
package/lib/celltoolbarwidget.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
2
|
-
import { CellMenu } from './cellmenu';
|
|
3
|
-
/**
|
|
4
|
-
* Cell Toolbar Widget
|
|
5
|
-
*/
|
|
6
|
-
export class CellToolbarWidget extends Widget {
|
|
7
|
-
constructor(commands, menuItems) {
|
|
8
|
-
super();
|
|
9
|
-
this.layout = new PanelLayout();
|
|
10
|
-
this.addClass('jp-cell-toolbar');
|
|
11
|
-
this.layout.addWidget(new CellMenu(commands, menuItems));
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=celltoolbarwidget.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"celltoolbarwidget.js","sourceRoot":"","sources":["../src/celltoolbarwidget.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,MAAM;IAC3C,YAAY,QAAyB,EAAE,SAA0B;QAC/D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QAChC,IAAI,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC;QAEhC,IAAI,CAAC,MAAsB,CAAC,SAAS,CAAC,IAAI,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;IAC5E,CAAC;CACF"}
|
package/lib/tokens.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { LabIcon } from '@jupyterlab/ui-components';
|
|
2
|
-
export declare const EXTENSION_ID = "@jupyterlab/cell-toolbar";
|
|
3
|
-
/**
|
|
4
|
-
* Menu action interface
|
|
5
|
-
*/
|
|
6
|
-
export interface ICellMenuItem {
|
|
7
|
-
/**
|
|
8
|
-
* Command to be triggered
|
|
9
|
-
*/
|
|
10
|
-
command: string;
|
|
11
|
-
/**
|
|
12
|
-
* Icon for the item
|
|
13
|
-
*/
|
|
14
|
-
icon: LabIcon | string;
|
|
15
|
-
/**
|
|
16
|
-
* Icon tooltip
|
|
17
|
-
*/
|
|
18
|
-
tooltip?: string;
|
|
19
|
-
/**
|
|
20
|
-
* Type of cell it applies on
|
|
21
|
-
*
|
|
22
|
-
* Undefined if it applies on all cell types
|
|
23
|
-
*/
|
|
24
|
-
cellType?: 'code' | 'markdown' | 'raw';
|
|
25
|
-
}
|
package/lib/tokens.js
DELETED
package/lib/tokens.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,YAAY,GAAG,0BAA0B,CAAC"}
|