@wordpress/block-library
Version:
Block library for the WordPress editor.
25 lines (24 loc) • 1.03 kB
JavaScript
import { useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
// packages/block-library/src/gallery/use-get-media.js
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.
*/
function useGetMedia(innerBlockImages) {
return useSelect((select) => {
const imageIds = innerBlockImages.map((imageBlock) => imageBlock.attributes.id).filter((id) => id !== void 0).sort((a, b) => a - b);
if (imageIds.length === 0) return EMPTY_IMAGE_MEDIA;
return select(store).getEntityRecords("postType", "attachment", {
include: imageIds.join(","),
per_page: -1
}) ?? EMPTY_IMAGE_MEDIA;
}, [innerBlockImages]);
}
export { useGetMedia as default };
//# sourceMappingURL=use-get-media.mjs.map