Skip to content

Commit

Permalink
Revert JS changes, moving them to a new PR #60008
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Mar 20, 2024
1 parent 86615bf commit b2c2057
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 76 deletions.
1 change: 0 additions & 1 deletion lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function gutenberg_render_background_support( $block_content, $block ) {
return $block_content;
}

// Grab any default value from block.json.
$background_styles = array();
$background_styles['backgroundSize'] = isset( $block_attributes['style']['background']['backgroundSize'] ) ? $block_attributes['style']['background']['backgroundSize'] : 'cover';
$background_styles['backgroundImage'] = isset( $block_attributes['style']['background']['backgroundImage'] ) ? $block_attributes['style']['background']['backgroundImage'] : array();
Expand Down
82 changes: 9 additions & 73 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,6 @@ import { store as blockEditorStore } from '../store';
export const BACKGROUND_SUPPORT_KEY = 'background';
export const IMAGE_BACKGROUND_TYPE = 'image';

function useBlockProps( { name, style } ) {
if ( ! hasBackgroundSupport( name ) ) {
return {};
}

const backgroundImage = style?.background?.backgroundImage;

if ( backgroundImage?.source === 'file' && !! backgroundImage?.url ) {
// Block background size defaults to cover.
const backgroundSize = style?.background?.backgroundSize ?? 'cover';
let backgroundPosition = style?.background?.backgroundPosition;

// If background size is set to contain, but no position is set, default to center.
if (
backgroundSize === 'contain' &&
backgroundPosition === undefined
) {
backgroundPosition = 'center';
}

return {
style: {
...style,
backgroundSize,
backgroundPosition,
},
};
}

return { style };
}

/**
* Checks if there is a current value in the background image block support
* attributes.
Expand Down Expand Up @@ -198,23 +166,18 @@ export function resetBackgroundImage( style = {}, setAttributes ) {
* Resets the background size block support attributes. This can be used when disabling
* the background size controls for a block via a `ToolsPanel`.
*
* @param {Object} style Style attribute.
* @param {Function} setAttributes Function to set block's attributes.
* @param {string} backgroundSizeDefaultValue Reset value for backgroundSize
* @param {Object} style Style attribute.
* @param {Function} setAttributes Function to set block's attributes.
*/
function resetBackgroundSize(
style = {},
setAttributes,
backgroundSizeDefaultValue
) {
function resetBackgroundSize( style = {}, setAttributes ) {
setAttributes( {
style: cleanEmptyObject( {
...style,
background: {
...style?.background,
backgroundPosition: undefined,
backgroundRepeat: undefined,
backgroundSize: backgroundSizeDefaultValue,
backgroundSize: undefined,
},
} ),
} );
Expand Down Expand Up @@ -294,7 +257,6 @@ function BackgroundImagePanelItem( {
[ clientId ]
);
const { id, title, url } = style?.background?.backgroundImage || {};
const sizeValue = style?.background?.backgroundSize;

const replaceContainerRef = useRef();

Expand Down Expand Up @@ -349,7 +311,6 @@ function BackgroundImagePanelItem( {
source: 'file',
title: media.title || undefined,
},
backgroundSize: sizeValue || 'cover',
},
};

Expand Down Expand Up @@ -479,7 +440,6 @@ function BackgroundSizePanelItem( {
clientId,
isShownByDefault,
setAttributes,
defaultValue = 'auto',
} ) {
const style = useSelect(
( select ) =>
Expand All @@ -489,9 +449,8 @@ function BackgroundSizePanelItem( {

const sizeValue = style?.background?.backgroundSize;
const repeatValue = style?.background?.backgroundRepeat;
const positionValue = style?.background?.backgroundPosition;

// An `undefined` value is treated as whatever the value of defaultValue by the toggle group control.
// An `undefined` value is treated as `cover` by the toggle group control.
// An empty string is treated as `auto` by the toggle group control. This
// allows a user to select "Size" and then enter a custom value, with an
// empty value being treated as `auto`.
Expand All @@ -501,7 +460,7 @@ function BackgroundSizePanelItem( {
sizeValue !== 'contain' ) ||
sizeValue === ''
? 'auto'
: sizeValue || defaultValue;
: sizeValue || 'cover';

// If the current value is `cover` and the repeat value is `undefined`, then
// the toggle should be unchecked as the default state. Otherwise, the toggle
Expand Down Expand Up @@ -530,21 +489,9 @@ function BackgroundSizePanelItem( {
const updateBackgroundSize = ( next ) => {
// When switching to 'contain' toggle the repeat off.
let nextRepeat = repeatValue;
let nextPosition = positionValue;

if ( next === 'contain' ) {
nextRepeat = 'no-repeat';
if ( nextPosition === undefined ) {
nextPosition = 'center';
}
}

if ( next !== 'contain' && nextPosition === 'center' ) {
nextPosition = undefined;
}

if ( next === 'cover' ) {
nextRepeat = undefined;
}

if (
Expand All @@ -560,15 +507,14 @@ function BackgroundSizePanelItem( {
...style,
background: {
...style?.background,
backgroundPosition: nextPosition,
backgroundRepeat: nextRepeat,
backgroundSize: next,
},
} ),
} );
};

const updateBackgroundPosition = ( next ) =>
const updateBackgroundPosition = ( next ) => {
setAttributes( {
style: cleanEmptyObject( {
...style,
Expand All @@ -578,6 +524,7 @@ function BackgroundSizePanelItem( {
},
} ),
} );
};

const toggleIsRepeated = () => {
setAttributes( {
Expand All @@ -599,9 +546,7 @@ function BackgroundSizePanelItem( {
className="single-column"
hasValue={ () => hasValue }
label={ __( 'Size' ) }
onDeselect={ () =>
resetBackgroundSize( style, setAttributes, defaultValue )
}
onDeselect={ () => resetBackgroundSize( style, setAttributes ) }
isShownByDefault={ isShownByDefault }
resetAllFilter={ resetAllFilter }
panelId={ clientId }
Expand Down Expand Up @@ -681,8 +626,6 @@ export function BackgroundImagePanel( props ) {
'__experimentalDefaultControls',
] );

const defaultBackgroundSize = 'cover';

return (
<InspectorControls group="background">
<BackgroundImagePanelItem
Expand All @@ -692,16 +635,9 @@ export function BackgroundImagePanel( props ) {
{ showBackgroundSize && (
<BackgroundSizePanelItem
isShownByDefault={ defaultControls?.backgroundSize }
defaultValue={ defaultBackgroundSize }
{ ...props }
/>
) }
</InspectorControls>
);
}

export default {
useBlockProps,
attributeKeys: [ 'style' ],
hasSupport: hasBackgroundSupport,
};
2 changes: 0 additions & 2 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import './metadata';
import blockHooks from './block-hooks';
import blockRenaming from './block-renaming';
import './use-bindings-attributes';
import background from './background';

createBlockEditFilter(
[
Expand All @@ -49,7 +48,6 @@ createBlockEditFilter(
);
createBlockListBlockFilter( [
align,
background,
style,
color,
dimensions,
Expand Down

0 comments on commit b2c2057

Please sign in to comment.