Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add the selected pattern category in metadata during insertion #61557

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading