@@ -178,49 +178,17 @@ const reducer = createReducer(INITIAL_STATE, (builder) => {
178178 lastCommit : commit ,
179179 } ;
180180 } ) ;
181- builder . addCase ( Actions . edit , ( state ) => {
182- if ( isActiveReadOnly ( state ) ) {
183- return ;
184- }
185- return { ...state , mode : "edit" } ;
186- } ) ;
187- builder . addCase ( Actions . view , ( state ) => {
188- return { ...state , mode : "view" } ;
189- } ) ;
190- builder . addCase ( Actions . clear , ( state ) => {
191- if ( ! state . active ) {
192- return ;
193- }
194- const selectedPoints = state . selected
195- ? Array . from ( PointRange . iterate ( state . selected ) )
196- : [ ] ;
197- const changes = selectedPoints . map ( ( point ) => {
198- const cell = Matrix . get ( point , state . data ) ;
199- return {
200- ...state ,
201- prevCell : cell || null ,
202- nextCell : null ,
203- } ;
204- } ) ;
205- return {
206- ...state ,
207- data : selectedPoints . reduce (
208- ( acc , point ) => Matrix . set ( point , undefined , acc ) ,
209- state . data
210- ) ,
211- ...commit ( changes ) ,
212- } ;
213- } ) ;
214- builder . addCase ( Actions . blur , ( state ) => {
215- return { ...state , active : null } ;
216- } ) ;
181+ builder . addCase ( Actions . edit , edit ) ;
182+ builder . addCase ( Actions . view , view ) ;
183+ builder . addCase ( Actions . clear , clear ) ;
184+ builder . addCase ( Actions . blur , blur ) ;
217185 builder . addCase ( Actions . keyPress , ( state , action ) => {
218186 const { event } = action . payload ;
219187 if ( isActiveReadOnly ( state ) || event . metaKey ) {
220188 return ;
221189 }
222190 if ( state . mode === "view" && state . active ) {
223- return { ... state , mode : " edit" } ;
191+ return edit ( state ) ;
224192 }
225193 return ;
226194 } ) ;
@@ -447,14 +415,16 @@ export function hasKeyDownHandler(
447415 return getKeyDownHandler ( state , event ) !== undefined ;
448416}
449417
450- function getActive < Cell extends Types . CellBase > (
418+ /** Returns whether the active cell is read only */
419+ export function isActiveReadOnly ( state : Types . StoreState ) : boolean {
420+ const activeCell = getActive ( state ) ;
421+ return Boolean ( activeCell ?. readOnly ) ;
422+ }
423+
424+ /** Gets active cell from given state */
425+ export function getActive < Cell extends Types . CellBase > (
451426 state : Types . StoreState < Cell >
452427) : Cell | null {
453428 const activeCell = state . active && Matrix . get ( state . active , state . data ) ;
454429 return activeCell || null ;
455430}
456-
457- const isActiveReadOnly = ( state : Types . StoreState < Types . CellBase > ) : boolean => {
458- const activeCell = getActive ( state ) ;
459- return Boolean ( activeCell && activeCell . readOnly ) ;
460- } ;
0 commit comments