UNPKG

@wordpress/block-editor

Version:
100 lines (98 loc) 3.06 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/list-view/use-list-view-images.js var use_list_view_images_exports = {}; __export(use_list_view_images_exports, { default: () => useListViewImages }); module.exports = __toCommonJS(use_list_view_images_exports); var import_element = require("@wordpress/element"); var import_data = require("@wordpress/data"); var import_store = require("../../store/index.cjs"); var MAX_IMAGES = 3; var IMAGE_GETTERS = { "core/image": ({ clientId, attributes }) => { if (attributes.url) { return { url: attributes.url, alt: attributes.alt || "", clientId }; } }, "core/cover": ({ clientId, attributes }) => { if (attributes.backgroundType === "image" && attributes.url) { return { url: attributes.url, alt: attributes.alt || "", clientId }; } }, "core/media-text": ({ clientId, attributes }) => { if (attributes.mediaType === "image" && attributes.mediaUrl) { return { url: attributes.mediaUrl, alt: attributes.mediaAlt || "", clientId }; } }, "core/gallery": ({ innerBlocks }) => { const images = []; const getValues = !!innerBlocks?.length ? IMAGE_GETTERS[innerBlocks[0].name] : void 0; if (!getValues) { return images; } for (const innerBlock of innerBlocks) { const img = getValues(innerBlock); if (img) { images.push(img); } if (images.length >= MAX_IMAGES) { return images; } } return images; } }; function getImagesFromBlock(block, isExpanded) { const getImages = IMAGE_GETTERS[block.name]; const images = !!getImages ? getImages(block) : void 0; if (!images) { return []; } if (!Array.isArray(images)) { return [images]; } return isExpanded ? [] : images; } function useListViewImages({ clientId, isExpanded }) { const { block } = (0, import_data.useSelect)( (select) => { return { block: select(import_store.store).getBlock(clientId) }; }, [clientId] ); const images = (0, import_element.useMemo)(() => { return getImagesFromBlock(block, isExpanded); }, [block, isExpanded]); return images; } //# sourceMappingURL=use-list-view-images.cjs.map