From 80ffe028baf3d13939079efa9be0a5da9a927d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20Van=C2=A0Durpe?= Date: Mon, 18 Feb 2019 14:50:48 +0100 Subject: [PATCH] onReplace: select the last block (#13294) * onReplace: select the last block * Update tests * Add second test to cover cases of REPLACE_BLOCKS --- packages/editor/src/store/reducer.js | 9 ++++---- packages/editor/src/store/test/reducer.js | 28 +++++++++++++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/editor/src/store/reducer.js b/packages/editor/src/store/reducer.js index a268e2670fc65..6e305185c96e7 100644 --- a/packages/editor/src/store/reducer.js +++ b/packages/editor/src/store/reducer.js @@ -15,7 +15,6 @@ import { isEqual, isEmpty, overSome, - get, } from 'lodash'; /** @@ -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; } diff --git a/packages/editor/src/store/test/reducer.js b/packages/editor/src/store/test/reducer.js index 2d11a0e319a2d..a8172df208a41 100644 --- a/packages/editor/src/store/test/reducer.js +++ b/packages/editor/src/store/test/reducer.js @@ -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', @@ -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', () => {