Path: blob/main/src/vs/workbench/contrib/notebook/common/model/cellEdit.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IResourceUndoRedoElement, UndoRedoElementType } from '../../../../../platform/undoRedo/common/undoRedo.js';6import { URI } from '../../../../../base/common/uri.js';7import { NotebookCellTextModel } from './notebookCellTextModel.js';8import { ISelectionState, NotebookCellMetadata } from '../notebookCommon.js';910/**11* It should not modify Undo/Redo stack12*/13export interface ITextCellEditingDelegate {14insertCell?(index: number, cell: NotebookCellTextModel, endSelections?: ISelectionState): void;15deleteCell?(index: number, endSelections?: ISelectionState): void;16replaceCell?(index: number, count: number, cells: NotebookCellTextModel[], endSelections?: ISelectionState): void;17moveCell?(fromIndex: number, length: number, toIndex: number, beforeSelections: ISelectionState | undefined, endSelections?: ISelectionState): void;18updateCellMetadata?(index: number, newMetadata: NotebookCellMetadata): void;19}2021export class MoveCellEdit implements IResourceUndoRedoElement {22type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;23get label() {24return this.length === 1 ? 'Move Cell' : 'Move Cells';25}26code: string = 'undoredo.textBufferEdit';2728constructor(29public resource: URI,30private fromIndex: number,31private length: number,32private toIndex: number,33private editingDelegate: ITextCellEditingDelegate,34private beforedSelections: ISelectionState | undefined,35private endSelections: ISelectionState | undefined36) {37}3839undo(): void {40if (!this.editingDelegate.moveCell) {41throw new Error('Notebook Move Cell not implemented for Undo/Redo');42}4344this.editingDelegate.moveCell(this.toIndex, this.length, this.fromIndex, this.endSelections, this.beforedSelections);45}4647redo(): void {48if (!this.editingDelegate.moveCell) {49throw new Error('Notebook Move Cell not implemented for Undo/Redo');50}5152this.editingDelegate.moveCell(this.fromIndex, this.length, this.toIndex, this.beforedSelections, this.endSelections);53}54}5556export class SpliceCellsEdit implements IResourceUndoRedoElement {57type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;58get label() {59// Compute the most appropriate labels60if (this.diffs.length === 1 && this.diffs[0][1].length === 0) {61return this.diffs[0][2].length > 1 ? 'Insert Cells' : 'Insert Cell';62}63if (this.diffs.length === 1 && this.diffs[0][2].length === 0) {64return this.diffs[0][1].length > 1 ? 'Delete Cells' : 'Delete Cell';65}66// Default to Insert Cell67return 'Insert Cell';68}69code: string = 'undoredo.textBufferEdit';70constructor(71public resource: URI,72private diffs: [number, NotebookCellTextModel[], NotebookCellTextModel[]][],73private editingDelegate: ITextCellEditingDelegate,74private beforeHandles: ISelectionState | undefined,75private endHandles: ISelectionState | undefined76) {77}7879undo(): void {80if (!this.editingDelegate.replaceCell) {81throw new Error('Notebook Replace Cell not implemented for Undo/Redo');82}8384this.diffs.forEach(diff => {85this.editingDelegate.replaceCell!(diff[0], diff[2].length, diff[1], this.beforeHandles);86});87}8889redo(): void {90if (!this.editingDelegate.replaceCell) {91throw new Error('Notebook Replace Cell not implemented for Undo/Redo');92}9394this.diffs.reverse().forEach(diff => {95this.editingDelegate.replaceCell!(diff[0], diff[1].length, diff[2], this.endHandles);96});97}98}99100export class CellMetadataEdit implements IResourceUndoRedoElement {101type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;102label: string = 'Update Cell Metadata';103code: string = 'undoredo.textBufferEdit';104constructor(105public resource: URI,106readonly index: number,107readonly oldMetadata: NotebookCellMetadata,108readonly newMetadata: NotebookCellMetadata,109private editingDelegate: ITextCellEditingDelegate,110) {111112}113114undo(): void {115if (!this.editingDelegate.updateCellMetadata) {116return;117}118119this.editingDelegate.updateCellMetadata(this.index, this.oldMetadata);120}121122redo(): void | Promise<void> {123if (!this.editingDelegate.updateCellMetadata) {124return;125}126127this.editingDelegate.updateCellMetadata(this.index, this.newMetadata);128}129}130131132