forked from jupyterlab/jupyterlab
/
actions.tsx
1740 lines (1518 loc) · 46.4 KB
/
actions.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { KernelMessage } from '@jupyterlab/services';
import {
IClientSession,
Clipboard,
Dialog,
showDialog
} from '@jupyterlab/apputils';
import { nbformat } from '@jupyterlab/coreutils';
import {
ICellModel,
ICodeCellModel,
CodeCell,
Cell,
MarkdownCell
} from '@jupyterlab/cells';
import { ArrayExt, each, toArray } from '@phosphor/algorithm';
import { JSONObject } from '@phosphor/coreutils';
import { ElementExt } from '@phosphor/domutils';
import { ISignal, Signal } from '@phosphor/signaling';
import * as React from 'react';
import { INotebookModel } from './model';
import { Notebook } from './widget';
// The message to display to the user when prompting to trust the notebook.
const TRUST_MESSAGE = (
<p>
A trusted Jupyter notebook may execute hidden malicious code when you open
it.
<br />
Selecting trust will re-render this notebook in a trusted state.
<br />
For more information, see the
<a href="https://jupyter-notebook.readthedocs.io/en/stable/security.html">
Jupyter security documentation
</a>
</p>
);
/**
* The mimetype used for Jupyter cell data.
*/
const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
/**
* A collection of actions that run against notebooks.
*
* #### Notes
* All of the actions are a no-op if there is no model on the notebook.
* The actions set the widget `mode` to `'command'` unless otherwise specified.
* The actions will preserve the selection on the notebook widget unless
* otherwise specified.
*/
export class NotebookActions {
/**
* A signal that emits whenever a cell is run.
*/
static get executed(): ISignal<any, { notebook: Notebook; cell: Cell }> {
return Private.executed;
}
/**
* A private constructor for the `NotebookActions` class.
*
* #### Notes
* This class can never be instantiated. Its static member `executed` will be
* merged with the `NotebookActions` namespace. The reason it exists as a
* standalone class is because at run time, the `Private.executed` variable
* does not yet exist, so it needs to be referenced via a getter.
*/
private constructor() {}
}
/**
* A namespace for `NotebookActions` static methods.
*/
export namespace NotebookActions {
/**
* Split the active cell into two cells.
*
* @param widget - The target notebook widget.
*
* #### Notes
* It will preserve the existing mode.
* The second cell will be activated.
* The existing selection will be cleared.
* The leading whitespace in the second cell will be removed.
* If there is no content, two empty cells will be created.
* Both cells will have the same type as the original cell.
* This action can be undone.
*/
export function splitCell(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
notebook.deselectAll();
const nbModel = notebook.model;
const index = notebook.activeCellIndex;
const child = notebook.widgets[index];
const editor = child.editor;
const position = editor.getCursorPosition();
const offset = editor.getOffsetAt(position);
const orig = child.model.value.text;
// Create new models to preserve history.
const clone0 = Private.cloneCell(nbModel, child.model);
const clone1 = Private.cloneCell(nbModel, child.model);
if (clone0.type === 'code') {
(clone0 as ICodeCellModel).outputs.clear();
}
clone0.value.text = orig
.slice(0, offset)
.replace(/^\n+/, '')
.replace(/\n+$/, '');
clone1.value.text = orig
.slice(offset)
.replace(/^\n+/, '')
.replace(/\n+$/, '');
// Make the changes while preserving history.
const cells = nbModel.cells;
cells.beginCompoundOperation();
cells.set(index, clone0);
cells.insert(index + 1, clone1);
cells.endCompoundOperation();
notebook.activeCellIndex++;
Private.handleState(notebook, state);
}
/**
* Merge the selected cells.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The widget mode will be preserved.
* If only one cell is selected, the next cell will be selected.
* If the active cell is a code cell, its outputs will be cleared.
* This action can be undone.
* The final cell will have the same type as the active cell.
* If the active cell is a markdown cell, it will be unrendered.
*/
export function mergeCells(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
const toMerge: string[] = [];
const toDelete: ICellModel[] = [];
const model = notebook.model;
const cells = model.cells;
const primary = notebook.activeCell;
const active = notebook.activeCellIndex;
// Get the cells to merge.
notebook.widgets.forEach((child, index) => {
if (notebook.isSelectedOrActive(child)) {
toMerge.push(child.model.value.text);
if (index !== active) {
toDelete.push(child.model);
}
}
});
// Check for only a single cell selected.
if (toMerge.length === 1) {
// Bail if it is the last cell.
if (active === cells.length - 1) {
return;
}
// Otherwise merge with the next cell.
const cellModel = cells.get(active + 1);
toMerge.push(cellModel.value.text);
toDelete.push(cellModel);
}
notebook.deselectAll();
// Create a new cell for the source to preserve history.
const newModel = Private.cloneCell(model, primary.model);
newModel.value.text = toMerge.join('\n\n');
if (newModel.type === 'code') {
(newModel as ICodeCellModel).outputs.clear();
}
// Make the changes while preserving history.
cells.beginCompoundOperation();
cells.set(active, newModel);
toDelete.forEach(cell => {
cells.removeValue(cell);
});
cells.endCompoundOperation();
// If the original cell is a markdown cell, make sure
// the new cell is unrendered.
if (primary instanceof MarkdownCell) {
(notebook.activeCell as MarkdownCell).rendered = false;
}
Private.handleState(notebook, state);
}
/**
* Delete the selected cells.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The cell after the last selected cell will be activated.
* It will add a code cell if all cells are deleted.
* This action can be undone.
*/
export function deleteCells(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
Private.deleteCells(notebook);
Private.handleState(notebook, state);
}
/**
* Insert a new code cell above the active cell.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The widget mode will be preserved.
* This action can be undone.
* The existing selection will be cleared.
* The new cell will the active cell.
*/
export function insertAbove(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
const model = notebook.model;
const cell = model.contentFactory.createCell(
notebook.notebookConfig.defaultCell,
{}
);
const active = notebook.activeCellIndex;
model.cells.insert(active, cell);
// Make the newly inserted cell active.
notebook.activeCellIndex = active;
notebook.deselectAll();
Private.handleState(notebook, state, true);
}
/**
* Insert a new code cell below the active cell.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The widget mode will be preserved.
* This action can be undone.
* The existing selection will be cleared.
* The new cell will be the active cell.
*/
export function insertBelow(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
const model = notebook.model;
const cell = model.contentFactory.createCell(
notebook.notebookConfig.defaultCell,
{}
);
model.cells.insert(notebook.activeCellIndex + 1, cell);
// Make the newly inserted cell active.
notebook.activeCellIndex++;
notebook.deselectAll();
Private.handleState(notebook, state, true);
}
/**
* Move the selected cell(s) down.
*
* @param notebook = The target notebook widget.
*/
export function moveDown(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
const cells = notebook.model.cells;
const widgets = notebook.widgets;
cells.beginCompoundOperation();
for (let i = cells.length - 2; i > -1; i--) {
if (notebook.isSelectedOrActive(widgets[i])) {
if (!notebook.isSelectedOrActive(widgets[i + 1])) {
cells.move(i, i + 1);
if (notebook.activeCellIndex === i) {
notebook.activeCellIndex++;
}
notebook.select(widgets[i + 1]);
notebook.deselect(widgets[i]);
}
}
}
cells.endCompoundOperation();
Private.handleState(notebook, state, true);
}
/**
* Move the selected cell(s) up.
*
* @param widget - The target notebook widget.
*/
export function moveUp(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
const cells = notebook.model.cells;
const widgets = notebook.widgets;
cells.beginCompoundOperation();
for (let i = 1; i < cells.length; i++) {
if (notebook.isSelectedOrActive(widgets[i])) {
if (!notebook.isSelectedOrActive(widgets[i - 1])) {
cells.move(i, i - 1);
if (notebook.activeCellIndex === i) {
notebook.activeCellIndex--;
}
notebook.select(widgets[i - 1]);
notebook.deselect(widgets[i]);
}
}
}
cells.endCompoundOperation();
Private.handleState(notebook, state, true);
}
/**
* Change the selected cell type(s).
*
* @param notebook - The target notebook widget.
*
* @param value - The target cell type.
*
* #### Notes
* It should preserve the widget mode.
* This action can be undone.
* The existing selection will be cleared.
* Any cells converted to markdown will be unrendered.
*/
export function changeCellType(
notebook: Notebook,
value: nbformat.CellType
): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
Private.changeCellType(notebook, value);
Private.handleState(notebook, state);
}
/**
* Run the selected cell(s).
*
* @param notebook - The target notebook widget.
*
* @param session - The optional client session object.
*
* #### Notes
* The last selected cell will be activated, but not scrolled into view.
* The existing selection will be cleared.
* An execution error will prevent the remaining code cells from executing.
* All markdown cells will be rendered.
*/
export function run(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const state = Private.getState(notebook);
const promise = Private.runSelected(notebook, session);
Private.handleRunState(notebook, state, false);
return promise;
}
/**
* Run the selected cell(s) and advance to the next cell.
*
* @param notebook - The target notebook widget.
*
* @param session - The optional client session object.
*
* #### Notes
* The existing selection will be cleared.
* The cell after the last selected cell will be activated and scrolled into view.
* An execution error will prevent the remaining code cells from executing.
* All markdown cells will be rendered.
* If the last selected cell is the last cell, a new code cell
* will be created in `'edit'` mode. The new cell creation can be undone.
*/
export function runAndAdvance(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const state = Private.getState(notebook);
const promise = Private.runSelected(notebook, session);
const model = notebook.model;
if (notebook.activeCellIndex === notebook.widgets.length - 1) {
const cell = model.contentFactory.createCell(
notebook.notebookConfig.defaultCell,
{}
);
model.cells.push(cell);
notebook.activeCellIndex++;
notebook.mode = 'edit';
} else {
notebook.activeCellIndex++;
}
Private.handleRunState(notebook, state, true);
return promise;
}
/**
* Run the selected cell(s) and insert a new code cell.
*
* @param notebook - The target notebook widget.
*
* @param session - The optional client session object.
*
* #### Notes
* An execution error will prevent the remaining code cells from executing.
* All markdown cells will be rendered.
* The widget mode will be set to `'edit'` after running.
* The existing selection will be cleared.
* The cell insert can be undone.
* The new cell will be scrolled into view.
*/
export function runAndInsert(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const state = Private.getState(notebook);
const promise = Private.runSelected(notebook, session);
const model = notebook.model;
const cell = model.contentFactory.createCell(
notebook.notebookConfig.defaultCell,
{}
);
model.cells.insert(notebook.activeCellIndex + 1, cell);
notebook.activeCellIndex++;
notebook.mode = 'edit';
Private.handleRunState(notebook, state, true);
return promise;
}
/**
* Run all of the cells in the notebook.
*
* @param notebook - The target notebook widget.
*
* @param session - The optional client session object.
*
* #### Notes
* The existing selection will be cleared.
* An execution error will prevent the remaining code cells from executing.
* All markdown cells will be rendered.
* The last cell in the notebook will be activated and scrolled into view.
*/
export function runAll(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const state = Private.getState(notebook);
notebook.widgets.forEach(child => {
notebook.select(child);
});
const promise = Private.runSelected(notebook, session);
Private.handleRunState(notebook, state, true);
return promise;
}
export function renderAllMarkdown(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const previousIndex = notebook.activeCellIndex;
const state = Private.getState(notebook);
notebook.widgets.forEach((child, index) => {
if (child.model.type === 'markdown') {
notebook.select(child);
// This is to make sure that the activeCell
// does not get executed
notebook.activeCellIndex = index;
}
});
if (notebook.activeCell.model.type !== 'markdown') {
return Promise.resolve(true);
}
const promise = Private.runSelected(notebook, session);
notebook.activeCellIndex = previousIndex;
Private.handleRunState(notebook, state, true);
return promise;
}
/**
* Run all of the cells before the currently active cell (exclusive).
*
* @param notebook - The target notebook widget.
*
* @param session - The optional client session object.
*
* #### Notes
* The existing selection will be cleared.
* An execution error will prevent the remaining code cells from executing.
* All markdown cells will be rendered.
* The currently active cell will remain selected.
*/
export function runAllAbove(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
const { activeCell, activeCellIndex, model } = notebook;
if (!model || !activeCell || activeCellIndex < 1) {
return Promise.resolve(false);
}
const state = Private.getState(notebook);
notebook.activeCellIndex--;
notebook.deselectAll();
for (let i = 0; i < notebook.activeCellIndex; ++i) {
notebook.select(notebook.widgets[i]);
}
const promise = Private.runSelected(notebook, session);
notebook.activeCellIndex++;
Private.handleRunState(notebook, state, true);
return promise;
}
/**
* Run all of the cells after the currently active cell (inclusive).
*
* @param notebook - The target notebook widget.
*
* @param session - The optional client session object.
*
* #### Notes
* The existing selection will be cleared.
* An execution error will prevent the remaining code cells from executing.
* All markdown cells will be rendered.
* The last cell in the notebook will be activated and scrolled into view.
*/
export function runAllBelow(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const state = Private.getState(notebook);
notebook.deselectAll();
for (let i = notebook.activeCellIndex; i < notebook.widgets.length; ++i) {
notebook.select(notebook.widgets[i]);
}
const promise = Private.runSelected(notebook, session);
Private.handleRunState(notebook, state, true);
return promise;
}
/**
* Select the above the active cell.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The widget mode will be preserved.
* This is a no-op if the first cell is the active cell.
* This will skip any collapsed cells.
* The existing selection will be cleared.
*/
export function selectAbove(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
if (notebook.activeCellIndex === 0) {
return;
}
let possibleNextCell = notebook.activeCellIndex - 1;
// find first non hidden cell above current cell
if (notebook.mode === 'edit') {
while (notebook.widgets[possibleNextCell].inputHidden) {
// If we are at the top cell, we cannot change selection.
if (possibleNextCell === 0) {
return;
}
possibleNextCell -= 1;
}
}
const state = Private.getState(notebook);
notebook.activeCellIndex = possibleNextCell;
notebook.deselectAll();
Private.handleState(notebook, state, true);
}
/**
* Select the cell below the active cell.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The widget mode will be preserved.
* This is a no-op if the last cell is the active cell.
* This will skip any collapsed cells.
* The existing selection will be cleared.
*/
export function selectBelow(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const maxCellIndex = notebook.widgets.length - 1;
if (notebook.activeCellIndex === maxCellIndex) {
return;
}
let possibleNextCell = notebook.activeCellIndex + 1;
// find first non hidden cell below current cell
if (notebook.mode === 'edit') {
while (notebook.widgets[possibleNextCell].inputHidden) {
// If we are at the bottom cell, we cannot change selection.
if (possibleNextCell === maxCellIndex) {
return;
}
possibleNextCell += 1;
}
}
const state = Private.getState(notebook);
notebook.activeCellIndex = possibleNextCell;
notebook.deselectAll();
Private.handleState(notebook, state, true);
}
/**
* Extend the selection to the cell above.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* This is a no-op if the first cell is the active cell.
* The new cell will be activated.
*/
export function extendSelectionAbove(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
// Do not wrap around.
if (notebook.activeCellIndex === 0) {
return;
}
const state = Private.getState(notebook);
notebook.mode = 'command';
notebook.extendContiguousSelectionTo(notebook.activeCellIndex - 1);
Private.handleState(notebook, state, true);
}
/**
* Extend the selection to the cell below.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* This is a no-op if the last cell is the active cell.
* The new cell will be activated.
*/
export function extendSelectionBelow(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
// Do not wrap around.
if (notebook.activeCellIndex === notebook.widgets.length - 1) {
return;
}
const state = Private.getState(notebook);
notebook.mode = 'command';
notebook.extendContiguousSelectionTo(notebook.activeCellIndex + 1);
Private.handleState(notebook, state, true);
}
/**
* Select all of the cells of the notebook.
*
* @param notebook - the target notebook widget.
*/
export function selectAll(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
notebook.widgets.forEach(child => {
notebook.select(child);
});
}
/**
* Deselect all of the cells of the notebook.
*
* @param notebook - the targe notebook widget.
*/
export function deselectAll(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
notebook.deselectAll();
}
/**
* Copy the selected cell data to a clipboard.
*
* @param notebook - The target notebook widget.
*/
export function copy(notebook: Notebook): void {
Private.copyOrCut(notebook, false);
}
/**
* Cut the selected cell data to a clipboard.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* This action can be undone.
* A new code cell is added if all cells are cut.
*/
export function cut(notebook: Notebook): void {
Private.copyOrCut(notebook, true);
}
/**
* Paste cells from the application clipboard.
*
* @param notebook - The target notebook widget.
*
* @param mode - the mode of the paste operation: 'below' pastes cells
* below the active cell, 'above' pastes cells above the active cell,
* and 'replace' removes the currently selected cells and pastes cells
* in their place.
*
* #### Notes
* The last pasted cell becomes the active cell.
* This is a no-op if there is no cell data on the clipboard.
* This action can be undone.
*/
export function paste(
notebook: Notebook,
mode: 'below' | 'above' | 'replace' = 'below'
): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const clipboard = Clipboard.getInstance();
if (!clipboard.hasData(JUPYTER_CELL_MIME)) {
return;
}
const state = Private.getState(notebook);
const values = clipboard.getData(JUPYTER_CELL_MIME) as nbformat.IBaseCell[];
const model = notebook.model;
notebook.mode = 'command';
const newCells = values.map(cell => {
switch (cell.cell_type) {
case 'code':
return model.contentFactory.createCodeCell({ cell });
case 'markdown':
return model.contentFactory.createMarkdownCell({ cell });
default:
return model.contentFactory.createRawCell({ cell });
}
});
const cells = notebook.model.cells;
let index: number;
cells.beginCompoundOperation();
// Set the starting index of the paste operation depending upon the mode.
switch (mode) {
case 'below':
index = notebook.activeCellIndex;
break;
case 'above':
index = notebook.activeCellIndex - 1;
break;
case 'replace':
// Find the cells to delete.
const toDelete: number[] = [];
notebook.widgets.forEach((child, index) => {
const deletable = child.model.metadata.get('deletable') !== false;
if (notebook.isSelectedOrActive(child) && deletable) {
toDelete.push(index);
}
});
// If cells are not deletable, we may not have anything to delete.
if (toDelete.length > 0) {
// Delete the cells as one undo event.
toDelete.reverse().forEach(i => {
cells.remove(i);
});
}
index = toDelete[0];
break;
default:
break;
}
newCells.forEach(cell => {
cells.insert(++index, cell);
});
cells.endCompoundOperation();
notebook.activeCellIndex += newCells.length;
notebook.deselectAll();
Private.handleState(notebook, state);
}
/**
* Undo a cell action.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* This is a no-op if if there are no cell actions to undo.
*/
export function undo(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
notebook.mode = 'command';
notebook.model.cells.undo();
notebook.deselectAll();
Private.handleState(notebook, state);
}
/**
* Redo a cell action.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* This is a no-op if there are no cell actions to redo.
*/
export function redo(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
notebook.mode = 'command';
notebook.model.cells.redo();
notebook.deselectAll();
Private.handleState(notebook, state);
}
/**
* Toggle the line number of all cells.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The original state is based on the state of the active cell.
* The `mode` of the widget will be preserved.
*/
export function toggleAllLineNumbers(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
const config = notebook.editorConfig;
const lineNumbers = !(
config.code.lineNumbers &&
config.markdown.lineNumbers &&
config.raw.lineNumbers
);
const newConfig = {
code: { ...config.code, lineNumbers },
markdown: { ...config.markdown, lineNumbers },
raw: { ...config.raw, lineNumbers }
};
notebook.editorConfig = newConfig;
Private.handleState(notebook, state);
}
/**
* Clear the code outputs of the selected cells.
*
* @param notebook - The target notebook widget.
*
* #### Notes
* The widget `mode` will be preserved.
*/
export function clearOutputs(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
const state = Private.getState(notebook);
each(notebook.model.cells, (cell: ICodeCellModel, index) => {
const child = notebook.widgets[index];