UNPKG

@wordpress/block-library

Version:
195 lines (194 loc) • 7.67 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_blocks = require("@wordpress/blocks"); let _wordpress_blob = require("@wordpress/blob"); let _wordpress_hooks = require("@wordpress/hooks"); let __constants_js = require("./constants.cjs"); let __shared_js = require("./shared.cjs"); // packages/block-library/src/gallery/transforms.js const parseShortcodeIds = (ids) => { if (!ids) return []; return ids.split(",").map((id) => parseInt(id, 10)); }; const cloneInnerBlocks = (innerBlocks) => innerBlocks.map((innerBlock) => (0, _wordpress_blocks.createBlock)(innerBlock.name, innerBlock.attributes, cloneInnerBlocks(innerBlock.innerBlocks || []))); /** * Third party block plugins don't have an easy way to detect if the * innerBlocks version of the Gallery is running when they run a * 3rdPartyBlock -> GalleryBlock transform so this transform filter * will handle this. Once the innerBlocks version is the default * in a core release, this could be deprecated and removed after * plugin authors have been given time to update transforms. * * @typedef {Object} Attributes * @typedef {Object} Block * @property {Attributes} attributes The attributes of the block. * @param {Block} block The transformed block. * @return {Block} The transformed block. */ function updateThirdPartyTransformToGallery(block) { if (block.name === "core/gallery" && block.attributes?.images.length > 0) { const innerBlocks = block.attributes.images.map(({ url, id, alt }) => { return (0, _wordpress_blocks.createBlock)("core/image", { url, id: id ? parseInt(id, 10) : null, alt, sizeSlug: block.attributes.sizeSlug, linkDestination: block.attributes.linkDestination }); }); delete block.attributes.ids; delete block.attributes.images; block.innerBlocks = innerBlocks; } return block; } (0, _wordpress_hooks.addFilter)("blocks.switchToBlockType.transformedBlock", "core/gallery/update-third-party-transform-to", updateThirdPartyTransformToGallery); /** * Third party block plugins don't have an easy way to detect if the * innerBlocks version of the Gallery is running when they run a * GalleryBlock -> 3rdPartyBlock transform so this transform filter * will handle this. Once the innerBlocks version is the default * in a core release, this could be deprecated and removed after * plugin authors have been given time to update transforms. * * @typedef {Object} Attributes * @typedef {Object} Block * @property {Attributes} attributes The attributes of the block. * @param {Block} toBlock The block to transform to. * @param {Block[]} fromBlocks The blocks to transform from. * @return {Block} The transformed block. */ function updateThirdPartyTransformFromGallery(toBlock, fromBlocks) { const galleryBlock = (Array.isArray(fromBlocks) ? fromBlocks : [fromBlocks]).find((transformedBlock) => transformedBlock.name === "core/gallery" && transformedBlock.innerBlocks.length > 0 && !transformedBlock.attributes.images?.length > 0 && !toBlock.name.includes("core/")); if (galleryBlock) { const images = galleryBlock.innerBlocks.map(({ attributes: { url, id, alt } }) => ({ url, id: id ? parseInt(id, 10) : null, alt })); const ids = images.map(({ id }) => id); galleryBlock.attributes.images = images; galleryBlock.attributes.ids = ids; } return toBlock; } (0, _wordpress_hooks.addFilter)("blocks.switchToBlockType.transformedBlock", "core/gallery/update-third-party-transform-from", updateThirdPartyTransformFromGallery); const layoutVariationTransform = (variationName, type, isActive) => ({ type: "block", blocks: ["core/gallery"], variationName, isMatch: ({ layout }) => !isActive(layout), transform: (attributes, innerBlocks) => { const { columnCount, minimumColumnWidth } = attributes.layout ?? {}; return (0, _wordpress_blocks.createBlock)("core/gallery", { ...attributes, layout: { type, ...Number.isInteger(columnCount) && columnCount > 0 && { columnCount }, ...typeof minimumColumnWidth === "string" && { minimumColumnWidth } } }, innerBlocks); } }); const transforms = { from: [ { type: "block", isMultiBlock: true, blocks: ["core/image"], transform: (attributes) => { let { align, sizeSlug } = attributes[0]; align = attributes.every((attribute) => attribute.align === align) ? align : void 0; sizeSlug = attributes.every((attribute) => attribute.sizeSlug === sizeSlug) ? sizeSlug : void 0; const innerBlocks = attributes.filter(({ url }) => url).map((image) => { image.width = void 0; image.height = void 0; return (0, _wordpress_blocks.createBlock)("core/image", image); }); return (0, _wordpress_blocks.createBlock)("core/gallery", { align, sizeSlug }, innerBlocks); } }, { type: "shortcode", tag: "gallery", transform({ named: { ids, columns = 3, link, orderby, size } }) { const imageIds = parseShortcodeIds(ids).map((id) => parseInt(id, 10)); let linkTo = __constants_js.LINK_DESTINATION_NONE; if (link === "post") linkTo = __constants_js.LINK_DESTINATION_ATTACHMENT; else if (link === "file") linkTo = __constants_js.LINK_DESTINATION_MEDIA; return (0, _wordpress_blocks.createBlock)("core/gallery", { columns: parseInt(columns, 10), linkTo, randomOrder: orderby === "rand", ...size && { sizeSlug: size } }, imageIds.map((imageId) => (0, _wordpress_blocks.createBlock)("core/image", { id: imageId, ...size && { sizeSlug: size } }))); }, isMatch({ named }) { return void 0 !== named.ids; } }, { type: "files", priority: 1, isMatch(files) { return files.length !== 1 && files.every((file) => file.type.indexOf("image/") === 0); }, transform(files) { const innerBlocks = files.map((file) => (0, _wordpress_blocks.createBlock)("core/image", { blob: (0, _wordpress_blob.createBlobURL)(file) })); return (0, _wordpress_blocks.createBlock)("core/gallery", {}, innerBlocks); } }, layoutVariationTransform("gallery-grid", "grid", (layout) => layout?.type === "grid"), layoutVariationTransform("gallery-flex", "flex", __shared_js.isGalleryFlexLayout) ], to: [{ type: "block", blocks: ["core/image"], isMatch: ({ dynamicContent }) => !dynamicContent, transform: ({ align }, innerBlocks) => { if (innerBlocks.length > 0) return innerBlocks.map(({ attributes: { url, alt, caption, title, href, rel, linkClass, id, sizeSlug: imageSizeSlug, linkDestination, linkTarget, anchor, className } }) => (0, _wordpress_blocks.createBlock)("core/image", { align, url, alt, caption, title, href, rel, linkClass, id, sizeSlug: imageSizeSlug, linkDestination, linkTarget, anchor, className })); return (0, _wordpress_blocks.createBlock)("core/image", { align }); } }, { type: "block", blocks: ["core/group"], variationName: "group-grid", isMatch: ({ dynamicContent }) => !dynamicContent, transform: (attributes, innerBlocks) => { const { allowResize, aspectRatio, caption, columns, dynamicContent, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, navigationButtonType, randomOrder, shortCodeTransforms, sizeSlug, ...rest } = attributes; return (0, _wordpress_blocks.createBlock)("core/group", { ...rest, layout: { type: "grid", columnCount: columns ?? (0, __shared_js.defaultColumnsNumber)(innerBlocks.length) } }, cloneInnerBlocks(innerBlocks)); } }] }; exports.default = transforms; //# sourceMappingURL=transforms.cjs.map