-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mobile] - Gallery block - Add useGetMedia native variant (#42178)
* Mobile - Gallery block - Add useGetMedia native variant - Image block: Don't call for media data if it already has it * Mobile - Image block - Update image URL if needed by checking if an image size was specified or if it doesn't contain a query param * Mobile - CHANGELOG update * Mobile - Update CHANGELOG
- Loading branch information
Gerardo Pacheco
authored
Jul 20, 2022
1 parent
5d24908
commit 4bb127e
Showing
4 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/block-library/src/gallery/use-get-media.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
const EMPTY_IMAGE_MEDIA = []; | ||
|
||
/** | ||
* Retrieves the extended media info for each gallery image from the store. This is used to | ||
* determine which image size options are available for the current gallery. | ||
* | ||
* @param {Array} innerBlockImages An array of the innerBlock images currently in the gallery. | ||
* | ||
* @return {Array} An array of media info options for each gallery image. | ||
*/ | ||
export default function useGetMedia( innerBlockImages ) { | ||
return useSelect( | ||
( select ) => { | ||
const imagesUploading = innerBlockImages.some( | ||
( { attributes } ) => attributes?.url?.indexOf( 'file:' ) === 0 | ||
); | ||
const imageIds = innerBlockImages | ||
.filter( ( { attributes } ) => { | ||
const { id, url } = attributes; | ||
return id !== undefined && url?.indexOf( 'file:' ) !== 0; | ||
} ) | ||
.map( ( imageBlock ) => imageBlock.attributes.id ); | ||
|
||
if ( imageIds.length === 0 || imagesUploading ) { | ||
return EMPTY_IMAGE_MEDIA; | ||
} | ||
|
||
return ( | ||
select( coreStore ).getMediaItems( { | ||
include: imageIds.join( ',' ), | ||
per_page: imageIds.length, | ||
orderby: 'include', | ||
} ) ?? EMPTY_IMAGE_MEDIA | ||
); | ||
}, | ||
[ innerBlockImages ] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters