Skip to content

Commit

Permalink
Site Editor Sidebar: Add "Areas" details panel to all templates and u…
Browse files Browse the repository at this point in the history
…pdate icon (#55677)

* Site Editor Sidebar: Add "Areas" details panel to all templates

* Use symbolFilled icon for general template parts

* Remove unnecessary code
  • Loading branch information
t-hamano authored Dec 11, 2023
1 parent 336e76f commit 2642697
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import {
CheckboxControl,
__experimentalInputControl as InputControl,
__experimentalNumberControl as NumberControl,
__experimentalTruncate as Truncate,
__experimentalItemGroup as ItemGroup,
} from '@wordpress/components';
import { header, footer, layout } from '@wordpress/icons';
import { useMemo, useState, useEffect } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { useState, useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -23,58 +19,19 @@ import {
SidebarNavigationScreenDetailsPanel,
SidebarNavigationScreenDetailsPanelRow,
} from '../sidebar-navigation-screen-details-panel';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import { useLink } from '../routes/link';
import SidebarNavigationItem from '../sidebar-navigation-item';
import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants';

const EMPTY_OBJECT = {};

function TemplateAreaButton( { postId, icon, title } ) {
const icons = {
header,
footer,
};
const linkInfo = useLink( {
postType: TEMPLATE_PART_POST_TYPE,
postId,
} );

return (
<SidebarNavigationItem
className="edit-site-sidebar-navigation-screen-template__template-area-button"
{ ...linkInfo }
icon={ icons[ icon ] ?? layout }
withChevron
>
<Truncate
limit={ 20 }
ellipsizeMode="tail"
numberOfLines={ 1 }
className="edit-site-sidebar-navigation-screen-template__template-area-label-text"
>
{ decodeEntities( title ) }
</Truncate>
</SidebarNavigationItem>
);
}

export default function HomeTemplateDetails() {
const { editEntityRecord } = useDispatch( coreStore );

const {
allowCommentsOnNewPosts,
templatePartAreas,
postsPerPage,
postsPageTitle,
postsPageId,
currentTemplateParts,
} = useSelect( ( select ) => {
const { getEntityRecord } = select( coreStore );
const { getSettings, getCurrentTemplateTemplateParts } = unlock(
select( editSiteStore )
);
const siteSettings = getEntityRecord( 'root', 'site' );
const _postsPageRecord = siteSettings?.page_for_posts
? getEntityRecord(
Expand All @@ -90,8 +47,6 @@ export default function HomeTemplateDetails() {
postsPageTitle: _postsPageRecord?.title?.rendered,
postsPageId: _postsPageRecord?.id,
postsPerPage: siteSettings?.posts_per_page,
templatePartAreas: getSettings()?.defaultTemplatePartAreas,
currentTemplateParts: getCurrentTemplateTemplateParts(),
};
}, [] );

Expand All @@ -111,36 +66,6 @@ export default function HomeTemplateDetails() {
setPostsCountValue( postsPerPage );
}, [ postsPageTitle, allowCommentsOnNewPosts, postsPerPage ] );

/*
* Merge data in currentTemplateParts with templatePartAreas,
* which contains the template icon and fallback labels
*/
const templateAreas = useMemo( () => {
// Keep track of template part IDs that have already been added to the array.
const templatePartIds = new Set();
const filterOutDuplicateTemplateParts = ( currentTemplatePart ) => {
// If the template part has already been added to the array, skip it.
if ( templatePartIds.has( currentTemplatePart.templatePart.id ) ) {
return;
}
// Add to the array of template part IDs.
templatePartIds.add( currentTemplatePart.templatePart.id );
return currentTemplatePart;
};

return currentTemplateParts.length && templatePartAreas
? currentTemplateParts
.filter( filterOutDuplicateTemplateParts )
.map( ( { templatePart, block } ) => ( {
...templatePartAreas?.find(
( { area } ) => area === templatePart?.area
),
...templatePart,
clientId: block.clientId,
} ) )
: [];
}, [ currentTemplateParts, templatePartAreas ] );

const setAllowCommentsOnNewPosts = ( newValue ) => {
setCommentsOnNewPostsValue( newValue );
editEntityRecord( 'root', 'site', undefined, {
Expand Down Expand Up @@ -214,26 +139,6 @@ export default function HomeTemplateDetails() {
/>
</SidebarNavigationScreenDetailsPanelRow>
</SidebarNavigationScreenDetailsPanel>
<SidebarNavigationScreenDetailsPanel
title={ __( 'Areas' ) }
spacing={ 3 }
>
<ItemGroup>
{ templateAreas.map(
( { clientId, label, icon, theme, slug, title } ) => (
<SidebarNavigationScreenDetailsPanelRow
key={ clientId }
>
<TemplateAreaButton
postId={ `${ theme }//${ slug }` }
title={ title?.rendered || label }
icon={ icon }
/>
</SidebarNavigationScreenDetailsPanelRow>
)
) }
</ItemGroup>
</SidebarNavigationScreenDetailsPanel>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import TemplateAreas from './template-areas';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import useEditedEntityRecord from '../use-edited-entity-record';
import { unlock } from '../../lock-unlock';
Expand Down Expand Up @@ -45,8 +46,13 @@ function useTemplateDetails( postType, postId ) {

const content =
record?.slug === 'home' || record?.slug === 'index' ? (
<HomeTemplateDetails />
) : null;
<>
<HomeTemplateDetails />
<TemplateAreas />
</>
) : (
<TemplateAreas />
);

const footer = record?.modified ? (
<SidebarNavigationScreenDetailsFooter record={ record } />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import {
__experimentalTruncate as Truncate,
__experimentalItemGroup as ItemGroup,
} from '@wordpress/components';
import { store as editorStore } from '@wordpress/editor';
import { useMemo } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import {
SidebarNavigationScreenDetailsPanel,
SidebarNavigationScreenDetailsPanelRow,
} from '../sidebar-navigation-screen-details-panel';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import { useLink } from '../routes/link';
import SidebarNavigationItem from '../sidebar-navigation-item';
import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants';

function TemplateAreaButton( { postId, area, title } ) {
const templatePartArea = useSelect(
( select ) => {
const defaultAreas =
select(
editorStore
).__experimentalGetDefaultTemplatePartAreas();

return defaultAreas.find(
( defaultArea ) => defaultArea.area === area
);
},
[ area ]
);
const linkInfo = useLink( {
postType: TEMPLATE_PART_POST_TYPE,
postId,
} );

return (
<SidebarNavigationItem
className="edit-site-sidebar-navigation-screen-template__template-area-button"
{ ...linkInfo }
icon={ templatePartArea?.icon }
withChevron
>
<Truncate
limit={ 20 }
ellipsizeMode="tail"
numberOfLines={ 1 }
className="edit-site-sidebar-navigation-screen-template__template-area-label-text"
>
{ decodeEntities( title ) }
</Truncate>
</SidebarNavigationItem>
);
}

export default function TemplateAreas() {
const { templatePartAreas, currentTemplateParts } = useSelect(
( select ) => {
const { getSettings, getCurrentTemplateTemplateParts } = unlock(
select( editSiteStore )
);
return {
templatePartAreas: getSettings()?.defaultTemplatePartAreas,
currentTemplateParts: getCurrentTemplateTemplateParts(),
};
},
[]
);

/*
* Merge data in currentTemplateParts with templatePartAreas,
* which contains the template icon and fallback labels
*/
const templateAreas = useMemo( () => {
// Keep track of template part IDs that have already been added to the array.
const templatePartIds = new Set();
const filterOutDuplicateTemplateParts = ( currentTemplatePart ) => {
// If the template part has already been added to the array, skip it.
if ( templatePartIds.has( currentTemplatePart.templatePart.id ) ) {
return;
}
// Add to the array of template part IDs.
templatePartIds.add( currentTemplatePart.templatePart.id );
return currentTemplatePart;
};

return currentTemplateParts.length && templatePartAreas
? currentTemplateParts
.filter( filterOutDuplicateTemplateParts )
.map( ( { templatePart, block } ) => ( {
...templatePartAreas?.find(
( { area } ) => area === templatePart?.area
),
...templatePart,
clientId: block.clientId,
} ) )
: [];
}, [ currentTemplateParts, templatePartAreas ] );

if ( ! templateAreas.length ) {
return null;
}

return (
<SidebarNavigationScreenDetailsPanel
title={ __( 'Areas' ) }
spacing={ 3 }
>
<ItemGroup>
{ templateAreas.map(
( { clientId, label, area, theme, slug, title } ) => (
<SidebarNavigationScreenDetailsPanelRow
key={ clientId }
>
<TemplateAreaButton
postId={ `${ theme }//${ slug }` }
title={ title?.rendered || label }
area={ area }
/>
</SidebarNavigationScreenDetailsPanelRow>
)
) }
</ItemGroup>
</SidebarNavigationScreenDetailsPanel>
);
}

1 comment on commit 2642697

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2642697.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7162692314
📝 Reported issues:

Please sign in to comment.