Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Support: Separate opt in for font style and weight options #26844

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace regex use to extract CSS variable value
  • Loading branch information
aaronrobertshaw committed Nov 23, 2020
commit c8637c490b4803a83d1ee163fa155965842e6acb
23 changes: 10 additions & 13 deletions packages/block-editor/src/hooks/font-appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ export function FontAppearanceEdit( props ) {
};

const fontStyle = getFontAppearanceValueFromStyle(
'font-style',
fontStyles,
style?.typography?.fontStyle
);

const fontWeight = getFontAppearanceValueFromStyle(
'font-weight',
fontWeights,
style?.typography?.fontWeight
);
Expand Down Expand Up @@ -137,22 +135,21 @@ export function useIsFontAppearanceDisabled( props ) {
}

/**
* Extracts the current selection, if available, from the CSS variable set as
* the corresponding style attribute property e.g. `style.typography.fontStyle`
* Extracts the current selection, if available, from the CSS variable set
* within a style attribute property e.g. `style.typography.fontStyle`
* or `style.typography.fontWeight`.
*
* @param {string} property Which CSS property to parse.
* @param {Array} presets Available preset options.
* @param {string} style Style attribute value for desired CSS property.
* @param {string} style Style attribute value to parse
* @return {string} Actual CSS property value.
*/
const getFontAppearanceValueFromStyle = ( property, presets, style ) => {
const regex = new RegExp( `var:preset\\|${ property }\\|(.+)` );
const parsed = regex.exec( style );

if ( parsed && parsed[ 1 ] ) {
return presets.find( ( { slug } ) => slug === parsed[ 1 ] )?.slug;
const getFontAppearanceValueFromStyle = ( presets, style ) => {
if ( ! style ) {
return undefined;
}

return style;
const parsedValue = style.slice( style.lastIndexOf( '|' ) + 1 );
const preset = presets.find( ( { slug } ) => slug === parsedValue );

return preset?.slug || style;
};