2019-09-09 13:25:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-07-22 12:20:54 +00:00
|
|
|
import { render, Suspense } from '@wordpress/element';
|
2019-11-21 08:08:47 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
2019-09-09 13:25:22 +00:00
|
|
|
|
2020-08-07 17:33:32 +00:00
|
|
|
// Some blocks take care of rendering their inner blocks automatically. For
|
|
|
|
// example, the empty cart. In those cases, we don't want to trigger the render
|
|
|
|
// function of inner components on load. Instead, the wrapper block can trigger
|
|
|
|
// the event `wc-blocks_render_blocks_frontend` to render its inner blocks.
|
|
|
|
const selectorsToSkipOnLoad = [ '.wp-block-woocommerce-cart' ];
|
|
|
|
|
|
|
|
// Given an element and a list of wrappers, check if the element is inside at
|
|
|
|
// least one of the wrappers.
|
|
|
|
const isElementInsideWrappers = ( el, wrappers ) => {
|
|
|
|
return Array.prototype.some.call(
|
|
|
|
wrappers,
|
|
|
|
( wrapper ) => wrapper.contains( el ) && ! wrapper.isSameNode( el )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-09-09 13:25:22 +00:00
|
|
|
/**
|
2020-08-07 17:33:32 +00:00
|
|
|
* Renders a block component in each `containers` node.
|
2019-09-09 13:25:22 +00:00
|
|
|
*
|
2020-08-07 17:33:32 +00:00
|
|
|
* @param {Object} props Render props.
|
|
|
|
* @param {Function} props.Block React component to use as a
|
|
|
|
* replacement.
|
|
|
|
* @param {NodeList} props.containers Containers to replace with
|
|
|
|
* the Block component.
|
|
|
|
* @param {Function} [props.getProps] Function to generate the
|
|
|
|
* props object for the block.
|
|
|
|
* @param {Function} [props.getErrorBoundaryProps] Function to generate the
|
|
|
|
* props object for the error
|
|
|
|
* boundary.
|
2019-09-09 13:25:22 +00:00
|
|
|
*/
|
2020-08-07 17:33:32 +00:00
|
|
|
const renderBlockInContainers = ( {
|
2020-03-06 11:43:40 +00:00
|
|
|
Block,
|
2020-08-07 17:33:32 +00:00
|
|
|
containers,
|
2020-03-06 11:43:40 +00:00
|
|
|
getProps = () => {},
|
2020-05-27 10:11:30 +00:00
|
|
|
getErrorBoundaryProps = () => {},
|
|
|
|
} ) => {
|
2020-08-07 17:33:32 +00:00
|
|
|
if ( containers.length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use Array.forEach for IE11 compatibility.
|
|
|
|
Array.prototype.forEach.call( containers, ( el, i ) => {
|
|
|
|
const props = getProps( el, i );
|
|
|
|
const errorBoundaryProps = getErrorBoundaryProps( el, i );
|
|
|
|
const attributes = {
|
|
|
|
...el.dataset,
|
|
|
|
...props.attributes,
|
2020-07-29 14:06:44 +00:00
|
|
|
};
|
2020-08-07 17:33:32 +00:00
|
|
|
el.classList.remove( 'is-loading' );
|
|
|
|
|
|
|
|
render(
|
|
|
|
<BlockErrorBoundary { ...errorBoundaryProps }>
|
2020-08-18 11:32:40 +00:00
|
|
|
<Suspense fallback={ <div className="wc-block-placeholder" /> }>
|
2020-08-07 17:33:32 +00:00
|
|
|
<Block { ...props } attributes={ attributes } />
|
2020-08-18 11:32:40 +00:00
|
|
|
</Suspense>
|
2020-08-07 17:33:32 +00:00
|
|
|
</BlockErrorBoundary>,
|
|
|
|
el
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the block frontend in the elements matched by the selector which are
|
|
|
|
* outside the wrapper elements.
|
|
|
|
*
|
|
|
|
* @param {Object} props Render props.
|
|
|
|
* @param {Function} props.Block React component to use as a
|
|
|
|
* replacement.
|
|
|
|
* @param {string} props.selector CSS selector to match the
|
|
|
|
* elements to replace.
|
|
|
|
* @param {Function} [props.getProps] Function to generate the
|
|
|
|
* props object for the block.
|
|
|
|
* @param {Function} [props.getErrorBoundaryProps] Function to generate the
|
|
|
|
* props object for the error
|
|
|
|
* boundary.
|
|
|
|
* @param {NodeList} props.wrappers All elements matched by the
|
|
|
|
* selector which are inside
|
|
|
|
* the wrapper will be ignored.
|
|
|
|
*/
|
|
|
|
const renderBlockOutsideWrappers = ( {
|
|
|
|
Block,
|
|
|
|
getProps,
|
|
|
|
getErrorBoundaryProps,
|
|
|
|
selector,
|
|
|
|
wrappers,
|
|
|
|
} ) => {
|
|
|
|
const containers = document.body.querySelectorAll( selector );
|
|
|
|
// Filter out blocks inside the wrappers.
|
|
|
|
if ( wrappers.length > 0 ) {
|
|
|
|
Array.prototype.filter.call( containers, ( el ) => {
|
|
|
|
return ! isElementInsideWrappers( el, wrappers );
|
2019-09-09 13:25:22 +00:00
|
|
|
} );
|
|
|
|
}
|
2020-08-07 17:33:32 +00:00
|
|
|
renderBlockInContainers( {
|
|
|
|
Block,
|
|
|
|
containers,
|
|
|
|
getProps,
|
|
|
|
getErrorBoundaryProps,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the block frontend in the elements matched by the selector inside the
|
|
|
|
* wrapper element.
|
|
|
|
*
|
|
|
|
* @param {Object} props Render props.
|
|
|
|
* @param {Function} props.Block React component to use as a
|
|
|
|
* replacement.
|
|
|
|
* @param {string} props.selector CSS selector to match the
|
|
|
|
* elements to replace.
|
|
|
|
* @param {Function} [props.getProps] Function to generate the
|
|
|
|
* props object for the block.
|
|
|
|
* @param {Function} [props.getErrorBoundaryProps] Function to generate the
|
|
|
|
* props object for the error
|
|
|
|
* boundary.
|
|
|
|
* @param {Element} props.wrapper Wrapper element to query the
|
|
|
|
* selector inside.
|
|
|
|
*/
|
|
|
|
const renderBlockInsideWrapper = ( {
|
|
|
|
Block,
|
|
|
|
getProps,
|
|
|
|
getErrorBoundaryProps,
|
|
|
|
selector,
|
|
|
|
wrapper,
|
|
|
|
} ) => {
|
|
|
|
const containers = wrapper.querySelectorAll( selector );
|
|
|
|
renderBlockInContainers( {
|
|
|
|
Block,
|
|
|
|
containers,
|
|
|
|
getProps,
|
|
|
|
getErrorBoundaryProps,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the block frontend on page load. If the block is contained inside a
|
|
|
|
* wrapper element that should be excluded from initial load, it adds the
|
|
|
|
* appropriate event listeners to render the block when the
|
|
|
|
* `blocks_render_blocks_frontend` event is triggered.
|
|
|
|
*
|
|
|
|
* @param {Object} props Render props.
|
|
|
|
* @param {Function} props.Block React component to use as a
|
|
|
|
* replacement.
|
|
|
|
* @param {string} props.selector CSS selector to match the
|
|
|
|
* elements to replace.
|
|
|
|
* @param {Function} [props.getProps] Function to generate the
|
|
|
|
* props object for the block.
|
|
|
|
* @param {Function} [props.getErrorBoundaryProps] Function to generate the
|
|
|
|
* props object for the error
|
|
|
|
* boundary.
|
|
|
|
*/
|
|
|
|
export const renderFrontend = ( props ) => {
|
|
|
|
const wrappersToSkipOnLoad = document.body.querySelectorAll(
|
|
|
|
selectorsToSkipOnLoad.join( ',' )
|
|
|
|
);
|
|
|
|
renderBlockOutsideWrappers( {
|
|
|
|
...props,
|
|
|
|
wrappers: wrappersToSkipOnLoad,
|
|
|
|
} );
|
|
|
|
// For each wrapper, add an event listener to render the inner blocks when
|
|
|
|
// `wc-blocks_render_blocks_frontend` event is triggered.
|
|
|
|
Array.prototype.forEach.call( wrappersToSkipOnLoad, ( wrapper ) => {
|
|
|
|
wrapper.addEventListener( 'wc-blocks_render_blocks_frontend', () => {
|
|
|
|
renderBlockInsideWrapper( { ...props, wrapper } );
|
|
|
|
} );
|
|
|
|
} );
|
2019-09-09 13:25:22 +00:00
|
|
|
};
|
2020-04-27 16:06:58 +00:00
|
|
|
|
|
|
|
export default renderFrontend;
|