2021-06-11 08:54:18 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { renderFrontend } from '@woocommerce/base-utils';
|
|
|
|
import {
|
|
|
|
Fragment,
|
|
|
|
Suspense,
|
|
|
|
isValidElement,
|
2021-09-03 13:25:09 +00:00
|
|
|
cloneElement,
|
2021-06-11 08:54:18 +00:00
|
|
|
} from '@wordpress/element';
|
|
|
|
import parse from 'html-react-parser';
|
2021-09-03 13:25:09 +00:00
|
|
|
import {
|
|
|
|
getRegisteredBlocks,
|
|
|
|
isInnerBlockArea,
|
|
|
|
} from '@woocommerce/blocks-checkout';
|
2021-06-11 08:54:18 +00:00
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* This file contains logic used on the frontend to convert DOM elements (saved by the block editor) to React
|
|
|
|
* Components. These components are registered using registerBlockComponent() and registerCheckoutBlock() and map 1:1
|
|
|
|
* to a block by name.
|
|
|
|
*
|
|
|
|
* Blocks using this system will have their blockName stored as a data attribute, for example:
|
|
|
|
* <div data-block-name="woocommerce/product-title"></div>
|
|
|
|
*
|
|
|
|
* This block name is then read, and using the map, dynamically converted to a real React Component.
|
|
|
|
*
|
|
|
|
* @see registerBlockComponent
|
|
|
|
* @see registerCheckoutBlock
|
|
|
|
*/
|
2021-06-11 08:54:18 +00:00
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* Gets a component from the block map for a given block name, or returns null if a component is not registered.
|
|
|
|
*/
|
|
|
|
const getBlockComponentFromMap = (
|
|
|
|
blockName: string,
|
|
|
|
blockMap: Record< string, React.ReactNode >
|
|
|
|
): React.ElementType | null => {
|
|
|
|
return blockName && blockMap[ blockName ]
|
|
|
|
? ( blockMap[ blockName ] as React.ElementType )
|
|
|
|
: null;
|
|
|
|
};
|
2021-06-11 08:54:18 +00:00
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* Render forced blocks which are missing from the template.
|
|
|
|
*
|
|
|
|
* Forced blocks are registered in registerCheckoutBlock. If a block is forced, it will be inserted in the editor
|
|
|
|
* automatically, however, until that happens they may be missing from the frontend. To fix this, we look up what blocks
|
|
|
|
* are registered as forced, and then append them here if they are missing.
|
|
|
|
*
|
|
|
|
* @see registerCheckoutBlock
|
|
|
|
*/
|
|
|
|
const renderForcedBlocks = (
|
|
|
|
blockName: string,
|
|
|
|
blockMap: Record< string, React.ReactNode >,
|
|
|
|
// Current children from the parent (siblings of the forced block)
|
|
|
|
blockChildren: HTMLCollection | null
|
|
|
|
) => {
|
|
|
|
if ( ! isInnerBlockArea( blockName ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentBlocks = blockChildren
|
|
|
|
? ( Array.from( blockChildren )
|
|
|
|
.map( ( element: Element ) =>
|
|
|
|
element instanceof HTMLElement
|
|
|
|
? element?.dataset.blockName || null
|
|
|
|
: null
|
|
|
|
)
|
|
|
|
.filter( Boolean ) as string[] )
|
|
|
|
: [];
|
|
|
|
|
|
|
|
const forcedBlocks = getRegisteredBlocks( blockName ).filter(
|
|
|
|
( { block, force } ) =>
|
|
|
|
force === true && ! currentBlocks.includes( block )
|
|
|
|
);
|
|
|
|
|
|
|
|
return forcedBlocks.map(
|
|
|
|
( { block, component }, index: number ): JSX.Element | null => {
|
|
|
|
const ForcedComponent = component
|
|
|
|
? component
|
|
|
|
: getBlockComponentFromMap( block, blockMap );
|
|
|
|
return ForcedComponent ? (
|
|
|
|
<ForcedComponent key={ `${ blockName }_forced_${ index }` } />
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2021-06-11 08:54:18 +00:00
|
|
|
|
|
|
|
/**
|
2021-09-03 13:25:09 +00:00
|
|
|
* Recursively replace block markup in the DOM with React Components.
|
2021-06-11 08:54:18 +00:00
|
|
|
*/
|
|
|
|
const renderInnerBlocks = ( {
|
2021-09-03 13:25:09 +00:00
|
|
|
// This is the parent block we're working within (see renderParentBlock)
|
2021-06-11 08:54:18 +00:00
|
|
|
blockName: parentBlockName,
|
2021-09-03 13:25:09 +00:00
|
|
|
// This is the map of blockNames->components
|
2021-06-11 08:54:18 +00:00
|
|
|
blockMap,
|
2021-09-03 13:25:09 +00:00
|
|
|
// Component which inner blocks are wrapped with.
|
2021-06-11 08:54:18 +00:00
|
|
|
blockWrapper,
|
2021-09-03 13:25:09 +00:00
|
|
|
// The children from the DOM we're currently iterating over.
|
2021-06-11 08:54:18 +00:00
|
|
|
children,
|
2021-09-03 13:25:09 +00:00
|
|
|
// Current depth of the children. Used to ensure keys are unique.
|
|
|
|
depth = 1,
|
|
|
|
}: {
|
|
|
|
// Parent Block Name. Used for inner block component mapping.
|
|
|
|
blockName: string;
|
|
|
|
// Map of block names to block components for children.
|
|
|
|
blockMap: Record< string, React.ReactNode >;
|
|
|
|
// Wrapper for inner components.
|
|
|
|
blockWrapper?: React.ElementType;
|
|
|
|
// Elements from the DOM being converted to components.
|
|
|
|
children: HTMLCollection;
|
|
|
|
// Depth within the DOM hierarchy.
|
|
|
|
depth?: number;
|
|
|
|
} ): ( JSX.Element | null )[] | null => {
|
|
|
|
if ( ! children || children.length === 0 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return Array.from( children ).map( ( element: Element, index: number ) => {
|
|
|
|
/**
|
|
|
|
* This will grab the blockName from the data- attributes stored in block markup. Without a blockName, we cannot
|
|
|
|
* convert the HTMLElement to a React component.
|
|
|
|
*/
|
2021-06-11 08:54:18 +00:00
|
|
|
const { blockName = '', ...componentProps } = {
|
|
|
|
key: `${ parentBlockName }_${ depth }_${ index }`,
|
2021-09-03 13:25:09 +00:00
|
|
|
...( element instanceof HTMLElement ? element.dataset : {} ),
|
2021-06-11 08:54:18 +00:00
|
|
|
};
|
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
const InnerBlockComponent = getBlockComponentFromMap(
|
|
|
|
blockName,
|
|
|
|
blockMap
|
|
|
|
);
|
2021-06-11 08:54:18 +00:00
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* If the component cannot be found, or blockName is missing, return the original element. This also ensures
|
|
|
|
* that children within the element are processed also, since it may be an element containing block markup.
|
|
|
|
*/
|
|
|
|
if ( ! InnerBlockComponent ) {
|
|
|
|
const parsedElement = parse( element.outerHTML );
|
2021-06-11 08:54:18 +00:00
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
if ( isValidElement( parsedElement ) ) {
|
|
|
|
const elementChildren =
|
|
|
|
element.children && element.children.length
|
|
|
|
? renderInnerBlocks( {
|
|
|
|
children: element.children,
|
|
|
|
blockName: parentBlockName,
|
|
|
|
blockMap,
|
|
|
|
depth: depth + 1,
|
|
|
|
blockWrapper,
|
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
return elementChildren
|
|
|
|
? cloneElement(
|
|
|
|
parsedElement,
|
|
|
|
componentProps,
|
|
|
|
elementChildren
|
|
|
|
)
|
|
|
|
: cloneElement( parsedElement, componentProps );
|
2021-06-11 08:54:18 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
// This will wrap inner blocks with the provided wrapper. If no wrapper is provided, we default to Fragment.
|
|
|
|
const InnerBlockComponentWrapper = blockWrapper
|
2021-06-11 08:54:18 +00:00
|
|
|
? blockWrapper
|
2021-09-03 13:25:09 +00:00
|
|
|
: Fragment;
|
2021-06-11 08:54:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Suspense
|
|
|
|
key={ `${ parentBlockName }_${ depth }_${ index }_suspense` }
|
|
|
|
fallback={ <div className="wc-block-placeholder" /> }
|
|
|
|
>
|
2021-09-03 13:25:09 +00:00
|
|
|
<InnerBlockComponentWrapper>
|
|
|
|
<InnerBlockComponent { ...componentProps }>
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Within this Inner Block Component we also need to recursively render it's children. This
|
|
|
|
* is done here with a depth+1. The same block map and parent is used, but we pass new
|
|
|
|
* children from this element.
|
|
|
|
*/
|
|
|
|
renderInnerBlocks( {
|
|
|
|
children: element.children,
|
|
|
|
blockName: parentBlockName,
|
|
|
|
blockMap,
|
|
|
|
depth: depth + 1,
|
|
|
|
blockWrapper,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* In addition to the inner blocks, we may also need to render FORCED blocks which have not
|
|
|
|
* yet been added to the inner block template. We do this by comparing the current children
|
|
|
|
* to the list of registered forced blocks.
|
|
|
|
*
|
|
|
|
* @see registerCheckoutBlock
|
|
|
|
*/
|
|
|
|
renderForcedBlocks(
|
|
|
|
blockName,
|
|
|
|
blockMap,
|
|
|
|
element.children
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</InnerBlockComponent>
|
|
|
|
</InnerBlockComponentWrapper>
|
2021-06-11 08:54:18 +00:00
|
|
|
</Suspense>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-03 13:25:09 +00:00
|
|
|
* Render a parent block on the frontend.
|
|
|
|
*
|
|
|
|
* This is the main entry point used on the frontend to convert Block Markup (with inner blocks) in the DOM to React
|
|
|
|
* Components.
|
|
|
|
*
|
|
|
|
* This uses renderFrontend(). The difference is, renderFrontend renders a single block, but renderParentBlock() also
|
|
|
|
* handles inner blocks by recursively running over children from the DOM.
|
|
|
|
*
|
|
|
|
* @see renderInnerBlocks
|
|
|
|
* @see renderFrontend
|
2021-06-11 08:54:18 +00:00
|
|
|
*/
|
|
|
|
export const renderParentBlock = ( {
|
|
|
|
Block,
|
|
|
|
selector,
|
|
|
|
blockName,
|
|
|
|
getProps = () => ( {} ),
|
|
|
|
blockMap,
|
|
|
|
blockWrapper,
|
2021-09-03 13:25:09 +00:00
|
|
|
}: {
|
|
|
|
// Parent Block Name. Used for inner block component mapping.
|
|
|
|
blockName: string;
|
|
|
|
// Map of block names to block components for children.
|
|
|
|
blockMap: Record< string, React.ReactNode >;
|
|
|
|
// Wrapper for inner components.
|
|
|
|
blockWrapper?: React.ElementType;
|
|
|
|
// React component to use as a replacement.
|
|
|
|
Block: React.FunctionComponent;
|
|
|
|
// CSS selector to match the elements to replace.
|
|
|
|
selector: string;
|
|
|
|
// Function to generate the props object for the block.
|
|
|
|
getProps: ( el: Element, i: number ) => Record< string, unknown >;
|
|
|
|
} ): void => {
|
|
|
|
/**
|
|
|
|
* In addition to getProps, we need to render and return the children. This adds children to props.
|
|
|
|
*/
|
|
|
|
const getPropsWithChildren = ( element: Element, i: number ) => {
|
2021-06-11 08:54:18 +00:00
|
|
|
const children =
|
2021-09-03 13:25:09 +00:00
|
|
|
element.children && element.children.length
|
2021-06-11 08:54:18 +00:00
|
|
|
? renderInnerBlocks( {
|
|
|
|
blockName,
|
|
|
|
blockMap,
|
2021-09-03 13:25:09 +00:00
|
|
|
children: element.children,
|
2021-06-11 08:54:18 +00:00
|
|
|
blockWrapper,
|
|
|
|
} )
|
|
|
|
: null;
|
2021-09-03 13:25:09 +00:00
|
|
|
return { ...getProps( element, i ), children };
|
2021-06-11 08:54:18 +00:00
|
|
|
};
|
2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* The only difference between using renderParentBlock and renderFrontend is that here we provide children.
|
|
|
|
*/
|
2021-06-11 08:54:18 +00:00
|
|
|
renderFrontend( {
|
|
|
|
Block,
|
|
|
|
selector,
|
|
|
|
getProps: getPropsWithChildren,
|
|
|
|
} );
|
|
|
|
};
|