Skip to content

Commit

Permalink
State: Abort state update if multi-select action has no effect
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 19, 2018
1 parent b217baa commit 6d6231e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
16 changes: 15 additions & 1 deletion editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ export function blockSelection( state = {
}, action ) {
switch ( action.type ) {
case 'CLEAR_SELECTED_BLOCK':
if ( state.start === null && state.end === null &&
state.focus === null && ! state.isMultiSelecting ) {
return state;
}

return {
...state,
start: null,
Expand All @@ -383,15 +388,24 @@ export function blockSelection( state = {
isMultiSelecting: false,
};
case 'START_MULTI_SELECT':
if ( state.isMultiSelecting ) {
return state;
}

return {
...state,
isMultiSelecting: true,
};
case 'STOP_MULTI_SELECT':
const nextFocus = state.start === state.end ? state.focus : null;
if ( ! state.isMultiSelecting && nextFocus === state.focus ) {
return state;
}

return {
...state,
isMultiSelecting: false,
focus: state.start === state.end ? state.focus : null,
focus: nextFocus,
};
case 'MULTI_SELECT':
return {
Expand Down
34 changes: 33 additions & 1 deletion editor/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,15 @@ describe( 'state', () => {
expect( state ).toEqual( { start: 'ribs', end: 'ribs', focus: { editable: 'citation' }, isMultiSelecting: true } );
} );

it( 'should return same reference if already multi-selecting', () => {
const original = deepFreeze( { start: 'ribs', end: 'ribs', focus: { editable: 'citation' }, isMultiSelecting: true } );
const state = blockSelection( original, {
type: 'START_MULTI_SELECT',
} );

expect( state ).toBe( original );
} );

it( 'should end multi selection with selection', () => {
const original = deepFreeze( { start: 'ribs', end: 'chicken', focus: { editable: 'citation' }, isMultiSelecting: true } );
const state = blockSelection( original, {
Expand All @@ -811,6 +820,15 @@ describe( 'state', () => {
expect( state ).toEqual( { start: 'ribs', end: 'chicken', focus: null, isMultiSelecting: false } );
} );

it( 'should return same reference if already ended multi-selecting', () => {
const original = deepFreeze( { start: 'ribs', end: 'chicken', focus: null, isMultiSelecting: false } );
const state = blockSelection( original, {
type: 'STOP_MULTI_SELECT',
} );

expect( state ).toBe( original );
} );

it( 'should end multi selection without selection', () => {
const original = deepFreeze( { start: 'ribs', end: 'ribs', focus: { editable: 'citation' }, isMultiSelecting: true } );
const state = blockSelection( original, {
Expand All @@ -831,14 +849,28 @@ describe( 'state', () => {
expect( state1 ).toBe( original );
} );

it( 'should unset multi selection and select inserted block', () => {
it( 'should unset multi selection', () => {
const original = deepFreeze( { start: 'ribs', end: 'chicken' } );

const state1 = blockSelection( original, {
type: 'CLEAR_SELECTED_BLOCK',
} );

expect( state1 ).toEqual( { start: null, end: null, focus: null, isMultiSelecting: false } );
} );

it( 'should return same reference if clearing selection but no selection', () => {
const original = deepFreeze( { start: null, end: null, focus: null, isMultiSelecting: false } );

const state1 = blockSelection( original, {
type: 'CLEAR_SELECTED_BLOCK',
} );

expect( state1 ).toBe( original );
} );

it( 'should select inserted block', () => {
const original = deepFreeze( { start: 'ribs', end: 'chicken' } );

const state3 = blockSelection( original, {
type: 'INSERT_BLOCKS',
Expand Down

0 comments on commit 6d6231e

Please sign in to comment.