UNPKG

@wordpress/block-library

Version:
173 lines (172 loc) • 7.34 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_data = require("@wordpress/data"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_blocks = require("@wordpress/blocks"); let __shared_js = require("./shared.cjs"); let __utils_js = require("./utils.cjs"); let ___image_utils_js = require("../image/utils.cjs"); let __dynamic_source_js = require("./dynamic-source.cjs"); // packages/block-library/src/gallery/use-dynamic-gallery.js const EMPTY_ARRAY = []; /** * Builds the attributes for a `core/image` block from a media (attachment) * record, applying the gallery-wide settings that affect how the image renders. * * Used to construct the (non-persisted) image blocks previewed in dynamic mode, * and the real image blocks created when a dynamic gallery is converted * ("pinned") back to individual images. The frontend equivalent is * `block_core_gallery_render_dynamic_image()` in `index.php`. * * @param {Object} media A media object as returned by the REST API. * @param {Object} galleryAttributes The gallery block's attributes. * @return {Object} Attributes to pass to `createBlock( 'core/image', ... )`. */ function buildImageBlockAttributes(media, galleryAttributes) { const { sizeSlug, linkTo, linkTarget, aspectRatio } = galleryAttributes; const hasAspectRatio = !!aspectRatio && aspectRatio !== "auto"; return { id: media.id, ...(0, __shared_js.pickRelevantMediaFiles)(media, sizeSlug), ...(0, __utils_js.getHrefAndDestination)(media, linkTo), ...(0, ___image_utils_js.getUpdatedLinkTargetSettings)(linkTarget, galleryAttributes), sizeSlug, caption: media.caption?.raw || "", alt: media.alt_text || "", aspectRatio: hasAspectRatio ? aspectRatio : void 0, scale: hasAspectRatio ? "cover" : void 0 }; } /** * Builds a set of `core/image` blocks from the resolved media, applying the * gallery-wide settings. Each call mints fresh client IDs, so it can produce * both the editor preview and the materialized inner blocks on convert. * * @param {Object[]} media Media records from the REST API. * @param {Object} galleryAttributes The image-relevant gallery attributes. * @return {Object[]} New `core/image` block instances. */ function buildImageBlocks(media, galleryAttributes) { return media.map((mediaItem) => (0, _wordpress_blocks.createBlock)("core/image", buildImageBlockAttributes(mediaItem, galleryAttributes))); } /** * Bundles the Gallery block's "dynamic mode" source resolution and actions. * * Dynamic mode resolves the gallery's images from a configured source * (`attributes.dynamicContent`) instead of from manually-added inner image * blocks. This hook centralizes the shared, single-instance pieces — the source * resolution (one `getEntityRecords`), the editor-preview blocks, and the * mode/ordering actions — out of the block's `edit` component. Transient UI * concerns (e.g. the convert-to-dynamic confirmation) live in the components * that own them. * * @param {Object} options * @param {Object} options.attributes The gallery block attributes. * @param {Function} options.setAttributes The block's `setAttributes`. * @param {string} options.clientId The block client ID. * @param {?number} options.postId The current post ID (from block context). * @param {?string} options.postType The current post type (from block context). * @return {Object} Dynamic-mode source data and actions. */ function useDynamicGallery({ attributes, setAttributes, clientId, postId, postType }) { const { dynamicContent } = attributes; const canUseDynamicSource = !!postType; const sourceDescriptor = (0, __dynamic_source_js.getDynamicSource)(dynamicContent?.source); const sourceOrderby = dynamicContent?.args?.orderBy ?? __dynamic_source_js.DEFAULT_ORDERBY; const sourceOrder = dynamicContent?.args?.order ?? __dynamic_source_js.DEFAULT_ORDER; const registry = (0, _wordpress_data.useRegistry)(); const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const query = (0, _wordpress_element.useMemo)(() => dynamicContent ? (0, __dynamic_source_js.getSourceQuery)(dynamicContent, { postId }) : null, [dynamicContent, postId]); const { dynamicMedia, dynamicMediaTotal, isResolvingDynamic } = (0, _wordpress_data.useSelect)((select) => { if (!query) return { dynamicMedia: EMPTY_ARRAY, dynamicMediaTotal: 0, isResolvingDynamic: false }; const selectorArgs = [ "postType", "attachment", query ]; return { dynamicMedia: select(_wordpress_core_data.store).getEntityRecords(...selectorArgs) ?? EMPTY_ARRAY, dynamicMediaTotal: select(_wordpress_core_data.store).getEntityRecordsTotalItems(...selectorArgs) ?? 0, isResolvingDynamic: !select(_wordpress_core_data.store).hasFinishedResolution("getEntityRecords", selectorArgs) }; }, [query]); const hasMoreImagesThanCap = dynamicMediaTotal > __dynamic_source_js.MAX_IMAGES; const { sizeSlug, linkTo, linkTarget, aspectRatio } = attributes; const imageAttributes = (0, _wordpress_element.useMemo)(() => ({ sizeSlug, linkTo, linkTarget, aspectRatio }), [ sizeSlug, linkTo, linkTarget, aspectRatio ]); const dynamicImageBlocks = (0, _wordpress_element.useMemo)(() => buildImageBlocks(dynamicMedia, imageAttributes), [dynamicMedia, imageAttributes]); const galleryContext = (0, _wordpress_element.useMemo)(() => ({ allowResize: attributes.allowResize ?? false, imageCrop: attributes.imageCrop, fixedHeight: attributes.fixedHeight, navigationButtonType: attributes.navigationButtonType }), [ attributes.allowResize, attributes.imageCrop, attributes.fixedHeight, attributes.navigationButtonType ]); function enableDynamicMode() { registry.batch(() => { setAttributes({ dynamicContent: { source: __dynamic_source_js.ATTACHED_MEDIA } }); __unstableMarkNextChangeAsNotPersistent(); replaceInnerBlocks(clientId, []); }); } function convertToStatic() { registry.batch(() => { replaceInnerBlocks(clientId, buildImageBlocks(dynamicMedia, imageAttributes)); __unstableMarkNextChangeAsNotPersistent(); setAttributes({ dynamicContent: void 0 }); }); } function setSourceOrder(nextOrderby, nextOrder) { const nextArgs = { ...dynamicContent?.args }; delete nextArgs.orderBy; delete nextArgs.order; if (nextOrderby !== void 0 && (nextOrderby !== __dynamic_source_js.DEFAULT_ORDERBY || nextOrder !== __dynamic_source_js.DEFAULT_ORDER)) { nextArgs.orderBy = nextOrderby; nextArgs.order = nextOrder; } const nextSource = { ...dynamicContent }; if (Object.keys(nextArgs).length) nextSource.args = nextArgs; else delete nextSource.args; setAttributes({ dynamicContent: nextSource }); } return { dynamicContent, canUseDynamicSource, sourceDescriptor, hasMoreImagesThanCap, dynamicMediaTotal, sourceOrderby, sourceOrder, dynamicMedia, dynamicImageBlocks, isResolvingDynamic, galleryContext, enableDynamicMode, convertToStatic, setSourceOrder }; } exports.default = useDynamicGallery; //# sourceMappingURL=use-dynamic-gallery.cjs.map