@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 12.1 kB
Source Map (JSON)
{"version":3,"file":"transforms.mjs","names":["blockEditorPrivateApis"],"sources":["../../src/cover/transforms.js"],"sourcesContent":["import { createBlock } from '@wordpress/blocks';\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\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\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\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\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":";;;;;AAKA,MAAM,EAAE,qBAAqB,OAAQA,WAAuB;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