-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 Parts: Add a replace flow to the inspector controls #55128
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3168b2d
Template Parts: Add a replace flow to the inspector controls
scruffian 5fbe0e8
create a function to share the code that creates block patterns from …
scruffian b9959b2
fix the template part selection
scruffian fc12186
show patterns either way
scruffian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
BlockSettingsMenuControls, | ||
useBlockProps, | ||
|
@@ -10,11 +10,14 @@ import { | |
RecursionProvider, | ||
useHasRecursion, | ||
InspectorControls, | ||
__experimentalBlockPatternsList as BlockPatternsList, | ||
} from '@wordpress/block-editor'; | ||
import { Spinner, Modal, MenuItem } from '@wordpress/components'; | ||
import { PanelBody, Spinner, Modal, MenuItem } from '@wordpress/components'; | ||
import { useAsyncList } from '@wordpress/compose'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useState } from '@wordpress/element'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -24,10 +27,12 @@ import TemplatePartSelectionModal from './selection-modal'; | |
import { TemplatePartAdvancedControls } from './advanced-controls'; | ||
import TemplatePartInnerBlocks from './inner-blocks'; | ||
import { createTemplatePartId } from './utils/create-template-part-id'; | ||
import { mapTemplatePartToBlockPattern } from './utils/map-template-part-to-block-pattern'; | ||
import { | ||
useAlternativeBlockPatterns, | ||
useAlternativeTemplateParts, | ||
useTemplatePartArea, | ||
useCreateTemplatePartFromBlocks, | ||
} from './utils/hooks'; | ||
|
||
function ReplaceButton( { | ||
|
@@ -43,7 +48,6 @@ function ReplaceButton( { | |
templatePartId | ||
); | ||
const blockPatterns = useAlternativeBlockPatterns( area, clientId ); | ||
|
||
const hasReplacements = !! templateParts.length || !! blockPatterns.length; | ||
const canReplace = | ||
isEntityAvailable && | ||
|
@@ -67,11 +71,29 @@ function ReplaceButton( { | |
); | ||
} | ||
|
||
function TemplatesList( { availableTemplates, onSelect } ) { | ||
const shownTemplates = useAsyncList( availableTemplates ); | ||
|
||
if ( ! availableTemplates ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockPatternsList | ||
label={ __( 'Templates' ) } | ||
blockPatterns={ availableTemplates } | ||
shownPatterns={ shownTemplates } | ||
onClickPattern={ onSelect } | ||
/> | ||
); | ||
} | ||
|
||
export default function TemplatePartEdit( { | ||
attributes, | ||
setAttributes, | ||
clientId, | ||
} ) { | ||
const { createSuccessNotice } = useDispatch( noticesStore ); | ||
const currentTheme = useSelect( | ||
( select ) => select( coreStore ).getCurrentTheme()?.stylesheet, | ||
[] | ||
|
@@ -117,12 +139,28 @@ export default function TemplatePartEdit( { | |
[ templatePartId, attributes.area, clientId ] | ||
); | ||
|
||
const { templateParts } = useAlternativeTemplateParts( | ||
area, | ||
templatePartId | ||
); | ||
const blockPatterns = useAlternativeBlockPatterns( area, clientId ); | ||
const hasReplacements = !! templateParts.length || !! blockPatterns.length; | ||
const areaObject = useTemplatePartArea( area ); | ||
const blockProps = useBlockProps(); | ||
const isPlaceholder = ! slug; | ||
const isEntityAvailable = ! isPlaceholder && ! isMissing && isResolved; | ||
const TagName = tagName || areaObject.tagName; | ||
|
||
const canReplace = | ||
isEntityAvailable && | ||
hasReplacements && | ||
( area === 'header' || area === 'footer' ); | ||
|
||
const createFromBlocks = useCreateTemplatePartFromBlocks( | ||
area, | ||
setAttributes | ||
); | ||
|
||
// We don't want to render a missing state if we have any inner blocks. | ||
// A new template part is automatically created if we have any inner blocks but no entity. | ||
if ( | ||
|
@@ -154,6 +192,28 @@ export default function TemplatePartEdit( { | |
); | ||
} | ||
|
||
const partsAsPatterns = templateParts.map( ( templatePart ) => | ||
mapTemplatePartToBlockPattern( templatePart ) | ||
); | ||
|
||
const onTemplatePartSelect = ( templatePart ) => { | ||
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. This is also defined in packages/block-library/src/template-part/edit/selection-modal.js, but I think they are simple enough that it's ok. |
||
setAttributes( { | ||
slug: templatePart.slug, | ||
theme: templatePart.theme, | ||
area: undefined, | ||
} ); | ||
createSuccessNotice( | ||
sprintf( | ||
/* translators: %s: template part title. */ | ||
__( 'Template Part "%s" replaceed.' ), | ||
templatePart.title?.rendered || templatePart.slug | ||
), | ||
{ | ||
type: 'snackbar', | ||
} | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<RecursionProvider uniqueId={ templatePartId }> | ||
|
@@ -207,6 +267,33 @@ export default function TemplatePartEdit( { | |
); | ||
} } | ||
</BlockSettingsMenuControls> | ||
|
||
{ canReplace && | ||
( partsAsPatterns.length > 0 || | ||
blockPatterns.length > 0 ) && ( | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Replace' ) }> | ||
<TemplatesList | ||
availableTemplates={ partsAsPatterns } | ||
onSelect={ ( pattern ) => { | ||
onTemplatePartSelect( | ||
pattern.templatePart | ||
); | ||
} } | ||
/> | ||
<TemplatesList | ||
availableTemplates={ blockPatterns } | ||
onSelect={ ( pattern, blocks ) => { | ||
createFromBlocks( | ||
blocks, | ||
pattern.title | ||
); | ||
} } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
) } | ||
|
||
{ isEntityAvailable && ( | ||
<TemplatePartInnerBlocks | ||
tagName={ TagName } | ||
|
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
23 changes: 23 additions & 0 deletions
23
packages/block-library/src/template-part/edit/utils/map-template-part-to-block-pattern.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,23 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { parse } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createTemplatePartId } from './create-template-part-id'; | ||
|
||
/** | ||
* This maps the properties of a template part to those of a block pattern. | ||
* @param {Object} templatePart | ||
* @return {Object} The template part in the shape of block pattern. | ||
*/ | ||
export function mapTemplatePartToBlockPattern( templatePart ) { | ||
return { | ||
name: createTemplatePartId( templatePart.theme, templatePart.slug ), | ||
title: templatePart.title.rendered, | ||
blocks: parse( templatePart.content.raw ), | ||
templatePart, | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This sort of reverted the changes I made here: #57856. I'll make a PR to fix it.