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

Patterns: Add user pattern categories to post editor inserter patterns tab #53933

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function PatternList( { filterValue, selectedCategory, patternCategories } ) {
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( {
shouldFocusBlock: true,
} );
const [ allPatterns, , onSelectBlockPattern ] = usePatternsState(
const { patterns: allPatterns, onClickPattern } = usePatternsState(
onInsertBlocks,
destinationRootClientId
);
Expand Down Expand Up @@ -116,7 +116,7 @@ function PatternList( { filterValue, selectedCategory, patternCategories } ) {
<BlockPatternsList
shownPatterns={ currentShownPatterns }
blockPatterns={ filteredBlockPatterns }
onClickPattern={ onSelectBlockPattern }
onClickPattern={ onClickPattern }
isDraggable={ false }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const noop = () => {};
// Preferred order of pattern categories. Any other categories should
// be at the bottom without any re-ordering.
const patternCategoriesOrder = [
'custom',
'featured',
'posts',
'text',
Expand All @@ -45,7 +44,7 @@ const patternCategoriesOrder = [
];

function usePatternsCategories( rootClientId ) {
const [ allPatterns, allCategories ] = usePatternsState(
const { patterns: allPatterns, allCategories } = usePatternsState(
undefined,
rootClientId
);
Expand All @@ -71,10 +70,10 @@ function usePatternsCategories( rootClientId ) {
pattern.categories?.includes( category.name )
)
)
.sort( ( { name: aName }, { name: bName } ) => {
.sort( ( { label: aLabel }, { label: bLabel } ) => {
// Sort categories according to `patternCategoriesOrder`.
let aIndex = patternCategoriesOrder.indexOf( aName );
let bIndex = patternCategoriesOrder.indexOf( bName );
let aIndex = patternCategoriesOrder.indexOf( aLabel );
let bIndex = patternCategoriesOrder.indexOf( bLabel );
// All other categories should come after that.
if ( aIndex < 0 ) aIndex = patternCategoriesOrder.length;
if ( bIndex < 0 ) bIndex = patternCategoriesOrder.length;
Expand Down Expand Up @@ -141,7 +140,7 @@ export function BlockPatternsCategoryPanel( {
category,
showTitlesAsTooltip,
} ) {
const [ allPatterns, , onClick ] = usePatternsState(
const { patterns: allPatterns, onClickPattern } = usePatternsState(
onInsert,
rootClientId
);
Expand Down Expand Up @@ -187,7 +186,7 @@ export function BlockPatternsCategoryPanel( {
<BlockPatternList
shownPatterns={ categoryPatternsList }
blockPatterns={ currentCategoryPatterns }
onClickPattern={ onClick }
onClickPattern={ onClickPattern }
onHover={ onHover }
label={ category.label }
orientation="vertical"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import { store as noticesStore } from '@wordpress/notices';
*/
import { store as blockEditorStore } from '../../../store';

const CUSTOM_CATEGORY = {
name: 'custom',
label: __( 'My patterns' ),
description: __( 'Custom patterns added by site users.' ),
};

/**
* Retrieves the block patterns inserter state.
*
Expand All @@ -27,24 +21,38 @@ const CUSTOM_CATEGORY = {
* @return {Array} Returns the patterns state. (patterns, categories, onSelect handler)
*/
const usePatternsState = ( onInsert, rootClientId ) => {
const { patternCategories, patterns } = useSelect(
const { patternCategories, patterns, userPatternCategories } = useSelect(
( select ) => {
const { __experimentalGetAllowedPatterns, getSettings } =
select( blockEditorStore );

const {
__experimentalUserPatternCategories,
__experimentalBlockPatternCategories,
} = getSettings();
return {
patterns: __experimentalGetAllowedPatterns( rootClientId ),
patternCategories:
getSettings().__experimentalBlockPatternCategories,
userPatternCategories:
__experimentalUserPatternCategories?.patternCategories,
patternCategories: __experimentalBlockPatternCategories,
};
},
[ rootClientId ]
);

const allCategories = useMemo(
() => [ ...patternCategories, CUSTOM_CATEGORY ],
[ patternCategories ]
);
const allCategories = useMemo( () => {
const categories = [ ...patternCategories ];
userPatternCategories?.forEach( ( userCategory ) => {
if (
! categories.find(
( existingCategory ) =>
existingCategory.name === userCategory.name
)
) {
categories.push( userCategory );
}
} );
return categories;
}, [ patternCategories, userPatternCategories ] );

const { createSuccessNotice } = useDispatch( noticesStore );
const onClickPattern = useCallback(
Expand All @@ -68,7 +76,7 @@ const usePatternsState = ( onInsert, rootClientId ) => {
[ createSuccessNotice, onInsert ]
);

return [ patterns, allCategories, onClickPattern ];
return { patterns, allCategories, onClickPattern };
};

export default usePatternsState;
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function QuickInserter( {
onInsertBlocks
);

const [ patterns ] = usePatternsState(
const { patterns } = usePatternsState(
onInsertBlocks,
destinationRootClientId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function InserterSearchResults( {
blockTypeCollections,
onSelectBlockType,
] = useBlockTypesState( destinationRootClientId, onInsertBlocks );
const [ patterns, , onSelectBlockPattern ] = usePatternsState(
const { patterns, onClickPattern } = usePatternsState(
onInsertBlocks,
destinationRootClientId
);
Expand Down Expand Up @@ -189,7 +189,7 @@ function InserterSearchResults( {
<BlockPatternsList
shownPatterns={ currentShownPatterns }
blockPatterns={ filteredBlockPatterns }
onClickPattern={ onSelectBlockPattern }
onClickPattern={ onClickPattern }
onHover={ onHoverPattern }
isDraggable={ isDraggable }
/>
Expand Down
16 changes: 14 additions & 2 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,8 @@ const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
function getUnsyncedPatterns( state ) {
const reusableBlocks =
state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY;

const { patternCategoriesMap: categories } =
state?.settings?.__experimentalUserPatternCategories ?? {};
return reusableBlocks
.filter(
( reusableBlock ) =>
Expand All @@ -2303,12 +2304,23 @@ function getUnsyncedPatterns( state ) {
return {
name: `core/block/${ reusableBlock.id }`,
title: reusableBlock.title.raw,
categories: [ 'custom' ],
categories: reusableBlock.wp_pattern_category.map( ( catId ) =>
categories && categories.get( catId )
? categories.get( catId ).slug
: catId
),
content: reusableBlock.content.raw,
};
} );
}

export const __experimentalUserPatternCategories = createSelector(
( state ) => {
return state?.settings?.__experimentalUserPatternCategories;
},
( state ) => [ state.settings.__experimentalUserPatternCategories ]
);

export const __experimentalGetParsedPattern = createSelector(
( state, patternName ) => {
const patterns = state.settings.__experimentalBlockPatterns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ function useBlockEditorSettings( settings, hasTemplate ) {
settings,
hasUploadPermissions,
reusableBlocks,
userPatternCategories,
blockPatterns,
blockPatternCategories,
userPatternCategories,
canUseUnfilteredHTML,
undo,
hasTemplate,
Expand Down
Loading