Path: blob/main/src/vs/workbench/contrib/notebook/browser/viewModel/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 { Range } from '../../../../../editor/common/core/range.js';6import { Selection } from '../../../../../editor/common/core/selection.js';7import { CellKind, IOutputDto, NotebookCellMetadata, SelectionStateType } from '../../common/notebookCommon.js';8import { IResourceUndoRedoElement, UndoRedoElementType } from '../../../../../platform/undoRedo/common/undoRedo.js';9import { URI } from '../../../../../base/common/uri.js';10import { BaseCellViewModel } from './baseCellViewModel.js';11import { CellFocusMode } from '../notebookBrowser.js';12import { NotebookCellTextModel } from '../../common/model/notebookCellTextModel.js';13import { ITextCellEditingDelegate } from '../../common/model/cellEdit.js';141516export interface IViewCellEditingDelegate extends ITextCellEditingDelegate {17createCellViewModel?(cell: NotebookCellTextModel): BaseCellViewModel;18createCell?(index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IOutputDto[]): BaseCellViewModel;19}2021export class JoinCellEdit implements IResourceUndoRedoElement {22type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;23label: string = 'Join Cell';24code: string = 'undoredo.textBufferEdit';25private _deletedRawCell: NotebookCellTextModel;26constructor(27public resource: URI,28private index: number,29private direction: 'above' | 'below',30private cell: BaseCellViewModel,31private selections: Selection[],32private inverseRange: Range,33private insertContent: string,34private removedCell: BaseCellViewModel,35private editingDelegate: IViewCellEditingDelegate,36) {37this._deletedRawCell = this.removedCell.model;38}3940async undo(): Promise<void> {41if (!this.editingDelegate.insertCell || !this.editingDelegate.createCellViewModel) {42throw new Error('Notebook Insert Cell not implemented for Undo/Redo');43}4445await this.cell.resolveTextModel();4647this.cell.textModel?.applyEdits([48{ range: this.inverseRange, text: '' }49]);5051this.cell.setSelections(this.selections);5253const cell = this.editingDelegate.createCellViewModel(this._deletedRawCell);54if (this.direction === 'above') {55this.editingDelegate.insertCell(this.index, this._deletedRawCell, { kind: SelectionStateType.Handle, primary: cell.handle, selections: [cell.handle] });56cell.focusMode = CellFocusMode.Editor;57} else {58this.editingDelegate.insertCell(this.index, cell.model, { kind: SelectionStateType.Handle, primary: this.cell.handle, selections: [this.cell.handle] });59this.cell.focusMode = CellFocusMode.Editor;60}61}6263async redo(): Promise<void> {64if (!this.editingDelegate.deleteCell) {65throw new Error('Notebook Delete Cell not implemented for Undo/Redo');66}6768await this.cell.resolveTextModel();69this.cell.textModel?.applyEdits([70{ range: this.inverseRange, text: this.insertContent }71]);7273this.editingDelegate.deleteCell(this.index, { kind: SelectionStateType.Handle, primary: this.cell.handle, selections: [this.cell.handle] });74this.cell.focusMode = CellFocusMode.Editor;75}76}777879