-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full Site Editing: Add a Template Part block (#32581)
* Add a Template Part block. It roughly works like the Content Slot block but, whereas that's a sort of placeholder for the template to be filled with a post or page, the Template Part is used to render a layout element into the template.
- Loading branch information
Showing
6 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
apps/full-site-editing/full-site-editing-plugin/blocks/template-part/edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* eslint-disable wpcalypso/jsx-classname-namespace */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classNames from 'classnames'; | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { IconButton, Placeholder, Toolbar } from '@wordpress/components'; | ||
import { withState } from '@wordpress/compose'; | ||
import { BlockControls } from '@wordpress/editor'; | ||
import { Fragment, RawHTML } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostAutocomplete from '../../components/post-autocomplete'; | ||
import fetchPost from '../../lib/fetch-post'; | ||
import './style.scss'; | ||
|
||
const setSelectedPost = async ( attributes, setState ) => { | ||
const { selectedPostId, selectedPostType } = attributes; | ||
const selectedPost = await fetchPost( selectedPostId, selectedPostType ); | ||
setState( { | ||
selectedPost, | ||
} ); | ||
}; | ||
|
||
const TemplatePartEdit = withState( { | ||
isEditing: false, | ||
selectedPost: null, | ||
} )( ( { attributes, isEditing, selectedPost, setAttributes, setState } ) => { | ||
const { align, selectedPostId } = attributes; | ||
|
||
if ( !! selectedPostId && ! selectedPost ) { | ||
setSelectedPost( attributes, setState ); | ||
} | ||
|
||
const toggleEditing = () => setState( { isEditing: ! isEditing } ); | ||
|
||
const onSelectPost = post => { | ||
setState( { isEditing: false, selectedPost: post } ); | ||
setAttributes( { | ||
selectedPostId: get( post, 'id' ), | ||
selectedPostType: get( post, 'type' ), | ||
} ); | ||
}; | ||
|
||
const showToggleButton = ! isEditing || !! selectedPostId; | ||
const showPlaceholder = isEditing || ! selectedPostId; | ||
const showContent = ! isEditing && !! selectedPostId; | ||
|
||
return ( | ||
<Fragment> | ||
{ showToggleButton && ( | ||
<BlockControls> | ||
<Toolbar> | ||
<IconButton | ||
className={ classNames( 'components-icon-button components-toolbar__control', { | ||
'is-active': isEditing, | ||
} ) } | ||
label={ __( 'Change Template Part' ) } | ||
onClick={ toggleEditing } | ||
icon="edit" | ||
/> | ||
</Toolbar> | ||
</BlockControls> | ||
) } | ||
<div | ||
className={ classNames( 'a8c-template-part-block', { | ||
[ `align${ align }` ]: align, | ||
} ) } | ||
> | ||
{ showPlaceholder && ( | ||
<Placeholder | ||
icon="layout" | ||
label={ __( 'Template Part' ) } | ||
instructions={ __( 'Select a template part to display' ) } | ||
> | ||
<div className="a8c-template-part-block__selector"> | ||
<PostAutocomplete | ||
selectedPostTitle={ get( selectedPost, 'title.rendered' ) } | ||
onSelectPost={ onSelectPost } | ||
/> | ||
{ !! selectedPostId && ( | ||
<a href={ `?post=${ selectedPostId }&action=edit` }>{ __( 'Edit' ) }</a> | ||
) } | ||
</div> | ||
</Placeholder> | ||
) } | ||
{ showContent && ( | ||
<RawHTML className="a8c-template-part-block__content"> | ||
{ get( selectedPost, 'content.rendered' ) } | ||
</RawHTML> | ||
) } | ||
</div> | ||
</Fragment> | ||
); | ||
} ); | ||
|
||
export default TemplatePartEdit; |
30 changes: 30 additions & 0 deletions
30
apps/full-site-editing/full-site-editing-plugin/blocks/template-part/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
import './style.scss'; | ||
|
||
registerBlockType( 'a8c/template-part', { | ||
title: __( 'Template Part' ), | ||
description: __( 'Display a template part.' ), | ||
icon: 'layout', | ||
category: 'layout', | ||
attributes: { | ||
selectedPostId: { type: 'number' }, | ||
selectedPostType: { type: 'string' }, | ||
}, | ||
supports: { | ||
align: [ 'wide', 'full' ], | ||
anchor: true, | ||
html: false, | ||
reusable: false, | ||
}, | ||
edit, | ||
save: () => null, | ||
} ); |
17 changes: 17 additions & 0 deletions
17
apps/full-site-editing/full-site-editing-plugin/blocks/template-part/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
function render_template_part_block( $attributes ) { | ||
if ( ! isset( $attributes['selectedPostId'] ) || ! is_int( $attributes['selectedPostId'] ) ) { | ||
return; | ||
} | ||
$align = isset( $attributes['align'] ) ? ' align' . $attributes['align'] : ''; | ||
$post = get_post( $attributes['selectedPostId'] ); | ||
setup_postdata( $post ); | ||
|
||
$content = '<div class="a8c-template-part'. $align . '">' | ||
. apply_filters( 'the_content', get_the_content() ) | ||
. '</div>'; | ||
|
||
wp_reset_postdata(); | ||
return $content; | ||
} |
21 changes: 21 additions & 0 deletions
21
apps/full-site-editing/full-site-editing-plugin/blocks/template-part/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.a8c-template-part-block.alignfull { | ||
padding: 0 12px; | ||
} | ||
|
||
.a8c-template-part-block__selector { | ||
width: 300px; | ||
a { | ||
font-family: sans-serif; | ||
font-size: 13px; | ||
padding-left: 8px; | ||
} | ||
} | ||
|
||
.a8c-template-part-block__content { | ||
pointer-events: none; | ||
&::after { | ||
content: ''; | ||
clear: both; | ||
display: table; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Internal dependencies | ||
*/ | ||
import './blocks/content-slot'; | ||
import './blocks/template-part'; |