Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/browser/viewModel/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 { Range } from '../../../../../editor/common/core/range.js';
7
import { Selection } from '../../../../../editor/common/core/selection.js';
8
import { CellKind, IOutputDto, NotebookCellMetadata, SelectionStateType } from '../../common/notebookCommon.js';
9
import { IResourceUndoRedoElement, UndoRedoElementType } from '../../../../../platform/undoRedo/common/undoRedo.js';
10
import { URI } from '../../../../../base/common/uri.js';
11
import { BaseCellViewModel } from './baseCellViewModel.js';
12
import { CellFocusMode } from '../notebookBrowser.js';
13
import { NotebookCellTextModel } from '../../common/model/notebookCellTextModel.js';
14
import { ITextCellEditingDelegate } from '../../common/model/cellEdit.js';
15
16
17
export interface IViewCellEditingDelegate extends ITextCellEditingDelegate {
18
createCellViewModel?(cell: NotebookCellTextModel): BaseCellViewModel;
19
createCell?(index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IOutputDto[]): BaseCellViewModel;
20
}
21
22
export class JoinCellEdit implements IResourceUndoRedoElement {
23
type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;
24
label: string = 'Join Cell';
25
code: string = 'undoredo.textBufferEdit';
26
private _deletedRawCell: NotebookCellTextModel;
27
constructor(
28
public resource: URI,
29
private index: number,
30
private direction: 'above' | 'below',
31
private cell: BaseCellViewModel,
32
private selections: Selection[],
33
private inverseRange: Range,
34
private insertContent: string,
35
private removedCell: BaseCellViewModel,
36
private editingDelegate: IViewCellEditingDelegate,
37
) {
38
this._deletedRawCell = this.removedCell.model;
39
}
40
41
async undo(): Promise<void> {
42
if (!this.editingDelegate.insertCell || !this.editingDelegate.createCellViewModel) {
43
throw new Error('Notebook Insert Cell not implemented for Undo/Redo');
44
}
45
46
await this.cell.resolveTextModel();
47
48
this.cell.textModel?.applyEdits([
49
{ range: this.inverseRange, text: '' }
50
]);
51
52
this.cell.setSelections(this.selections);
53
54
const cell = this.editingDelegate.createCellViewModel(this._deletedRawCell);
55
if (this.direction === 'above') {
56
this.editingDelegate.insertCell(this.index, this._deletedRawCell, { kind: SelectionStateType.Handle, primary: cell.handle, selections: [cell.handle] });
57
cell.focusMode = CellFocusMode.Editor;
58
} else {
59
this.editingDelegate.insertCell(this.index, cell.model, { kind: SelectionStateType.Handle, primary: this.cell.handle, selections: [this.cell.handle] });
60
this.cell.focusMode = CellFocusMode.Editor;
61
}
62
}
63
64
async redo(): Promise<void> {
65
if (!this.editingDelegate.deleteCell) {
66
throw new Error('Notebook Delete Cell not implemented for Undo/Redo');
67
}
68
69
await this.cell.resolveTextModel();
70
this.cell.textModel?.applyEdits([
71
{ range: this.inverseRange, text: this.insertContent }
72
]);
73
74
this.editingDelegate.deleteCell(this.index, { kind: SelectionStateType.Handle, primary: this.cell.handle, selections: [this.cell.handle] });
75
this.cell.focusMode = CellFocusMode.Editor;
76
}
77
}
78
79