Skip to content

Commit

Permalink
withBlockTree: simplify code that replaces/removes controlled blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Apr 30, 2024
1 parent 0cb3850 commit d666359
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,15 @@ const withBlockTree =
action.blocks
);
newState.tree = new Map( newState.tree );
action.replacedClientIds
.concat(
// Controlled inner blocks are only removed
// if the block doesn't move to another position
// otherwise their content will be lost.
action.replacedClientIds
.filter(
( clientId ) => ! inserterClientIds[ clientId ]
)
.map( ( clientId ) => 'controlled||' + clientId )
)
.forEach( ( key ) => {
newState.tree.delete( key );
} );
action.replacedClientIds.forEach( ( clientId ) => {
newState.tree.delete( clientId );
// Controlled inner blocks are only removed
// if the block doesn't move to another position
// otherwise their content will be lost.
if ( ! inserterClientIds[ clientId ] ) {
newState.tree.delete( 'controlled||' + clientId );
}
} );

updateBlockTreeForBlocks( newState, action.blocks );
updateParentInnerBlocksInTree(
Expand Down Expand Up @@ -358,15 +353,10 @@ const withBlockTree =
}
}
newState.tree = new Map( newState.tree );
action.removedClientIds
.concat(
action.removedClientIds.map(
( clientId ) => 'controlled||' + clientId
)
)
.forEach( ( key ) => {
newState.tree.delete( key );
} );
action.removedClientIds.forEach( ( clientId ) => {
newState.tree.delete( clientId );
newState.tree.delete( 'controlled||' + clientId );
} );
updateParentInnerBlocksInTree(
newState,
parentsOfRemovedBlocks,
Expand Down

0 comments on commit d666359

Please sign in to comment.