Skip to content

Commit

Permalink
Fix issue with images not loading on first selection from media gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Davies committed Nov 4, 2020
1 parent 729907f commit 53cf527
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { toString, isEqual, isEmpty, find } from 'lodash';
import { isEqual, isEmpty, find } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -87,8 +87,6 @@ function GalleryEdit( props ) {
);

const currentImageOptions = { linkTarget, linkTo, sizeSlug };

const [ images, setImages ] = useState( [] );
const [ imageSettings, setImageSettings ] = useState( currentImageOptions );
const [ dirtyImageOptions, setDirtyImageOptions ] = useState( false );

Expand All @@ -110,6 +108,20 @@ function GalleryEdit( props ) {
};
}, [] );

const images = useSelect( ( select ) => {
const newImages = select( 'core/block-editor' )
.getBlock( clientId )
.innerBlocks.map( ( block ) => {
return {
id: block.attributes.id,
url: block.attributes.url,
attributes: block.attributes,
imageData: getMedia( block.attributes.id ),
};
} );
return newImages;
} );

const imageSizeOptions = useImageSizes( images, isSelected, getSettings );

const { replaceInnerBlocks, updateBlockAttributes } = useDispatch(
Expand Down Expand Up @@ -155,12 +167,6 @@ function GalleryEdit( props ) {
} );
} );

setImages(
newImages.map( ( newImage ) => ( {
id: toString( newImage.id ),
url: newImage.url,
} ) )
);
replaceInnerBlocks( clientId, newBlocks );
}

Expand Down Expand Up @@ -226,21 +232,6 @@ function GalleryEdit( props ) {
}
}, [ imageUploads ] );

useEffect( () => {
const newImages = getBlock( clientId ).innerBlocks.map( ( block ) => {
return {
id: block.attributes.id,
url: block.attributes.url,
attributes: block.attributes,
imageData: getMedia( block.attributes.id ),
};
} );

if ( ! isEqual( newImages, images ) ) {
setImages( newImages );
}
} );

useEffect( () => {
// linkTo attribute must be saved so blocks don't break when changing image_default_link_type in options.php
if ( ! linkTo ) {
Expand Down

0 comments on commit 53cf527

Please sign in to comment.