2022-05-30 14:38:52 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { Coordinates, ImageFit } from './types';
|
2022-05-19 16:16:46 +00:00
|
|
|
|
2022-05-30 14:38:52 +00:00
|
|
|
/**
|
|
|
|
* Given x and y coordinates between 0 and 1 returns a rounded percentage string.
|
|
|
|
*
|
|
|
|
* Useful for converting to a CSS-compatible position string.
|
|
|
|
*/
|
|
|
|
export function calculatePercentPositionFromCoordinates( coords: Coordinates ) {
|
2022-05-19 16:16:46 +00:00
|
|
|
if ( ! coords ) return '';
|
|
|
|
|
|
|
|
const x = Math.round( coords.x * 100 );
|
|
|
|
const y = Math.round( coords.y * 100 );
|
|
|
|
|
|
|
|
return `${ x }% ${ y }%`;
|
|
|
|
}
|
|
|
|
|
2022-05-30 14:38:52 +00:00
|
|
|
/**
|
|
|
|
* Given x and y coordinates between 0 and 1 returns a CSS `objectPosition`.
|
|
|
|
*/
|
|
|
|
export function calculateBackgroundImagePosition( coords: Coordinates ) {
|
|
|
|
if ( ! coords ) return {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
objectPosition: calculatePercentPositionFromCoordinates( coords ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-19 16:16:46 +00:00
|
|
|
/**
|
|
|
|
* Generate the style object of the background image of the block.
|
|
|
|
*
|
|
|
|
* It outputs styles for either an `img` element or a `div` with a background,
|
|
|
|
* depending on what is needed.
|
|
|
|
*/
|
|
|
|
export function getBackgroundImageStyles( {
|
|
|
|
focalPoint,
|
|
|
|
imageFit,
|
|
|
|
isImgElement,
|
|
|
|
isRepeated,
|
|
|
|
url,
|
2022-05-30 14:38:52 +00:00
|
|
|
}: {
|
|
|
|
focalPoint: Coordinates;
|
|
|
|
imageFit: ImageFit;
|
|
|
|
isImgElement: boolean;
|
|
|
|
isRepeated: boolean;
|
|
|
|
url: string;
|
2022-05-19 16:16:46 +00:00
|
|
|
} ) {
|
|
|
|
let styles = {};
|
|
|
|
|
|
|
|
if ( isImgElement ) {
|
|
|
|
styles = {
|
|
|
|
...styles,
|
|
|
|
...calculateBackgroundImagePosition( focalPoint ),
|
|
|
|
objectFit: imageFit,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
styles = {
|
|
|
|
...styles,
|
|
|
|
...( url && {
|
|
|
|
backgroundImage: `url(${ url })`,
|
|
|
|
} ),
|
|
|
|
backgroundPosition: calculatePercentPositionFromCoordinates(
|
|
|
|
focalPoint
|
|
|
|
),
|
|
|
|
...( ! isRepeated && {
|
|
|
|
backgroundRepeat: 'no-repeat',
|
|
|
|
backgroundSize: imageFit === 'cover' ? imageFit : 'auto',
|
|
|
|
} ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the CSS class prefix for scoping elements to a block.
|
|
|
|
*/
|
2022-05-30 14:38:52 +00:00
|
|
|
export function getClassPrefixFromName( blockName: string ) {
|
2022-05-19 16:16:46 +00:00
|
|
|
return `wc-block-${ blockName.split( '/' )[ 1 ] }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the selected ratio to the correct background class.
|
|
|
|
*
|
2022-05-30 14:38:52 +00:00
|
|
|
* @param ratio Selected opacity from 0 to 100.
|
|
|
|
* @return The class name, if applicable (not used for ratio 0 or 50).
|
2022-05-19 16:16:46 +00:00
|
|
|
*/
|
2022-05-30 14:38:52 +00:00
|
|
|
export function dimRatioToClass( ratio: number ) {
|
2022-05-19 16:16:46 +00:00
|
|
|
return ratio === 0 || ratio === 50
|
|
|
|
? null
|
|
|
|
: `has-background-dim-${ 10 * Math.round( ratio / 10 ) }`;
|
|
|
|
}
|