Skip to content
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

Pattern block: Try adding Pattern Part block #50899

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function gutenberg_reregister_core_block_types() {
'more',
'nextpage',
'paragraph',
'pattern-part',
'preformatted',
'pullquote',
'quote',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import * as navigationLink from './navigation-link';
import * as navigationSubmenu from './navigation-submenu';
import * as nextpage from './nextpage';
import * as pattern from './pattern';
import * as patternPart from './pattern-part';
import * as pageList from './page-list';
import * as pageListItem from './page-list-item';
import * as paragraph from './paragraph';
Expand Down Expand Up @@ -160,6 +161,7 @@ const getAllBlocks = () => {
pageList,
pageListItem,
pattern,
patternPart,
preformatted,
pullquote,
reusableBlock,
Expand Down
19 changes: 19 additions & 0 deletions packages/block-library/src/pattern-part/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/pattern-part",
"title": "Pattern part",
"category": "text",
"description": "A wrapper for a pattern's inner blocks",
"keywords": [ "bob" ],
"textdomain": "default",
"attributes": {
"slug": {
"type": "string"
}
},
"supports": {
"html": false,
"inserter": false
}
}
10 changes: 10 additions & 0 deletions packages/block-library/src/pattern-part/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* WordPress dependencies
*/
import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';
export default function Edit() {
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps );
console.log( 'innerBlocksProps', innerBlocksProps.children );
return <div { ...innerBlocksProps }></div>;
}
19 changes: 19 additions & 0 deletions packages/block-library/src/pattern-part/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import edit from './edit';
import metadata from './block.json';
import save from './save';

const { name } = metadata;

export { metadata, name };

const settings = {
edit,
save,
};

export { settings };
export const init = () => initBlock( { name, metadata, settings } );
6 changes: 6 additions & 0 deletions packages/block-library/src/pattern-part/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Internal dependencies
*/
import { init } from './';

export default init();
10 changes: 10 additions & 0 deletions packages/block-library/src/pattern-part/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* WordPress dependencies
*/
import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';

export default function save() {
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;
}
27 changes: 22 additions & 5 deletions packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { cloneBlock } from '@wordpress/blocks';
import { cloneBlock, createBlock } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import {
Expand Down 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,9 +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 ) =>
cloneBlock( block )
);
const clonedBlocks =
selectedPattern.blocks.map( clonePatternBlock );
__unstableMarkNextChangeAsNotPersistent();
if ( syncStatus === 'partial' ) {
replaceInnerBlocks( clientId, clonedBlocks );
Expand All @@ -70,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
3 changes: 2 additions & 1 deletion packages/block-library/src/pattern/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import initBlock from '../utils/init-block';
import metadata from './block.json';
import PatternEditV1 from './v1/edit';
import PatternEditV2 from './edit';
import save from './save';

const { name } = metadata;
export { metadata, name };

export const settings = window?.__experimentalEnablePatternEnhancements
? { edit: PatternEditV2 }
? { edit: PatternEditV2, save }
: { edit: PatternEditV1 };

export const init = () => initBlock( { name, metadata, settings } );
19 changes: 19 additions & 0 deletions packages/block-library/src/pattern/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* WordPress dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';

export default function save( {
attributes: { syncStatus, slug },
innerBlocks,
} ) {
if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) {
return;
}
//test
const blockProps = useBlockProps.save( {
className: slug?.replace( '/', '-' ),
} );
const innerBlocksProps = useInnerBlocksProps.save( blockProps );
return <div { ...innerBlocksProps }>{ innerBlocksProps.children }</div>;
}
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