Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/common/model/cellEdit.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IResourceUndoRedoElement, UndoRedoElementType } from '../../../../../platform/undoRedo/common/undoRedo.js';
7
import { URI } from '../../../../../base/common/uri.js';
8
import { NotebookCellTextModel } from './notebookCellTextModel.js';
9
import { ISelectionState, NotebookCellMetadata } from '../notebookCommon.js';
10
11
/**
12
* It should not modify Undo/Redo stack
13
*/
14
export interface ITextCellEditingDelegate {
15
insertCell?(index: number, cell: NotebookCellTextModel, endSelections?: ISelectionState): void;
16
deleteCell?(index: number, endSelections?: ISelectionState): void;
17
replaceCell?(index: number, count: number, cells: NotebookCellTextModel[], endSelections?: ISelectionState): void;
18
moveCell?(fromIndex: number, length: number, toIndex: number, beforeSelections: ISelectionState | undefined, endSelections?: ISelectionState): void;
19
updateCellMetadata?(index: number, newMetadata: NotebookCellMetadata): void;
20
}
21
22
export class MoveCellEdit implements IResourceUndoRedoElement {
23
type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;
24
get label() {
25
return this.length === 1 ? 'Move Cell' : 'Move Cells';
26
}
27
code: string = 'undoredo.textBufferEdit';
28
29
constructor(
30
public resource: URI,
31
private fromIndex: number,
32
private length: number,
33
private toIndex: number,
34
private editingDelegate: ITextCellEditingDelegate,
35
private beforedSelections: ISelectionState | undefined,
36
private endSelections: ISelectionState | undefined
37
) {
38
}
39
40
undo(): void {
41
if (!this.editingDelegate.moveCell) {
42
throw new Error('Notebook Move Cell not implemented for Undo/Redo');
43
}
44
45
this.editingDelegate.moveCell(this.toIndex, this.length, this.fromIndex, this.endSelections, this.beforedSelections);
46
}
47
48
redo(): void {
49
if (!this.editingDelegate.moveCell) {
50
throw new Error('Notebook Move Cell not implemented for Undo/Redo');
51
}
52
53
this.editingDelegate.moveCell(this.fromIndex, this.length, this.toIndex, this.beforedSelections, this.endSelections);
54
}
55
}
56
57
export class SpliceCellsEdit implements IResourceUndoRedoElement {
58
type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;
59
get label() {
60
// Compute the most appropriate labels
61
if (this.diffs.length === 1 && this.diffs[0][1].length === 0) {
62
return this.diffs[0][2].length > 1 ? 'Insert Cells' : 'Insert Cell';
63
}
64
if (this.diffs.length === 1 && this.diffs[0][2].length === 0) {
65
return this.diffs[0][1].length > 1 ? 'Delete Cells' : 'Delete Cell';
66
}
67
// Default to Insert Cell
68
return 'Insert Cell';
69
}
70
code: string = 'undoredo.textBufferEdit';
71
constructor(
72
public resource: URI,
73
private diffs: [number, NotebookCellTextModel[], NotebookCellTextModel[]][],
74
private editingDelegate: ITextCellEditingDelegate,
75
private beforeHandles: ISelectionState | undefined,
76
private endHandles: ISelectionState | undefined
77
) {
78
}
79
80
undo(): void {
81
if (!this.editingDelegate.replaceCell) {
82
throw new Error('Notebook Replace Cell not implemented for Undo/Redo');
83
}
84
85
this.diffs.forEach(diff => {
86
this.editingDelegate.replaceCell!(diff[0], diff[2].length, diff[1], this.beforeHandles);
87
});
88
}
89
90
redo(): void {
91
if (!this.editingDelegate.replaceCell) {
92
throw new Error('Notebook Replace Cell not implemented for Undo/Redo');
93
}
94
95
this.diffs.reverse().forEach(diff => {
96
this.editingDelegate.replaceCell!(diff[0], diff[1].length, diff[2], this.endHandles);
97
});
98
}
99
}
100
101
export class CellMetadataEdit implements IResourceUndoRedoElement {
102
type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;
103
label: string = 'Update Cell Metadata';
104
code: string = 'undoredo.textBufferEdit';
105
constructor(
106
public resource: URI,
107
readonly index: number,
108
readonly oldMetadata: NotebookCellMetadata,
109
readonly newMetadata: NotebookCellMetadata,
110
private editingDelegate: ITextCellEditingDelegate,
111
) {
112
113
}
114
115
undo(): void {
116
if (!this.editingDelegate.updateCellMetadata) {
117
return;
118
}
119
120
this.editingDelegate.updateCellMetadata(this.index, this.oldMetadata);
121
}
122
123
redo(): void | Promise<void> {
124
if (!this.editingDelegate.updateCellMetadata) {
125
return;
126
}
127
128
this.editingDelegate.updateCellMetadata(this.index, this.newMetadata);
129
}
130
}
131
132