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

Post Featured Image: Optimize store subscriptions #60770

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
<>
Expand Down Expand Up @@ -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 );
/**
Expand All @@ -75,7 +92,7 @@ const DimensionControls = ( {
height || ( aspectRatio && aspectRatio !== 'auto' );

return (
<InspectorControls group="dimensions">
<>
<ToolsPanelItem
hasValue={ () => !! aspectRatio }
label={ __( 'Aspect ratio' ) }
Expand Down Expand Up @@ -230,7 +247,7 @@ const DimensionControls = ( {
/>
</ToolsPanelItem>
) }
</InspectorControls>
</>
);
};

Expand Down
40 changes: 15 additions & 25 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
MediaPlaceholder,
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
__experimentalUseBorderProps as useBorderProps,
__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,
useBlockEditingMode,
Expand Down Expand Up @@ -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( {
Expand Down Expand Up @@ -200,17 +186,21 @@ export default function PostFeaturedImageEdit( {

const controls = blockEditingMode === 'default' && (
<>
<OverlayControls
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
<DimensionControls
clientId={ clientId }
attributes={ attributes }
setAttributes={ setAttributes }
imageSizeOptions={ imageSizeOptions }
/>
<InspectorControls group="color">
<OverlayControls
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
</InspectorControls>
<InspectorControls group="dimensions">
<DimensionControls
clientId={ clientId }
attributes={ attributes }
setAttributes={ setAttributes }
media={ media }
/>
</InspectorControls>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import {
InspectorControls,
withColors,
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
__experimentalUseGradient,
__experimentalUseGradient as useGradient,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
} from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
Expand All @@ -23,15 +22,15 @@ const Overlay = ( {
setOverlayColor,
} ) => {
const { dimRatio } = attributes;
const { gradientValue, setGradient } = __experimentalUseGradient();
const { gradientValue, setGradient } = useGradient();
const colorGradientSettings = useMultipleOriginColorsAndGradients();

if ( ! colorGradientSettings.hasColorsOrGradients ) {
return null;
}

return (
<InspectorControls group="color">
<>
<ColorGradientSettingsDropdown
__experimentalIsRenderedInSidebar
settings={ [
Expand Down Expand Up @@ -79,7 +78,7 @@ const Overlay = ( {
__next40pxDefaultSize
/>
</ToolsPanelItem>
</InspectorControls>
</>
);
};

Expand Down
Loading