Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/8205.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Scroll the notebook editor when giving focus or changing line of a code cell.
4 changes: 2 additions & 2 deletions src/datascience-ui/history-react/interactiveCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CollapseButton } from '../interactive-common/collapseButton';
import { ExecutionCount } from '../interactive-common/executionCount';
import { InformationMessages } from '../interactive-common/informationMessages';
import { InputHistory } from '../interactive-common/inputHistory';
import { ICellViewModel, IFont } from '../interactive-common/mainState';
import { CursorPos, ICellViewModel, IFont } from '../interactive-common/mainState';
import { IKeyboardEvent } from '../react-common/event';
import { getLocString } from '../react-common/locReactSide';
import { getSettings } from '../react-common/settingsReactSide';
Expand Down Expand Up @@ -100,7 +100,7 @@ export class InteractiveCell extends React.Component<IInteractiveCellProps> {
// This depends upon what type of cell we are.
if (this.props.cellVM.cell.data.cell_type === 'code') {
if (this.codeRef.current) {
this.codeRef.current.giveFocus();
this.codeRef.current.giveFocus(CursorPos.Current);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/datascience-ui/interactive-common/cellInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { IKeyboardEvent } from '../react-common/event';
import { getLocString } from '../react-common/locReactSide';
import { Code } from './code';
import { InputHistory } from './inputHistory';
import { ICellViewModel, IFont } from './mainState';
import { CursorPos, ICellViewModel, IFont } from './mainState';
import { Markdown } from './markdown';

// tslint:disable-next-line: no-require-importss
Expand Down Expand Up @@ -56,19 +56,19 @@ export class CellInput extends React.Component<ICellInputProps> {

public componentDidUpdate(prevProps: ICellInputProps) {
if (this.props.cellVM.focused && !prevProps.cellVM.focused) {
this.giveFocus();
this.giveFocus(CursorPos.Current);
}
}

public giveFocus() {
public giveFocus(cursorPos: CursorPos) {
// This depends upon what type of cell we are.
if (this.props.cellVM.cell.data.cell_type === 'code') {
if (this.codeRef.current) {
this.codeRef.current.giveFocus();
this.codeRef.current.giveFocus(cursorPos);
}
} else {
if (this.markdownRef.current) {
this.markdownRef.current.giveFocus();
this.markdownRef.current.giveFocus(cursorPos);
}
this.setState({ showingMarkdownEditor: true });
}
Expand Down
8 changes: 4 additions & 4 deletions src/datascience-ui/interactive-common/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { InputHistory } from '../interactive-common/inputHistory';
import { IKeyboardEvent } from '../react-common/event';
import { getLocString } from '../react-common/locReactSide';
import { Editor } from './editor';
import { IFont } from './mainState';
import { CursorPos, IFont } from './mainState';

export interface ICodeProps {
autoFocus: boolean;
Expand Down Expand Up @@ -79,15 +79,15 @@ export class Code extends React.Component<ICodeProps, ICodeState> {
);
}

public giveFocus() {
public giveFocus(cursorPos: CursorPos) {
if (this.editorRef && this.editorRef.current) {
this.editorRef.current.giveFocus();
this.editorRef.current.giveFocus(cursorPos);
}
}
private clickWatermark = (ev: React.MouseEvent<HTMLDivElement>) => {
ev.stopPropagation();
// Give focus to the editor
this.giveFocus();
this.giveFocus(CursorPos.Current);
}

private getWatermarkString = () : string => {
Expand Down
57 changes: 12 additions & 45 deletions src/datascience-ui/interactive-common/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { noop } from '../../client/common/utils/misc';
import { IKeyboardEvent } from '../react-common/event';
import { MonacoEditor } from '../react-common/monacoEditor';
import { InputHistory } from './inputHistory';
import { IFont } from './mainState';
import { CursorPos, IFont } from './mainState';

// tslint:disable-next-line: import-name
export interface IEditorProps {
Expand Down Expand Up @@ -37,7 +37,6 @@ export interface IEditorProps {
interface IEditorState {
editor: monacoEditor.editor.IStandaloneCodeEditor | undefined;
model: monacoEditor.editor.ITextModel | null;
visibleLineCount: number;
forceMonaco: boolean;
}

Expand All @@ -48,7 +47,7 @@ export class Editor extends React.Component<IEditorProps, IEditorState> {

constructor(prop: IEditorProps) {
super(prop);
this.state = {editor: undefined, model: null, visibleLineCount: 0, forceMonaco: false};
this.state = {editor: undefined, model: null, forceMonaco: false};
}

public componentWillUnmount = () => {
Expand All @@ -65,11 +64,17 @@ export class Editor extends React.Component<IEditorProps, IEditorState> {
);
}

public giveFocus() {
public giveFocus(cursorPos: CursorPos) {
const readOnly = this.props.readOnly;
if (this.state.editor && !readOnly) {
this.state.editor.focus();
}
if (this.state.editor && cursorPos !== CursorPos.Current) {
const current = this.state.editor.getPosition();
const lineNumber = cursorPos === CursorPos.Top ? 1 : this.state.editor.getModel()!.getLineCount();
const column = current && current.lineNumber === lineNumber ? current.column : 1;
this.state.editor.setPosition({ lineNumber, column });
}
}

private renderQuickEditor = (): JSX.Element => {
Expand Down Expand Up @@ -130,7 +135,6 @@ export class Editor extends React.Component<IEditorProps, IEditorState> {
options={options}
openLink={this.props.openLink}
ref={this.monacoRef}
lineCountChanged={this.visibleCountChanged}
/>
);
}
Expand All @@ -145,10 +149,6 @@ export class Editor extends React.Component<IEditorProps, IEditorState> {
this.setState({forceMonaco: true});
}

private visibleCountChanged = (newCount: number) => {
this.setState({visibleLineCount: newCount});
}

private editorDidMount = (editor: monacoEditor.editor.IStandaloneCodeEditor) => {
// Update our state
const model = editor.getModel();
Expand Down Expand Up @@ -186,44 +186,11 @@ export class Editor extends React.Component<IEditorProps, IEditorState> {
private onKeyDown = (e: monacoEditor.IKeyboardEvent) => {
if (this.state.editor && this.state.model && this.monacoRef && this.monacoRef.current) {
const cursor = this.state.editor.getPosition();
const editorDomNode = this.state.editor.getDomNode();
let currentLine = -1;

// This gets the cell/monaco editor line where the cursor is located. With it we can include wrapped lines
// when the isFirstLine and the isLastLine settings are created.
if (cursor && editorDomNode) {
// Get the cursor's position on the cell/monaco editor.
const currentPosition = this.state.model.getValueInRange({ startLineNumber: 1, startColumn: 1, endLineNumber: cursor.lineNumber, endColumn: cursor.column }).length;

if (currentPosition === 0) {
currentLine = 0;
} else {
// Get the lines as they are being displayed, including wrapped ones.
const container = editorDomNode.getElementsByClassName('view-lines')[0] as HTMLElement;

if (container) {
let charCounter = 0;
let index = 0;

// Go through each line, and compare if a character counter is bigger or equal than the cursor position.
// If it is, we found the current line.
while (index < container.childNodes.length) {
if (charCounter < currentPosition && container.childNodes[index].textContent) {
charCounter += container.childNodes[index].textContent!.length;
index += 1;
} else {
break;
}
}

currentLine = index - 1;
}
}
}

const currentLine = this.monacoRef.current.getCurrentVisibleLine();
const visibleLineCount = this.monacoRef.current.getVisibleLineCount();
const isSuggesting = this.monacoRef.current.isSuggesting();
const isFirstLine = currentLine === 0;
const isLastLine = currentLine === this.state.visibleLineCount - 1;
const isLastLine = currentLine === visibleLineCount - 1;
const isDirty = this.state.model!.getVersionId() > this.lastCleanVersionId;

// See if we need to use the history or not
Expand Down
6 changes: 6 additions & 0 deletions src/datascience-ui/interactive-common/mainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface ICellViewModel {
inputBlockToggled(id: string): void;
}

export enum CursorPos {
Top,
Bottom,
Current
}

export interface IMainState {
cellVMs: ICellViewModel[];
editCellVM: ICellViewModel | undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/datascience-ui/interactive-common/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from 'react';

import { IKeyboardEvent } from '../react-common/event';
import { Editor } from './editor';
import { IFont } from './mainState';
import { CursorPos, IFont } from './mainState';

export interface IMarkdownProps {
autoFocus: boolean;
Expand Down Expand Up @@ -67,9 +67,9 @@ export class Markdown extends React.Component<IMarkdownProps> {
);
}

public giveFocus() {
public giveFocus(cursorPos: CursorPos) {
if (this.editorRef && this.editorRef.current) {
this.editorRef.current.giveFocus();
this.editorRef.current.giveFocus(cursorPos);
}
}

Expand Down
33 changes: 16 additions & 17 deletions src/datascience-ui/native-editor/nativeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CellInput } from '../interactive-common/cellInput';
import { CellOutput } from '../interactive-common/cellOutput';
import { ExecutionCount } from '../interactive-common/executionCount';
import { InformationMessages } from '../interactive-common/informationMessages';
import { ICellViewModel, IFont } from '../interactive-common/mainState';
import { CursorPos, ICellViewModel, IFont } from '../interactive-common/mainState';
import { IKeyboardEvent } from '../react-common/event';
import { Image, ImageName } from '../react-common/image';
import { ImageButton } from '../react-common/imageButton';
Expand All @@ -36,9 +36,8 @@ interface INativeCellProps {
monacoTheme: string | undefined;
lastCell: boolean;
font: IFont;
focusCell(cellId: string, focusCode: boolean): void;
selectCell(cellId: string, focusCode: boolean): void;

focusCell(cellId: string, focusCode: boolean, cursorPos: CursorPos): void;
Comment thread
DonJayamanne marked this conversation as resolved.
Resolved
selectCell(cellId: string, focusCode: boolean, cursorPos: CursorPos): void;
}

// tslint:disable: react-this-binding-issue
Expand All @@ -62,7 +61,7 @@ export class NativeCell extends React.Component<INativeCellProps> {

public componentDidUpdate(prevProps: INativeCellProps) {
if (this.props.cellVM.selected && !prevProps.cellVM.selected) {
this.giveFocus(this.props.cellVM.focused);
this.giveFocus(this.props.cellVM.focused, CursorPos.Current);
}

// Anytime we update, reset the key. This object will be reused for different cell ids
Expand All @@ -73,15 +72,15 @@ export class NativeCell extends React.Component<INativeCellProps> {
return !fastDeepEqual(this.props, nextProps);
}

public giveFocus(giveCodeFocus: boolean) {
public giveFocus(giveCodeFocus: boolean, cursorPos: CursorPos) {
// Start out with ourselves
if (this.wrapperRef && this.wrapperRef.current) {
this.wrapperRef.current.focus();
}
// Then attempt to move into the object
if (giveCodeFocus) {
if (this.inputRef && this.inputRef.current) {
this.inputRef.current.giveFocus();
this.inputRef.current.giveFocus(cursorPos);
}
}
}
Expand All @@ -96,7 +95,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
const wasFocused = this.isFocused();
const cellId = this.cellId;
this.props.stateController.moveCellUp(cellId);
setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false), 1);
setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false, CursorPos.Current), 1);
}
}

Expand All @@ -105,7 +104,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
const wasFocused = this.isFocused();
const cellId = this.cellId;
this.props.stateController.moveCellDown(cellId);
setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false), 1);
setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false, CursorPos.Current), 1);
}
}

Expand Down Expand Up @@ -197,7 +196,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
private onMouseDoubleClick = (ev: React.MouseEvent<HTMLDivElement>) => {
// When we receive double click, propagate upwards. Might change our state
ev.stopPropagation();
this.props.focusCell(this.cellId, true);
this.props.focusCell(this.cellId, true, CursorPos.Current);
}

private shouldRenderCodeEditor = () : boolean => {
Expand Down Expand Up @@ -377,7 +376,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
if (this.wrapperRef && this.wrapperRef.current && this.isFocused()) {
e.stopPropagation();
this.isCodeCell() ? this.onCodeUnfocused() : this.onMarkdownUnfocused();
this.props.focusCell(this.cellId, false);
this.props.focusCell(this.cellId, false, CursorPos.Current);
this.props.stateController.sendCommand(NativeCommandType.Unfocus, 'keyboard');
}
}
Expand All @@ -386,7 +385,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
const prevCellId = this.getPrevCellId();
if (prevCellId) {
e.stopPropagation();
this.moveSelection(prevCellId, this.isFocused());
this.moveSelection(prevCellId, this.isFocused(), CursorPos.Bottom);
}

this.props.stateController.sendCommand(NativeCommandType.ArrowUp, 'keyboard');
Expand All @@ -397,7 +396,7 @@ export class NativeCell extends React.Component<INativeCellProps> {

if (nextCellId) {
e.stopPropagation();
this.moveSelection(nextCellId, this.isFocused());
this.moveSelection(nextCellId, this.isFocused(), CursorPos.Top);
}

this.props.stateController.sendCommand(NativeCommandType.ArrowDown, 'keyboard');
Expand All @@ -408,7 +407,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
if (!this.isFocused() && !e.editorInfo && this.wrapperRef && this.wrapperRef && this.isSelected()) {
e.stopPropagation();
e.preventDefault();
this.props.focusCell(this.cellId, true);
this.props.focusCell(this.cellId, true, CursorPos.Current);
}
}

Expand Down Expand Up @@ -475,8 +474,8 @@ export class NativeCell extends React.Component<INativeCellProps> {
this.props.stateController.sendCommand(NativeCommandType.Run, 'keyboard');
}

private moveSelection(cellId: string, focusCode: boolean) {
this.props.selectCell(cellId, focusCode);
private moveSelection(cellId: string, focusCode: boolean, cursorPos: CursorPos = CursorPos.Current) {
this.props.selectCell(cellId, focusCode, cursorPos);
}

private submitCell = (possibleContents?: string) => {
Expand All @@ -501,7 +500,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
this.props.stateController.sendCommand(NativeCommandType.AddToEnd, 'mouse');
if (newCell) {
// Make async because the click changes focus.
setTimeout(() => this.props.focusCell(newCell, true), 0);
setTimeout(() => this.props.focusCell(newCell, true, CursorPos.Top), 0);
}
}

Expand Down
Loading