-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Template Parts: Show existing template parts and a list of block patterns at creation flow. #38814
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1a2a04e
Update template part creation and replacement flows
youknowriad 20ef770
Further simplification
youknowriad 7ea39d1
Add start blank
youknowriad a33b0ee
Fix bug
youknowriad fb31d12
Simplify headings
youknowriad 3b1eee1
Extract hooks to fetch available template parts and patterns
youknowriad 4fbb342
Move the start blank button to the placeholder
youknowriad a4e25b3
Replace blocks when template part defined
youknowriad c11e04a
Fix e2e tests
youknowriad 9992c51
Use a two columns layout
youknowriad 6ebdd90
Remove title when starting from patterns
youknowriad cb5c7b9
Consistent modal height
youknowriad 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 hidden or 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 hidden or 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,22 +15,28 @@ import { | |||||||
| store as blockEditorStore, | ||||||||
| } from '@wordpress/block-editor'; | ||||||||
| import { | ||||||||
| Dropdown, | ||||||||
| ToolbarGroup, | ||||||||
| ToolbarButton, | ||||||||
| Spinner, | ||||||||
| Modal, | ||||||||
| } from '@wordpress/components'; | ||||||||
| import { __, sprintf } from '@wordpress/i18n'; | ||||||||
| import { store as coreStore } from '@wordpress/core-data'; | ||||||||
| import { useState } from '@wordpress/element'; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Internal dependencies | ||||||||
| */ | ||||||||
| import TemplatePartPlaceholder from './placeholder'; | ||||||||
| import TemplatePartSelection from './selection'; | ||||||||
| import TemplatePartSelectionModal from './selection-modal'; | ||||||||
| import { TemplatePartAdvancedControls } from './advanced-controls'; | ||||||||
| import TemplatePartInnerBlocks from './inner-blocks'; | ||||||||
| import { createTemplatePartId } from './utils/create-template-part-id'; | ||||||||
| import { | ||||||||
| useAlternativeBlockPatterns, | ||||||||
| useAlternativeTemplateParts, | ||||||||
| useTemplatePartArea, | ||||||||
| } from './utils/hooks'; | ||||||||
|
|
||||||||
| export default function TemplatePartEdit( { | ||||||||
| attributes, | ||||||||
|
|
@@ -39,29 +45,22 @@ export default function TemplatePartEdit( { | |||||||
| } ) { | ||||||||
| const { slug, theme, tagName, layout = {} } = attributes; | ||||||||
| const templatePartId = createTemplatePartId( theme, slug ); | ||||||||
|
|
||||||||
| const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders( | ||||||||
| templatePartId | ||||||||
| ); | ||||||||
| const [ | ||||||||
| isTemplatePartSelectionOpen, | ||||||||
| setIsTemplatePartSelectionOpen, | ||||||||
| ] = useState( false ); | ||||||||
|
|
||||||||
| // Set the postId block attribute if it did not exist, | ||||||||
| // but wait until the inner blocks have loaded to allow | ||||||||
| // new edits to trigger this. | ||||||||
| const { | ||||||||
| isResolved, | ||||||||
| innerBlocks, | ||||||||
| isMissing, | ||||||||
| defaultWrapper, | ||||||||
| area, | ||||||||
| enableSelection, | ||||||||
| hasResolvedReplacements, | ||||||||
| } = useSelect( | ||||||||
| const { isResolved, innerBlocks, isMissing, area } = useSelect( | ||||||||
| ( select ) => { | ||||||||
| const { | ||||||||
| getEditedEntityRecord, | ||||||||
| getEntityRecords, | ||||||||
| hasFinishedResolution, | ||||||||
| } = select( coreStore ); | ||||||||
| const { getEditedEntityRecord, hasFinishedResolution } = select( | ||||||||
| coreStore | ||||||||
| ); | ||||||||
| const { getBlocks } = select( blockEditorStore ); | ||||||||
|
|
||||||||
| const getEntityArgs = [ | ||||||||
|
|
@@ -73,54 +72,33 @@ export default function TemplatePartEdit( { | |||||||
| ? getEditedEntityRecord( ...getEntityArgs ) | ||||||||
| : null; | ||||||||
| const _area = entityRecord?.area || attributes.area; | ||||||||
|
|
||||||||
| // Check whether other entities exist for switching/selection. | ||||||||
| const availableReplacementArgs = [ | ||||||||
| 'postType', | ||||||||
| 'wp_template_part', | ||||||||
| _area && 'uncategorized' !== _area && { area: _area }, | ||||||||
| ]; | ||||||||
| const matchingReplacements = getEntityRecords( | ||||||||
| ...availableReplacementArgs | ||||||||
| ); | ||||||||
| const _enableSelection = templatePartId | ||||||||
| ? matchingReplacements?.length > 1 | ||||||||
| : matchingReplacements?.length > 0; | ||||||||
|
|
||||||||
| const hasResolvedEntity = templatePartId | ||||||||
| ? hasFinishedResolution( | ||||||||
| 'getEditedEntityRecord', | ||||||||
| getEntityArgs | ||||||||
| ) | ||||||||
| : false; | ||||||||
|
|
||||||||
| // FIXME: @wordpress/block-library should not depend on @wordpress/editor. | ||||||||
| // Blocks can be loaded into a *non-post* block editor. | ||||||||
| // eslint-disable-next-line @wordpress/data-no-store-string-literals | ||||||||
| const defaultWrapperElement = select( 'core/editor' ) | ||||||||
| .__experimentalGetDefaultTemplatePartAreas() | ||||||||
| .find( ( { area: value } ) => value === _area )?.area_tag; | ||||||||
|
|
||||||||
| return { | ||||||||
| innerBlocks: getBlocks( clientId ), | ||||||||
| isResolved: hasResolvedEntity, | ||||||||
| isMissing: hasResolvedEntity && isEmpty( entityRecord ), | ||||||||
| defaultWrapper: defaultWrapperElement || 'div', | ||||||||
| area: _area, | ||||||||
| enableSelection: _enableSelection, | ||||||||
| hasResolvedReplacements: hasFinishedResolution( | ||||||||
| 'getEntityRecords', | ||||||||
| availableReplacementArgs | ||||||||
| ), | ||||||||
| }; | ||||||||
| }, | ||||||||
| [ templatePartId, 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 || defaultWrapper; | ||||||||
| const TagName = tagName || areaObject.tagName; | ||||||||
|
|
||||||||
| // 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. | ||||||||
|
|
@@ -160,43 +138,31 @@ export default function TemplatePartEdit( { | |||||||
| setAttributes={ setAttributes } | ||||||||
| isEntityAvailable={ isEntityAvailable } | ||||||||
| templatePartId={ templatePartId } | ||||||||
| defaultWrapper={ defaultWrapper } | ||||||||
| defaultWrapper={ areaObject.tagName } | ||||||||
| /> | ||||||||
| { isPlaceholder && ( | ||||||||
| <TagName { ...blockProps }> | ||||||||
| <TemplatePartPlaceholder | ||||||||
| area={ attributes.area } | ||||||||
| templatePartId={ templatePartId } | ||||||||
| clientId={ clientId } | ||||||||
| setAttributes={ setAttributes } | ||||||||
| enableSelection={ enableSelection } | ||||||||
| hasResolvedReplacements={ hasResolvedReplacements } | ||||||||
| onOpenSelectionModal={ () => | ||||||||
| setIsTemplatePartSelectionOpen( true ) | ||||||||
| } | ||||||||
| /> | ||||||||
| </TagName> | ||||||||
| ) } | ||||||||
| { isEntityAvailable && enableSelection && ( | ||||||||
| { isEntityAvailable && hasReplacements && ( | ||||||||
| <BlockControls> | ||||||||
| <ToolbarGroup className="wp-block-template-part__block-control-group"> | ||||||||
| <Dropdown | ||||||||
| className="wp-block-template-part__preview-dropdown-button" | ||||||||
| contentClassName="wp-block-template-part__preview-dropdown-content" | ||||||||
| position="bottom right left" | ||||||||
| renderToggle={ ( { isOpen, onToggle } ) => ( | ||||||||
| <ToolbarButton | ||||||||
| aria-expanded={ isOpen } | ||||||||
| onClick={ onToggle } | ||||||||
| > | ||||||||
| { __( 'Replace' ) } | ||||||||
| </ToolbarButton> | ||||||||
| ) } | ||||||||
| renderContent={ ( { onClose } ) => ( | ||||||||
| <TemplatePartSelection | ||||||||
| setAttributes={ setAttributes } | ||||||||
| onClose={ onClose } | ||||||||
| area={ area } | ||||||||
| templatePartId={ templatePartId } | ||||||||
| /> | ||||||||
| ) } | ||||||||
| /> | ||||||||
| <ToolbarButton | ||||||||
| onClick={ () => | ||||||||
| setIsTemplatePartSelectionOpen( true ) | ||||||||
| } | ||||||||
| > | ||||||||
| { __( 'Replace' ) } | ||||||||
| </ToolbarButton> | ||||||||
| </ToolbarGroup> | ||||||||
| </BlockControls> | ||||||||
| ) } | ||||||||
|
|
@@ -215,6 +181,30 @@ export default function TemplatePartEdit( { | |||||||
| <Spinner /> | ||||||||
| </TagName> | ||||||||
| ) } | ||||||||
| { isTemplatePartSelectionOpen && ( | ||||||||
| <Modal | ||||||||
| className="block-editor-template-part__selection-modal" | ||||||||
| title={ sprintf( | ||||||||
| // Translators: %s as template part area title ("Header", "Footer", etc.). | ||||||||
| __( 'Choose a %s' ), | ||||||||
| areaObject.label.toLowerCase() | ||||||||
| ) } | ||||||||
| closeLabel={ __( 'Cancel' ) } | ||||||||
| onRequestClose={ () => | ||||||||
| setIsTemplatePartSelectionOpen( false ) | ||||||||
| } | ||||||||
|
Contributor
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.
Suggested change
Contributor
Author
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. I tried this but the behavior is a bit weird, the width of the modal changes as the patterns load. |
||||||||
| > | ||||||||
| <TemplatePartSelectionModal | ||||||||
| templatePartId={ templatePartId } | ||||||||
| clientId={ clientId } | ||||||||
| area={ area } | ||||||||
| setAttributes={ setAttributes } | ||||||||
| onClose={ () => | ||||||||
| setIsTemplatePartSelectionOpen( false ) | ||||||||
| } | ||||||||
| /> | ||||||||
| </Modal> | ||||||||
| ) } | ||||||||
| </RecursionProvider> | ||||||||
| ); | ||||||||
| } | ||||||||
78 changes: 78 additions & 0 deletions
78
packages/block-library/src/template-part/edit/placeholder.js
This file contains hidden or 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,78 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { Placeholder, Button, Spinner } from '@wordpress/components'; | ||
| import { useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { | ||
| useAlternativeBlockPatterns, | ||
| useAlternativeTemplateParts, | ||
| useCreateTemplatePartFromBlocks, | ||
| useTemplatePartArea, | ||
| } from './utils/hooks'; | ||
| import TitleModal from './title-modal'; | ||
|
|
||
| export default function TemplatePartPlaceholder( { | ||
| area, | ||
| clientId, | ||
| templatePartId, | ||
| onOpenSelectionModal, | ||
| setAttributes, | ||
| } ) { | ||
| const { templateParts, isResolving } = useAlternativeTemplateParts( | ||
| area, | ||
| templatePartId | ||
| ); | ||
| const blockPatterns = useAlternativeBlockPatterns( area, clientId ); | ||
| const [ showTitleModal, setShowTitleModal ] = useState( false ); | ||
| const areaObject = useTemplatePartArea( area ); | ||
| const createFromBlocks = useCreateTemplatePartFromBlocks( | ||
| area, | ||
| setAttributes | ||
| ); | ||
|
|
||
| return ( | ||
| <Placeholder | ||
| icon={ areaObject.icon } | ||
| label={ areaObject.label } | ||
| instructions={ sprintf( | ||
| // Translators: %s as template part area title ("Header", "Footer", etc.). | ||
| __( 'Choose an existing %s or create a new one.' ), | ||
| areaObject.label.toLowerCase() | ||
| ) } | ||
| > | ||
| { isResolving && <Spinner /> } | ||
|
|
||
| { ! isResolving && | ||
| !! ( templateParts.length || blockPatterns.length ) && ( | ||
| <Button variant="primary" onClick={ onOpenSelectionModal }> | ||
| { __( 'Choose' ) } | ||
| </Button> | ||
| ) } | ||
|
|
||
| { ! isResolving && ( | ||
| <Button | ||
| variant="secondary" | ||
| onClick={ () => { | ||
| setShowTitleModal( true ); | ||
| } } | ||
| > | ||
| { __( 'Start blank' ) } | ||
| </Button> | ||
| ) } | ||
| { showTitleModal && ( | ||
| <TitleModal | ||
| areaLabel={ areaObject.label } | ||
| onClose={ () => setShowTitleModal( false ) } | ||
| onSubmit={ ( title ) => { | ||
| createFromBlocks( [], title ); | ||
| } } | ||
| /> | ||
| ) } | ||
| </Placeholder> | ||
| ); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.