From 1ad7cf838d99159df897bd3a05498d888004cd65 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Tue, 4 Feb 2020 12:07:48 -0800 Subject: [PATCH] Fix focusing of cells when navigating cells using up/down arrow (#9885) For #9884 --- .../native-editor/redux/reducers/effects.ts | 14 +++++++---- .../native-editor/redux/reducers/execution.ts | 23 +++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/datascience-ui/native-editor/redux/reducers/effects.ts b/src/datascience-ui/native-editor/redux/reducers/effects.ts index 65d35324431a..ba0c901ba81b 100644 --- a/src/datascience-ui/native-editor/redux/reducers/effects.ts +++ b/src/datascience-ui/native-editor/redux/reducers/effects.ts @@ -126,12 +126,18 @@ export namespace Effects { return arg.prevState; } - export function selectCell(arg: NativeEditorReducerArg): 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, 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) { @@ -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 }; @@ -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 }; } diff --git a/src/datascience-ui/native-editor/redux/reducers/execution.ts b/src/datascience-ui/native-editor/redux/reducers/execution.ts index 86be83ac613a..0accb65b8923 100644 --- a/src/datascience-ui/native-editor/redux/reducers/execution.ts +++ b/src/datascience-ui/native-editor/redux/reducers/execution.ts @@ -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;