Skip to content

Commit

Permalink
Fix reset and hasValue logic, add tests, update label
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Feb 5, 2024
1 parent 6c9bbce commit d9d87ba
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 55 deletions.
63 changes: 15 additions & 48 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ export function hasBackgroundImageValue( style ) {

/**
* Checks if there is a current value in the background size block support
* attributes.
* attributes. Background size values include background size as well
* as background position.
*
* @param {Object} style Style attribute.
* @return {boolean} Whether or not the block has a background size value set.
*/
export function hasBackgroundSizeValue( style ) {
return style?.background?.backgroundSize !== undefined;
return (
style?.background?.backgroundPosition !== undefined ||
style?.background?.backgroundSize !== undefined
);
}

/**
Expand Down Expand Up @@ -131,6 +135,7 @@ function resetBackgroundSize( style = {}, setAttributes ) {
...style,
background: {
...style?.background,
backgroundPosition: undefined,
backgroundRepeat: undefined,
backgroundSize: undefined,
},
Expand Down Expand Up @@ -368,60 +373,22 @@ function backgroundSizeHelpText( value ) {
return __( 'Set a fixed width.' );
}

const coordsToBackgroundPosition = ( value ) => {
if ( ! value ) {
export const coordsToBackgroundPosition = ( value ) => {
if ( ! value || isNaN( value.x ) || isNaN( value.y ) ) {
return undefined;
}

if ( value.x === 0 && value.y === 0 ) {
return 'left top';
}

if ( value.x === 0 && value.y === 1 ) {
return 'left bottom';
}

if ( value.x === 1 && value.y === 0 ) {
return 'right top';
}

if ( value.x === 1 && value.y === 1 ) {
return 'right bottom';
}

if ( value.x === 0.5 && value.y === 0.5 ) {
return 'center';
}

return `${ value.x * 100 }% ${ value.y * 100 }%`;
};

const backgroundPositionToCoords = ( value ) => {
export const backgroundPositionToCoords = ( value ) => {
if ( ! value ) {
return { x: 0.5, y: 0.5 };
}

if ( value === 'left top' ) {
return { x: 0, y: 0 };
}

if ( value === 'left bottom' ) {
return { x: 0, y: 1 };
}

if ( value === 'right top' ) {
return { x: 1, y: 0 };
}

if ( value === 'right bottom' ) {
return { x: 1, y: 1 };
}

if ( value === 'center' ) {
return { x: 0.5, y: 0.5 };
return { x: undefined, y: undefined };
}

const [ x, y ] = value.split( ' ' ).map( ( v ) => parseFloat( v ) / 100 );
let [ x, y ] = value.split( ' ' ).map( ( v ) => parseFloat( v ) / 100 );
x = isNaN( x ) ? undefined : x;
y = isNaN( y ) ? x : y;

return { x, y };
};
Expand Down Expand Up @@ -545,7 +512,7 @@ function BackgroundSizePanelItem( {
<FocalPointPicker
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Focal point' ) }
label={ __( 'Position' ) }
url={ style?.background?.backgroundImage?.url }
value={ backgroundPositionToCoords(
style?.background?.backgroundPosition
Expand Down
50 changes: 50 additions & 0 deletions packages/block-editor/src/hooks/test/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Internal dependencies
*/

import {
backgroundPositionToCoords,
coordsToBackgroundPosition,
} from '../background';

describe( 'backgroundPositionToCoords', () => {
it( 'should return the correct coordinates for a percentage value using 2-value syntax', () => {
expect( backgroundPositionToCoords( '25% 75%' ) ).toEqual( {
x: 0.25,
y: 0.75,
} );
} );

it( 'should return the correct coordinates for a percentage using 1-value syntax', () => {
expect( backgroundPositionToCoords( '50%' ) ).toEqual( {
x: 0.5,
y: 0.5,
} );
} );

it( 'should return undefined coords in given an empty value', () => {
expect( backgroundPositionToCoords( '' ) ).toEqual( {
x: undefined,
y: undefined,
} );
} );

it( 'should return undefined coords in given a string that cannot be converted', () => {
expect( backgroundPositionToCoords( 'apples' ) ).toEqual( {
x: undefined,
y: undefined,
} );
} );
} );

describe( 'coordsToBackgroundPosition', () => {
it( 'should return the correct background position for a set of coordinates', () => {
expect( coordsToBackgroundPosition( { x: 0.25, y: 0.75 } ) ).toBe(
'25% 75%'
);
} );

it( 'should return undefined if no coordinates are provided', () => {
expect( coordsToBackgroundPosition( {} ) ).toBeUndefined();
} );
} );
14 changes: 7 additions & 7 deletions packages/style-engine/src/styles/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ const backgroundImage = {
},
};

const backgroundRepeat = {
const backgroundPosition = {
name: 'backgroundRepeat',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'background', 'backgroundRepeat' ],
'backgroundRepeat'
[ 'background', 'backgroundPosition' ],
'backgroundPosition'
);
},
};

const backgroundPosition = {
const backgroundRepeat = {
name: 'backgroundRepeat',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'background', 'backgroundPosition' ],
'backgroundPosition'
[ 'background', 'backgroundRepeat' ],
'backgroundRepeat'
);
},
};
Expand Down Expand Up @@ -103,7 +103,7 @@ const backgroundSize = {

export default [
backgroundImage,
backgroundRepeat,
backgroundPosition,
backgroundRepeat,
backgroundSize,
];
6 changes: 6 additions & 0 deletions packages/style-engine/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ describe( 'getCSSRules', () => {
source: 'file',
url: 'https://example.com/image.jpg',
},
backgroundPosition: '50% 50%',
backgroundRepeat: 'no-repeat',
backgroundSize: '300px',
},
Expand Down Expand Up @@ -384,6 +385,11 @@ describe( 'getCSSRules', () => {
key: 'backgroundImage',
value: "url( 'https://example.com/image.jpg' )",
},
{
selector: '.some-selector',
key: 'backgroundPosition',
value: '50% 50%',
},
{
selector: '.some-selector',
key: 'backgroundRepeat',
Expand Down

0 comments on commit d9d87ba

Please sign in to comment.