-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Template inspector: Surface related patterns #55091
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { PanelBody } from '@wordpress/components'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { PanelBody, PanelRow } from '@wordpress/components'; | ||
import { | ||
PageAttributesPanel, | ||
PostDiscussionPanel, | ||
|
@@ -15,6 +15,10 @@ import { | |
import { store as coreStore } from '@wordpress/core-data'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { navigation, symbol } from '@wordpress/icons'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useAsyncList } from '@wordpress/compose'; | ||
import { serialize } from '@wordpress/blocks'; | ||
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -23,36 +27,71 @@ import { store as editSiteStore } from '../../../store'; | |
import TemplateActions from './template-actions'; | ||
import TemplateAreas from './template-areas'; | ||
import SidebarCard from '../sidebar-card'; | ||
import { useAvailablePatterns } from './hooks'; | ||
import { TEMPLATE_PART_POST_TYPE } from '../../../utils/constants'; | ||
|
||
const CARD_ICONS = { | ||
wp_block: symbol, | ||
wp_navigation: navigation, | ||
}; | ||
|
||
function TemplatesList( { availableTemplates, onSelect } ) { | ||
const shownTemplates = useAsyncList( availableTemplates ); | ||
if ( ! availableTemplates || availableTemplates?.length < 2 ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, to help with performance, in the other places that we list patterns we have added client-side pagination, eg. https://github.com/WordPress/gutenberg/blob/trunk/packages/edit-site/src/components/page-patterns/patterns-list.js#L210, you may want to think about also adding it here, but could be a follow up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When displaying patterns for a particular template or a part of it, we anticipate that we won't have to show too many patterns. However, if this does become a problem, we can revisit the idea of adding pagination in the future. |
||
<BlockPatternsList | ||
label={ __( 'Templates' ) } | ||
blockPatterns={ availableTemplates } | ||
shownPatterns={ shownTemplates } | ||
onClickPattern={ onSelect } | ||
showTitlesAsTooltip={ true } | ||
/> | ||
); | ||
} | ||
|
||
export default function TemplatePanel() { | ||
const { title, description, icon, record } = useSelect( ( select ) => { | ||
const { getEditedPostType, getEditedPostId } = select( editSiteStore ); | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const { __experimentalGetTemplateInfo: getTemplateInfo } = | ||
select( editorStore ); | ||
const { title, description, icon, record, postType, postId } = useSelect( | ||
( select ) => { | ||
const { getEditedPostType, getEditedPostId } = | ||
select( editSiteStore ); | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const { __experimentalGetTemplateInfo: getTemplateInfo } = | ||
select( editorStore ); | ||
|
||
const type = getEditedPostType(); | ||
const _postId = getEditedPostId(); | ||
const _record = getEditedEntityRecord( 'postType', type, _postId ); | ||
const info = getTemplateInfo( _record ); | ||
|
||
const type = getEditedPostType(); | ||
const postId = getEditedPostId(); | ||
const _record = getEditedEntityRecord( 'postType', type, postId ); | ||
const info = getTemplateInfo( _record ); | ||
return { | ||
title: info.title, | ||
description: info.description, | ||
icon: info.icon, | ||
record: _record, | ||
postType: type, | ||
postId: _postId, | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
return { | ||
title: info.title, | ||
description: info.description, | ||
icon: info.icon, | ||
record: _record, | ||
}; | ||
}, [] ); | ||
const availablePatterns = useAvailablePatterns( record ); | ||
const { editEntityRecord } = useDispatch( coreStore ); | ||
|
||
if ( ! title && ! description ) { | ||
return null; | ||
} | ||
|
||
const onTemplateSelect = async ( selectedTemplate ) => { | ||
await editEntityRecord( 'postType', postType, postId, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot directly mutate a record as that way the store update would not propagate. Instead, we need to call editEntityRecord as we are doing currently. |
||
blocks: selectedTemplate.blocks, | ||
content: serialize( selectedTemplate.blocks ), | ||
} ); | ||
}; | ||
|
||
return ( | ||
<> | ||
<PanelBody> | ||
|
@@ -66,6 +105,22 @@ export default function TemplatePanel() { | |
<TemplateAreas /> | ||
</SidebarCard> | ||
</PanelBody> | ||
<PanelBody | ||
title={ __( 'Transform into:' ) } | ||
initialOpen={ postType === TEMPLATE_PART_POST_TYPE } | ||
> | ||
<PanelRow> | ||
<p> | ||
{ __( | ||
'Choose a predefined pattern to switch up the look of your template.' // TODO - make this dynamic? | ||
) } | ||
</p> | ||
</PanelRow> | ||
<TemplatesList | ||
availableTemplates={ availablePatterns } | ||
onSelect={ onTemplateSelect } | ||
/> | ||
</PanelBody> | ||
<PostLastRevisionPanel /> | ||
<PostTaxonomiesPanel /> | ||
<PostFeaturedImagePanel /> | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also filter out the currently used pattern? Can we if we don't know which one it is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's possible to determine the pattern used after it has been applied as there is no information available about the pattern used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can save the slug in the metadata like we do in #59251 - then we will have a way to know.