Skip to content

Commit

Permalink
Fix withPatternCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Mar 21, 2024
1 parent f14a365 commit 25a33bd
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,6 @@ const withResetControlledBlocks = ( reducer ) => ( state, action ) => {
return reducer( state, action );
};

function cloneDeep( object ) {
return ! object ? {} : JSON.parse( JSON.stringify( object ) );
}

/**
* Higher-order reducer which adds the selected pattern category to the outer block.
*
Expand All @@ -769,19 +765,18 @@ function cloneDeep( object ) {
const withPatternCategory = ( reducer ) => ( state, action ) => {
if ( action.type === 'INSERT_BLOCKS' ) {
const { blocks, meta } = action;
const nextState = reducer( state, action );
const firstBlockClientId = blocks[ 0 ].clientId;
const firstBlockAttributes = cloneDeep( blocks[ 0 ].attributes );
if ( meta?.category && blocks.length === 1 ) {
nextState.attributes.set( firstBlockClientId, {
...firstBlockAttributes,
const newState = { ...state };
newState.attributes = new Map( state.attributes );
newState.attributes.set( blocks[ 0 ].clientId, {
...blocks[ 0 ].attributes,
metadata: {
...( firstBlockAttributes.metadata || {} ),
...( blocks[ 0 ].attributes.metadata || {} ),
categories: [ meta.category.name ],
},
} );
return reducer( newState, action );
}
return nextState;
}

return reducer( state, action );
Expand Down

0 comments on commit 25a33bd

Please sign in to comment.