提交 dc744d61 编写于 作者: R rebornix

Restore focus after moving cells or deleting cells.

上级 2e7e1311
......@@ -697,7 +697,7 @@ registerAction2(class extends Action2 {
});
}
run(accessor: ServicesAccessor, context?: INotebookCellActionContext) {
async run(accessor: ServicesAccessor, context?: INotebookCellActionContext) {
if (!isCellActionContext(context)) {
context = getActiveCellContext(accessor);
if (!context) {
......@@ -705,14 +705,28 @@ registerAction2(class extends Action2 {
}
}
return context.notebookEditor.deleteNotebookCell(context.cell);
const index = context.notebookEditor.viewModel!.getCellIndex(context.cell);
const result = await context.notebookEditor.deleteNotebookCell(context.cell);
if (result) {
// deletion succeeds, move focus to the next cell
if (index < context.notebookEditor.viewModel!.length) {
context.notebookEditor.focusNotebookCell(context.notebookEditor.viewModel!.viewCells[index], false);
}
}
}
});
async function moveCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise<void> {
direction === 'up' ?
const result = direction === 'up' ?
context.notebookEditor.moveCellUp(context.cell) :
context.notebookEditor.moveCellDown(context.cell);
if (result) {
// move cell command only works when the cell container has focus
context.notebookEditor.focusNotebookCell(context.cell, false);
}
}
async function copyCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise<void> {
......
......@@ -134,22 +134,22 @@ export interface INotebookEditor {
/**
* Delete a cell from the notebook
*/
deleteNotebookCell(cell: ICellViewModel): void;
deleteNotebookCell(cell: ICellViewModel): Promise<boolean>;
/**
* Move a cell up one spot
*/
moveCellUp(cell: ICellViewModel): void;
moveCellUp(cell: ICellViewModel): Promise<boolean>;
/**
* Move a cell down one spot
*/
moveCellDown(cell: ICellViewModel): void;
moveCellDown(cell: ICellViewModel): Promise<boolean>;
/**
* Move a cell above or below another cell
*/
moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<void>;
moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean>;
/**
* Switch the cell into editing mode.
......
......@@ -675,35 +675,36 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
return new Promise(resolve => { r = resolve; });
}
async deleteNotebookCell(cell: ICellViewModel): Promise<void> {
async deleteNotebookCell(cell: ICellViewModel): Promise<boolean> {
(cell as CellViewModel).save();
const index = this.notebookViewModel!.getCellIndex(cell);
this.notebookViewModel!.deleteCell(index, true);
return true;
}
async moveCellDown(cell: ICellViewModel): Promise<void> {
async moveCellDown(cell: ICellViewModel): Promise<boolean> {
const index = this.notebookViewModel!.getCellIndex(cell);
if (index === this.notebookViewModel!.length - 1) {
return;
return false;
}
const newIdx = index + 1;
return this.moveCellToIndex(index, newIdx);
}
async moveCellUp(cell: ICellViewModel): Promise<void> {
async moveCellUp(cell: ICellViewModel): Promise<boolean> {
const index = this.notebookViewModel!.getCellIndex(cell);
if (index === 0) {
return;
return false;
}
const newIdx = index - 1;
return this.moveCellToIndex(index, newIdx);
}
async moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<void> {
async moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean> {
if (cell === relativeToCell) {
return;
return false;
}
const originalIdx = this.notebookViewModel!.getCellIndex(cell);
......@@ -713,15 +714,15 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
return this.moveCellToIndex(originalIdx, newIdx);
}
private async moveCellToIndex(index: number, newIdx: number): Promise<void> {
private async moveCellToIndex(index: number, newIdx: number): Promise<boolean> {
if (!this.notebookViewModel!.moveCellToIdx(index, newIdx, true)) {
return;
throw new Error('Notebook Editor move cell, index out of range');
}
let r: () => void;
let r: (val: boolean) => void;
DOM.scheduleAtNextAnimationFrame(() => {
this.list?.revealElementInView(this.notebookViewModel!.viewCells[newIdx]);
r();
r(true);
});
return new Promise(resolve => { r = resolve; });
......
......@@ -95,15 +95,15 @@ export class TestNotebookEditor implements INotebookEditor {
throw new Error('Method not implemented.');
}
moveCellDown(cell: CellViewModel): void {
moveCellDown(cell: CellViewModel): Promise<boolean> {
throw new Error('Method not implemented.');
}
moveCellUp(cell: CellViewModel): void {
moveCellUp(cell: CellViewModel): Promise<boolean> {
throw new Error('Method not implemented.');
}
moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<void> {
moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean> {
throw new Error('Method not implemented.');
}
......@@ -153,8 +153,8 @@ export class TestNotebookEditor implements INotebookEditor {
async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<CellViewModel> {
throw new Error('Method not implemented.');
}
deleteNotebookCell(cell: CellViewModel): void {
// throw new Error('Method not implemented.');
deleteNotebookCell(cell: CellViewModel): Promise<boolean> {
throw new Error('Method not implemented.');
}
editNotebookCell(cell: CellViewModel): void {
// throw new Error('Method not implemented.');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册