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,
|
2021-09-07 16:01:14 +00:00
|
|
|
hasInnerBlocks,
|
2021-09-03 13:25:09 +00:00
|
|
|
} 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 = (
|
2021-09-07 16:01:14 +00:00
|
|
|
block: string,
|
2021-09-03 13:25:09 +00:00
|
|
|
blockMap: Record< string, React.ReactNode >
|
|
|
|
): React.ElementType | null => {
|
2021-09-07 16:01:14 +00:00
|
|
|
return block && blockMap[ block ]
|
|
|
|
? ( blockMap[ block ] as React.ElementType )
|
2021-09-03 13:25:09 +00:00
|
|
|
: 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 = (
|
2021-09-07 16:01:14 +00:00
|
|
|
block: string,
|
2021-09-03 13:25:09 +00:00
|
|
|
blockMap: Record< string, React.ReactNode >,
|
|
|
|
// Current children from the parent (siblings of the forced block)
|
|
|
|
blockChildren: HTMLCollection | null
|
|
|
|
) => {
|
2021-09-07 16:01:14 +00:00
|
|
|
if ( ! hasInnerBlocks( block ) ) {
|
2021-09-03 13:25:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentBlocks = blockChildren
|
|
|
|
? ( Array.from( blockChildren )
|
|
|
|
.map( ( element: Element ) =>
|
|
|
|
element instanceof HTMLElement
|
|
|
|
? element?.dataset.blockName || null
|
|
|
|
: null
|
|
|
|
)
|
|
|
|
.filter( Boolean ) as string[] )
|
|
|
|
: [];
|
|
|
|
|
2021-09-07 16:01:14 +00:00
|
|
|
const forcedBlocks = getRegisteredBlocks( block ).filter(
|
|
|
|
( { blockName, force } ) =>
|
|
|
|
force === true && ! currentBlocks.includes( blockName )
|
2021-09-03 13:25:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return forcedBlocks.map(
|
2021-09-07 16:01:14 +00:00
|
|
|
( { blockName, component }, index: number ): JSX.Element | null => {
|
2021-09-03 13:25:09 +00:00
|
|
|
const ForcedComponent = component
|
|
|
|
? component
|
2021-09-07 16:01:14 +00:00
|
|
|
: getBlockComponentFromMap( blockName, blockMap );
|
2021-09-03 13:25:09 +00:00
|
|
|
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-09-07 16:01:14 +00:00
|
|
|
block,
|
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,
|
|
|
|
}: {
|
2021-09-07 16:01:14 +00:00
|
|
|
// Block (parent) being rendered. Used for inner block component mapping.
|
|
|
|
block: string;
|
2021-09-03 13:25:09 +00:00
|
|
|
// 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 } = {
|
2021-09-07 16:01:14 +00:00
|
|
|
key: `${ block }_${ 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 ) ) {
|
2021-09-16 12:16:21 +00:00
|
|
|
const elementChildren = renderInnerBlocks( {
|
|
|
|
block,
|
|
|
|
blockMap,
|
|
|
|
children: element.children || [],
|
|
|
|
depth: depth + 1,
|
|
|
|
blockWrapper,
|
|
|
|
} );
|
2021-09-03 13:25:09 +00:00
|
|
|
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
|
2021-09-07 16:01:14 +00:00
|
|
|
key={ `${ block }_${ depth }_${ index }_suspense` }
|
2021-06-11 08:54:18 +00:00
|
|
|
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( {
|
2021-09-07 16:01:14 +00:00
|
|
|
block,
|
2021-09-03 13:25:09 +00:00
|
|
|
blockMap,
|
2021-09-07 16:01:14 +00:00
|
|
|
children: element.children,
|
2021-09-03 13:25:09 +00:00
|
|
|
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-09-16 12:16:21 +00:00
|
|
|
const children = renderInnerBlocks( {
|
|
|
|
block: blockName,
|
|
|
|
blockMap,
|
|
|
|
children: element.children || [],
|
|
|
|
blockWrapper,
|
|
|
|
} );
|
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,
|
|
|
|
} );
|
|
|
|
};
|