Skip to content

Commit

Permalink
Media & Text: Optimize block editor store subscriptions (#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 <[email protected]>
  • Loading branch information
Mamaduka and Mamaduka authored Dec 27, 2024
1 parent b4424d8 commit 5c7f039
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

1 comment on commit 5c7f039

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 5c7f039.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12514359572
📝 Reported issues:

Please sign in to comment.