-
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
[WIP] Initial attempt to leave a pattern block wrapped around a pattern #49967
Closed
Closed
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a7998ce
Initial attempt to leave a pattern block wrapped around a pattern
glendaviesnz 189b194
Add a wrapper div
glendaviesnz eef4498
Add rough prototype of sync versus unsynced
glendaviesnz 0e2aab1
Pattern Block: Make enhancement explorations a Gutenberg experiment
glendaviesnz dc6eaef
Add default layout attributes and change sync status to enum
glendaviesnz 00d381d
Remove wrapper div from frontend
glendaviesnz 2e9ef80
Pattern Enhancements: Render unsynced pattern content (#50114)
aaronrobertshaw 4834b46
Use a fixed custom align attrib so block toolbar align button doesn't…
glendaviesnz f4105c7
Fix issue with duplicated block toolbar and inspector controls by clo…
glendaviesnz 7e0b617
Add wrapper to frontend and add pattern slug to use as selector
glendaviesnz 6ab1c71
Add ability to sync and unsync
glendaviesnz e9e9308
Make reverting to `Sync` revert to saved pattern blocks
glendaviesnz 0d1f42e
Change default syncStatus to unsynced for barckwards compat
glendaviesnz 2fab790
Get widest align from child blocks (#50230)
glendaviesnz 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
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
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 |
---|---|---|
@@ -1,46 +1,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { cloneBlock } from '@wordpress/blocks'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { | ||
store as blockEditorStore, | ||
useBlockProps, | ||
useInnerBlocksProps, | ||
BlockControls, | ||
} from '@wordpress/block-editor'; | ||
import { ToolbarButton } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const PatternEdit = ( { attributes, clientId } ) => { | ||
const selectedPattern = useSelect( | ||
( select ) => | ||
select( blockEditorStore ).__experimentalGetParsedPattern( | ||
attributes.slug | ||
), | ||
[ attributes.slug ] | ||
const PatternEdit = ( { attributes, clientId, setAttributes } ) => { | ||
const { forcedAlignment, slug, syncStatus } = attributes; | ||
const { selectedPattern, innerBlocks } = useSelect( | ||
( select ) => { | ||
return { | ||
selectedPattern: | ||
select( blockEditorStore ).__experimentalGetParsedPattern( | ||
slug | ||
), | ||
innerBlocks: | ||
select( blockEditorStore ).getBlock( clientId ) | ||
?.innerBlocks, | ||
}; | ||
}, | ||
[ slug, clientId ] | ||
); | ||
|
||
const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } = | ||
const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = | ||
useDispatch( blockEditorStore ); | ||
|
||
// Run this effect when the component loads. | ||
// This adds the Pattern's contents to the post. | ||
// This change won't be saved. | ||
// It will continue to pull from the pattern file unless changes are made to its respective template part. | ||
useEffect( () => { | ||
if ( selectedPattern?.blocks ) { | ||
if ( selectedPattern?.blocks && ! innerBlocks?.length ) { | ||
// We batch updates to block list settings to avoid triggering cascading renders | ||
// for each container block included in a tree and optimize initial render. | ||
// Since the above uses microtasks, we need to use a microtask here as well, | ||
// because nested pattern blocks cannot be inserted if the parent block supports | ||
// inner blocks but doesn't have blockSettings in the state. | ||
window.queueMicrotask( () => { | ||
__unstableMarkNextChangeAsNotPersistent(); | ||
replaceBlocks( clientId, selectedPattern.blocks ); | ||
replaceInnerBlocks( | ||
clientId, | ||
selectedPattern.blocks.map( ( block ) => | ||
cloneBlock( block ) | ||
) | ||
); | ||
} ); | ||
} | ||
}, [ clientId, selectedPattern?.blocks ] ); | ||
}, [ | ||
clientId, | ||
selectedPattern?.blocks, | ||
replaceInnerBlocks, | ||
__unstableMarkNextChangeAsNotPersistent, | ||
innerBlocks, | ||
] ); | ||
|
||
const blockProps = useBlockProps( { | ||
className: forcedAlignment && `align${ forcedAlignment }`, | ||
} ); | ||
const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); | ||
|
||
const props = useBlockProps(); | ||
const handleSync = () => { | ||
if ( syncStatus === 'synced' ) { | ||
setAttributes( { syncStatus: 'unsynced' } ); | ||
} else { | ||
setAttributes( { syncStatus: 'synced' } ); | ||
replaceInnerBlocks( | ||
clientId, | ||
selectedPattern.blocks.map( ( block ) => cloneBlock( block ) ) | ||
); | ||
} | ||
}; | ||
|
||
return <div { ...props } />; | ||
return ( | ||
<> | ||
<div { ...innerBlocksProps } data-pattern-slug={ slug } /> | ||
<BlockControls group="other"> | ||
<ToolbarButton onClick={ handleSync }> | ||
{ syncStatus === 'unsynced' | ||
? __( 'Sync' ) | ||
: __( 'Desync' ) } | ||
</ToolbarButton> | ||
</BlockControls> | ||
</> | ||
); | ||
}; | ||
|
||
export default PatternEdit; |
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
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,14 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; | ||
|
||
export default function save( { attributes } ) { | ||
if ( attributes.syncStatus === 'synced' ) { | ||
return null; | ||
} | ||
|
||
const blockProps = useBlockProps.save(); | ||
const innerBlocksProps = useInnerBlocksProps.save( blockProps ); | ||
return <>{ innerBlocksProps.children }</>; | ||
} |
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,51 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { | ||
store as blockEditorStore, | ||
useBlockProps, | ||
} from '@wordpress/block-editor'; | ||
|
||
const PatternEdit = ( { attributes, clientId } ) => { | ||
const selectedPattern = useSelect( | ||
( select ) => | ||
select( blockEditorStore ).__experimentalGetParsedPattern( | ||
attributes.slug | ||
), | ||
[ attributes.slug ] | ||
); | ||
|
||
const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } = | ||
useDispatch( blockEditorStore ); | ||
|
||
// Run this effect when the component loads. | ||
// This adds the Pattern's contents to the post. | ||
// This change won't be saved. | ||
// It will continue to pull from the pattern file unless changes are made to its respective template part. | ||
useEffect( () => { | ||
if ( selectedPattern?.blocks ) { | ||
// We batch updates to block list settings to avoid triggering cascading renders | ||
// for each container block included in a tree and optimize initial render. | ||
// Since the above uses microtasks, we need to use a microtask here as well, | ||
// because nested pattern blocks cannot be inserted if the parent block supports | ||
// inner blocks but doesn't have blockSettings in the state. | ||
window.queueMicrotask( () => { | ||
__unstableMarkNextChangeAsNotPersistent(); | ||
replaceBlocks( clientId, selectedPattern.blocks ); | ||
} ); | ||
} | ||
}, [ | ||
clientId, | ||
selectedPattern?.blocks, | ||
__unstableMarkNextChangeAsNotPersistent, | ||
replaceBlocks, | ||
] ); | ||
|
||
const props = useBlockProps(); | ||
|
||
return <div { ...props } />; | ||
}; | ||
|
||
export default PatternEdit; |
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
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.
Curious to know why we want to allow the pattern block to be shown in the inserter? Moreover, should it be allowed to create a pattern block without the
slug
attribute? What's the difference between it and a group block?