Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media & Text: Optimize block editor store subscriptions #68290

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ 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 }
options={ imageSizeOptions }
onChange={ onChange }
/>
);
}

function MediaTextEdit( {
attributes,
isSelected,
Expand Down Expand Up @@ -175,6 +200,20 @@ function MediaTextEdit( {
[ featuredImage ]
);

const { image } = useSelect(
( select ) => {
return {
image:
mediaId && isSelected
? select( coreStore ).getMedia( mediaId, {
context: 'view',
} )
: null,
};
},
[ isSelected, mediaId ]
);
Comment on lines +204 to +216
Copy link
Member Author

Choose a reason for hiding this comment

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

Just a cosmetic change collocates "similar" hooks.


const featuredImageURL = useFeaturedImage
? featuredImageMedia?.source_url
: '';
Expand All @@ -199,22 +238,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 @@ -262,10 +285,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 @@ -411,9 +430,9 @@ function MediaTextEdit( {
</ToolsPanelItem>
) }
{ mediaType === 'image' && ! useFeaturedImage && (
<ResolutionTool
<MediaTextResolutionTool
image={ image }
value={ mediaSizeSlug }
options={ imageSizeOptions }
onChange={ updateImage }
/>
) }
Expand Down
Loading