Skip to content

Commit

Permalink
Changes from PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Davies committed Mar 1, 2021
1 parent fb12b5d commit c72e442
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 53 deletions.
12 changes: 2 additions & 10 deletions packages/block-library/src/gallery/use-get-media.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { some } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -17,8 +12,7 @@ export default function useGetMedia( innerBlockImages ) {
( select ) => {
if (
! innerBlockImages?.length ||
some(
innerBlockImages,
innerBlockImages.some(
( imageBlock ) => ! imageBlock.attributes.id
)
) {
Expand Down Expand Up @@ -48,8 +42,7 @@ export default function useGetMedia( innerBlockImages ) {

if (
imageMedia?.length !== currentImageMedia.length ||
some(
imageMedia,
imageMedia.some(
( newImage ) =>
! currentImageMedia.find(
( currentImage ) => currentImage.id === newImage.id
Expand All @@ -59,6 +52,5 @@ export default function useGetMedia( innerBlockImages ) {
setCurrentImageMedia( imageMedia );
return imageMedia;
}

return currentImageMedia;
}
69 changes: 30 additions & 39 deletions packages/block-library/src/gallery/use-image-sizes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, reduce, map, filter, some } from 'lodash';
import { get, some } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -19,47 +19,38 @@ export default function useImageSizes( images, isSelected, getSettings ) {
let resizedImages = {};

if ( isSelected ) {
resizedImages = reduce(
images,
( currentResizedImages, img ) => {
if ( ! img.id ) {
return currentResizedImages;
}
const image = img;
const sizes = reduce(
imageSizes,
( currentSizes, size ) => {
const defaultUrl = get( image, [
'sizes',
size.slug,
'url',
] );
const mediaDetailsUrl = get( image, [
'media_details',
'sizes',
size.slug,
'source_url',
] );
return {
...currentSizes,
[ size.slug ]: defaultUrl || mediaDetailsUrl,
};
},
{}
);
resizedImages = images.reduce( ( currentResizedImages, img ) => {
if ( ! img.id ) {
return currentResizedImages;
}

const sizes = imageSizes.reduce( ( currentSizes, size ) => {
const defaultUrl = get( img, [
'sizes',
size.slug,
'url',
] );
const mediaDetailsUrl = get( img, [
'media_details',
'sizes',
size.slug,
'source_url',
] );
return {
...currentResizedImages,
[ parseInt( img.id, 10 ) ]: sizes,
...currentSizes,
[ size.slug ]: defaultUrl || mediaDetailsUrl,
};
},
{}
);
}, {} );
return {
...currentResizedImages,
[ parseInt( img.id, 10 ) ]: sizes,
};
}, {} );
}
return map(
filter( imageSizes, ( { slug } ) =>
return imageSizes
.filter( ( { slug } ) =>
some( resizedImages, ( sizes ) => sizes[ slug ] )
),
( { name, slug } ) => ( { value: slug, label: name } )
);
)
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { every } from 'lodash';
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

export default function useShortCodeTransform( shortCodeTransforms ) {
const newImageData = useSelect(
( select ) => {
if ( ! shortCodeTransforms || shortCodeTransforms.length === 0 ) {
return;
}
const getMedia = select( 'core' ).getMedia;
const getMedia = select( coreStore ).getMedia;
return shortCodeTransforms.map( ( image ) => {
const imageData = getMedia( image.id );
if ( imageData ) {
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/gallery/v1/gallery-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { Button, Spinner, ButtonGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { BACKSPACE, DELETE } from '@wordpress/keycodes';
import { withSelect, withDispatch } from '@wordpress/data';
import { RichText, MediaPlaceholder } from '@wordpress/block-editor';
import {
RichText,
MediaPlaceholder,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { isBlobURL } from '@wordpress/blob';
import { compose } from '@wordpress/compose';
import {
Expand All @@ -22,6 +26,7 @@ import {
edit,
image as imageIcon,
} from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -295,7 +300,7 @@ class GalleryImage extends Component {

export default compose( [
withSelect( ( select, ownProps ) => {
const { getMedia } = select( 'core' );
const { getMedia } = select( coreStore );
const { id } = ownProps;

return {
Expand All @@ -304,7 +309,7 @@ export default compose( [
} ),
withDispatch( ( dispatch ) => {
const { __unstableMarkNextChangeAsNotPersistent } = dispatch(
'core/block-editor'
blockEditorStore
);
return {
__unstableMarkNextChangeAsNotPersistent,
Expand Down

0 comments on commit c72e442

Please sign in to comment.