2020-06-05 12:18:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-07-14 11:35:15 +00:00
|
|
|
import { Suspense, cloneElement, isValidElement } from '@wordpress/element';
|
2020-06-05 12:18:16 +00:00
|
|
|
import parse from 'html-react-parser';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getBlockMap } from './get-block-map';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces saved block HTML markup with Inner Block Components.
|
|
|
|
*
|
|
|
|
* @param {Object} props Render props.
|
|
|
|
* @param {Array} props.children Children/inner blocks to render.
|
|
|
|
* @param {string} props.blockName Parent Block Name used to get the block map and for keys.
|
|
|
|
* @param {number} [props.depth] Depth of inner blocks being rendered.
|
|
|
|
*/
|
|
|
|
export const renderInnerBlocks = ( {
|
|
|
|
children,
|
|
|
|
blockName: parentBlockName,
|
|
|
|
depth = 1,
|
|
|
|
} ) => {
|
|
|
|
const blockMap = getBlockMap( parentBlockName );
|
|
|
|
|
|
|
|
return Array.from( children ).map( ( el, index ) => {
|
2020-07-22 12:20:54 +00:00
|
|
|
const componentProps = {
|
|
|
|
...el.dataset,
|
|
|
|
key: `${ parentBlockName }_${ depth }_${ index }`,
|
|
|
|
};
|
2020-06-05 12:18:16 +00:00
|
|
|
|
|
|
|
const componentChildren =
|
|
|
|
el.children && el.children.length
|
|
|
|
? renderInnerBlocks( {
|
|
|
|
children: el.children,
|
|
|
|
blockName: parentBlockName,
|
|
|
|
depth: depth + 1,
|
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const LayoutComponent =
|
|
|
|
componentProps.blockName && blockMap[ componentProps.blockName ]
|
|
|
|
? blockMap[ componentProps.blockName ]
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ( ! LayoutComponent ) {
|
|
|
|
const element = parse( el.outerHTML );
|
|
|
|
|
|
|
|
if ( isValidElement( element ) ) {
|
|
|
|
return componentChildren
|
|
|
|
? cloneElement( element, componentProps, componentChildren )
|
|
|
|
: cloneElement( element, componentProps );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-07-14 11:35:15 +00:00
|
|
|
<Suspense
|
2020-07-22 12:20:54 +00:00
|
|
|
key={ `${ parentBlockName }_${ depth }_${ index }_suspense` }
|
2020-07-14 11:35:15 +00:00
|
|
|
fallback={ <div className="wc-block-placeholder" /> }
|
|
|
|
>
|
|
|
|
<LayoutComponent { ...componentProps }>
|
|
|
|
{ componentChildren }
|
|
|
|
</LayoutComponent>
|
|
|
|
</Suspense>
|
2020-06-05 12:18:16 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|