From 8a960081e55e2e27c4b260ad3f0c02484965eeeb Mon Sep 17 00:00:00 2001 From: rebornix Date: Wed, 24 Mar 2021 12:02:16 -0700 Subject: [PATCH] fix #119771. --- .../contrib/clipboard/notebookClipboard.ts | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts b/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts index 456d28de988..9f090803b28 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts @@ -14,7 +14,7 @@ import { CopyAction, CutAction, PasteAction } from 'vs/editor/contrib/clipboard/ import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService'; import { CellViewModel, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { cloneNotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel'; -import { CellEditType, ICellEditOperation, ICellRange, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { CellEditType, ICellEditOperation, ICellRange, ISelectionState, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService'; import * as platform from 'vs/base/common/platform'; import { MenuId, registerAction2 } from 'vs/platform/actions/common/actions'; @@ -104,34 +104,46 @@ class NotebookClipboardContribution extends Disposable { return false; } + const originalState: ISelectionState = { + kind: SelectionStateType.Index, + focus: viewModel.getFocus(), + selections: viewModel.getSelections() + }; + if (activeCell) { const currCellIndex = viewModel.getCellIndex(activeCell); - - let topPastedCell: CellViewModel | undefined = undefined; - pasteCells.items.reverse().map(cell => cloneNotebookCellTextModel(cell)).forEach(pasteCell => { - const newIdx = typeof currCellIndex === 'number' ? currCellIndex + 1 : 0; - topPastedCell = viewModel.createCell(newIdx, pasteCell.source, pasteCell.language, pasteCell.cellKind, pasteCell.metadata, pasteCell.outputs, true); - }); - - if (topPastedCell) { - editor.focusNotebookCell(topPastedCell, 'container'); - } + const newFocusIndex = typeof currCellIndex === 'number' ? currCellIndex + 1 : 0; + viewModel.notebookDocument.applyEdits([ + { + editType: CellEditType.Replace, + index: newFocusIndex, + count: 0, + cells: pasteCells.items.map(cell => cloneNotebookCellTextModel(cell)) + } + ], true, originalState, () => ({ + kind: SelectionStateType.Index, + focus: { start: newFocusIndex, end: newFocusIndex + 1 }, + selections: [{ start: newFocusIndex, end: newFocusIndex + pasteCells.items.length }] + }), undefined); } else { if (viewModel.length !== 0) { return false; } - let topPastedCell: CellViewModel | undefined = undefined; - pasteCells.items.reverse().map(cell => cloneNotebookCellTextModel(cell)).forEach(pasteCell => { - topPastedCell = viewModel.createCell(0, pasteCell.source, pasteCell.language, pasteCell.cellKind, pasteCell.metadata, pasteCell.outputs, true); - }); - - if (topPastedCell) { - editor.focusNotebookCell(topPastedCell, 'container'); - } + viewModel.notebookDocument.applyEdits([ + { + editType: CellEditType.Replace, + index: 0, + count: 0, + cells: pasteCells.items.map(cell => cloneNotebookCellTextModel(cell)) + } + ], true, originalState, () => ({ + kind: SelectionStateType.Index, + focus: { start: 0, end: 1 }, + selections: [{ start: 1, end: pasteCells.items.length + 1 }] + }), undefined); } - return true; }); }