UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 13.4 kB
{ "version": 3, "sources": ["../../src/cover/transforms.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createBlock } from '@wordpress/blocks';\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { IMAGE_BACKGROUND_TYPE, VIDEO_BACKGROUND_TYPE } from './shared';\nimport { unlock } from '../lock-unlock';\n\nconst { cleanEmptyObject } = unlock( blockEditorPrivateApis );\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/image' ],\n\t\t\ttransform: ( { caption, url, alt, align, id, anchor, style } ) =>\n\t\t\t\tcreateBlock(\n\t\t\t\t\t'core/cover',\n\t\t\t\t\t{\n\t\t\t\t\t\tdimRatio: 50,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\talt,\n\t\t\t\t\t\talign,\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tanchor,\n\t\t\t\t\t\tstyle: {\n\t\t\t\t\t\t\tcolor: {\n\t\t\t\t\t\t\t\tduotone: style?.color?.duotone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[\n\t\t\t\t\t\tcreateBlock( 'core/paragraph', {\n\t\t\t\t\t\t\tcontent: caption,\n\t\t\t\t\t\t\tfontSize: 'large',\n\t\t\t\t\t\t\tstyle: {\n\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\ttextAlign: 'center',\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t} ),\n\t\t\t\t\t]\n\t\t\t\t),\n\t\t},\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/video' ],\n\t\t\ttransform: ( { caption, src, align, id, anchor } ) =>\n\t\t\t\tcreateBlock(\n\t\t\t\t\t'core/cover',\n\t\t\t\t\t{\n\t\t\t\t\t\tdimRatio: 50,\n\t\t\t\t\t\turl: src,\n\t\t\t\t\t\talign,\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tbackgroundType: VIDEO_BACKGROUND_TYPE,\n\t\t\t\t\t\tanchor,\n\t\t\t\t\t},\n\t\t\t\t\t[\n\t\t\t\t\t\tcreateBlock( 'core/paragraph', {\n\t\t\t\t\t\t\tcontent: caption,\n\t\t\t\t\t\t\tfontSize: 'large',\n\t\t\t\t\t\t\tstyle: {\n\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\ttextAlign: 'center',\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t} ),\n\t\t\t\t\t]\n\t\t\t\t),\n\t\t},\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/group' ],\n\t\t\ttransform: ( attributes, innerBlocks ) => {\n\t\t\t\tconst { align, anchor, backgroundColor, gradient, style } =\n\t\t\t\t\tattributes;\n\n\t\t\t\t// If the Group block being transformed has a Cover block as its\n\t\t\t\t// only child return that Cover block.\n\t\t\t\tif (\n\t\t\t\t\tinnerBlocks?.length === 1 &&\n\t\t\t\t\tinnerBlocks[ 0 ]?.name === 'core/cover'\n\t\t\t\t) {\n\t\t\t\t\treturn createBlock(\n\t\t\t\t\t\t'core/cover',\n\t\t\t\t\t\tinnerBlocks[ 0 ].attributes,\n\t\t\t\t\t\tinnerBlocks[ 0 ].innerBlocks\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// If no background or gradient color is provided, default to 50% opacity.\n\t\t\t\t// This matches the styling of a Cover block with a background image,\n\t\t\t\t// in the state where a background image has been removed.\n\t\t\t\tconst dimRatio =\n\t\t\t\t\tbackgroundColor ||\n\t\t\t\t\tgradient ||\n\t\t\t\t\tstyle?.color?.background ||\n\t\t\t\t\tstyle?.color?.gradient\n\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t: 50;\n\n\t\t\t\t// Move the background or gradient color to the parent Cover block.\n\t\t\t\tconst parentAttributes = {\n\t\t\t\t\talign,\n\t\t\t\t\tanchor,\n\t\t\t\t\tdimRatio,\n\t\t\t\t\toverlayColor: backgroundColor,\n\t\t\t\t\tcustomOverlayColor: style?.color?.background,\n\t\t\t\t\tgradient,\n\t\t\t\t\tcustomGradient: style?.color?.gradient,\n\t\t\t\t};\n\n\t\t\t\tconst attributesWithoutBackgroundColors = {\n\t\t\t\t\t...attributes,\n\t\t\t\t\tbackgroundColor: undefined,\n\t\t\t\t\tgradient: undefined,\n\t\t\t\t\tstyle: cleanEmptyObject( {\n\t\t\t\t\t\t...attributes?.style,\n\t\t\t\t\t\tcolor: style?.color\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t...style?.color,\n\t\t\t\t\t\t\t\t\tbackground: undefined,\n\t\t\t\t\t\t\t\t\tgradient: undefined,\n\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t} ),\n\t\t\t\t};\n\n\t\t\t\t// Preserve the block by nesting it within the Cover block,\n\t\t\t\t// instead of converting the Group block directly to the Cover block.\n\t\t\t\treturn createBlock( 'core/cover', parentAttributes, [\n\t\t\t\t\tcreateBlock(\n\t\t\t\t\t\t'core/group',\n\t\t\t\t\t\tattributesWithoutBackgroundColors,\n\t\t\t\t\t\tinnerBlocks\n\t\t\t\t\t),\n\t\t\t\t] );\n\t\t\t},\n\t\t},\n\t],\n\tto: [\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/image' ],\n\t\t\tisMatch: ( {\n\t\t\t\tbackgroundType,\n\t\t\t\turl,\n\t\t\t\toverlayColor,\n\t\t\t\tcustomOverlayColor,\n\t\t\t\tgradient,\n\t\t\t\tcustomGradient,\n\t\t\t} ) => {\n\t\t\t\tif ( url ) {\n\t\t\t\t\t// If a url exists the transform could happen if that URL represents an image background.\n\t\t\t\t\treturn backgroundType === IMAGE_BACKGROUND_TYPE;\n\t\t\t\t}\n\t\t\t\t// If a url is not set the transform could happen if the cover has no background color or gradient;\n\t\t\t\treturn (\n\t\t\t\t\t! overlayColor &&\n\t\t\t\t\t! customOverlayColor &&\n\t\t\t\t\t! gradient &&\n\t\t\t\t\t! customGradient\n\t\t\t\t);\n\t\t\t},\n\t\t\ttransform: ( { title, url, alt, align, id, anchor, style } ) =>\n\t\t\t\tcreateBlock( 'core/image', {\n\t\t\t\t\tcaption: title,\n\t\t\t\t\turl,\n\t\t\t\t\talt,\n\t\t\t\t\talign,\n\t\t\t\t\tid,\n\t\t\t\t\tanchor,\n\t\t\t\t\tstyle: {\n\t\t\t\t\t\tcolor: {\n\t\t\t\t\t\t\tduotone: style?.color?.duotone,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t} ),\n\t\t},\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/video' ],\n\t\t\tisMatch: ( {\n\t\t\t\tbackgroundType,\n\t\t\t\turl,\n\t\t\t\toverlayColor,\n\t\t\t\tcustomOverlayColor,\n\t\t\t\tgradient,\n\t\t\t\tcustomGradient,\n\t\t\t} ) => {\n\t\t\t\tif ( url ) {\n\t\t\t\t\t// If a url exists the transform could happen if that URL represents a video background.\n\t\t\t\t\treturn backgroundType === VIDEO_BACKGROUND_TYPE;\n\t\t\t\t}\n\t\t\t\t// If a url is not set the transform could happen if the cover has no background color or gradient;\n\t\t\t\treturn (\n\t\t\t\t\t! overlayColor &&\n\t\t\t\t\t! customOverlayColor &&\n\t\t\t\t\t! gradient &&\n\t\t\t\t\t! customGradient\n\t\t\t\t);\n\t\t\t},\n\t\t\ttransform: ( { title, url, align, id, anchor } ) =>\n\t\t\t\tcreateBlock( 'core/video', {\n\t\t\t\t\tcaption: title,\n\t\t\t\t\tsrc: url,\n\t\t\t\t\tid,\n\t\t\t\t\talign,\n\t\t\t\t\tanchor,\n\t\t\t\t} ),\n\t\t},\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tblocks: [ 'core/group' ],\n\t\t\tisMatch: ( { url, useFeaturedImage } ) => {\n\t\t\t\t// If the Cover block uses background media, skip this transform,\n\t\t\t\t// and instead use the Group block's default transform.\n\t\t\t\tif ( url || useFeaturedImage ) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t},\n\t\t\ttransform: ( attributes, innerBlocks ) => {\n\t\t\t\t// Convert Cover overlay colors to comparable Group background colors.\n\t\t\t\tconst transformedColorAttributes = {\n\t\t\t\t\tbackgroundColor: attributes?.overlayColor,\n\t\t\t\t\tgradient: attributes?.gradient,\n\t\t\t\t\tstyle: cleanEmptyObject( {\n\t\t\t\t\t\t...attributes?.style,\n\t\t\t\t\t\tcolor:\n\t\t\t\t\t\t\tattributes?.customOverlayColor ||\n\t\t\t\t\t\t\tattributes?.customGradient ||\n\t\t\t\t\t\t\tattributes?.style?.color\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\tbackground:\n\t\t\t\t\t\t\t\t\t\t\tattributes?.customOverlayColor,\n\t\t\t\t\t\t\t\t\t\tgradient: attributes?.customGradient,\n\t\t\t\t\t\t\t\t\t\t...attributes?.style?.color,\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t} ),\n\t\t\t\t};\n\n\t\t\t\t// If the Cover block contains only a single Group block as a direct child,\n\t\t\t\t// then attempt to merge the Cover's background colors with the child Group block,\n\t\t\t\t// and remove the Cover block as the wrapper.\n\t\t\t\tif (\n\t\t\t\t\tinnerBlocks?.length === 1 &&\n\t\t\t\t\tinnerBlocks[ 0 ]?.name === 'core/group'\n\t\t\t\t) {\n\t\t\t\t\tconst groupAttributes = cleanEmptyObject(\n\t\t\t\t\t\tinnerBlocks[ 0 ].attributes || {}\n\t\t\t\t\t);\n\n\t\t\t\t\t// If the Group block contains any kind of background color or gradient,\n\t\t\t\t\t// skip merging Cover background colors, and preserve the Group block's colors.\n\t\t\t\t\tif (\n\t\t\t\t\t\tgroupAttributes?.backgroundColor ||\n\t\t\t\t\t\tgroupAttributes?.gradient ||\n\t\t\t\t\t\tgroupAttributes?.style?.color?.background ||\n\t\t\t\t\t\tgroupAttributes?.style?.color?.gradient\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn createBlock(\n\t\t\t\t\t\t\t'core/group',\n\t\t\t\t\t\t\tgroupAttributes,\n\t\t\t\t\t\t\tinnerBlocks[ 0 ]?.innerBlocks\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn createBlock(\n\t\t\t\t\t\t'core/group',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t...transformedColorAttributes,\n\t\t\t\t\t\t\t...groupAttributes,\n\t\t\t\t\t\t\tstyle: cleanEmptyObject( {\n\t\t\t\t\t\t\t\t...groupAttributes?.style,\n\t\t\t\t\t\t\t\tcolor:\n\t\t\t\t\t\t\t\t\ttransformedColorAttributes?.style?.color ||\n\t\t\t\t\t\t\t\t\tgroupAttributes?.style?.color\n\t\t\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\t\t\t...transformedColorAttributes\n\t\t\t\t\t\t\t\t\t\t\t\t\t?.style?.color,\n\t\t\t\t\t\t\t\t\t\t\t\t...groupAttributes?.style\n\t\t\t\t\t\t\t\t\t\t\t\t\t?.color,\n\t\t\t\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\t} ),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tinnerBlocks[ 0 ]?.innerBlocks\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// In all other cases, transform the Cover block directly to a Group block.\n\t\t\t\treturn createBlock(\n\t\t\t\t\t'core/group',\n\t\t\t\t\t{ ...attributes, ...transformedColorAttributes },\n\t\t\t\t\tinnerBlocks\n\t\t\t\t);\n\t\t\t},\n\t\t},\n\t],\n};\n\nexport default transforms;\n"], "mappings": ";AAGA,SAAS,mBAAmB;AAC5B,SAAS,eAAe,8BAA8B;A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