Skip to content

Commit

Permalink
Add hard code PoC to update inner blocks based on serialised blocks i…
Browse files Browse the repository at this point in the history
…n pattern parent attribs
  • Loading branch information
glendaviesnz committed Nov 15, 2023
1 parent 5e02b61 commit 7c24dec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/block-editor/src/hooks/custom-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) => {
// Check if the current block is a paragraph or image block.
// Currently, only these two blocks are supported.
if (
! [ 'core/paragraph', 'core/image', 'core/heading' ].includes(
props.name
)
! [
'core/paragraph',
'core/image',
'core/heading',
'core/list',
].includes( props.name )
) {
return <BlockEdit { ...props } />;
}
Expand All @@ -71,6 +74,7 @@ const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) => {
if ( props.name === 'core/paragraph' ) attributeName = 'content';
if ( props.name === 'core/image' ) attributeName = 'url';
if ( props.name === 'core/heading' ) attributeName = 'content';
if ( props.name === 'core/list' ) attributeName = 'innerBlocks';

const connectionSource =
props.attributes?.connections?.attributes?.[ attributeName ]
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
}
},
"supports": {
"__experimentalConnections": true,
"anchor": true,
"className": false,
"typography": {
Expand Down
37 changes: 37 additions & 0 deletions packages/block-library/src/list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,43 @@ function IndentUI( { clientId } ) {

export default function Edit( { attributes, setAttributes, clientId, style } ) {
const { ordered, type, reversed, start } = attributes;

const { replaceInnerBlocks } = useDispatch( blockEditorStore );
const { patternParentAttributes } = useSelect( ( select ) => {
const { getBlockParentsByBlockName, getBlockAttributes } =
select( blockEditorStore );
const parent = getBlockParentsByBlockName(
clientId,
'core/block'
)?.[ 0 ];
return {
patternParent: parent,
patternParentAttributes: getBlockAttributes( parent ),
};
} );

useEffect( () => {
if (
Array.isArray( patternParentAttributes?.dynamicContent?.myList )
) {
const patternInstanceInnerBlocks =
patternParentAttributes?.dynamicContent?.myList.map(
( block ) => {
return createBlock(
block.name,
block.attributes,
block.innerBlocks
);
}
);
replaceInnerBlocks( clientId, patternInstanceInnerBlocks );
}
}, [
clientId,
patternParentAttributes?.dynamicContent?.myList,
replaceInnerBlocks,
] );

const blockProps = useBlockProps( {
style: {
...( Platform.isNative && style ),
Expand Down

0 comments on commit 7c24dec

Please sign in to comment.