Skip to content

Commit

Permalink
Media & Text: Optimize block editor store subscriptions (WordPress#68290
Browse files Browse the repository at this point in the history
)

* Media & Text: Optimize block editor store subscriptions
* Colocate media details store selectors
* Set default value

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
2 people authored and Gulamdastgir-Momin committed Jan 23, 2025
1 parent 83f0441 commit 49a3043
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ function attributesFromMedia( {
};
}

function MediaTextResolutionTool( { image, value, onChange } ) {
const { imageSizes } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return {
imageSizes: getSettings().imageSizes,
};
}, [] );

if ( ! imageSizes?.length ) {
return null;
}

const imageSizeOptions = imageSizes
.filter( ( { slug } ) => getImageSourceUrlBySizeSlug( image, slug ) )
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );

return (
<ResolutionTool
value={ value }
defaultValue={ DEFAULT_MEDIA_SIZE_SLUG }
options={ imageSizeOptions }
onChange={ onChange }
/>
);
}

function MediaTextEdit( {
attributes,
isSelected,
Expand All @@ -154,12 +180,12 @@ function MediaTextEdit( {
mediaType,
mediaUrl,
mediaWidth,
mediaSizeSlug,
rel,
verticalAlignment,
allowedBlocks,
useFeaturedImage,
} = attributes;
const mediaSizeSlug = attributes.mediaSizeSlug || DEFAULT_MEDIA_SIZE_SLUG;

const [ featuredImage ] = useEntityProp(
'postType',
Expand All @@ -182,6 +208,20 @@ function MediaTextEdit( {
[ featuredImage, useFeaturedImage ]
);

const { image } = useSelect(
( select ) => {
return {
image:
mediaId && isSelected
? select( coreStore ).getMedia( mediaId, {
context: 'view',
} )
: null,
};
},
[ isSelected, mediaId ]
);

const featuredImageURL = useFeaturedImage
? featuredImageMedia?.source_url
: '';
Expand All @@ -206,22 +246,6 @@ function MediaTextEdit( {
} );
};

const { imageSizes, image } = useSelect(
( select ) => {
const { getSettings } = select( blockEditorStore );
return {
image:
mediaId && isSelected
? select( coreStore ).getMedia( mediaId, {
context: 'view',
} )
: null,
imageSizes: getSettings()?.imageSizes,
};
},
[ isSelected, mediaId ]
);

const refMedia = useRef();
const imperativeFocalPointPreview = ( value ) => {
const { style } = refMedia.current;
Expand Down Expand Up @@ -269,10 +293,6 @@ function MediaTextEdit( {
const onVerticalAlignmentChange = ( alignment ) => {
setAttributes( { verticalAlignment: alignment } );
};

const imageSizeOptions = imageSizes
.filter( ( { slug } ) => getImageSourceUrlBySizeSlug( image, slug ) )
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );
const updateImage = ( newMediaSizeSlug ) => {
const newUrl = getImageSourceUrlBySizeSlug( image, newMediaSizeSlug );

Expand Down Expand Up @@ -418,9 +438,9 @@ function MediaTextEdit( {
</ToolsPanelItem>
) }
{ mediaType === 'image' && ! useFeaturedImage && (
<ResolutionTool
<MediaTextResolutionTool
image={ image }
value={ mediaSizeSlug }
options={ imageSizeOptions }
onChange={ updateImage }
/>
) }
Expand Down

0 comments on commit 49a3043

Please sign in to comment.