Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add media controls to the Featured Product block #6348

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 75 additions & 10 deletions assets/js/blocks/featured-product/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { __ } from '@wordpress/i18n';
import {
AlignmentToolbar,
Expand All @@ -13,6 +13,8 @@ import {
MediaReplaceFlow,
RichText,
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
__experimentalImageEditingProvider as ImageEditingProvider,
__experimentalImageEditor as ImageEditor,
__experimentalPanelColorGradientSettings as PanelColorGradientSettings,
__experimentalUseGradient as useGradient,
} from '@wordpress/block-editor';
Expand All @@ -26,6 +28,7 @@ import {
ResizableBox,
Spinner,
ToggleControl,
ToolbarButton,
ToolbarGroup,
withSpokenMessages,
__experimentalToggleGroupControl as ToggleGroupControl,
Expand All @@ -40,7 +43,7 @@ import ProductControl from '@woocommerce/editor-components/product-control';
import ErrorPlaceholder from '@woocommerce/editor-components/error-placeholder';
import TextToolbarButton from '@woocommerce/editor-components/text-toolbar-button';
import { withProduct } from '@woocommerce/block-hocs';
import { Icon, starEmpty } from '@wordpress/icons';
import { crop, Icon, starEmpty } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -52,6 +55,11 @@ import {
} from '../../utils/products';
import { useThrottle } from '../../utils/useThrottle';

const DEFAULT_EDITOR_SIZE = {
height: 500,
width: 500,
};

export const ConstrainedResizable = ( {
className = '',
onResize,
Expand Down Expand Up @@ -110,10 +118,18 @@ const FeaturedProduct = ( {
setAttributes,
triggerUrlUpdate = () => void null,
} ) => {
const { mediaId, mediaSrc } = attributes;

const [ isEditingImage, setIsEditingImage ] = useState( false );
const [ backgroundImageSize, setBackgroundImageSize ] = useState( {} );
const { setGradient } = useGradient( {
gradientAttribute: 'overlayGradient',
customGradientAttribute: 'overlayGradient',
} );

const backgroundImageSrc = mediaSrc || getImageSrcFromProduct( product );
const backgroundImageId = mediaId || getImageIdFromProduct( product );

const onResize = useCallback(
( _event, _direction, elt ) => {
setAttributes( { minHeight: parseInt( elt.style.height, 10 ) } );
Expand All @@ -130,6 +146,10 @@ const FeaturedProduct = ( {
/>
);

useEffect( () => {
setIsEditingImage( false );
}, [ isSelected ] );

const renderEditMode = () => {
const onDone = () => {
setAttributes( { editMode: false } );
Expand Down Expand Up @@ -180,8 +200,7 @@ const FeaturedProduct = ( {
};

const getBlockControls = () => {
const { contentAlign, editMode, mediaSrc } = attributes;
const mediaId = attributes.mediaId || getImageIdFromProduct( product );
const { contentAlign, editMode } = attributes;

return (
<BlockControls>
Expand All @@ -192,8 +211,18 @@ const FeaturedProduct = ( {
} }
/>
<ToolbarGroup>
{ ! isEditingImage && (
<ToolbarButton
onClick={ () => setIsEditingImage( true ) }
icon={ crop }
label={ __(
'Edit product image',
'woo-gutenberg-products-block'
) }
/>
) }
<MediaReplaceFlow
mediaId={ mediaId }
mediaId={ backgroundImageId }
mediaURL={ mediaSrc }
accept="image/*"
onSelect={ ( media ) => {
Expand All @@ -204,7 +233,7 @@ const FeaturedProduct = ( {
} }
allowedTypes={ [ 'image' ] }
/>
{ mediaId && mediaSrc ? (
{ backgroundImageId && mediaSrc ? (
<TextToolbarButton
onClick={ () =>
setAttributes( { mediaId: 0, mediaSrc: '' } )
Expand Down Expand Up @@ -400,7 +429,6 @@ const FeaturedProduct = ( {
dimRatio,
focalPoint,
imageFit,
mediaSrc,
minHeight,
overlayColor,
overlayGradient,
Expand Down Expand Up @@ -430,9 +458,6 @@ const FeaturedProduct = ( {
minHeight,
};

const backgroundImageSrc =
mediaSrc || getImageSrcFromProduct( product );

const backgroundImageStyle = {
...calculateBackgroundImagePosition( focalPoint ),
objectFit: imageFit,
Expand Down Expand Up @@ -465,6 +490,12 @@ const FeaturedProduct = ( {
className="wc-block-featured-product__background-image"
src={ backgroundImageSrc }
style={ backgroundImageStyle }
onLoad={ ( e ) => {
setBackgroundImageSize( {
height: e.target?.naturalHeight,
width: e.target?.naturalWidth,
} );
} }
/>
<h2
className="wc-block-featured-product__title"
Expand Down Expand Up @@ -580,6 +611,40 @@ const FeaturedProduct = ( {
return renderEditMode();
}

if ( isEditingImage ) {
return (
<>
<ImageEditingProvider
id={ backgroundImageId }
url={ backgroundImageSrc }
naturalHeight={
backgroundImageSize.height || DEFAULT_EDITOR_SIZE.height
}
naturalWidth={
backgroundImageSize.width || DEFAULT_EDITOR_SIZE.width
}
onSaveImage={ ( { id, url } ) => {
setAttributes( { mediaId: id, mediaSrc: url } );
} }
isEditing={ isEditingImage }
onFinishEditing={ () => setIsEditingImage( false ) }
>
<ImageEditor
url={ backgroundImageSrc }
height={
backgroundImageSize.height ||
DEFAULT_EDITOR_SIZE.height
}
width={
backgroundImageSize.width ||
DEFAULT_EDITOR_SIZE.width
}
/>
</ImageEditingProvider>
</>
);
}

return (
<>
{ getBlockControls() }
Expand Down
15 changes: 15 additions & 0 deletions assets/js/blocks/featured-product/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
height: auto !important;
}
}

// Applying image edits
.is-applying {
.components-spinner {
position: absolute;
top: 50%;
left: 50%;
margin-top: -9px;
margin-left: -9px;
}

img {
opacity: 0.3;
}
}
}

.wc-block-featured-product {
Expand Down