diff --git a/packages/block-library/src/cover/deprecated.js b/packages/block-library/src/cover/deprecated.js index 9500c31fda0162..ac7c2a68a13012 100644 --- a/packages/block-library/src/cover/deprecated.js +++ b/packages/block-library/src/cover/deprecated.js @@ -24,12 +24,15 @@ import { __ } from '@wordpress/i18n'; import { IMAGE_BACKGROUND_TYPE, VIDEO_BACKGROUND_TYPE, - backgroundImageStyles, getPositionClassName, isContentPositionCenter, dimRatioToClass, } from './shared'; +function backgroundImageStyles( url ) { + return url ? { backgroundImage: `url(${ url })` } : {}; +} + /** * Original function to determine the background opacity classname * @@ -81,91 +84,232 @@ const blockAttributes = { }, }; -// Deprecation for blocks with `minHeightUnit` set but no `minHeight`. -const v9 = { - attributes: { - url: { - type: 'string', - }, - id: { - type: 'number', - }, - alt: { - type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'alt', - default: '', - }, - hasParallax: { - type: 'boolean', - default: false, - }, - isRepeated: { - type: 'boolean', - default: false, - }, - dimRatio: { - type: 'number', - default: 100, - }, - overlayColor: { - type: 'string', - }, - customOverlayColor: { - type: 'string', - }, - backgroundType: { - type: 'string', - default: 'image', - }, - focalPoint: { - type: 'object', - }, - minHeight: { - type: 'number', - }, - minHeightUnit: { - type: 'string', - }, - gradient: { - type: 'string', - }, - customGradient: { - type: 'string', - }, - contentPosition: { - type: 'string', - }, - isDark: { - type: 'boolean', - default: true, - }, - allowedBlocks: { - type: 'array', - }, - templateLock: { - type: [ 'string', 'boolean' ], - enum: [ 'all', 'insert', false ], - }, +const v8ToV10BlockAttributes = { + url: { + type: 'string', }, - supports: { - anchor: true, - align: true, - html: false, - spacing: { + id: { + type: 'number', + }, + alt: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + hasParallax: { + type: 'boolean', + default: false, + }, + isRepeated: { + type: 'boolean', + default: false, + }, + dimRatio: { + type: 'number', + default: 100, + }, + overlayColor: { + type: 'string', + }, + customOverlayColor: { + type: 'string', + }, + backgroundType: { + type: 'string', + default: 'image', + }, + focalPoint: { + type: 'object', + }, + minHeight: { + type: 'number', + }, + minHeightUnit: { + type: 'string', + }, + gradient: { + type: 'string', + }, + customGradient: { + type: 'string', + }, + contentPosition: { + type: 'string', + }, + isDark: { + type: 'boolean', + default: true, + }, + allowedBlocks: { + type: 'array', + }, + templateLock: { + type: [ 'string', 'boolean' ], + enum: [ 'all', 'insert', false ], + }, +}; + +const v7toV10BlockSupports = { + anchor: true, + align: true, + html: false, + spacing: { + padding: true, + __experimentalDefaultControls: { padding: true, - __experimentalDefaultControls: { - padding: true, - }, - }, - color: { - __experimentalDuotone: - '> .wp-block-cover__image-background, > .wp-block-cover__video-background', - text: false, - background: false, }, }, + color: { + __experimentalDuotone: + '> .wp-block-cover__image-background, > .wp-block-cover__video-background', + text: false, + background: false, + }, +}; + +// Deprecation for blocks that renders fixed background as backgroud from the main block container. +const v10 = { + attributes: v8ToV10BlockAttributes, + supports: v7toV10BlockSupports, + save( { attributes } ) { + const { + backgroundType, + gradient, + contentPosition, + customGradient, + customOverlayColor, + dimRatio, + focalPoint, + useFeaturedImage, + hasParallax, + isDark, + isRepeated, + overlayColor, + url, + alt, + id, + minHeight: minHeightProp, + minHeightUnit, + } = attributes; + const overlayColorClass = getColorClassName( + 'background-color', + overlayColor + ); + const gradientClass = __experimentalGetGradientClass( gradient ); + const minHeight = + minHeightProp && minHeightUnit + ? `${ minHeightProp }${ minHeightUnit }` + : minHeightProp; + + const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType; + const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType; + + const isImgElement = ! ( hasParallax || isRepeated ); + + const style = { + ...( isImageBackground && ! isImgElement && ! useFeaturedImage + ? backgroundImageStyles( url ) + : {} ), + minHeight: minHeight || undefined, + }; + + const bgStyle = { + backgroundColor: ! overlayColorClass + ? customOverlayColor + : undefined, + background: customGradient ? customGradient : undefined, + }; + + const objectPosition = + // prettier-ignore + focalPoint && isImgElement + ? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round( focalPoint.y * 100 ) }%` + : undefined; + + const classes = classnames( + { + 'is-light': ! isDark, + 'has-parallax': hasParallax, + 'is-repeated': isRepeated, + 'has-custom-content-position': ! isContentPositionCenter( + contentPosition + ), + }, + getPositionClassName( contentPosition ) + ); + + const gradientValue = gradient || customGradient; + + return ( +
+