From b5a632980cf2efed6ee998da0422b965fc447f38 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 11 Oct 2021 12:43:07 +1300 Subject: [PATCH] Cover block: add option to set opacity when background colour used (#35065) Co-authored-by: Glen Davies --- packages/block-library/src/cover/block.json | 2 +- .../block-library/src/cover/deprecated.js | 1217 ++++++++++------- packages/block-library/src/cover/edit.js | 67 +- packages/block-library/src/cover/save.js | 40 +- packages/block-library/src/cover/shared.js | 2 +- packages/block-library/src/cover/style.scss | 9 +- .../inserting-blocks.test.js.snap | 2 +- .../fixtures/blocks/core__cover.html | 19 +- .../fixtures/blocks/core__cover.json | 6 +- .../fixtures/blocks/core__cover.parsed.json | 10 +- .../blocks/core__cover.serialized.html | 6 +- .../core__cover__deprecated-1.serialized.html | 4 +- .../core__cover__deprecated-2.serialized.html | 4 +- .../core__cover__deprecated-3.serialized.html | 4 +- .../core__cover__deprecated-4.serialized.html | 4 +- .../core__cover__deprecated-5.serialized.html | 4 +- .../blocks/core__cover__deprecated-6.json | 5 +- .../core__cover__deprecated-6.serialized.html | 8 +- .../blocks/core__cover__deprecated-7.html | 12 + .../blocks/core__cover__deprecated-7.json | 32 + .../core__cover__deprecated-7.parsed.json | 30 + .../core__cover__deprecated-7.serialized.html | 7 + .../blocks/core__cover__gradient-image.html | 6 +- .../blocks/core__cover__gradient-image.json | 2 +- .../core__cover__gradient-image.parsed.json | 4 +- ...ore__cover__gradient-image.serialized.html | 2 +- .../blocks/core__cover__gradient-video.html | 4 +- .../blocks/core__cover__gradient-video.json | 2 +- .../core__cover__gradient-video.parsed.json | 4 +- ...ore__cover__gradient-video.serialized.html | 2 +- .../blocks/core__cover__gradient.html | 3 +- .../blocks/core__cover__gradient.json | 4 +- .../blocks/core__cover__gradient.parsed.json | 4 +- .../core__cover__gradient.serialized.html | 2 +- .../blocks/core__cover__solid-color.html | 3 +- .../blocks/core__cover__solid-color.json | 4 +- .../core__cover__solid-color.parsed.json | 4 +- .../core__cover__solid-color.serialized.html | 2 +- .../blocks/core__cover__video-overlay.html | 19 +- .../blocks/core__cover__video-overlay.json | 2 +- .../core__cover__video-overlay.parsed.json | 4 +- ...core__cover__video-overlay.serialized.html | 2 +- .../fixtures/blocks/core__cover__video.html | 16 +- .../fixtures/blocks/core__cover__video.json | 2 +- .../blocks/core__cover__video.parsed.json | 4 +- .../blocks/core__cover__video.serialized.html | 2 +- 46 files changed, 925 insertions(+), 672 deletions(-) create mode 100644 test/integration/fixtures/blocks/core__cover__deprecated-7.html create mode 100644 test/integration/fixtures/blocks/core__cover__deprecated-7.json create mode 100644 test/integration/fixtures/blocks/core__cover__deprecated-7.parsed.json create mode 100644 test/integration/fixtures/blocks/core__cover__deprecated-7.serialized.html diff --git a/packages/block-library/src/cover/block.json b/packages/block-library/src/cover/block.json index 096cade78b166..ab967afd2b67b 100644 --- a/packages/block-library/src/cover/block.json +++ b/packages/block-library/src/cover/block.json @@ -29,7 +29,7 @@ }, "dimRatio": { "type": "number", - "default": 50 + "default": 100 }, "overlayColor": { "type": "string" diff --git a/packages/block-library/src/cover/deprecated.js b/packages/block-library/src/cover/deprecated.js index f5f2efd3f4af7..cb8225ca33f96 100644 --- a/packages/block-library/src/cover/deprecated.js +++ b/packages/block-library/src/cover/deprecated.js @@ -24,11 +24,31 @@ import { IMAGE_BACKGROUND_TYPE, VIDEO_BACKGROUND_TYPE, backgroundImageStyles, - dimRatioToClass, getPositionClassName, isContentPositionCenter, } from './shared'; +/** + * Original function to determine the background opacity classname + * + * Used in deprecations: v1-7. + * + * @param {number} ratio ratio to use for opacity. + * @return {string} background opacity class . + */ +function dimRatioToClassV1( ratio ) { + return ratio === 0 || ratio === 50 || ! ratio + ? null + : 'has-background-dim-' + 10 * Math.round( ratio / 10 ); +} + +function migrateDimRatio( attributes ) { + return { + ...attributes, + dimRatio: ! attributes.url ? 100 : attributes.dimRatio, + }; +} + const blockAttributes = { url: { type: 'string', @@ -59,556 +79,733 @@ const blockAttributes = { }, }; -const deprecated = [ - { - attributes: { - ...blockAttributes, - isRepeated: { - type: 'boolean', - default: false, - }, - minHeight: { - type: 'number', - }, - minHeightUnit: { - type: 'string', - }, - gradient: { - type: 'string', - }, - customGradient: { - type: 'string', - }, - contentPosition: { - type: 'string', +const v7 = { + attributes: { + ...blockAttributes, + isRepeated: { + type: 'boolean', + default: false, + }, + minHeight: { + type: 'number', + }, + minHeightUnit: { + type: 'string', + }, + gradient: { + type: 'string', + }, + customGradient: { + type: 'string', + }, + contentPosition: { + type: 'string', + }, + alt: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + }, + supports: { + anchor: true, + align: true, + html: false, + spacing: { + padding: true, + __experimentalDefaultControls: { + padding: true, }, }, - supports: { - align: true, + color: { + __experimentalDuotone: + '> .wp-block-cover__image-background, > .wp-block-cover__video-background', + text: false, + background: false, }, - save( { attributes } ) { - const { - backgroundType, - gradient, - contentPosition, - customGradient, - customOverlayColor, - dimRatio, - focalPoint, - hasParallax, - isRepeated, - overlayColor, - url, - minHeight: minHeightProp, - minHeightUnit, - } = attributes; - const overlayColorClass = getColorClassName( - 'background-color', - overlayColor - ); - const gradientClass = __experimentalGetGradientClass( gradient ); - const minHeight = minHeightUnit - ? `${ minHeightProp }${ minHeightUnit }` - : minHeightProp; - - const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType; - const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType; - - const style = isImageBackground ? backgroundImageStyles( url ) : {}; - const videoStyle = {}; - - if ( ! overlayColorClass ) { - style.backgroundColor = customOverlayColor; - } + }, + save( { attributes } ) { + const { + backgroundType, + gradient, + contentPosition, + customGradient, + customOverlayColor, + dimRatio, + focalPoint, + hasParallax, + isRepeated, + overlayColor, + url, + alt, + id, + minHeight: minHeightProp, + minHeightUnit, + } = attributes; + const overlayColorClass = getColorClassName( + 'background-color', + overlayColor + ); + const gradientClass = __experimentalGetGradientClass( gradient ); + const minHeight = minHeightUnit + ? `${ minHeightProp }${ minHeightUnit }` + : minHeightProp; - if ( customGradient && ! url ) { - style.background = customGradient; - } - style.minHeight = minHeight || undefined; + const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType; + const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType; - let positionValue; + const isImgElement = ! ( hasParallax || isRepeated ); - if ( focalPoint ) { - positionValue = `${ Math.round( - focalPoint.x * 100 - ) }% ${ Math.round( focalPoint.y * 100 ) }%`; + const style = { + ...( isImageBackground && ! isImgElement + ? backgroundImageStyles( url ) + : {} ), + backgroundColor: ! overlayColorClass + ? customOverlayColor + : undefined, + background: customGradient && ! url ? customGradient : undefined, + minHeight: minHeight || undefined, + }; - if ( isImageBackground && ! hasParallax ) { - style.backgroundPosition = positionValue; - } + const objectPosition = + // prettier-ignore + focalPoint && isImgElement + ? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round( focalPoint.y * 100 ) }%` + : undefined; - if ( isVideoBackground ) { - videoStyle.objectPosition = positionValue; - } - } + const classes = classnames( + dimRatioToClassV1( dimRatio ), + overlayColorClass, + { + 'has-background-dim': dimRatio !== 0, + 'has-parallax': hasParallax, + 'is-repeated': isRepeated, + 'has-background-gradient': gradient || customGradient, + [ gradientClass ]: ! url && gradientClass, + 'has-custom-content-position': ! isContentPositionCenter( + contentPosition + ), + }, + getPositionClassName( contentPosition ) + ); - const classes = classnames( - dimRatioToClass( dimRatio ), - overlayColorClass, - { - 'has-background-dim': dimRatio !== 0, - 'has-parallax': hasParallax, - 'is-repeated': isRepeated, - 'has-background-gradient': gradient || customGradient, - [ gradientClass ]: ! url && gradientClass, - 'has-custom-content-position': ! isContentPositionCenter( - contentPosition - ), - }, - getPositionClassName( contentPosition ) - ); - - return ( -
- { url && - ( gradient || customGradient ) && - dimRatio !== 0 && ( -