Focus cell editor if there is only one code cell in the document.

This commit is contained in:
rebornix 2020-06-15 12:15:28 -07:00
parent 4dfc14b846
commit fdf1ab38a8
4 changed files with 21 additions and 0 deletions

View file

@ -110,6 +110,7 @@ export interface ICellViewModel {
currentTokenSource: CancellationTokenSource | undefined;
focusMode: CellFocusMode;
getText(): string;
getTextLength(): number;
metadata: NotebookCellMetadata | undefined;
textModel: ITextModel | undefined;
hasModel(): this is IEditableCellViewModel;

View file

@ -440,6 +440,18 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
}
}
}
} else if (this.notebookViewModel!.viewCells.length === 1 && this.notebookViewModel!.viewCells[0].cellKind === CellKind.Code) {
// there is only one code cell in the document
const cell = this.notebookViewModel!.viewCells[0];
if (cell.getTextLength() === 0) {
// the cell is empty, very likely a template cell, focus it
this.selectElement(cell);
await this.revealLineInCenterAsync(cell, 1);
const editor = this.renderedEditors.get(cell)!;
if (editor) {
editor.focus();
}
}
}
}

View file

@ -197,6 +197,10 @@ export abstract class BaseCellViewModel extends Disposable {
return this.model.getValue();
}
getTextLength(): number {
return this.model.getTextLength();
}
private saveViewState(): void {
if (!this._textEditor) {
return;

View file

@ -93,6 +93,10 @@ export class NotebookCellTextModel extends Disposable implements ICell {
}
}
getTextLength(): number {
return this.textBuffer.getLength();
}
getFullModelRange() {
const lineCount = this.textBuffer.getLineCount();
return new Range(1, 1, lineCount, this.textBuffer.getLineLength(lineCount) + 1);