From 390cf5b39071038cababa7514018110c62f7cd84 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 16 Apr 2024 11:59:09 +0400 Subject: [PATCH 1/2] Post Featured Image: Optimize store subscriptions --- .../post-featured-image/dimension-controls.js | 25 ++++++++++-- .../src/post-featured-image/edit.js | 40 +++++++------------ .../post-featured-image/overlay-controls.js | 5 +-- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/post-featured-image/dimension-controls.js b/packages/block-library/src/post-featured-image/dimension-controls.js index 3c380d3dfe0125..d59d586eb04b4a 100644 --- a/packages/block-library/src/post-featured-image/dimension-controls.js +++ b/packages/block-library/src/post-featured-image/dimension-controls.js @@ -10,7 +10,11 @@ import { __experimentalUseCustomUnits as useCustomUnits, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; -import { InspectorControls, useSettings } from '@wordpress/block-editor'; +import { + useSettings, + store as blockEditorStore, +} from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; const SCALE_OPTIONS = ( <> @@ -51,12 +55,25 @@ const DimensionControls = ( { clientId, attributes: { aspectRatio, width, height, scale, sizeSlug }, setAttributes, - imageSizeOptions = [], + media, } ) => { const [ availableUnits ] = useSettings( 'spacing.units' ); const units = useCustomUnits( { availableUnits: availableUnits || [ 'px', '%', 'vw', 'em', 'rem' ], } ); + const imageSizes = useSelect( + ( select ) => select( blockEditorStore ).getSettings().imageSizes, + [] + ); + const imageSizeOptions = imageSizes + .filter( ( { slug } ) => { + return media?.media_details?.sizes?.[ slug ]?.source_url; + } ) + .map( ( { name, slug } ) => ( { + value: slug, + label: name, + } ) ); + const onDimensionChange = ( dimension, nextValue ) => { const parsedValue = parseFloat( nextValue ); /** @@ -75,7 +92,7 @@ const DimensionControls = ( { height || ( aspectRatio && aspectRatio !== 'auto' ); return ( - + <> !! aspectRatio } label={ __( 'Aspect ratio' ) } @@ -230,7 +247,7 @@ const DimensionControls = ( { /> ) } - + ); }; diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js index 25133a2babcfc0..14e9bfdc74a87b 100644 --- a/packages/block-library/src/post-featured-image/edit.js +++ b/packages/block-library/src/post-featured-image/edit.js @@ -24,7 +24,6 @@ import { MediaPlaceholder, MediaReplaceFlow, useBlockProps, - store as blockEditorStore, __experimentalUseBorderProps as useBorderProps, __experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles, useBlockEditingMode, @@ -132,19 +131,6 @@ export default function PostFeaturedImageEdit( { const mediaUrl = getMediaSourceUrlBySizeSlug( media, sizeSlug ); - const imageSizes = useSelect( - ( select ) => select( blockEditorStore ).getSettings().imageSizes, - [] - ); - const imageSizeOptions = imageSizes - .filter( ( { slug } ) => { - return media?.media_details?.sizes?.[ slug ]?.source_url; - } ) - .map( ( { name, slug } ) => ( { - value: slug, - label: name, - } ) ); - const blockProps = useBlockProps( { style: { width, height, aspectRatio }, className: classnames( { @@ -200,17 +186,21 @@ export default function PostFeaturedImageEdit( { const controls = blockEditingMode === 'default' && ( <> - - + + + + + + + <> - + ); }; From c1ffc3792cb4aaa0b841ba8ae304475cb63a8ade Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 16 Apr 2024 12:05:26 +0400 Subject: [PATCH 2/2] Fix hook import --- .../block-library/src/post-featured-image/overlay-controls.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-featured-image/overlay-controls.js b/packages/block-library/src/post-featured-image/overlay-controls.js index f670b674c1bb1e..3dabb60f77fb18 100644 --- a/packages/block-library/src/post-featured-image/overlay-controls.js +++ b/packages/block-library/src/post-featured-image/overlay-controls.js @@ -8,7 +8,7 @@ import { import { withColors, __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown, - __experimentalUseGradient, + __experimentalUseGradient as useGradient, __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients, } from '@wordpress/block-editor'; import { compose } from '@wordpress/compose'; @@ -22,7 +22,7 @@ const Overlay = ( { setOverlayColor, } ) => { const { dimRatio } = attributes; - const { gradientValue, setGradient } = __experimentalUseGradient(); + const { gradientValue, setGradient } = useGradient(); const colorGradientSettings = useMultipleOriginColorsAndGradients(); if ( ! colorGradientSettings.hasColorsOrGradients ) {