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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold on a second, don't we already have a focus cell action? This looks like what got us into trouble before, by having functions perform their own version of an action instead of using the base action.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To explain better, previously for defocus we had a defocus action where "stuff" had to happen. But some other actions were defocusing cells without using that action so we were not performing the correct defocusing work. This looks similar.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new changes in the ds/custom_editor branch, we no longer need a defocus concept.
Basically setting focus or selecting a cell, automatically unselects and unfocuses the previously selected/focused cells.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an existing action for selectCell, that also sets focus to the cell if required.
I don't think we have a focus action.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take that back, we do have a focus action
Looks like it needs to be cleaned up.
Today the select action also sets focus!
If you're ok with it, i'd rather fix that as a seprate PR, I can do that in the ds/custom_editor branch.

And I agree, it needs to be fixed up, to avoid this confusion (basically fix the separation of concerns... focus will deal with focus, and select with deal with select)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new PR is fine. Just want to make sure that we don't get into trouble like before.

// 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