Skip to content

Commit 1536d8b

Browse files
authored
Fix selection and focus not updating when clicking around in a notebook (microsoft#7806)
* Fix selection and focus not updating when clicking around in a notebook editor. * Also fix add cell to put focus in the new cell
1 parent 987b4de commit 1536d8b

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

news/2 Fixes/7802.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix selection and focus not updating when clicking around in a notebook editor.

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datascience-ui/interactive-common/cellInput.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ interface ICellInputProps {
2626
monacoTheme: string | undefined;
2727
editorOptions?: monacoEditor.editor.IEditorOptions;
2828
editorMeasureClassName?: string;
29-
focusedCell?: string;
3029
showLineNumbers?: boolean;
3130
font: IFont;
3231
onCodeChange(changes: monacoEditor.editor.IModelContentChange[], cellId: string, modelId: string): void;
@@ -56,7 +55,7 @@ export class CellInput extends React.Component<ICellInputProps> {
5655
}
5756

5857
public componentDidUpdate(prevProps: ICellInputProps) {
59-
if (this.props.focusedCell === this.props.cellVM.cell.id && prevProps.focusedCell !== this.props.focusedCell) {
58+
if (this.props.cellVM.focused && !prevProps.cellVM.focused) {
6059
this.giveFocus();
6160
}
6261
}

src/datascience-ui/interactive-common/mainStateController.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ export class MainStateController implements IMessageHandler {
314314
// Update our state
315315
this.setState({
316316
cellVMs: newVMs,
317-
selectedCell: newSelection,
318-
focusedCell: newFocused,
317+
selectedCellId: newSelection,
318+
focusedCellId: newFocused,
319319
undoStack: this.pushStack(this.pendingState.undoStack, this.pendingState.cellVMs),
320320
skipNextScroll: true
321321
});
@@ -509,7 +509,7 @@ export class MainStateController implements IMessageHandler {
509509
newVMs[oldSelect] = { ...newVMs[oldSelect], focused: false };
510510
}
511511
// Only unfocus if we haven't switched somewhere else yet
512-
this.setState({ focusedCell: undefined, cellVMs: newVMs });
512+
this.setState({ focusedCellId: undefined, cellVMs: newVMs });
513513
}
514514
}
515515

@@ -528,13 +528,13 @@ export class MainStateController implements IMessageHandler {
528528
}
529529

530530
// Save the whole thing in our state.
531-
this.setState({ selectedCell: cellId, focusedCell: cellId, cellVMs: newVMs });
531+
this.setState({ selectedCellId: cellId, focusedCellId: cellId, cellVMs: newVMs });
532532
}
533533
}
534534

535-
public selectCell = (cellId: string, focusedCell?: string) => {
535+
public selectCell = (cellId: string, focusedCellId?: string) => {
536536
// Skip if already the same cell
537-
if (this.pendingState.selectedCellId !== cellId || this.pendingState.focusedCellId !== focusedCell) {
537+
if (this.pendingState.selectedCellId !== cellId || this.pendingState.focusedCellId !== focusedCellId) {
538538
const newVMs = [...this.pendingState.cellVMs];
539539
// Switch the old vm
540540
const oldSelect = this.findCellIndex(this.pendingState.selectedCellId);
@@ -543,11 +543,11 @@ export class MainStateController implements IMessageHandler {
543543
}
544544
const newSelect = this.findCellIndex(cellId);
545545
if (newSelect >= 0) {
546-
newVMs[newSelect] = { ...newVMs[newSelect], selected: true, focused: focusedCell === newVMs[newSelect].cell.id };
546+
newVMs[newSelect] = { ...newVMs[newSelect], selected: true, focused: focusedCellId === newVMs[newSelect].cell.id };
547547
}
548548

549549
// Save the whole thing in our state.
550-
this.setState({ selectedCell: cellId, focusedCell, cellVMs: newVMs });
550+
this.setState({ selectedCellId: cellId, focusedCellId, cellVMs: newVMs });
551551
}
552552
}
553553

src/datascience-ui/native-editor/nativeCell.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ export class NativeCell extends React.Component<INativeCellProps> {
186186
// Not a click on an button in a toolbar, select the cell.
187187
ev.stopPropagation();
188188
this.lastKeyPressed = undefined;
189-
const focusedCell = this.isFocused() ? this.cellId : undefined;
190-
this.props.stateController.selectCell(this.cellId, focusedCell);
189+
const focusedCellId = this.isFocused() ? this.cellId : undefined;
190+
this.props.stateController.selectCell(this.cellId, focusedCellId);
191191
}
192192
}
193193
}
@@ -487,7 +487,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
487487
const newCell = this.props.stateController.insertBelow(this.props.cellVM.cell.id, true);
488488
this.props.stateController.sendCommand(NativeCommandType.AddToEnd, 'mouse');
489489
if (newCell) {
490-
this.props.selectCell(newCell);
490+
this.props.focusCell(newCell, true);
491491
}
492492
}
493493

src/datascience-ui/native-editor/nativeEditor.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,11 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
172172
// tslint:disable: react-this-binding-issue
173173
private renderToolbarPanel() {
174174
const addCell = () => {
175-
this.stateController.addNewCell();
175+
const newCell = this.stateController.addNewCell();
176176
this.stateController.sendCommand(NativeCommandType.AddToEnd, 'mouse');
177+
if (newCell) {
178+
this.stateController.selectCell(newCell.cell.id, newCell.cell.id);
179+
}
177180
};
178181
const runAll = () => {
179182
this.stateController.runAll();

0 commit comments

Comments
 (0)