提交 6224a65e 编写于 作者: R Rob Lourens

Preserve focus when duplicating cells

上级 fa28db2b
......@@ -179,9 +179,10 @@ registerAction2(class extends Action2 {
// Try to select below, fall back on inserting
const nextCell = editor.viewModel?.viewCells[idx + 1];
if (nextCell) {
editor.focusNotebookCell(nextCell, false);
editor.focusNotebookCell(nextCell, activeCell.editState === CellEditState.Editing);
} else {
await editor.insertNotebookCell(activeCell, CellKind.Code, 'below');
const newCell = editor.insertNotebookCell(activeCell, CellKind.Code, 'below');
editor.focusNotebookCell(newCell, true);
}
}
});
......@@ -211,7 +212,8 @@ registerAction2(class extends Action2 {
return;
}
await editor.insertNotebookCell(activeCell, CellKind.Code, 'below');
const newCell = editor.insertNotebookCell(activeCell, CellKind.Code, 'below');
editor.focusNotebookCell(newCell, true);
}
});
......@@ -410,7 +412,7 @@ async function changeActiveCellToKind(kind: CellKind, accessor: ServicesAccessor
}
const text = activeCell.getText();
await editor.insertNotebookCell(activeCell, kind, 'below', text);
editor.insertNotebookCell(activeCell, kind, 'below', text);
const idx = editor.viewModel?.getCellIndex(activeCell);
if (typeof idx !== 'number') {
return;
......@@ -421,7 +423,7 @@ async function changeActiveCellToKind(kind: CellKind, accessor: ServicesAccessor
return;
}
editor.focusNotebookCell(newCell, true);
editor.focusNotebookCell(newCell, activeCell.editState === CellEditState.Editing);
editor.deleteNotebookCell(activeCell);
}
......@@ -471,8 +473,8 @@ abstract class InsertCellCommand extends Action2 {
}
}
const newCell = await context.notebookEditor.insertNotebookCell(context.cell, this.kind, this.direction);
await context.notebookEditor.focusNotebookCell(newCell, true);
const newCell = context.notebookEditor.insertNotebookCell(context.cell, this.kind, this.direction);
context.notebookEditor.focusNotebookCell(newCell, true);
}
}
......@@ -690,7 +692,8 @@ async function moveCell(context: INotebookCellActionContext, direction: 'up' | '
async function copyCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise<void> {
const text = context.cell.getText();
const newCellDirection = direction === 'up' ? 'above' : 'below';
await context.notebookEditor.insertNotebookCell(context.cell, context.cell.cellKind, newCellDirection, text);
const newCell = context.notebookEditor.insertNotebookCell(context.cell, context.cell.cellKind, newCellDirection, text);
context.notebookEditor.focusNotebookCell(newCell, true);
}
registerAction2(class extends Action2 {
......
......@@ -150,7 +150,7 @@ export interface INotebookEditor {
/**
* Insert a new cell around `cell`
*/
insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText?: string): Promise<CellViewModel>;
insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText?: string): CellViewModel;
/**
* Delete a cell from the notebook
......
......@@ -673,25 +673,18 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
return new Promise(resolve => { r = resolve; });
}
async insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText: string = ''): Promise<CellViewModel> {
insertNotebookCell(cell: ICellViewModel, type: CellKind, direction: 'above' | 'below', initialText: string = ''): CellViewModel {
const newLanguages = this.notebookViewModel!.languages;
const language = newLanguages && newLanguages.length ? newLanguages[0] : 'markdown';
const index = this.notebookViewModel!.getCellIndex(cell);
const insertIndex = direction === 'above' ? index : index + 1;
const newCell = this.notebookViewModel!.createCell(insertIndex, initialText.split(/\r?\n/g), language, type, true);
this.list?.focusElement(newCell);
if (type === CellKind.Markdown) {
newCell.editState = CellEditState.Editing;
}
let r: (cell: CellViewModel) => void;
DOM.scheduleAtNextAnimationFrame(() => {
this.list?.revealElementInCenterIfOutsideViewport(cell);
r(newCell);
});
return new Promise(resolve => { r = resolve; });
return newCell;
}
async deleteNotebookCell(cell: ICellViewModel): Promise<boolean> {
......
......@@ -160,7 +160,7 @@ export class TestNotebookEditor implements INotebookEditor {
revealInCenterIfOutsideViewport(cell: CellViewModel): void {
throw new Error('Method not implemented.');
}
async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<CellViewModel> {
insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): CellViewModel {
throw new Error('Method not implemented.');
}
deleteNotebookCell(cell: CellViewModel): Promise<boolean> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册