Skip to content

Fix focusing of cells when navigating cells using up/down arrow (#9885) #9908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2020
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
14 changes: 10 additions & 4 deletions src/datascience-ui/native-editor/redux/reducers/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,18 @@ export namespace Effects {
return arg.prevState;
}

export function selectCell(arg: NativeEditorReducerArg<ICellAndCursorAction>): IMainState {
/**
* Select a cell.
*
* @param {boolean} [shouldFocusCell] If provided, then will control the focus behavior of the cell. (defaults to focus state of previously selected cell).
*/
export function selectCell(arg: NativeEditorReducerArg<ICellAndCursorAction>, shouldFocusCell?: boolean): IMainState {
// Skip doing anything if already selected.
if (arg.payload.cellId !== arg.prevState.selectedCellId) {
let prevState = arg.prevState;
const addIndex = prevState.cellVMs.findIndex(c => c.cell.id === arg.payload.cellId);

const anotherCellWasFocusedAndSelected = typeof prevState.focusedCellId === 'string' && prevState.focusedCellId === prevState.selectedCellId;
const shouldSetFocusToCell = typeof shouldFocusCell === 'boolean' ? shouldFocusCell : anotherCellWasFocusedAndSelected;
// First find the old focused cell and unfocus it
let removeFocusIndex = arg.prevState.cellVMs.findIndex(c => c.cell.id === arg.prevState.focusedCellId);
if (removeFocusIndex < 0) {
Expand All @@ -149,7 +155,7 @@ export namespace Effects {
if (addIndex >= 0 && arg.payload.cellId !== prevState.selectedCellId) {
newVMs[addIndex] = {
...newVMs[addIndex],
focused: prevState.focusedCellId !== undefined && prevState.focusedCellId === prevState.selectedCellId,
focused: shouldSetFocusToCell,
selected: true,
cursorPos: arg.payload.cursorPos
};
Expand All @@ -158,7 +164,7 @@ export namespace Effects {
return {
...prevState,
cellVMs: newVMs,
focusedCellId: prevState.focusedCellId !== undefined ? arg.payload.cellId : undefined,
focusedCellId: shouldSetFocusToCell ? arg.payload.cellId : undefined,
selectedCellId: arg.payload.cellId
};
}
Expand Down
23 changes: 13 additions & 10 deletions src/datascience-ui/native-editor/redux/reducers/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,20 @@ export namespace Execution {
case 'select':
// Select the cell below this one, but don't focus it
if (index < arg.prevState.cellVMs.length - 1) {
return Effects.selectCell({
...arg,
prevState: {
...executeResult
return Effects.selectCell(
{
...arg,
prevState: {
...executeResult
},
payload: {
...arg.payload,
cellId: arg.prevState.cellVMs[index + 1].cell.id,
cursorPos: CursorPos.Current
}
},
payload: {
...arg.payload,
cellId: arg.prevState.cellVMs[index + 1].cell.id,
cursorPos: CursorPos.Current
}
});
false
);
}
return executeResult;

Expand Down