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: Try inner blocks syncing for editable content attributes #50901

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
68 changes: 61 additions & 7 deletions packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
useBlockProps,
useInnerBlocksProps,
} from '@wordpress/block-editor';
import { Placeholder, Spinner } from '@wordpress/components';

const PatternEdit = ( { attributes, clientId } ) => {
function PatternFullEdit( { attributes, clientId } ) {
const { slug, syncStatus } = attributes;
const { selectedPattern, innerBlocks } = useSelect(
( select ) => {
Expand Down Expand Up @@ -69,15 +70,68 @@ const PatternEdit = ( { attributes, clientId } ) => {
className: slug?.replace( '/', '-' ),
} );

return <div { ...blockProps } />;
}

function PatternPartialEdit( { attributes: { slug }, clientId } ) {
const { designPattern } = useSelect(
( select ) => {
return {
designPattern:
select( blockEditorStore ).__experimentalGetParsedPattern(
slug
),
/*innerBlocks:
select( blockEditorStore ).getBlock( clientId )
?.innerBlocks,*/
};
},
[ slug ]
);

const blockProps = useBlockProps( {
className: slug?.replace( '/', '-' ),
} );
if ( ! designPattern?.blocks?.length ) {
return (
<div { ...blockProps }>
<Placeholder>
<Spinner />
</Placeholder>
</div>
);
}

return (
<PatternInnerBlocks
clientId={ clientId }
blockProps={ blockProps }
template={ designPattern.blocks }
/>
);
}

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

if ( syncStatus !== 'partial' ) {
return <div { ...blockProps } />;
}
const { replaceInnerBlocks } = useDispatch( blockEditorStore );

useEffect( () => {
const blocks = template.map( ( block ) => cloneBlock( block ) );
replaceInnerBlocks( clientId, blocks );
}, [ clientId, replaceInnerBlocks, template ] );

return <div { ...innerBlocksProps } />;
};
}

export default PatternEdit;
export default function PatternEdit( props ) {
const { syncStatus } = props.attributes;

return syncStatus === 'partial' ? (
<PatternPartialEdit { ...props } />
) : (
<PatternFullEdit { ...props } />
);
}
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 } );
12 changes: 12 additions & 0 deletions packages/block-library/src/pattern/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

export default function save( { attributes: { syncStatus } } ) {
if ( syncStatus !== 'partial' ) {
return null;
}

return <InnerBlocks.Content />;
}