From 50aca4b50fc426008330fe6b679e2a3e9ca498e2 Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Mon, 8 Jun 2020 21:57:07 +0300 Subject: [PATCH] Image editing: use hooks --- .../src/rich-image/rich-image/index.js | 452 ++++++++---------- 1 file changed, 202 insertions(+), 250 deletions(-) diff --git a/packages/block-library/src/rich-image/rich-image/index.js b/packages/block-library/src/rich-image/rich-image/index.js index 5b6a7d3102375..42b746ee81220 100644 --- a/packages/block-library/src/rich-image/rich-image/index.js +++ b/packages/block-library/src/rich-image/rich-image/index.js @@ -13,14 +13,14 @@ import { BlockControls, __experimentalBlock as Block, } from '@wordpress/block-editor'; -import { Component } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { - rotateLeft, - rotateRight, - flipHorizontal, - flipVertical, - crop, - aspectRatio, + rotateLeft as rotateLeftIcon, + rotateRight as rotateRightIcon, + flipHorizontal as flipHorizontalIcon, + flipVertical as flipVerticalIcon, + crop as cropIcon, + aspectRatio as aspectRatioIcon, } from '@wordpress/icons'; import { ToolbarGroup, @@ -35,7 +35,6 @@ import { MenuItem, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; -import { compose } from '@wordpress/compose'; /** * Internal dependencies @@ -76,7 +75,7 @@ function AspectGroup( { aspectRatios, isDisabled, label, onClick } ) { function AspectMenu( { isDisabled, onClick, toggleProps } ) { return ( ; } - adjustImage( action, attrs ) { - const { setAttributes, attributes, noticeOperations } = this.props; - const { id } = attributes; - - this.setState( { inProgress: action } ); + function adjustImage( action, attrs ) { + setIsProgress( action ); noticeOperations.removeAllNotices(); richImageRequest( id, action, attrs ) .then( ( response ) => { - this.setState( { inProgress: null, isCrop: false } ); + setIsProgress( null ); + setIsCrop( false ); if ( response.mediaID && response.mediaID !== id ) { setAttributes( { @@ -197,237 +200,186 @@ class RichImage extends Component { 'Unable to perform the image modification. Please check your media storage.' ) ); - this.setState( { inProgress: null, isCrop: false } ); + setIsProgress( null ); + setIsCrop( false ); } ); } - cropImage() { - this.adjustImage( 'crop', { - cropX: this.state.crop.x, - cropY: this.state.crop.y, - cropWidth: this.state.crop.width, - cropHeight: this.state.crop.height, + function cropImage() { + adjustImage( 'crop', { + cropX: crop.x, + cropY: crop.y, + cropWidth: crop.width, + cropHeight: crop.height, } ); } - render() { - const { - isSelected, - attributes, - originalBlock: OriginalBlock, - noticeUI, - } = this.props; - const { - isCrop, - inProgress, - position, - zoom, - aspect, - imageSize, - } = this.state; - const { url } = attributes; - const isEditing = ! isCrop && isSelected && url; - - if ( ! isSelected ) { - return ; - } - - const classes = classnames( { - richimage__working: inProgress !== null, - [ 'richimage__working__' + inProgress ]: inProgress !== null, - } ); - - return ( - <> - { noticeUI } - -
- { inProgress && ( -
- -
- ) } + const classes = classnames( { + richimage__working: inProgress !== null, + [ 'richimage__working__' + inProgress ]: inProgress !== null, + } ); - { isCrop ? ( - -
- { - this.setState( { - position: newPosition, - } ); - } } - onCropComplete={ ( newCrop ) => { - this.setState( { crop: newCrop } ); - } } - onZoomChange={ ( newZoom ) => { - this.setState( { zoom: newZoom } ); - } } - onMediaLoaded={ ( newImageSize ) => { - this.setState( { - imageSize: newImageSize, - } ); - } } - /> -
- { - this.setState( { zoom: newZoom } ); - } } + return ( + <> + { noticeUI } +
+ { inProgress && ( +
+ +
+ ) } + { isCrop ? ( + +
+ - - ) : ( - + - ) } -
- - { isEditing && ( - - - - { ( toggleProps ) => ( - - this.adjustImage( - 'rotate', - { - angle: -ROTATE_STEP, - } - ), +
+ ) : ( + + ) } +
+ { isEditing && ( + + + + { ( toggleProps ) => ( + - this.adjustImage( - 'rotate', - { - angle: ROTATE_STEP, - } - ), + }, + { + icon: rotateRightIcon, + title: __( 'Rotate right' ), + isDisabled: inProgress, + onClick() { + adjustImage( 'rotate', { + angle: ROTATE_STEP, + } ); }, - ] } - /> - ) } - - - { ( toggleProps ) => ( - - this.adjustImage( 'flip', { - direction: 'vertical', - } ), + }, + ] } + /> + ) } + + + { ( toggleProps ) => ( + { + adjustImage( 'flip', { + direction: 'vertical', + } ); }, - { - icon: flipHorizontal, - title: __( 'Flip horizontal' ), - isDisabled: inProgress, - onClick: () => - this.adjustImage( 'flip', { - direction: 'horizontal', - } ), + }, + { + icon: flipHorizontalIcon, + title: __( 'Flip horizontal' ), + isDisabled: inProgress, + onClick: () => { + adjustImage( 'flip', { + direction: 'horizontal', + } ); }, - ] } - /> - ) } - - - this.setState( { - isCrop: ! isCrop, - crop: DEFAULT_CROP, - } ) - } - /> - - - ) } - - { isCrop && ( - - -
- -
-
- - - { ( toggleProps ) => ( - { - this.setState( { - aspect: newAspect, - } ); - } } - /> - ) } - - - - - { __( 'Apply' ) } - - - this.setState( { isCrop: false } ) - } - > - { __( 'Cancel' ) } - - -
- ) } - - ); - } + }, + ] } + /> + ) } + + { + setIsCrop( ! isCrop ); + setCrop( DEFAULT_CROP ); + } } + /> + + + ) } + { isCrop && ( + + +
+ +
+
+ + + { ( toggleProps ) => ( + + ) } + + + + + { __( 'Apply' ) } + + { + setIsCrop( false ); + } } + > + { __( 'Cancel' ) } + + +
+ ) } + + ); } -export default compose( [ withNotices ] )( RichImage ); +export default withNotices( RichImage );