2022-05-19 16:16:46 +00:00
|
|
|
/* eslint-disable @wordpress/no-unsafe-wp-apis */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-05-30 14:38:52 +00:00
|
|
|
import type { BlockAlignment } from '@wordpress/blocks';
|
|
|
|
import { ProductResponseItem } from '@woocommerce/types';
|
2022-05-19 16:16:46 +00:00
|
|
|
import { __experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles } from '@wordpress/block-editor';
|
|
|
|
import { Icon, Placeholder, Spinner } from '@wordpress/components';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { isEmpty } from 'lodash';
|
2022-05-30 14:38:52 +00:00
|
|
|
import {
|
|
|
|
ComponentType,
|
|
|
|
Dispatch,
|
|
|
|
SetStateAction,
|
|
|
|
useCallback,
|
|
|
|
useState,
|
|
|
|
} from 'react';
|
|
|
|
import { WP_REST_API_Category } from 'wp-types';
|
2022-05-19 16:16:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { CallToAction } from './call-to-action';
|
|
|
|
import { ConstrainedResizable } from './constrained-resizable';
|
2022-05-30 14:38:52 +00:00
|
|
|
import { EditorBlock, GenericBlockUIConfig } from './types';
|
2022-05-19 16:16:46 +00:00
|
|
|
import { useBackgroundImage } from './use-background-image';
|
|
|
|
import {
|
|
|
|
dimRatioToClass,
|
|
|
|
getBackgroundImageStyles,
|
|
|
|
getClassPrefixFromName,
|
|
|
|
} from './utils';
|
|
|
|
|
2022-05-30 14:38:52 +00:00
|
|
|
interface WithFeaturedItemConfig extends GenericBlockUIConfig {
|
|
|
|
emptyMessage: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FeaturedItemRequiredAttributes {
|
|
|
|
contentAlign: BlockAlignment;
|
|
|
|
dimRatio: number;
|
|
|
|
focalPoint: { x: number; y: number };
|
|
|
|
hasParallax: boolean;
|
|
|
|
imageFit: 'cover' | 'none';
|
|
|
|
isRepeated: boolean;
|
|
|
|
linkText: string;
|
|
|
|
mediaId: number;
|
|
|
|
mediaSrc: string;
|
|
|
|
minHeight: number;
|
|
|
|
overlayColor: string;
|
|
|
|
overlayGradient: string;
|
|
|
|
showDesc: boolean;
|
|
|
|
showPrice: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FeaturedCategoryRequiredAttributes
|
|
|
|
extends FeaturedItemRequiredAttributes {
|
|
|
|
categoryId: number | 'preview';
|
|
|
|
productId: never;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FeaturedProductRequiredAttributes
|
|
|
|
extends FeaturedItemRequiredAttributes {
|
|
|
|
categoryId: never;
|
|
|
|
productId: number | 'preview';
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FeaturedItemRequiredProps< T > {
|
|
|
|
attributes: (
|
|
|
|
| FeaturedCategoryRequiredAttributes
|
|
|
|
| FeaturedProductRequiredAttributes
|
|
|
|
) &
|
|
|
|
EditorBlock< T >[ 'attributes' ] & {
|
2022-05-31 15:43:03 +00:00
|
|
|
// This is hardcoded because border and color are not yet included
|
|
|
|
// in Gutenberg's official types.
|
|
|
|
style: {
|
|
|
|
border?: { radius?: number };
|
|
|
|
color?: { text?: string };
|
|
|
|
};
|
2022-06-07 08:44:45 +00:00
|
|
|
textColor?: string;
|
2022-05-30 14:38:52 +00:00
|
|
|
};
|
|
|
|
isLoading: boolean;
|
|
|
|
setAttributes: ( attrs: Partial< FeaturedItemRequiredAttributes > ) => void;
|
|
|
|
useEditingImage: [ boolean, Dispatch< SetStateAction< boolean > > ];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FeaturedCategoryProps< T > extends FeaturedItemRequiredProps< T > {
|
|
|
|
category: WP_REST_API_Category;
|
|
|
|
product: never;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FeaturedProductProps< T > extends FeaturedItemRequiredProps< T > {
|
|
|
|
category: never;
|
|
|
|
product: ProductResponseItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
type FeaturedItemProps< T extends EditorBlock< T > > =
|
|
|
|
| ( T & FeaturedCategoryProps< T > )
|
|
|
|
| ( T & FeaturedProductProps< T > );
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
export const withFeaturedItem =
|
|
|
|
( { emptyMessage, icon, label }: WithFeaturedItemConfig ) =>
|
|
|
|
< T extends EditorBlock< T > >( Component: ComponentType< T > ) =>
|
|
|
|
( props: FeaturedItemProps< T > ) => {
|
|
|
|
const [ isEditingImage ] = props.useEditingImage;
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const {
|
|
|
|
attributes,
|
|
|
|
category,
|
|
|
|
isLoading,
|
|
|
|
isSelected,
|
|
|
|
name,
|
|
|
|
product,
|
|
|
|
setAttributes,
|
|
|
|
} = props;
|
|
|
|
const { mediaId, mediaSrc } = attributes;
|
|
|
|
const item = category || product;
|
|
|
|
const [ backgroundImageSize, setBackgroundImageSize ] = useState( {} );
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const { backgroundImageSrc } = useBackgroundImage( {
|
|
|
|
item,
|
|
|
|
mediaId,
|
|
|
|
mediaSrc,
|
|
|
|
blockName: name,
|
|
|
|
} );
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const className = getClassPrefixFromName( name );
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const onResize = useCallback(
|
|
|
|
( _event, _direction, elt ) => {
|
|
|
|
setAttributes( {
|
|
|
|
minHeight: parseInt( elt.style.height, 10 ),
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
[ setAttributes ]
|
2022-05-19 16:16:46 +00:00
|
|
|
);
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const renderButton = () => {
|
|
|
|
const { categoryId, linkText, productId } = attributes;
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
return (
|
|
|
|
<CallToAction
|
|
|
|
itemId={ categoryId || productId }
|
|
|
|
linkText={ linkText }
|
|
|
|
permalink={ ( category || product ).permalink as string }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const renderNoItem = () => (
|
|
|
|
<Placeholder
|
|
|
|
className={ className }
|
|
|
|
icon={ <Icon icon={ icon } /> }
|
|
|
|
label={ label }
|
|
|
|
>
|
|
|
|
{ isLoading ? <Spinner /> : emptyMessage }
|
|
|
|
</Placeholder>
|
2022-05-19 16:16:46 +00:00
|
|
|
);
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const renderItem = () => {
|
|
|
|
const {
|
|
|
|
contentAlign,
|
|
|
|
dimRatio,
|
|
|
|
focalPoint,
|
|
|
|
hasParallax,
|
|
|
|
isRepeated,
|
|
|
|
imageFit,
|
|
|
|
minHeight,
|
|
|
|
overlayColor,
|
|
|
|
overlayGradient,
|
|
|
|
showDesc,
|
|
|
|
showPrice,
|
|
|
|
style,
|
|
|
|
textColor,
|
|
|
|
} = attributes;
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const classes = classnames(
|
|
|
|
className,
|
|
|
|
{
|
|
|
|
'is-selected':
|
|
|
|
isSelected &&
|
|
|
|
attributes.categoryId !== 'preview' &&
|
|
|
|
attributes.productId !== 'preview',
|
|
|
|
'is-loading': ! item && isLoading,
|
|
|
|
'is-not-found': ! item && ! isLoading,
|
|
|
|
'has-background-dim': dimRatio !== 0,
|
|
|
|
'is-repeated': isRepeated,
|
|
|
|
},
|
|
|
|
dimRatioToClass( dimRatio ),
|
|
|
|
contentAlign !== 'center' && `has-${ contentAlign }-content`
|
|
|
|
);
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const containerStyle = {
|
|
|
|
borderRadius: style?.border?.radius,
|
|
|
|
color: textColor
|
|
|
|
? `var(--wp--preset--color--${ textColor })`
|
|
|
|
: style?.color?.text,
|
|
|
|
};
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const wrapperStyle = {
|
|
|
|
...getSpacingClassesAndStyles( attributes ).style,
|
|
|
|
minHeight,
|
|
|
|
};
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const isImgElement = ! isRepeated && ! hasParallax;
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const backgroundImageStyle = getBackgroundImageStyles( {
|
|
|
|
focalPoint,
|
|
|
|
imageFit,
|
|
|
|
isImgElement,
|
|
|
|
isRepeated,
|
|
|
|
url: backgroundImageSrc,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const overlayStyle = {
|
|
|
|
background: overlayGradient,
|
|
|
|
backgroundColor: overlayColor,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConstrainedResizable
|
|
|
|
enable={ { bottom: true } }
|
|
|
|
onResize={ onResize }
|
|
|
|
showHandle={ isSelected }
|
|
|
|
style={ { minHeight } }
|
|
|
|
/>
|
|
|
|
<div className={ classes } style={ containerStyle }>
|
2022-05-19 16:16:46 +00:00
|
|
|
<div
|
2022-06-15 09:56:52 +00:00
|
|
|
className={ `${ className }__wrapper` }
|
|
|
|
style={ wrapperStyle }
|
|
|
|
>
|
2022-05-19 16:16:46 +00:00
|
|
|
<div
|
2022-06-15 09:56:52 +00:00
|
|
|
className="background-dim__overlay"
|
|
|
|
style={ overlayStyle }
|
2022-05-19 16:16:46 +00:00
|
|
|
/>
|
2022-06-15 09:56:52 +00:00
|
|
|
{ backgroundImageSrc &&
|
|
|
|
( isImgElement ? (
|
|
|
|
<img
|
|
|
|
alt={ item.name }
|
|
|
|
className={ `${ className }__background-image` }
|
|
|
|
src={ backgroundImageSrc }
|
|
|
|
style={ backgroundImageStyle }
|
|
|
|
onLoad={ ( e ) => {
|
|
|
|
setBackgroundImageSize( {
|
|
|
|
height: e.currentTarget
|
|
|
|
?.naturalHeight,
|
|
|
|
width: e.currentTarget
|
|
|
|
?.naturalWidth,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
`${ className }__background-image`,
|
|
|
|
{
|
|
|
|
'has-parallax': hasParallax,
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
style={ backgroundImageStyle }
|
|
|
|
/>
|
|
|
|
) ) }
|
|
|
|
<h2
|
|
|
|
className={ `${ className }__title` }
|
2022-05-19 16:16:46 +00:00
|
|
|
dangerouslySetInnerHTML={ {
|
2022-06-15 09:56:52 +00:00
|
|
|
__html: item.name,
|
2022-05-19 16:16:46 +00:00
|
|
|
} }
|
|
|
|
/>
|
2022-06-15 09:56:52 +00:00
|
|
|
{ ! isEmpty( product?.variation ) && (
|
|
|
|
<h3
|
|
|
|
className={ `${ className }__variation` }
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html: product.variation,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ showDesc && (
|
|
|
|
<div
|
|
|
|
className={ `${ className }__description` }
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html:
|
|
|
|
category?.description ||
|
|
|
|
product?.short_description,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ showPrice && (
|
|
|
|
<div
|
|
|
|
className={ `${ className }__price` }
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html: product.price_html,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
<div className={ `${ className }__link` }>
|
|
|
|
{ renderButton() }
|
|
|
|
</div>
|
2022-05-19 16:16:46 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-15 09:56:52 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( isEditingImage ) {
|
|
|
|
return (
|
|
|
|
<Component
|
|
|
|
{ ...props }
|
|
|
|
backgroundImageSize={ backgroundImageSize }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2022-05-19 16:16:46 +00:00
|
|
|
|
|
|
|
return (
|
2022-06-15 09:56:52 +00:00
|
|
|
<>
|
|
|
|
<Component
|
|
|
|
{ ...props }
|
|
|
|
backgroundImageSize={ backgroundImageSize }
|
|
|
|
/>
|
|
|
|
{ item ? renderItem() : renderNoItem() }
|
|
|
|
</>
|
2022-05-19 16:16:46 +00:00
|
|
|
);
|
2022-06-15 09:56:52 +00:00
|
|
|
};
|