From b9e01523c47990bcf0b06a3f17a4fe063381b002 Mon Sep 17 00:00:00 2001 From: Amit Raj Date: Thu, 27 Jun 2024 02:48:47 +0530 Subject: [PATCH] Adds Aspect ratio control on Image blocks in Grids --- .../src/components/dimensions-tool/index.js | 94 ++++++++++--------- packages/block-library/src/image/image.js | 20 +++- 2 files changed, 67 insertions(+), 47 deletions(-) diff --git a/packages/block-editor/src/components/dimensions-tool/index.js b/packages/block-editor/src/components/dimensions-tool/index.js index bbd3ac5ffb86e4..74dbc2faf20f84 100644 --- a/packages/block-editor/src/components/dimensions-tool/index.js +++ b/packages/block-editor/src/components/dimensions-tool/index.js @@ -58,6 +58,7 @@ function DimensionsTool( { scaleOptions, // Default options handled by ScaleTool. defaultScale = 'fill', // Match CSS default value for object-fit. unitsOptions, // Default options handled by UnitControl. + parentLayoutType, } ) { // Coerce undefined and CSS default values to be null. const width = @@ -131,56 +132,61 @@ function DimensionsTool( { onChange( nextValue ); } } /> - { - const nextValue = { ...value }; + { parentLayoutType !== 'grid' && ( + { + const nextValue = { ...value }; - // 'auto' is CSS default, so it gets treated as null. - nextWidth = nextWidth === 'auto' ? null : nextWidth; - nextHeight = nextHeight === 'auto' ? null : nextHeight; + // 'auto' is CSS default, so it gets treated as null. + nextWidth = nextWidth === 'auto' ? null : nextWidth; + nextHeight = nextHeight === 'auto' ? null : nextHeight; - // Update width. - if ( ! nextWidth ) { - delete nextValue.width; - } else { - nextValue.width = nextWidth; - } + // Update width. + if ( ! nextWidth ) { + delete nextValue.width; + } else { + nextValue.width = nextWidth; + } - // Update height. - if ( ! nextHeight ) { - delete nextValue.height; - } else { - nextValue.height = nextHeight; - } + // Update height. + if ( ! nextHeight ) { + delete nextValue.height; + } else { + nextValue.height = nextHeight; + } - // Auto-update aspectRatio. - if ( nextWidth && nextHeight ) { - delete nextValue.aspectRatio; - } else if ( lastAspectRatio ) { - nextValue.aspectRatio = lastAspectRatio; - } else { - // No setting defaultAspectRatio here, because - // aspectRatio is optional in this scenario, - // unlike scale. - } + // Auto-update aspectRatio. + if ( nextWidth && nextHeight ) { + delete nextValue.aspectRatio; + } else if ( lastAspectRatio ) { + nextValue.aspectRatio = lastAspectRatio; + } else { + // No setting defaultAspectRatio here, because + // aspectRatio is optional in this scenario, + // unlike scale. + } - // Auto-update scale. - if ( ! lastAspectRatio && !! nextWidth !== !! nextHeight ) { - delete nextValue.scale; - } else if ( lastScale ) { - nextValue.scale = lastScale; - } else { - nextValue.scale = defaultScale; - setLastScale( defaultScale ); - } + // Auto-update scale. + if ( + ! lastAspectRatio && + !! nextWidth !== !! nextHeight + ) { + delete nextValue.scale; + } else if ( lastScale ) { + nextValue.scale = lastScale; + } else { + nextValue.scale = defaultScale; + setLastScale( defaultScale ); + } - onChange( nextValue ); - } } - /> - { showScaleControl && ( + onChange( nextValue ); + } } + /> + ) } + { showScaleControl && parentLayoutType !== 'grid' && ( image?.media_details?.sizes?.[ slug ]?.source_url @@ -400,6 +399,18 @@ export default function Image( { defaultAspectRatio="auto" scaleOptions={ scaleOptions } unitsOptions={ dimensionsUnitsOptions } + parentLayoutType={ parentLayoutType } + /> + ); + + const aspectRatioControl = ( + { + setAttributes( { aspectRatio: newAspectRatio } ); + } } + defaultAspectRatio="auto" + parentLayoutType={ parentLayoutType } /> ); @@ -421,7 +432,10 @@ export default function Image( { resetAll={ resetAll } dropdownMenuProps={ TOOLSPANEL_DROPDOWNMENU_PROPS } > - { isResizable && dimensionsControl } + { isResizable && + ( parentLayoutType === 'grid' + ? aspectRatioControl + : dimensionsControl ) } );