Skip to content

Commit

Permalink
Add nested pattern-part blocks to all children of a pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz committed May 24, 2023
1 parent d59075d commit 7d5160f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/block-library/src/pattern-part/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';
export default function Edit() {
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps );
return <>{ innerBlocksProps.children }</>;
console.log( 'innerBlocksProps', innerBlocksProps.children );
return <div { ...innerBlocksProps }></div>;
}
6 changes: 4 additions & 2 deletions packages/block-library/src/pattern-part/save.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';

export default function save() {
return <InnerBlocks.Content />;
const blockProps = useBlockProps.save();
const innerBlocksProps = useInnerBlocksProps.save( blockProps );
return <div { ...innerBlocksProps }>{ innerBlocksProps.children }</div>;
}
3 changes: 3 additions & 0 deletions packages/block-library/src/pattern-part/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wp-block-pattern-part {
display: contents;
}
37 changes: 21 additions & 16 deletions packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ const PatternEdit = ( { attributes, clientId } ) => {
// Run this effect when the component loads.
// This adds the Pattern's contents to the post.
useEffect( () => {
function clonePatternBlock( block ) {
let newInnerBlocks = [];
if ( block.innerBlocks.length > 0 ) {
newInnerBlocks = block.innerBlocks.map( ( innerBlock ) => {
let nestedInnerBlocks = [];
if ( innerBlock.innerBlocks.length > 0 ) {
nestedInnerBlocks =
innerBlock.innerBlocks.map( clonePatternBlock );
}
return createBlock( 'core/pattern-part', {}, [
cloneBlock( innerBlock, {}, nestedInnerBlocks ),
] );
} );
}
return createBlock( 'core/pattern-part', {}, [
cloneBlock( block, {}, newInnerBlocks ),
] );
}
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.
Expand All @@ -44,21 +62,8 @@ const PatternEdit = ( { attributes, clientId } ) => {
window.queueMicrotask( () => {
// Clone blocks from the pattern before insertion to ensure they receive
// distinct client ids. See https://github.com/WordPress/gutenberg/issues/50628.
const clonedBlocks = selectedPattern.blocks.map( ( block ) => {
let newInnerBlocks = [];
if ( block.innerBlocks.length > 0 ) {
newInnerBlocks = block.innerBlocks.map(
( innerBlock ) => {
return createBlock( 'core/pattern-part', {}, [
cloneBlock( innerBlock ),
] );
}
);
}
return createBlock( 'core/pattern-part', {}, [
cloneBlock( block, {}, newInnerBlocks ),
] );
} );
const clonedBlocks =
selectedPattern.blocks.map( clonePatternBlock );
__unstableMarkNextChangeAsNotPersistent();
if ( syncStatus === 'partial' ) {
replaceInnerBlocks( clientId, clonedBlocks );
Expand All @@ -82,7 +87,7 @@ const PatternEdit = ( { attributes, clientId } ) => {
} );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
templateLock: syncStatus === 'partial' ? 'contentOnly' : false,
templateLock: syncStatus === 'partial' ? false : false,
} );

if ( syncStatus !== 'partial' ) {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@import "./navigation-link/style.scss";
@import "./page-list/style.scss";
@import "./paragraph/style.scss";
@import "./pattern-part/style.scss";
@import "./post-author/style.scss";
@import "./post-comments-form/style.scss";
@import "./post-date/style.scss";
Expand Down

0 comments on commit 7d5160f

Please sign in to comment.