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

Site Editor: Use server definition for the Template Areas #37215

Merged
merged 2 commits into from
Dec 8, 2021
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,27 +6,39 @@ import {
Button,
__experimentalHeading as Heading,
} from '@wordpress/components';
import { getTemplatePartIcon } from '@wordpress/editor';
import { store as editorStore } from '@wordpress/editor';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
import { TEMPLATE_PART_AREA_TO_NAME } from '../../../store/constants';

function TemplateAreaItem( { area, clientId } ) {
const { selectBlock, toggleBlockHighlight } = useDispatch(
blockEditorStore
);
const templatePartArea = useSelect(
( select ) => {
const defaultAreas = select(
editorStore
).__experimentalGetDefaultTemplatePartAreas();

return defaultAreas.find(
( defaultArea ) => defaultArea.area === area
);
Comment on lines +28 to +30
Copy link
Member Author

Choose a reason for hiding this comment

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

I doubt myself this morning, but this will correctly pass isShallowEqual check if the returned item is the same, right? 😄

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think so 😆 .

},
[ area ]
);

const highlightBlock = () => toggleBlockHighlight( clientId, true );
const cancelHighlightBlock = () => toggleBlockHighlight( clientId, false );

return (
<Button
className="edit-site-template-card__template-areas-item"
icon={ getTemplatePartIcon( area ) }
icon={ templatePartArea?.icon }
onMouseOver={ highlightBlock }
onMouseLeave={ cancelHighlightBlock }
onFocus={ highlightBlock }
Expand All @@ -35,7 +47,7 @@ function TemplateAreaItem( { area, clientId } ) {
selectBlock( clientId );
} }
>
{ TEMPLATE_PART_AREA_TO_NAME[ area ] }
{ templatePartArea?.label }
</Button>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import { sprintf, __ } from '@wordpress/i18n';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { getTemplatePartIcon } from '@wordpress/editor';
import { store as editorStore } from '@wordpress/editor';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { moreVertical } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { TEMPLATE_PART_AREA_TO_NAME } from '../../store/constants';
import isTemplateRevertable from '../../utils/is-template-revertable';

function TemplatePartItemMore( {
Expand Down Expand Up @@ -67,6 +66,18 @@ function TemplatePartItem( {
const { selectBlock, toggleBlockHighlight } = useDispatch(
blockEditorStore
);
const templatePartArea = useSelect(
( select ) => {
const defaultAreas = select(
editorStore
).__experimentalGetDefaultTemplatePartAreas();

return defaultAreas.find(
( defaultArea ) => defaultArea.area === templatePart.area
);
},
[ templatePart.area ]
);
const highlightBlock = () => toggleBlockHighlight( clientId, true );
const cancelHighlightBlock = () => toggleBlockHighlight( clientId, false );

Expand All @@ -77,7 +88,7 @@ function TemplatePartItem( {
>
<MenuItem
role="button"
icon={ getTemplatePartIcon( templatePart.area ) }
icon={ templatePartArea?.icon }
iconPosition="left"
onClick={ () => {
selectBlock( clientId );
Expand All @@ -87,7 +98,7 @@ function TemplatePartItem( {
onFocus={ highlightBlock }
onBlur={ cancelHighlightBlock }
>
{ TEMPLATE_PART_AREA_TO_NAME[ templatePart.area ] }
{ templatePartArea?.label }
</MenuItem>

<DropdownMenu
Expand Down
12 changes: 0 additions & 12 deletions packages/edit-site/src/store/constants.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* The identifier for the data store.
*
Expand All @@ -14,10 +9,3 @@ export const TEMPLATE_PART_AREA_HEADER = 'header';
export const TEMPLATE_PART_AREA_FOOTER = 'footer';
export const TEMPLATE_PART_AREA_SIDEBAR = 'sidebar';
export const TEMPLATE_PART_AREA_GENERAL = 'uncategorized';

export const TEMPLATE_PART_AREA_TO_NAME = {
[ TEMPLATE_PART_AREA_HEADER ]: __( 'Header' ),
[ TEMPLATE_PART_AREA_FOOTER ]: __( 'Footer' ),
[ TEMPLATE_PART_AREA_SIDEBAR ]: __( 'Sidebar' ),
[ TEMPLATE_PART_AREA_GENERAL ]: __( 'General' ),
};