Skip to content

Commit 20e5ee2

Browse files
authored
Fix focusing of cells when navigating cells using up/down arrow (#9885) (#9908)
For #9884
1 parent 15d40c4 commit 20e5ee2

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/datascience-ui/native-editor/redux/reducers/effects.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,18 @@ export namespace Effects {
126126
return arg.prevState;
127127
}
128128

129-
export function selectCell(arg: NativeEditorReducerArg<ICellAndCursorAction>): IMainState {
129+
/**
130+
* Select a cell.
131+
*
132+
* @param {boolean} [shouldFocusCell] If provided, then will control the focus behavior of the cell. (defaults to focus state of previously selected cell).
133+
*/
134+
export function selectCell(arg: NativeEditorReducerArg<ICellAndCursorAction>, shouldFocusCell?: boolean): IMainState {
130135
// Skip doing anything if already selected.
131136
if (arg.payload.cellId !== arg.prevState.selectedCellId) {
132137
let prevState = arg.prevState;
133138
const addIndex = prevState.cellVMs.findIndex(c => c.cell.id === arg.payload.cellId);
134-
139+
const anotherCellWasFocusedAndSelected = typeof prevState.focusedCellId === 'string' && prevState.focusedCellId === prevState.selectedCellId;
140+
const shouldSetFocusToCell = typeof shouldFocusCell === 'boolean' ? shouldFocusCell : anotherCellWasFocusedAndSelected;
135141
// First find the old focused cell and unfocus it
136142
let removeFocusIndex = arg.prevState.cellVMs.findIndex(c => c.cell.id === arg.prevState.focusedCellId);
137143
if (removeFocusIndex < 0) {
@@ -149,7 +155,7 @@ export namespace Effects {
149155
if (addIndex >= 0 && arg.payload.cellId !== prevState.selectedCellId) {
150156
newVMs[addIndex] = {
151157
...newVMs[addIndex],
152-
focused: prevState.focusedCellId !== undefined && prevState.focusedCellId === prevState.selectedCellId,
158+
focused: shouldSetFocusToCell,
153159
selected: true,
154160
cursorPos: arg.payload.cursorPos
155161
};
@@ -158,7 +164,7 @@ export namespace Effects {
158164
return {
159165
...prevState,
160166
cellVMs: newVMs,
161-
focusedCellId: prevState.focusedCellId !== undefined ? arg.payload.cellId : undefined,
167+
focusedCellId: shouldSetFocusToCell ? arg.payload.cellId : undefined,
162168
selectedCellId: arg.payload.cellId
163169
};
164170
}

src/datascience-ui/native-editor/redux/reducers/execution.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ export namespace Execution {
7777
case 'select':
7878
// Select the cell below this one, but don't focus it
7979
if (index < arg.prevState.cellVMs.length - 1) {
80-
return Effects.selectCell({
81-
...arg,
82-
prevState: {
83-
...executeResult
80+
return Effects.selectCell(
81+
{
82+
...arg,
83+
prevState: {
84+
...executeResult
85+
},
86+
payload: {
87+
...arg.payload,
88+
cellId: arg.prevState.cellVMs[index + 1].cell.id,
89+
cursorPos: CursorPos.Current
90+
}
8491
},
85-
payload: {
86-
...arg.payload,
87-
cellId: arg.prevState.cellVMs[index + 1].cell.id,
88-
cursorPos: CursorPos.Current
89-
}
90-
});
92+
false
93+
);
9194
}
9295
return executeResult;
9396

0 commit comments

Comments
 (0)