Skip to content

Commit

Permalink
WIP add option for defaultFontSizes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Nov 30, 2023
1 parent 1b66261 commit ab1cb4a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 34 deletions.
29 changes: 15 additions & 14 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class WP_Theme_JSON_Gutenberg {
),
array(
'path' => array( 'typography', 'fontSizes' ),
'prevent_override' => false,
'prevent_override' => array( 'typography', 'defaultFontSizes'),

Check failure on line 158 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space before the array closer in a single line array. Found: no spaces
'use_default_names' => true,
'value_func' => 'gutenberg_get_typography_font_size_value',
'css_vars' => '--wp--preset--font-size--$slug',
Expand Down Expand Up @@ -410,19 +410,20 @@ class WP_Theme_JSON_Gutenberg {
'defaultPresets' => null,
),
'typography' => array(
'fluid' => null,
'customFontSize' => null,
'dropCap' => null,
'fontFamilies' => null,
'fontSizes' => null,
'fontStyle' => null,
'fontWeight' => null,
'letterSpacing' => null,
'lineHeight' => null,
'textColumns' => null,
'textDecoration' => null,
'textTransform' => null,
'writingMode' => null,
'fluid' => null,
'customFontSize' => null,
'defaultFontSizes' => null,
'dropCap' => null,
'fontFamilies' => null,
'fontSizes' => null,
'fontStyle' => null,
'fontWeight' => null,
'letterSpacing' => null,
'lineHeight' => null,
'textColumns' => null,
'textDecoration' => null,
'textTransform' => null,
'writingMode' => null,
),
);

Expand Down
1 change: 1 addition & 0 deletions lib/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
},
"typography": {
"customFontSize": true,
"defaultFontSizes": true,
"dropCap": true,
"fontSizes": [
{
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const VALID_SETTINGS = [
'spacing.units',
'typography.fluid',
'typography.customFontSize',
'typography.defaultFontSizes',
'typography.dropCap',
'typography.fontFamilies',
'typography.fontSizes',
Expand Down Expand Up @@ -235,6 +236,7 @@ export function useSettingsForBlockElement(
...updatedSettings.typography,
fontSizes: {},
customFontSize: false,
defaultFontSizes: false,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -53,7 +53,7 @@ export function useHasTypographyPanel( settings ) {

function useHasFontSizeControl( settings ) {
return (
hasMergedOrigins( settings?.typography?.fontSizes ) ||
settings?.typography?.fontSizes?.length ||
settings?.typography?.customFontSize
);
}
Expand Down Expand Up @@ -100,18 +100,6 @@ function useHasTextColumnsControl( settings ) {
return settings?.typography?.textColumns;
}

function getUniqueFontSizesBySlug( settings ) {
const fontSizes = settings?.typography?.fontSizes;
const mergedFontSizes = fontSizes ? mergeOrigins( fontSizes ) : [];
const uniqueSizes = [];
for ( const currentSize of mergedFontSizes ) {
if ( ! uniqueSizes.some( ( { slug } ) => slug === currentSize.slug ) ) {
uniqueSizes.push( currentSize );
}
}
return uniqueSizes;
}

function TypographyToolsPanel( {
resetAllFilter,
onChange,
Expand Down Expand Up @@ -147,6 +135,46 @@ const DEFAULT_CONTROLS = {
textColumns: true,
};

function useMergedSettings( parentSettings ) {
return useMemo( () => {
const updatedSettings = { ...parentSettings };

// Remove default font sizes if disabled.
if (
updatedSettings?.typography?.defaultFontSizes === false &&
updatedSettings?.typography?.fontSizes?.default
) {
updatedSettings.typography = {
...updatedSettings.typography,
fontSizes: {
custom: updatedSettings.typography.fontSizes.custom,
theme: updatedSettings.typography.fontSizes.theme,
},
};
}

// Merge origins and remove duplicates.
if ( updatedSettings?.typography?.fontSizes ) {
const fontSizes = mergeOrigins(
updatedSettings.typography.fontSizes
);
const uniqueSizes = [];
for ( const currentSize of fontSizes ) {
if (
! uniqueSizes.some(
( { slug } ) => slug === currentSize.slug
)
) {
uniqueSizes.push( currentSize );
}
}
updatedSettings.typography.fontSizes = uniqueSizes;
}

return updatedSettings;
}, [ parentSettings ] );
}

export default function TypographyPanel( {
as: Wrapper = TypographyToolsPanel,
value,
Expand All @@ -156,6 +184,8 @@ export default function TypographyPanel( {
panelId,
defaultControls = DEFAULT_CONTROLS,
} ) {
settings = useMergedSettings( settings );

const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue );

Expand Down Expand Up @@ -184,7 +214,7 @@ export default function TypographyPanel( {
// Font Size
const hasFontSizeEnabled = useHasFontSizeControl( settings );
const disableCustomFontSizes = ! settings?.typography?.customFontSize;
const mergedFontSizes = getUniqueFontSizesBySlug( settings );
const fontSizes = settings?.typography?.fontSizes;

const fontSize = decodeValue( inheritedValue?.typography?.fontSize );
const setFontSize = ( newValue, metadata ) => {
Expand Down Expand Up @@ -367,7 +397,7 @@ export default function TypographyPanel( {
<FontSizePicker
value={ fontSize }
onChange={ setFontSize }
fontSizes={ mergedFontSizes }
fontSizes={ fontSizes }
disableCustomFontSizes={ disableCustomFontSizes }
withReset={ false }
withSlider
Expand Down
20 changes: 16 additions & 4 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ export function useStyleOverride( { id, css, assets, __unstableType } = {} ) {
export function useBlockSettings( name, parentLayout ) {
const [
fontFamilies,
fontSizes,
userFontSizes,
themeFontSizes,
defaultFontSizes,
defaultFontSizesEnabled,
customFontSize,
fontStyle,
fontWeight,
Expand Down Expand Up @@ -203,7 +206,10 @@ export function useBlockSettings( name, parentLayout ) {
isButtonEnabled,
] = useSettings(
'typography.fontFamilies',
'typography.fontSizes',
'typography.fontSizes.custom',
'typography.fontSizes.theme',
'typography.fontSizes.default',
'typography.defaultFontSizes',
'typography.customFontSize',
'typography.fontStyle',
'typography.fontWeight',
Expand Down Expand Up @@ -281,9 +287,12 @@ export function useBlockSettings( name, parentLayout ) {
custom: fontFamilies,
},
fontSizes: {
custom: fontSizes,
custom: userFontSizes,
theme: themeFontSizes,
default: defaultFontSizes,
},
customFontSize,
defaultFontSizes: defaultFontSizesEnabled,
fontStyle,
fontWeight,
lineHeight,
Expand Down Expand Up @@ -316,7 +325,10 @@ export function useBlockSettings( name, parentLayout ) {
};
}, [
fontFamilies,
fontSizes,
userFontSizes,
themeFontSizes,
defaultFontSizes,
defaultFontSizesEnabled,
customFontSize,
fontStyle,
fontWeight,
Expand Down

0 comments on commit ab1cb4a

Please sign in to comment.