{"version":3,"file":"transforms.mjs","names":[],"sources":["../../src/gallery/transforms.js"],"sourcesContent":["import { createBlock } from '@wordpress/blocks';\nimport { createBlobURL } from '@wordpress/blob';\nimport { addFilter } from '@wordpress/hooks';\nimport {\n\tLINK_DESTINATION_ATTACHMENT,\n\tLINK_DESTINATION_NONE,\n\tLINK_DESTINATION_MEDIA,\n} from './constants';\nimport { defaultColumnsNumber, isGalleryFlexLayout } from './shared';\n\nconst parseShortcodeIds = ( ids ) => {\n\tif ( ! ids ) {\n\t\treturn [];\n\t}\n\n\treturn ids.split( ',' ).map( ( id ) => parseInt( id, 10 ) );\n};\n\nconst cloneInnerBlocks = ( innerBlocks ) =>\n\tinnerBlocks.map( ( innerBlock ) =>\n\t\tcreateBlock(\n\t\t\tinnerBlock.name,\n\t\t\tinnerBlock.attributes,\n\t\t\tcloneInnerBlocks( innerBlock.innerBlocks || [] )\n\t\t)\n\t);\n\n/**\n * Third party block plugins don't have an easy way to detect if the\n * innerBlocks version of the Gallery is running when they run a\n * 3rdPartyBlock -> GalleryBlock transform so this transform filter\n * will handle this. Once the innerBlocks version is the default\n * in a core release, this could be deprecated and removed after\n * plugin authors have been given time to update transforms.\n *\n * @typedef  {Object} Attributes\n * @typedef  {Object} Block\n * @property {Attributes} attributes The attributes of the block.\n * @param    {Block}      block      The transformed block.\n * @return   {Block}                 The transformed block.\n */\nfunction updateThirdPartyTransformToGallery( block ) {\n\tif (\n\t\tblock.name === 'core/gallery' &&\n\t\tblock.attributes?.images.length > 0\n\t) {\n\t\tconst innerBlocks = block.attributes.images.map(\n\t\t\t( { url, id, alt } ) => {\n\t\t\t\treturn createBlock( 'core/image', {\n\t\t\t\t\turl,\n\t\t\t\t\tid: id ? parseInt( id, 10 ) : null,\n\t\t\t\t\talt,\n\t\t\t\t\tsizeSlug: block.attributes.sizeSlug,\n\t\t\t\t\tlinkDestination: block.attributes.linkDestination,\n\t\t\t\t} );\n\t\t\t}\n\t\t);\n\n\t\tdelete block.attributes.ids;\n\t\tdelete block.attributes.images;\n\t\tblock.innerBlocks = innerBlocks;\n\t}\n\n\treturn block;\n}\naddFilter(\n\t'blocks.switchToBlockType.transformedBlock',\n\t'core/gallery/update-third-party-transform-to',\n\tupdateThirdPartyTransformToGallery\n);\n\n/**\n * Third party block plugins don't have an easy way to detect if the\n * innerBlocks version of the Gallery is running when they run a\n * GalleryBlock -> 3rdPartyBlock transform so this transform filter\n * will handle this. Once the innerBlocks version is the default\n * in a core release, this could be deprecated and removed after\n * plugin authors have been given time to update transforms.\n *\n * @typedef  {Object} Attributes\n * @typedef  {Object} Block\n * @property {Attributes} attributes The attributes of the block.\n * @param    {Block}      toBlock    The block to transform to.\n * @param    {Block[]}    fromBlocks The blocks to transform from.\n * @return   {Block}                 The transformed block.\n */\nfunction updateThirdPartyTransformFromGallery( toBlock, fromBlocks ) {\n\tconst from = Array.isArray( fromBlocks ) ? fromBlocks : [ fromBlocks ];\n\tconst galleryBlock = from.find(\n\t\t( transformedBlock ) =>\n\t\t\ttransformedBlock.name === 'core/gallery' &&\n\t\t\ttransformedBlock.innerBlocks.length > 0 &&\n\t\t\t! transformedBlock.attributes.images?.length > 0 &&\n\t\t\t! toBlock.name.includes( 'core/' )\n\t);\n\n\tif ( galleryBlock ) {\n\t\tconst images = galleryBlock.innerBlocks.map(\n\t\t\t( { attributes: { url, id, alt } } ) => ( {\n\t\t\t\turl,\n\t\t\t\tid: id ? parseInt( id, 10 ) : null,\n\t\t\t\talt,\n\t\t\t} )\n\t\t);\n\t\tconst ids = images.map( ( { id } ) => id );\n\t\tgalleryBlock.attributes.images = images;\n\t\tgalleryBlock.attributes.ids = ids;\n\t}\n\n\treturn toBlock;\n}\naddFilter(\n\t'blocks.switchToBlockType.transformedBlock',\n\t'core/gallery/update-third-party-transform-from',\n\tupdateThirdPartyTransformFromGallery\n);\n\n// Flex keeps the Grid settings so switching back to Grid restores them.\nconst layoutVariationTransform = ( variationName, type, isActive ) => ( {\n\ttype: 'block',\n\tblocks: [ 'core/gallery' ],\n\tvariationName,\n\t// Hides the active variation from the block switcher's Transform list.\n\tisMatch: ( { layout } ) => ! isActive( layout ),\n\ttransform: ( attributes, innerBlocks ) => {\n\t\tconst { columnCount, minimumColumnWidth } = attributes.layout ?? {};\n\n\t\treturn createBlock(\n\t\t\t'core/gallery',\n\t\t\t{\n\t\t\t\t...attributes,\n\t\t\t\tlayout: {\n\t\t\t\t\ttype,\n\t\t\t\t\t...( Number.isInteger( columnCount ) &&\n\t\t\t\t\t\tcolumnCount > 0 && { columnCount } ),\n\t\t\t\t\t...( typeof minimumColumnWidth === 'string' && {\n\t\t\t\t\t\tminimumColumnWidth,\n\t\t\t\t\t} ),\n\t\t\t\t},\n\t\t\t},\n\t\t\tinnerBlocks\n\t\t);\n\t},\n} );\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tisMultiBlock: true,\n\t\t\tblocks: [ 'core/image' ],\n\t\t\ttransform: ( attributes ) => {\n\t\t\t\t// Init the align and size from the first item which may be either the placeholder or an image.\n\t\t\t\tlet { align, sizeSlug } = attributes[ 0 ];\n\t\t\t\t// Loop through all the images and check if they have the same align and size.\n\t\t\t\talign = attributes.every(\n\t\t\t\t\t( attribute ) => attribute.align === align\n\t\t\t\t)\n\t\t\t\t\t? align\n\t\t\t\t\t: undefined;\n\t\t\t\tsizeSlug = attributes.every(\n\t\t\t\t\t( attribute ) => attribute.sizeSlug === sizeSlug\n\t\t\t\t)\n\t\t\t\t\t? sizeSlug\n\t\t\t\t\t: undefined;\n\n\t\t\t\tconst validImages = attributes.filter( ( { url } ) => url );\n\n\t\t\t\tconst innerBlocks = validImages.map( ( image ) => {\n\t\t\t\t\t// Gallery images can't currently be resized so make sure height and width are undefined.\n\t\t\t\t\timage.width = undefined;\n\t\t\t\t\timage.height = undefined;\n\t\t\t\t\treturn createBlock( 'core/image', image );\n\t\t\t\t} );\n\n\t\t\t\treturn createBlock(\n\t\t\t\t\t'core/gallery',\n\t\t\t\t\t{\n\t\t\t\t\t\talign,\n\t\t\t\t\t\tsizeSlug,\n\t\t\t\t\t},\n\t\t\t\t\tinnerBlocks\n\t\t\t\t);\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttype: 'shortcode',\n\t\t\ttag: 'gallery',\n\t\t\ttransform( { named: { ids, columns = 3, link, orderby, size } } ) {\n\t\t\t\tconst imageIds = parseShortcodeIds( ids ).map( ( id ) =>\n\t\t\t\t\tparseInt( id, 10 )\n\t\t\t\t);\n\n\t\t\t\tlet linkTo = LINK_DESTINATION_NONE;\n\t\t\t\tif ( link === 'post' ) {\n\t\t\t\t\tlinkTo = LINK_DESTINATION_ATTACHMENT;\n\t\t\t\t} else if ( link === 'file' ) {\n\t\t\t\t\tlinkTo = LINK_DESTINATION_MEDIA;\n\t\t\t\t}\n\n\t\t\t\tconst galleryBlock = createBlock(\n\t\t\t\t\t'core/gallery',\n\t\t\t\t\t{\n\t\t\t\t\t\tcolumns: parseInt( columns, 10 ),\n\t\t\t\t\t\tlinkTo,\n\t\t\t\t\t\trandomOrder: orderby === 'rand',\n\t\t\t\t\t\t...( size && { sizeSlug: size } ),\n\t\t\t\t\t},\n\t\t\t\t\timageIds.map( ( imageId ) =>\n\t\t\t\t\t\tcreateBlock( 'core/image', {\n\t\t\t\t\t\t\tid: imageId,\n\t\t\t\t\t\t\t...( size && { sizeSlug: size } ),\n\t\t\t\t\t\t} )\n\t\t\t\t\t)\n\t\t\t\t);\n\n\t\t\t\treturn galleryBlock;\n\t\t\t},\n\t\t\tisMatch( { named } ) {\n\t\t\t\treturn undefined !== named.ids;\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\t// When created by drag and dropping multiple files on an insertion point. Because multiple\n\t\t\t// files must not be transformed to a gallery when dropped within a gallery there is another transform\n\t\t\t// within the image block to handle that case. Therefore this transform has to have priority 1\n\t\t\t// set so that it overrides the image block transformation when multiple images are dropped outside\n\t\t\t// of a gallery block.\n\t\t\ttype: 'files',\n\t\t\tpriority: 1,\n\t\t\tisMatch( files ) {\n\t\t\t\treturn (\n\t\t\t\t\tfiles.length !== 1 &&\n\t\t\t\t\tfiles.every(\n\t\t\t\t\t\t( file ) => file.type.indexOf( 'image/' ) === 0\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t},\n\t\t\ttransform( files ) {\n\t\t\t\tconst innerBlocks = files.map( ( file ) =>\n\t\t\t\t\tcreateBlock( 'core/image', {\n\t\t\t\t\t\tblob: createBlobURL( file ),\n\t\t\t\t\t} )\n\t\t\t\t);\n\n\t\t\t\treturn createBlock( 'core/gallery', {}, innerBlocks );\n\t\t\t},\n\t\t},\n\t\tlayoutVariationTransform(\n\t\t\t'gallery-grid',\n\t\t\t'grid',\n\t\t\t( layout ) => layout?.type === 'grid'\n\t\t),\n\t\tlayoutVariationTransform( 'gallery-flex', 'flex', isGalleryFlexLayout ),\n\t],\n\tto: [\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/image' ],\n\t\t\t// A dynamic gallery has no inner blocks — its images are resolved\n\t\t\t// from a source at render time — so this transform would produce a\n\t\t\t// single empty image and drop the source.\n\t\t\tisMatch: ( { dynamicContent } ) => ! dynamicContent,\n\t\t\ttransform: ( { align }, innerBlocks ) => {\n\t\t\t\tif ( innerBlocks.length > 0 ) {\n\t\t\t\t\treturn innerBlocks.map(\n\t\t\t\t\t\t( {\n\t\t\t\t\t\t\tattributes: {\n\t\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\t\talt,\n\t\t\t\t\t\t\t\tcaption,\n\t\t\t\t\t\t\t\ttitle,\n\t\t\t\t\t\t\t\thref,\n\t\t\t\t\t\t\t\trel,\n\t\t\t\t\t\t\t\tlinkClass,\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t\tsizeSlug: imageSizeSlug,\n\t\t\t\t\t\t\t\tlinkDestination,\n\t\t\t\t\t\t\t\tlinkTarget,\n\t\t\t\t\t\t\t\tanchor,\n\t\t\t\t\t\t\t\tclassName,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t} ) =>\n\t\t\t\t\t\t\tcreateBlock( 'core/image', {\n\t\t\t\t\t\t\t\talign,\n\t\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\t\talt,\n\t\t\t\t\t\t\t\tcaption,\n\t\t\t\t\t\t\t\ttitle,\n\t\t\t\t\t\t\t\thref,\n\t\t\t\t\t\t\t\trel,\n\t\t\t\t\t\t\t\tlinkClass,\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t\tsizeSlug: imageSizeSlug,\n\t\t\t\t\t\t\t\tlinkDestination,\n\t\t\t\t\t\t\t\tlinkTarget,\n\t\t\t\t\t\t\t\tanchor,\n\t\t\t\t\t\t\t\tclassName,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn createBlock( 'core/image', { align } );\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/group' ],\n\t\t\tvariationName: 'group-grid',\n\t\t\t// As above: with no inner blocks to clone, a dynamic gallery would\n\t\t\t// transform into an empty grid.\n\t\t\tisMatch: ( { dynamicContent } ) => ! dynamicContent,\n\t\t\ttransform: ( attributes, innerBlocks ) => {\n\t\t\t\tconst {\n\t\t\t\t\tallowResize,\n\t\t\t\t\taspectRatio,\n\t\t\t\t\tcaption,\n\t\t\t\t\tcolumns,\n\t\t\t\t\tdynamicContent,\n\t\t\t\t\tfixedHeight,\n\t\t\t\t\tids,\n\t\t\t\t\timageCrop,\n\t\t\t\t\timages,\n\t\t\t\t\tlinkTarget,\n\t\t\t\t\tlinkTo,\n\t\t\t\t\tnavigationButtonType,\n\t\t\t\t\trandomOrder,\n\t\t\t\t\tshortCodeTransforms,\n\t\t\t\t\tsizeSlug,\n\t\t\t\t\t...rest\n\t\t\t\t} = attributes;\n\t\t\t\treturn createBlock(\n\t\t\t\t\t'core/group',\n\t\t\t\t\t{\n\t\t\t\t\t\t...rest,\n\t\t\t\t\t\tlayout: {\n\t\t\t\t\t\t\ttype: 'grid',\n\t\t\t\t\t\t\tcolumnCount:\n\t\t\t\t\t\t\t\tcolumns ??\n\t\t\t\t\t\t\t\tdefaultColumnsNumber( innerBlocks.length ),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\tcloneInnerBlocks( innerBlocks )\n\t\t\t\t);\n\t\t\t},\n\t\t},\n\t],\n};\n\nexport default transforms;\n"],"mappings":";;;;;;AAUA,MAAM,qBAAsB,QAAS;CACpC,IAAK,CAAE,KACN,OAAO,CAAC;CAGT,OAAO,IAAI,MAAO,GAAI,CAAC,CAAC,KAAO,OAAQ,SAAU,IAAI,EAAG,CAAE;AAC3D;AAEA,MAAM,oBAAqB,gBAC1B,YAAY,KAAO,eAClB,YACC,WAAW,MACX,WAAW,YACX,iBAAkB,WAAW,eAAe,CAAC,CAAE,CAChD,CACD;;;;;;;;;;;;;;;AAgBD,SAAS,mCAAoC,OAAQ;CACpD,IACC,MAAM,SAAS,kBACf,MAAM,YAAY,OAAO,SAAS,GACjC;EACD,MAAM,cAAc,MAAM,WAAW,OAAO,KACzC,EAAE,KAAK,IAAI,UAAW;GACvB,OAAO,YAAa,cAAc;IACjC;IACA,IAAI,KAAK,SAAU,IAAI,EAAG,IAAI;IAC9B;IACA,UAAU,MAAM,WAAW;IAC3B,iBAAiB,MAAM,WAAW;GACnC,CAAE;EACH,CACD;EAEA,OAAO,MAAM,WAAW;EACxB,OAAO,MAAM,WAAW;EACxB,MAAM,cAAc;CACrB;CAEA,OAAO;AACR;AACA,UACC,6CACA,gDACA,kCACD;;;;;;;;;;;;;;;;AAiBA,SAAS,qCAAsC,SAAS,YAAa;CAEpE,MAAM,gBADO,MAAM,QAAS,UAAW,IAAI,aAAa,CAAE,UAAW,EAAA,CAC3C,MACvB,qBACD,iBAAiB,SAAS,kBAC1B,iBAAiB,YAAY,SAAS,KACtC,CAAE,iBAAiB,WAAW,QAAQ,SAAS,KAC/C,CAAE,QAAQ,KAAK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