forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add product images block to product editor (woocommerce#37455)
* Add images block to the product editor * Allow html in section block descriptions * Add changelog entry * Add client changelog entry * Remove SVG related changes * Fix up lock file after rebase * Remove unused import * Fix up php lint errors * Move sanitize function to utils folder
- Loading branch information
Showing
13 changed files
with
363 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add images block to product editor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/js/product-editor/src/components/images/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "woocommerce/product-images", | ||
"title": "Product images", | ||
"category": "widgets", | ||
"description": "The product images.", | ||
"keywords": [ "products", "image", "images", "gallery" ], | ||
"textdomain": "default", | ||
"attributes": { | ||
"mediaId": { | ||
"type": "number", | ||
"__experimentalRole": "content" | ||
}, | ||
"images": { | ||
"__experimentalRole": "content", | ||
"type": "array", | ||
"items": { | ||
"type": "number" | ||
}, | ||
"default": [] | ||
} | ||
}, | ||
"supports": { | ||
"align": false, | ||
"html": false, | ||
"multiple": false, | ||
"reusable": false, | ||
"inserter": false, | ||
"lock": false | ||
} | ||
} |
184 changes: 184 additions & 0 deletions
184
packages/js/product-editor/src/components/images/edit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { CardBody, DropZone } from '@wordpress/components'; | ||
import classnames from 'classnames'; | ||
import { createElement, useState } from '@wordpress/element'; | ||
import { Icon, trash } from '@wordpress/icons'; | ||
import { MediaItem } from '@wordpress/media-utils'; | ||
import { | ||
MediaUploader, | ||
ImageGallery, | ||
ImageGalleryItem, | ||
} from '@woocommerce/components'; | ||
import { recordEvent } from '@woocommerce/tracks'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore No types for this exist yet. | ||
// eslint-disable-next-line @woocommerce/dependency-group | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
|
||
type Image = MediaItem & { | ||
src: string; | ||
}; | ||
|
||
export function Edit() { | ||
const [ images, setImages ] = useEntityProp< MediaItem[] >( | ||
'postType', | ||
'product', | ||
'images' | ||
); | ||
const [ isRemovingZoneVisible, setIsRemovingZoneVisible ] = | ||
useState< boolean >( false ); | ||
const [ isRemoving, setIsRemoving ] = useState< boolean >( false ); | ||
const [ draggedImageId, setDraggedImageId ] = useState< number | null >( | ||
null | ||
); | ||
|
||
const blockProps = useBlockProps( { | ||
className: classnames( { | ||
'has-images': images.length > 0, | ||
} ), | ||
} ); | ||
|
||
const toggleRemoveZone = () => { | ||
setIsRemovingZoneVisible( ! isRemovingZoneVisible ); | ||
}; | ||
|
||
const orderImages = ( newOrder: JSX.Element[] ) => { | ||
const orderedImages = newOrder.map( ( image ) => { | ||
return images.find( | ||
( file ) => file.id === parseInt( image?.props?.id, 10 ) | ||
); | ||
} ); | ||
recordEvent( 'product_images_change_image_order_via_image_gallery' ); | ||
setImages( orderedImages as MediaItem[] ); | ||
}; | ||
|
||
const onFileUpload = ( files: MediaItem[] ) => { | ||
if ( files[ 0 ].id ) { | ||
recordEvent( 'product_images_add_via_file_upload_area' ); | ||
setImages( [ ...images, ...files ] ); | ||
} | ||
}; | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<div className="woocommerce-product-form__image-drop-zone"> | ||
{ isRemovingZoneVisible ? ( | ||
<CardBody> | ||
<div className="woocommerce-product-form__remove-image-drop-zone"> | ||
<span> | ||
<Icon | ||
icon={ trash } | ||
size={ 20 } | ||
className="icon-control" | ||
/> | ||
{ __( 'Drop here to remove', 'woocommerce' ) } | ||
</span> | ||
<DropZone | ||
onHTMLDrop={ () => setIsRemoving( true ) } | ||
onDrop={ () => setIsRemoving( true ) } | ||
label={ __( | ||
'Drop here to remove', | ||
'woocommerce' | ||
) } | ||
/> | ||
</div> | ||
</CardBody> | ||
) : ( | ||
<CardBody> | ||
<MediaUploader | ||
multipleSelect={ true } | ||
onError={ () => null } | ||
onFileUploadChange={ onFileUpload } | ||
onSelect={ ( files ) => { | ||
const newImages = files.filter( | ||
( img: Image ) => | ||
! images.find( | ||
( image ) => image.id === img.id | ||
) | ||
); | ||
if ( newImages.length > 0 ) { | ||
recordEvent( | ||
'product_images_add_via_media_library' | ||
); | ||
setImages( [ ...images, ...newImages ] ); | ||
} | ||
} } | ||
onUpload={ ( files ) => { | ||
if ( files[ 0 ].id ) { | ||
recordEvent( | ||
'product_images_add_via_drag_and_drop_upload' | ||
); | ||
setImages( [ ...images, ...files ] ); | ||
} | ||
} } | ||
label={ '' } | ||
/> | ||
</CardBody> | ||
) } | ||
</div> | ||
<ImageGallery | ||
onDragStart={ ( event ) => { | ||
const { id: imageId, dataset } = | ||
event.target as HTMLElement; | ||
if ( imageId ) { | ||
setDraggedImageId( parseInt( imageId, 10 ) ); | ||
} else { | ||
const index = dataset?.index; | ||
if ( index ) { | ||
setDraggedImageId( | ||
images[ parseInt( index, 10 ) ]?.id | ||
); | ||
} | ||
} | ||
toggleRemoveZone(); | ||
} } | ||
onDragEnd={ () => { | ||
if ( isRemoving && draggedImageId ) { | ||
recordEvent( | ||
'product_images_remove_image_button_click' | ||
); | ||
setImages( | ||
images.filter( | ||
( img ) => img.id !== draggedImageId | ||
) | ||
); | ||
setIsRemoving( false ); | ||
setDraggedImageId( null ); | ||
} | ||
toggleRemoveZone(); | ||
} } | ||
onOrderChange={ orderImages } | ||
onReplace={ ( { replaceIndex, media } ) => { | ||
if ( | ||
images.find( ( img ) => media.id === img.id ) === | ||
undefined | ||
) { | ||
images[ replaceIndex ] = media as MediaItem; | ||
recordEvent( | ||
'product_images_replace_image_button_click' | ||
); | ||
setImages( images ); | ||
} | ||
} } | ||
onSelectAsCover={ () => | ||
recordEvent( | ||
'product_images_select_image_as_cover_button_click' | ||
) | ||
} | ||
> | ||
{ images.map( ( image ) => ( | ||
<ImageGalleryItem | ||
key={ image.id || image.url } | ||
alt={ image.alt } | ||
src={ image.url || image.src } | ||
id={ `${ image.id }` } | ||
/> | ||
) ) } | ||
</ImageGallery> | ||
</div> | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/js/product-editor/src/components/images/editor.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.wp-block-woocommerce-product-images { | ||
.components-card__body { | ||
padding: 0 0 40px 0; | ||
} | ||
.woocommerce-media-uploader { | ||
text-align: left; | ||
} | ||
.woocommerce-media-uploader__label { | ||
display: none; | ||
} | ||
.woocommerce-sortable { | ||
margin-top: 0; | ||
padding: 0; | ||
} | ||
|
||
&:not(.has-images) { | ||
.woocommerce-sortable { | ||
display: none; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { initBlock } from '../../utils'; | ||
import metadata from './block.json'; | ||
import { Edit } from './edit'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
example: {}, | ||
edit: Edit, | ||
}; | ||
|
||
export const init = () => | ||
initBlock( { | ||
name, | ||
metadata: metadata as never, | ||
settings, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { sanitize } from 'dompurify'; | ||
|
||
const ALLOWED_TAGS = [ 'a', 'b', 'em', 'i', 'strong', 'p', 'br' ]; | ||
const ALLOWED_ATTR = [ 'target', 'href', 'rel', 'name', 'download' ]; | ||
|
||
export function sanitizeHTML( html: string ) { | ||
return { | ||
__html: sanitize( html, { ALLOWED_TAGS, ALLOWED_ATTR } ), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add images block to product editor template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.