Skip to content

Commit

Permalink
onReplace: select the last block (#13294)
Browse files Browse the repository at this point in the history
* onReplace: select the last block

* Update tests

* Add second test to cover cases of REPLACE_BLOCKS
  • Loading branch information
ellatrix authored and youknowriad committed Mar 6, 2019
1 parent dd44786 commit 80ffe02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
9 changes: 5 additions & 4 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
isEqual,
isEmpty,
overSome,
get,
} from 'lodash';

/**
Expand Down Expand Up @@ -887,9 +886,11 @@ export function blockSelection( state = {
return state;
}

// If there is replacement block(s), assign first's client ID as
// the next selected block. If empty replacement, reset to null.
const nextSelectedBlockClientId = get( action.blocks, [ 0, 'clientId' ], null );
// If there are replacement blocks, assign last block as the next
// selected block, otherwise set to null.
const lastBlock = last( action.blocks );
const nextSelectedBlockClientId = lastBlock ? lastBlock.clientId : null;

if ( nextSelectedBlockClientId === state.start && nextSelectedBlockClientId === state.end ) {
return state;
}
Expand Down
28 changes: 26 additions & 2 deletions packages/editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,26 @@ describe( 'state', () => {
} );
} );

it( 'should not replace the selected block if we keep it when replacing blocks', () => {
it( 'should not replace the selected block if we keep it at the end when replacing blocks', () => {
const original = deepFreeze( { start: 'wings', end: 'wings' } );
const state = blockSelection( original, {
type: 'REPLACE_BLOCKS',
clientIds: [ 'wings' ],
blocks: [
{
clientId: 'chicken',
name: 'core/freeform',
},
{
clientId: 'wings',
name: 'core/freeform',
} ],
} );

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

it( 'should replace the selected block if we keep it not at the end when replacing blocks', () => {
const original = deepFreeze( { start: 'chicken', end: 'chicken' } );
const state = blockSelection( original, {
type: 'REPLACE_BLOCKS',
Expand All @@ -1826,7 +1845,12 @@ describe( 'state', () => {
} ],
} );

expect( state ).toBe( original );
expect( state ).toEqual( {
start: 'wings',
end: 'wings',
initialPosition: null,
isMultiSelecting: false,
} );
} );

it( 'should reset if replacing with empty set', () => {
Expand Down

0 comments on commit 80ffe02

Please sign in to comment.