Skip to content

Commit

Permalink
Expose Template part block variations to the Inserter
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Mar 19, 2021
1 parent a4b2b42 commit 0d6d2f4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
3 changes: 3 additions & 0 deletions packages/block-library/src/template-part/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
"tagName": {
"type": "string"
},
"area": {
"type": "string"
}
},
"supports": {
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import { TemplatePartAdvancedControls } from './advanced-controls';
import { getTagBasedOnArea } from './get-tag-based-on-area';

export default function TemplatePartEdit( {
attributes: { slug, theme, tagName },
attributes,
setAttributes,
clientId,
} ) {
const { slug, theme, tagName } = attributes;
const templatePartId = theme && slug ? theme + '//' + slug : null;

const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
Expand Down Expand Up @@ -116,6 +117,7 @@ export default function TemplatePartEdit( {
<TagName { ...blockProps }>
{ isPlaceholder && (
<TemplatePartPlaceholder
area={ attributes.area }
setAttributes={ setAttributes }
innerBlocks={ innerBlocks }
/>
Expand Down
20 changes: 14 additions & 6 deletions packages/block-library/src/template-part/edit/placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,34 @@ import { store as coreStore } from '@wordpress/core-data';
import TemplatePartSelection from '../selection';

export default function TemplatePartPlaceholder( {
area,
setAttributes,
innerBlocks,
} ) {
const { saveEntityRecord } = useDispatch( coreStore );
const onCreate = useCallback( async () => {
const title = __( 'Untitled Template Part' );
const record = {
title,
slug: 'template-part',
content: serialize( innerBlocks ),
};
// If we have `area` set from block attributes, means an exposed
// block variation was inserted. So add this prop to the template
// part entity on creation. Afterwards remove `area` value from
// block attributes.
if ( [ 'header', 'footer' ].includes( area ) ) record.area = area;
const templatePart = await saveEntityRecord(
'postType',
'wp_template_part',
{
title,
slug: 'template-part',
content: serialize( innerBlocks ),
}
record
);
setAttributes( {
slug: templatePart.slug,
theme: templatePart.theme,
area: undefined,
} );
}, [ setAttributes ] );
}, [ setAttributes, area ] );

return (
<Placeholder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function TemplatePartItem( {
const { createSuccessNotice } = useDispatch( noticesStore );

const onClick = useCallback( () => {
setAttributes( { slug, theme } );
setAttributes( { slug, theme, area: undefined } );
createSuccessNotice(
sprintf(
/* translators: %s: template part title. */
Expand Down
46 changes: 28 additions & 18 deletions packages/block-library/src/template-part/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ import { store as coreDataStore } from '@wordpress/core-data';
import { select } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

const createIsActiveBasedOnArea = ( area ) => ( { theme, slug } ) => {
if ( ! slug ) {
return false;
}

const entity = select( coreDataStore ).getEntityRecord(
'postType',
'wp_template_part',
`${ theme }//${ slug }`
);

return entity?.area === area;
};

const variations = [
{
name: 'header',
Expand All @@ -28,8 +14,8 @@ const variations = [
"The header template defines a page area that typically contains a title, logo, and main navigation. Since it's a global element it can be present across all pages and posts."
),
icon: header,
isActive: createIsActiveBasedOnArea( 'header' ),
scope: [],
attributes: { area: 'header' },
scope: [ 'inserter' ],
},
{
name: 'footer',
Expand All @@ -38,9 +24,33 @@ const variations = [
"The footer template defines a page area that typically contains site credits, social links, or any other combination of blocks. Since it's a global element it can be present across all pages and posts."
),
icon: footer,
isActive: createIsActiveBasedOnArea( 'footer' ),
scope: [],
attributes: { area: 'footer' },
scope: [ 'inserter' ],
},
];

/**
* Add `isActive` function to all `Template Part` variations, if not defined.
* `isActive` function is used to find a variation match from a created
* Block by providing its attributes.
*/
variations.forEach( ( variation ) => {
if ( variation.isActive ) return;
variation.isActive = ( blockAttributes, variationAttributes ) => {
const { area, theme, slug } = blockAttributes;
// We first check the `area` block attribute which is set during insertion.
// This property is removed on the creation of a template part.
if ( area ) return area === variationAttributes.area;
// Find a matching variation from the created template part
// by checking the entity's `area` property.
if ( ! slug ) return false;
const entity = select( coreDataStore ).getEntityRecord(
'postType',
'wp_template_part',
`${ theme }//${ slug }`
);
return entity?.area === variationAttributes.area;
};
} );

export default variations;

0 comments on commit 0d6d2f4

Please sign in to comment.