diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 42e063a4af276e..7cd26194bfa4a6 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -230,6 +230,7 @@ export function* replaceBlocks( clientIds, blocks ) { first( clientIds ) ); // Replace is valid if the new blocks can be inserted in the root block + // or if we had a block of the same type in the position of the block being replaced. for ( let index = 0; index < blocks.length; index++ ) { const block = blocks[ index ]; const canInsertBlock = yield select( @@ -239,7 +240,12 @@ export function* replaceBlocks( clientIds, blocks ) { rootClientId ); if ( ! canInsertBlock ) { - return; + const clientIdToReplace = clientIds[ index ]; + const nameOfBlockToReplace = clientIdToReplace && + ( yield select( 'core/block-editor', 'getBlockName', clientIdToReplace ) ); + if ( ! nameOfBlockToReplace || ( nameOfBlockToReplace !== block.name ) ) { + return; + } } } yield { diff --git a/packages/block-editor/src/store/test/actions.js b/packages/block-editor/src/store/test/actions.js index 710453933968b3..b24171a94237d3 100644 --- a/packages/block-editor/src/store/test/actions.js +++ b/packages/block-editor/src/store/test/actions.js @@ -215,7 +215,7 @@ describe( 'actions', () => { } ); } ); - it( 'should yield the REPLACE_BLOCKS action if the replacement is possible', () => { + it( 'should yield the REPLACE_BLOCKS action if the all the replacement blocks can be inserted in the parent block', () => { const blocks = [ { clientId: 'ribs', name: 'core/test-ribs', @@ -277,6 +277,66 @@ describe( 'actions', () => { done: true, } ); } ); + + it( 'should yield the REPLACE_BLOCKS if the block being replaced and the replacement are of the same type', () => { + const blocks = [ { + clientId: 'ribs', + name: 'core/test-block', + } ]; + + const replaceBlockGenerator = replaceBlocks( [ 'chicken' ], blocks ); + expect( + replaceBlockGenerator.next().value, + ).toEqual( { + args: [ 'chicken' ], + selectorName: 'getBlockRootClientId', + storeName: 'core/block-editor', + type: 'SELECT', + } ); + + expect( + replaceBlockGenerator.next().value, + ).toEqual( { + args: [ 'core/test-block', undefined ], + selectorName: 'canInsertBlockType', + storeName: 'core/block-editor', + type: 'SELECT', + } ); + + expect( + replaceBlockGenerator.next( false ).value, + ).toEqual( { + args: [ 'chicken' ], + selectorName: 'getBlockName', + storeName: 'core/block-editor', + type: 'SELECT', + } ); + + expect( + replaceBlockGenerator.next( 'core/test-block' ).value, + ).toEqual( { + type: 'REPLACE_BLOCKS', + clientIds: [ 'chicken' ], + blocks, + time: expect.any( Number ), + } ); + + expect( + replaceBlockGenerator.next().value, + ).toEqual( { + args: [], + selectorName: 'getBlockCount', + storeName: 'core/block-editor', + type: 'SELECT', + } ); + + expect( + replaceBlockGenerator.next( 1 ), + ).toEqual( { + value: undefined, + done: true, + } ); + } ); } ); describe( 'insertBlock', () => {