Skip to content

Commit

Permalink
Readd logic to accept the replacement if a block of the same type exi…
Browse files Browse the repository at this point in the history
…sted in a given position
  • Loading branch information
jorgefilipecosta committed Apr 3, 2019
1 parent 84f6fdd commit 44f7b7a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
62 changes: 61 additions & 1 deletion packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 44f7b7a

Please sign in to comment.