Skip to content

Commit c77deed

Browse files
committed
moveCellIdx
1 parent 3f1206a commit c77deed

5 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,16 @@ export interface INotebookEditor extends IEditor {
241241
moveCellDown(cell: ICellViewModel): Promise<ICellViewModel | null>;
242242

243243
/**
244+
* @deprecated Note that this method doesn't support batch operations, use #moveCellToIdx instead.
244245
* Move a cell above or below another cell
245246
*/
246247
moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<ICellViewModel | null>;
247248

249+
/**
250+
* Move a cell to a specific position
251+
*/
252+
moveCellToIdx(cell: ICellViewModel, index: number): Promise<ICellViewModel | null>;
253+
248254
/**
249255
* Focus the container of a cell (the monaco editor inside is not focused).
250256
*/

src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,15 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
11821182
return this._moveCellToIndex(originalIdx, newIdx);
11831183
}
11841184

1185+
async moveCellToIdx(cell: ICellViewModel, index: number): Promise<ICellViewModel | null> {
1186+
if (!this._notebookViewModel!.metadata.editable) {
1187+
return null;
1188+
}
1189+
1190+
const originalIdx = this._notebookViewModel!.getCellIndex(cell);
1191+
return this._moveCellToIndex(originalIdx, index);
1192+
}
1193+
11851194
private async _moveCellToIndex(index: number, newIdx: number): Promise<ICellViewModel | null> {
11861195
if (index === newIdx) {
11871196
return null;

src/vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,14 @@ export class CellDragAndDropController extends Disposable {
687687
}
688688

689689
private async moveCells(draggedCells: ICellViewModel[], ontoCell: ICellViewModel, direction: 'above' | 'below') {
690+
const relativeToIndex = this.notebookEditor!.viewModel!.getCellIndex(ontoCell);
691+
const newIdx = direction === 'above' ? relativeToIndex : relativeToIndex + 1;
692+
690693
this.notebookEditor.textModel!.pushStackElement('Move Cells');
691-
for (let i = 0; i < draggedCells.length; i++) {
692-
await this.notebookEditor.moveCell(draggedCells[i], ontoCell, direction);
694+
for (let i = draggedCells.length - 1; i >= 0; i--) {
695+
await this.notebookEditor.moveCellToIdx(draggedCells[i], newIdx);
693696
}
697+
694698
this.notebookEditor.textModel!.pushStackElement('Move Cells');
695699
}
696700

@@ -699,7 +703,7 @@ export class CellDragAndDropController extends Disposable {
699703
let firstNewCell: ICellViewModel | undefined = undefined;
700704
let firstNewCellState: CellEditState = CellEditState.Preview;
701705
for (let i = 0; i < draggedCells.length; i++) {
702-
const draggedCell = draggedCells[0];
706+
const draggedCell = draggedCells[i];
703707
const newCell = this.notebookEditor.insertNotebookCell(ontoCell, draggedCell.cellKind, direction, draggedCell.getText());
704708

705709
if (newCell && !firstNewCell) {

src/vs/workbench/contrib/notebook/common/model/cellEdit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class DeleteCellEdit implements IResourceUndoRedoElement {
9595

9696
export class MoveCellEdit implements IResourceUndoRedoElement {
9797
type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;
98-
label: string = 'Delete Cell';
98+
label: string = 'Move Cell';
9999

100100
constructor(
101101
public resource: URI,

src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ export class TestNotebookEditor implements INotebookEditor {
160160
throw new Error('Method not implemented.');
161161
}
162162

163+
moveCellToIdx(cell: ICellViewModel, index: number): Promise<ICellViewModel | null> {
164+
throw new Error('Method not implemented.');
165+
}
166+
163167
moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<ICellViewModel | null> {
164168
throw new Error('Method not implemented.');
165169
}

0 commit comments

Comments
 (0)