This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'add/4965-product-title' of github.com:woocommerce/wooco…
…mmerce-gutenberg-products-block into add/4965-category-list
- Loading branch information
Showing
3 changed files
with
152 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* eslint-disable @wordpress/no-unsafe-wp-apis */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __experimentalUseColorProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isFeaturePluginBuild } from '../settings/blocks/feature-flags'; | ||
import { isString, isObject } from '../types/type-guards'; | ||
|
||
type WithClass = { | ||
className: string; | ||
}; | ||
|
||
type WithStyle = { | ||
style: Record< string, unknown >; | ||
}; | ||
|
||
const parseStyle = ( style: unknown ): Record< string, unknown > => { | ||
if ( isString( style ) ) { | ||
return JSON.parse( style ) || {}; | ||
} | ||
|
||
if ( isObject( style ) ) { | ||
return style; | ||
} | ||
|
||
return {}; | ||
}; | ||
|
||
const parseSpacingStyle = ( | ||
spacing: Record< string, unknown > | ||
): Record< string, unknown > => { | ||
const keys = [ 'margin' ]; | ||
|
||
const getValueOrDefault = ( value: unknown ) => { | ||
return isString( value ) && value.length > 0 ? value : '0'; | ||
}; | ||
|
||
return Object.keys( spacing ).reduce( ( acc, key ) => { | ||
const spacingProperty = isObject( spacing[ key ] ) | ||
? ( spacing[ key ] as Record< string, unknown > ) | ||
: {}; | ||
|
||
if ( keys.includes( key ) ) { | ||
return { | ||
...acc, | ||
[ key ]: `${ getValueOrDefault( | ||
spacingProperty.top | ||
) } ${ getValueOrDefault( | ||
spacingProperty.right | ||
) } ${ getValueOrDefault( | ||
spacingProperty.bottom | ||
) } ${ getValueOrDefault( spacingProperty.left ) }`, | ||
}; | ||
} | ||
|
||
return acc; | ||
}, {} ); | ||
}; | ||
|
||
export const useSpacingProps = ( attributes: unknown ): WithStyle => { | ||
const style = isObject( attributes ) ? parseStyle( attributes.style ) : {}; | ||
const spacingStyles = isObject( style.spacing ) ? style.spacing : {}; | ||
|
||
return { | ||
style: parseSpacingStyle( spacingStyles ), | ||
}; | ||
}; | ||
|
||
export const useTypographyProps = ( attributes: unknown ): WithStyle => { | ||
const attributesObject = isObject( attributes ) ? attributes : {}; | ||
const style = parseStyle( attributesObject.style ); | ||
const typography = isObject( style.typography ) | ||
? ( style.typography as Record< string, unknown > ) | ||
: {}; | ||
|
||
return { | ||
style: { | ||
fontSize: attributesObject.fontSize || typography.fontSize, | ||
lineHeight: typography.lineHeight, | ||
fontWeight: typography.fontWeight, | ||
textTransform: typography.textTransform, | ||
fontFamily: attributesObject.fontFamily, | ||
}, | ||
}; | ||
}; | ||
|
||
export const useColorProps = ( attributes: unknown ): WithStyle & WithClass => { | ||
if ( ! isFeaturePluginBuild() ) { | ||
return { | ||
className: '', | ||
style: {}, | ||
}; | ||
} | ||
|
||
const attributesObject = isObject( attributes ) ? attributes : {}; | ||
const style = parseStyle( attributesObject.style ); | ||
|
||
return __experimentalUseColorProps( { ...attributesObject, style } ); | ||
}; |