UNPKG

@wordpress/block-library

Version:
1 lines • 4.84 kB
{"version":3,"file":"shared.cjs","names":["getUpdatedLinkTargetSettings","getHrefAndDestination"],"sources":["../../src/gallery/shared.js"],"sourcesContent":["import { getUpdatedLinkTargetSettings } from '../image/utils';\nimport { getHrefAndDestination } from './utils';\n\nexport function defaultColumnsNumber( imageCount ) {\n\treturn imageCount ? Math.min( 3, imageCount ) : 3;\n}\n\n/**\n * Whether a value is a plain object (and not an array).\n *\n * @param {*} value The value to check.\n * @return {boolean} Whether the value is a plain object.\n */\nexport function isObject( value ) {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n\n/**\n * Whether the Gallery should use its default Flex layout behavior.\n *\n * Gallery blocks created before layout variations existed do not have an\n * explicit layout attribute. Treat missing and malformed layout data as Flex\n * so existing galleries keep their current appearance.\n *\n * @param {*} layout The Gallery layout attribute.\n * @return {boolean} Whether the Gallery uses its Flex layout.\n */\nexport function isGalleryFlexLayout( layout ) {\n\tconst layoutType = isObject( layout ) ? layout.type : undefined;\n\n\treturn (\n\t\ttypeof layoutType !== 'string' ||\n\t\tlayoutType === '' ||\n\t\tlayoutType === 'flex'\n\t);\n}\n\nexport const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => {\n\tconst imageProps = Object.fromEntries(\n\t\tObject.entries( image ?? {} ).filter( ( [ key ] ) =>\n\t\t\t[ 'alt', 'id', 'link' ].includes( key )\n\t\t)\n\t);\n\n\timageProps.url =\n\t\timage?.sizes?.[ sizeSlug ]?.url ||\n\t\timage?.media_details?.sizes?.[ sizeSlug ]?.source_url ||\n\t\timage?.url ||\n\t\timage?.source_url;\n\tconst fullUrl =\n\t\timage?.sizes?.full?.url ||\n\t\timage?.media_details?.sizes?.full?.source_url;\n\tif ( fullUrl ) {\n\t\timageProps.fullUrl = fullUrl;\n\t}\n\treturn imageProps;\n};\n\n/**\n * Returns the image attributes for a gallery aspect ratio.\n *\n * @param {string} aspectRatio The gallery aspect ratio.\n * @return {Object} The `aspectRatio` and `scale` attributes.\n */\nexport function getAspectRatioAttributes( aspectRatio ) {\n\tconst hasAspectRatio = !! aspectRatio && aspectRatio !== 'auto';\n\treturn {\n\t\taspectRatio: hasAspectRatio ? aspectRatio : undefined,\n\t\tscale: hasAspectRatio ? 'cover' : undefined,\n\t};\n}\n\n/**\n * Returns the attributes that apply the gallery settings to an added image.\n *\n * @param {Object} gallery The gallery attributes, with the resolved `linkTo`.\n * @param {Object} attributes The image block attributes.\n * @param {Object} media The image attachment.\n * @return {Object} The attributes to set on the image block.\n */\nexport function getImageAttributes( gallery, attributes, media ) {\n\t// An image transformed from an image block keeps its own link target.\n\tconst linkTargetAttributes =\n\t\tattributes.linkTarget || attributes.rel\n\t\t\t? { linkTarget: attributes.linkTarget, rel: attributes.rel }\n\t\t\t: getUpdatedLinkTargetSettings( gallery.linkTarget, gallery );\n\n\treturn {\n\t\t...pickRelevantMediaFiles( media, gallery.sizeSlug ),\n\t\t...getHrefAndDestination(\n\t\t\tmedia,\n\t\t\tgallery.linkTo,\n\t\t\tattributes.linkDestination\n\t\t),\n\t\t...linkTargetAttributes,\n\t\tclassName: attributes.className || undefined,\n\t\tsizeSlug: gallery.sizeSlug,\n\t\tcaption:\n\t\t\tattributes.caption.length > 0\n\t\t\t\t? attributes.caption\n\t\t\t\t: media.caption?.raw,\n\t\talt: attributes.alt || media.alt_text,\n\t\t...getAspectRatioAttributes( gallery.aspectRatio ),\n\t};\n}\n"],"mappings":";;;;;;;;AAGA,SAAgB,qBAAsB,YAAa;CAClD,OAAO,aAAa,KAAK,IAAK,GAAG,UAAW,IAAI;AACjD;;;;;;;AAQA,SAAgB,SAAU,OAAQ;CACjC,OAAO,CAAC,CAAE,SAAS,OAAO,UAAU,YAAY,CAAE,MAAM,QAAS,KAAM;AACxE;;;;;;;;;;;AAYA,SAAgB,oBAAqB,QAAS;CAC7C,MAAM,aAAa,SAAU,MAAO,IAAI,OAAO,OAAO,KAAA;CAEtD,OACC,OAAO,eAAe,YACtB,e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