Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add nested pattern-part blocks to all children of a pattern
  • Loading branch information
glendaviesnz committed May 24, 2023
commit 7d5160f7b1d044cde5e482a7367624ba0f779823
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