Skip to content

Commit

Permalink
Only add the selected pattern category in metadata during insertion (#…
Browse files Browse the repository at this point in the history
…61557)

Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
  • Loading branch information
3 people authored May 10, 2024
1 parent 1f131ab commit a3acb34
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
29 changes: 27 additions & 2 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useEffect, useState, forwardRef } from '@wordpress/element';
import { cloneBlock } from '@wordpress/blocks';
import { useEffect, useState, forwardRef, useMemo } from '@wordpress/element';
import {
VisuallyHidden,
Tooltip,
Expand Down Expand Up @@ -47,16 +48,38 @@ function BlockPattern( {
onHover,
showTitle = true,
showTooltip,
category,
} ) {
const [ isDragging, setIsDragging ] = useState( false );
const { blocks, viewportWidth } = pattern;
const instanceId = useInstanceId( BlockPattern );
const descriptionId = `block-editor-block-patterns-list__item-description-${ instanceId }`;

// When we have a selected category and the pattern is draggable, we need to update the
// pattern's categories in metadata to only contain the selected category, and pass this to
// InserterDraggableBlocks component. We do that because we use this information for pattern
// shuffling and it makes more sense to show only the ones from the initially selected category during insertion.
const patternBlocks = useMemo( () => {
if ( ! category || ! isDraggable ) {
return blocks;
}
return ( blocks ?? [] ).map( ( block ) => {
const clonedBlock = cloneBlock( block );
if (
clonedBlock.attributes.metadata?.categories?.includes(
category
)
) {
clonedBlock.attributes.metadata.categories = [ category ];
}
return clonedBlock;
} );
}, [ blocks, isDraggable, category ] );

return (
<InserterDraggableBlocks
isEnabled={ isDraggable }
blocks={ blocks }
blocks={ patternBlocks }
pattern={ pattern }
>
{ ( { draggable, onDragStart, onDragEnd } ) => (
Expand Down Expand Up @@ -173,6 +196,7 @@ function BlockPatternsList(
onClickPattern,
orientation,
label = __( 'Block patterns' ),
category,
showTitle = true,
showTitlesAsTooltip,
pagingProps,
Expand Down Expand Up @@ -209,6 +233,7 @@ function BlockPatternsList(
isDraggable={ isDraggable }
showTitle={ showTitle }
showTooltip={ showTitlesAsTooltip }
category={ category }
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function PatternList( {
} );
const [ patterns, , onClickPattern ] = usePatternsState(
onInsertBlocks,
destinationRootClientId
destinationRootClientId,
selectedCategory
);

const registeredPatternCategories = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export function PatternCategoryPreviews( {
} ) {
const [ allPatterns, , onClickPattern ] = usePatternsState(
onInsert,
rootClientId
rootClientId,
category?.name
);
const [ patternSyncFilter, setPatternSyncFilter ] = useState( 'all' );
const [ patternSourceFilter, setPatternSourceFilter ] = useState( 'all' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import { INSERTER_PATTERN_TYPES } from '../block-patterns-tab/utils';
/**
* Retrieves the block patterns inserter state.
*
* @param {Function} onInsert function called when inserter a list of blocks.
* @param {string=} rootClientId Insertion's root client ID.
* @param {Function} onInsert function called when inserter a list of blocks.
* @param {string=} rootClientId Insertion's root client ID.
*
* @param {string} selectedCategory The selected pattern category.
* @return {Array} Returns the patterns state. (patterns, categories, onSelect handler)
*/
const usePatternsState = ( onInsert, rootClientId ) => {
const usePatternsState = ( onInsert, rootClientId, selectedCategory ) => {
const { patternCategories, patterns, userPatternCategories } = useSelect(
( select ) => {
const { __experimentalGetAllowedPatterns, getSettings } =
Expand Down Expand Up @@ -63,7 +64,19 @@ const usePatternsState = ( onInsert, rootClientId ) => {
? [ createBlock( 'core/block', { ref: pattern.id } ) ]
: blocks;
onInsert(
( patternBlocks ?? [] ).map( ( block ) => cloneBlock( block ) ),
( patternBlocks ?? [] ).map( ( block ) => {
const clonedBlock = cloneBlock( block );
if (
clonedBlock.attributes.metadata?.categories?.includes(
selectedCategory
)
) {
clonedBlock.attributes.metadata.categories = [
selectedCategory,
];
}
return clonedBlock;
} ),
pattern.name
);
createSuccessNotice(
Expand All @@ -78,7 +91,7 @@ const usePatternsState = ( onInsert, rootClientId ) => {
}
);
},
[ createSuccessNotice, onInsert ]
[ createSuccessNotice, onInsert, selectedCategory ]
);

return [ patterns, allCategories, onClickPattern ];
Expand Down

0 comments on commit a3acb34

Please sign in to comment.