Skip to content

Commit b99790b

Browse files
committed
Don't allow deleting the last notebook cell - add a new one in its place
1 parent dbfe561 commit b99790b

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,13 @@ registerAction2(class extends Action2 {
673673

674674
if (result) {
675675
// deletion succeeds, move focus to the next cell
676-
677676
const nextCellIdx = index < context.notebookEditor.viewModel!.length ? index : context.notebookEditor.viewModel!.length - 1;
678677
if (nextCellIdx >= 0) {
679678
context.notebookEditor.focusNotebookCell(context.notebookEditor.viewModel!.viewCells[nextCellIdx], false);
679+
} else {
680+
// No cells left, insert a new empty one
681+
const newCell = context.notebookEditor.insertNotebookCell(undefined, context.cell.cellKind);
682+
context.notebookEditor.focusNotebookCell(newCell, true);
680683
}
681684
}
682685
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export interface INotebookEditor {
150150
/**
151151
* Insert a new cell around `cell`
152152
*/
153-
insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText?: string): CellViewModel;
153+
insertNotebookCell(cell: ICellViewModel | undefined, type: CellKind, direction?: 'above' | 'below', initialText?: string): CellViewModel;
154154

155155
/**
156156
* Delete a cell from the notebook

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,13 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
676676
return new Promise(resolve => { r = resolve; });
677677
}
678678

679-
insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText: string = ''): CellViewModel {
679+
insertNotebookCell(cell: ICellViewModel | undefined, type: CellKind, direction: 'above' | 'below' = 'above', initialText: string = ''): CellViewModel {
680680
const newLanguages = this.notebookViewModel!.languages;
681681
const language = newLanguages && newLanguages.length ? newLanguages[0] : 'markdown';
682-
const index = this.notebookViewModel!.getCellIndex(cell);
683-
const insertIndex = direction === 'above' ? index : index + 1;
682+
const index = cell ? this.notebookViewModel!.getCellIndex(cell) : 0;
683+
const insertIndex = cell ?
684+
(direction === 'above' ? index : index + 1) :
685+
index;
684686
const newCell = this.notebookViewModel!.createCell(insertIndex, initialText.split(/\r?\n/g), language, type, true);
685687

686688
if (type === CellKind.Markdown) {

0 commit comments

Comments
 (0)