Skip to content

Commit

Permalink
Fix updating the block list after block removal (#35721)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Oct 18, 2021
1 parent ec67990 commit 0256b04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ const withBlockTree = ( reducer ) => ( state = {}, action ) => {
},
action.blocks.map( ( b ) => b.clientId )
);

// If there are no replaced blocks, it means we're removing blocks so we need to update their parent.
const parentsOfRemovedBlocks = [];
for ( const clientId of action.clientIds ) {
if (
state.parents[ clientId ] !== undefined &&
( state.parents[ clientId ] === '' ||
newState.byClientId[ state.parents[ clientId ] ] )
) {
parentsOfRemovedBlocks.push( state.parents[ clientId ] );
}
}
newState.tree = updateParentInnerBlocksInTree(
newState,
newState.tree,
parentsOfRemovedBlocks
);
break;
}
case 'REMOVE_BLOCKS_AUGMENTED_WITH_CHILDREN':
Expand Down
22 changes: 22 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,28 @@ describe( 'state', () => {
} );
} );

it( 'Replacing the block with an empty list should remove it', () => {
const original = blocks( undefined, {
type: 'RESET_BLOCKS',
blocks: [
{
clientId: 'chicken',
name: 'core/test-block',
attributes: {},
innerBlocks: [],
},
],
} );
const state = blocks( original, {
type: 'REPLACE_BLOCKS',
clientIds: [ 'chicken' ],
blocks: [],
} );

expect( Object.keys( state.byClientId ) ).toHaveLength( 0 );
expect( state.tree[ '' ].innerBlocks ).toHaveLength( 0 );
} );

it( 'should replace the block and remove references to its inner blocks', () => {
const original = blocks( undefined, {
type: 'RESET_BLOCKS',
Expand Down

0 comments on commit 0256b04

Please sign in to comment.