@@ -126,12 +126,18 @@ export namespace Effects {
126
126
return arg . prevState ;
127
127
}
128
128
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 {
130
135
// Skip doing anything if already selected.
131
136
if ( arg . payload . cellId !== arg . prevState . selectedCellId ) {
132
137
let prevState = arg . prevState ;
133
138
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 ;
135
141
// First find the old focused cell and unfocus it
136
142
let removeFocusIndex = arg . prevState . cellVMs . findIndex ( c => c . cell . id === arg . prevState . focusedCellId ) ;
137
143
if ( removeFocusIndex < 0 ) {
@@ -149,7 +155,7 @@ export namespace Effects {
149
155
if ( addIndex >= 0 && arg . payload . cellId !== prevState . selectedCellId ) {
150
156
newVMs [ addIndex ] = {
151
157
...newVMs [ addIndex ] ,
152
- focused : prevState . focusedCellId !== undefined && prevState . focusedCellId === prevState . selectedCellId ,
158
+ focused : shouldSetFocusToCell ,
153
159
selected : true ,
154
160
cursorPos : arg . payload . cursorPos
155
161
} ;
@@ -158,7 +164,7 @@ export namespace Effects {
158
164
return {
159
165
...prevState ,
160
166
cellVMs : newVMs ,
161
- focusedCellId : prevState . focusedCellId !== undefined ? arg . payload . cellId : undefined ,
167
+ focusedCellId : shouldSetFocusToCell ? arg . payload . cellId : undefined ,
162
168
selectedCellId : arg . payload . cellId
163
169
} ;
164
170
}
0 commit comments