Skip to content

Commit

Permalink
Make sure that we always recaculate the possible sizeOptions.
Browse files Browse the repository at this point in the history
Before we we calculating the sizeOptions in the constructor which didn't happen often enough. And cased issue when you first inserted the image.
  • Loading branch information
enejb committed Jun 30, 2021
1 parent 38013cc commit fafe93e
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { withSelect, withDispatch } from '@wordpress/data';
import {
image as placeholderIcon,
replace,
fullscreen,
expand,
textColor,
} from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -119,19 +119,6 @@ export class ImageEdit extends Component {
placeholder: __( 'None' ),
},
};

// Only map available image sizes.
this.sizeOptions = map(
filter( this.props.imageSizes, ( { slug } ) =>
get( this.props.image, [
'media_details',
'sizes',
slug,
'source_url',
] )
),
( { name, slug } ) => ( { value: slug, label: name } )
);
}

componentDidMount() {
Expand Down Expand Up @@ -499,25 +486,35 @@ export class ImageEdit extends Component {
imageDefaultSize,
featuredImageId,
wasBlockJustInserted,
imageSizes,
} = this.props;
const { align, url, alt, id, sizeSlug, className } = attributes;

// Only map available image sizes.
const sizeOptions = map(
filter( imageSizes, ( { slug } ) =>
get( image, [ 'media_details', 'sizes', slug, 'source_url' ] )
),
( { name, slug } ) => ( { value: slug, label: name } )
);

let selectedSizeOption = sizeSlug || imageDefaultSize;
let sizeOptionsValid = find( this.sizeOptions, [

let sizeOptionsValid = find( sizeOptions, [
'value',
sizeSlug || imageDefaultSize,
selectedSizeOption,
] );

// By default, it's only possible to set images that have been uploaded to a site's library as featured.
// Images that haven't been uploaded to a site's library have an id of 'undefined', which the 'canImageBeFeatured' check filters out.
const canImageBeFeatured = typeof attributes.id !== 'undefined';

if ( ! sizeOptionsValid ) {
// Default to 'full' size if the default large size is not available.
sizeOptionsValid = find( this.sizeOptions, [ 'value', 'full' ] );
sizeOptionsValid = find( sizeOptions, [ 'value', 'full' ] );
selectedSizeOption = 'full';
}

// By default, it's only possible to set images that have been uploaded to a site's library as featured.
// Images that haven't been uploaded to a site's library have an id of 'undefined', which the 'canImageBeFeatured' check filters out.
const canImageBeFeatured = typeof attributes.id !== 'undefined';

const isFeaturedImage =
canImageBeFeatured && featuredImageId === attributes.id;

Expand Down Expand Up @@ -551,7 +548,7 @@ export class ImageEdit extends Component {
<BottomSheetSelectControl
icon={ expand }
label={ __( 'Size' ) }
options={ this.sizeOptions }
options={ sizeOptions }
onChange={ this.onSizeChangeValue }
value={ selectedSizeOption }
/>
Expand Down

0 comments on commit fafe93e

Please sign in to comment.