Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onReplace: select the last block #13294

Merged
merged 3 commits into from
Feb 18, 2019
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
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;

ellatrix marked this conversation as resolved.
Show resolved Hide resolved
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 );
mcsf marked this conversation as resolved.
Show resolved Hide resolved
expect( state ).toEqual( {
start: 'wings',
end: 'wings',
initialPosition: null,
isMultiSelecting: false,
} );
} );

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