From 9e8763a9e66dc5b185e69b8e2bbaab6b53fa694a Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Fri, 7 Jul 2023 12:06:09 +1000
Subject: [PATCH] Patterns: Display all custom template part areas in sidebar
nav (#52355)
---
.../components/page-patterns/use-patterns.js | 10 +-
.../index.js | 186 ++++++++----------
.../use-template-part-areas.js | 40 +++-
3 files changed, 116 insertions(+), 120 deletions(-)
diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js
index cef7b4721193f4..a394aabf572c48 100644
--- a/packages/edit-site/src/components/page-patterns/use-patterns.js
+++ b/packages/edit-site/src/components/page-patterns/use-patterns.js
@@ -38,14 +38,8 @@ const templatePartToPattern = ( templatePart ) => ( {
templatePart,
} );
-const templatePartCategories = [ 'header', 'footer', 'sidebar' ];
-const templatePartHasCategory = ( item, category ) => {
- if ( category === 'uncategorized' ) {
- return ! templatePartCategories.includes( item.templatePart.area );
- }
-
- return item.templatePart.area === category;
-};
+const templatePartHasCategory = ( item, category ) =>
+ item.templatePart.area === category;
const useTemplatePartsAsPatterns = (
categoryId,
diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js
index 97789e6ec45fea..f200382f963113 100644
--- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js
+++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js
@@ -30,12 +30,79 @@ import usePatternCategories from './use-pattern-categories';
import useMyPatterns from './use-my-patterns';
import useTemplatePartAreas from './use-template-part-areas';
-const templatePartAreaLabels = {
- header: __( 'Headers' ),
- footer: __( 'Footers' ),
- sidebar: __( 'Sidebar' ),
- uncategorized: __( 'Uncategorized' ),
-};
+function TemplatePartGroup( { areas, currentArea, currentType } ) {
+ return (
+ <>
+
+
{ __( 'Template parts' ) }
+
{ __( 'Synced patterns for use in template building.' ) }
+
+
+ { Object.entries( areas ).map(
+ ( [ area, { label, templateParts } ] ) => (
+
+ )
+ ) }
+
+ >
+ );
+}
+
+function ThemePatternsGroup( { categories, currentCategory, currentType } ) {
+ return (
+ <>
+
+
{ __( 'Theme patterns' ) }
+
+ { __(
+ 'For insertion into documents where they can then be customized.'
+ ) }
+
+
+
+ { categories.map( ( category ) => (
+
+ { category.label }
+
+
+
+
+
+
+ }
+ icon={ file }
+ id={ category.name }
+ type="pattern"
+ isActive={
+ currentCategory === `${ category.name }` &&
+ currentType === 'pattern'
+ }
+ />
+ ) ) }
+
+ >
+ );
+}
export default function SidebarNavigationScreenPatterns() {
const isMobileViewport = useViewportMatch( 'medium', '<' );
@@ -110,105 +177,18 @@ export default function SidebarNavigationScreenPatterns() {
) }
{ hasTemplateParts && (
- <>
-
-
- { __( 'Template parts' ) }
-
-
- { __(
- 'Synced patterns for use in template building.'
- ) }
-
-
-
- { Object.entries(
- templatePartAreas
- ).map( ( [ area, parts ] ) => (
-
- ) ) }
-
- >
+
) }
{ hasPatterns && (
- <>
-
-
- { __( 'Theme patterns' ) }
-
-
- { __(
- 'For insertion into documents where they can then be customized.'
- ) }
-
-
-
- { patternCategories.map(
- ( category ) => (
-
- { category.label }
-
-
-
-
-
-
- }
- icon={ file }
- id={ category.name }
- type="pattern"
- isActive={
- currentCategory ===
- `${ category.name }` &&
- currentType ===
- 'pattern'
- }
- />
- )
- ) }
-
- >
+
) }
>
) }
diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js
index aa258344d132da..bc538c5e7a85fa 100644
--- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js
+++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js
@@ -2,19 +2,41 @@
* WordPress dependencies
*/
import { useEntityRecords } from '@wordpress/core-data';
+import { useSelect } from '@wordpress/data';
+import { store as editorStore } from '@wordpress/editor';
-const getTemplatePartAreas = ( items ) => {
+const useTemplatePartsGroupedByArea = ( items ) => {
const allItems = items || [];
- const groupedByArea = allItems.reduce(
- ( accumulator, item ) => {
- const key = accumulator[ item.area ] ? item.area : 'uncategorized';
- accumulator[ key ].push( item );
- return accumulator;
- },
- { header: [], footer: [], sidebar: [], uncategorized: [] }
+ const templatePartAreas = useSelect(
+ ( select ) =>
+ select( editorStore ).__experimentalGetDefaultTemplatePartAreas(),
+ []
);
+ // Create map of template areas ensuring that default areas are displayed before
+ // any custom registered template part areas.
+ const knownAreas = {
+ header: {},
+ footer: {},
+ sidebar: {},
+ uncategorized: {},
+ };
+
+ templatePartAreas.forEach(
+ ( templatePartArea ) =>
+ ( knownAreas[ templatePartArea.area ] = {
+ ...templatePartArea,
+ templateParts: [],
+ } )
+ );
+
+ const groupedByArea = allItems.reduce( ( accumulator, item ) => {
+ const key = accumulator[ item.area ] ? item.area : 'uncategorized';
+ accumulator[ key ].templateParts.push( item );
+ return accumulator;
+ }, knownAreas );
+
return groupedByArea;
};
@@ -28,6 +50,6 @@ export default function useTemplatePartAreas() {
return {
hasTemplateParts: templateParts ? !! templateParts.length : false,
isLoading,
- templatePartAreas: getTemplatePartAreas( templateParts ),
+ templatePartAreas: useTemplatePartsGroupedByArea( templateParts ),
};
}