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

Extract the Global Styles Typography Panel into block-editor package #47356

Merged
merged 17 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const FONT_WEIGHTS = [
* @param {boolean} hasFontWeights Whether font weights are enabled and present.
* @return {string} A label representing what font appearance is being edited.
*/
export const getFontAppearanceLabel = ( hasFontStyles, hasFontWeights ) => {
const getFontAppearanceLabel = ( hasFontStyles, hasFontWeights ) => {
if ( ! hasFontStyles ) {
return __( 'Font weight' );
}
Expand Down
207 changes: 141 additions & 66 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,67 @@ import { get, set } from 'lodash';
/**
* WordPress dependencies
*/
import { useContext, useCallback } from '@wordpress/element';
import { useContext, useCallback, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { getValueFromVariable, getPresetVariableFromValue } from './utils';
import { GlobalStylesContext } from './context';
import { unlock } from '../../lock-unlock';

const EMPTY_CONFIG = { settings: {}, styles: {} };

const VALID_SETTINGS = [
'appearanceTools',
'useRootPaddingAwareAlignments',
'border.color',
'border.radius',
'border.style',
'border.width',
'shadow.presets',
'shadow.defaultPresets',
'color.background',
'color.custom',
'color.customDuotone',
'color.customGradient',
'color.defaultDuotone',
'color.defaultGradients',
'color.defaultPalette',
'color.duotone',
'color.gradients',
'color.link',
'color.palette',
'color.text',
'custom',
'dimensions.minHeight',
'layout.contentSize',
'layout.definitions',
'layout.wideSize',
'position.fixed',
'position.sticky',
'spacing.customSpacingSize',
'spacing.spacingSizes',
'spacing.spacingScale',
'spacing.blockGap',
'spacing.margin',
'spacing.padding',
'spacing.units',
'typography.fuild',
'typography.customFontSize',
'typography.dropCap',
'typography.fontFamilies',
'typography.fontSizes',
'typography.fontStyle',
'typography.fontWeight',
'typography.letterSpacing',
'typography.lineHeight',
'typography.textDecoration',
'typography.textTransform',
];

export const useGlobalStylesReset = () => {
const { user: config, setUserConfig } = useContext( GlobalStylesContext );
const canReset = !! config && ! fastDeepEqual( config, EMPTY_CONFIG );
Expand All @@ -29,68 +80,78 @@ export const useGlobalStylesReset = () => {
];
};

export function useGlobalSetting( path, blockName, source = 'all' ) {
const {
merged: mergedConfig,
base: baseConfig,
user: userConfig,
setUserConfig,
} = useContext( GlobalStylesContext );
export function useGlobalSetting( propertyPath, blockName, source = 'all' ) {
const { setUserConfig, ...configs } = useContext( GlobalStylesContext );

const fullPath = ! blockName
? `settings.${ path }`
: `settings.blocks.${ blockName }.${ path }`;
const appendedBlockPath = blockName ? '.blocks.' + blockName : '';
const appendedPropertyPath = propertyPath ? '.' + propertyPath : '';
const contextualPath = `settings${ appendedBlockPath }${ appendedPropertyPath }`;
const globalPath = `settings${ appendedPropertyPath }`;
const sourceKey = source === 'all' ? 'merged' : source;

const settingValue = useMemo( () => {
const configToUse = configs[ sourceKey ];
if ( ! configToUse ) {
throw 'Unsupported source';
}

if ( propertyPath ) {
return (
get( configToUse, contextualPath ) ??
get( configToUse, globalPath )
);
}

const result = {};
VALID_SETTINGS.forEach( ( setting ) => {
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
const value =
get(
configToUse,
`settings${ appendedBlockPath }.${ setting }`
) ?? get( configToUse, `settings.${ setting }` );
if ( value ) {
set( result, setting, value );
}
} );
return result;
}, [
configs,
sourceKey,
propertyPath,
contextualPath,
globalPath,
appendedBlockPath,
] );

const setSetting = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
set( newUserConfig, fullPath, newValue );
set( newUserConfig, contextualPath, newValue );

return newUserConfig;
} );
};

const getSettingValueForContext = ( name ) => {
const currentPath = ! name
? `settings.${ path }`
: `settings.blocks.${ name }.${ path }`;

let result;
switch ( source ) {
case 'all':
result = get( mergedConfig, currentPath );
break;
case 'user':
result = get( userConfig, currentPath );
break;
case 'base':
result = get( baseConfig, currentPath );
break;
default:
throw 'Unsupported source';
}

return result;
};

// Unlike styles settings get inherited from top level settings.
const resultWithFallback =
getSettingValueForContext( blockName ) ?? getSettingValueForContext();

return [ resultWithFallback, setSetting ];
return [ settingValue, setSetting ];
}

export function useGlobalStyle( path, blockName, source = 'all' ) {
export function useGlobalStyle(
path,
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
blockName,
source = 'all',
{ shouldDecodeEncode = true } = {}
) {
const {
merged: mergedConfig,
base: baseConfig,
user: userConfig,
setUserConfig,
} = useContext( GlobalStylesContext );
const appendedPath = path ? '.' + path : '';
const finalPath = ! blockName
? `styles.${ path }`
: `styles.blocks.${ blockName }.${ path }`;
? `styles${ appendedPath }`
: `styles.blocks.${ blockName }${ appendedPath }`;
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

const setStyle = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
Expand All @@ -99,47 +160,61 @@ export function useGlobalStyle( path, blockName, source = 'all' ) {
set(
newUserConfig,
finalPath,
getPresetVariableFromValue(
mergedConfig.settings,
blockName,
path,
newValue
)
shouldDecodeEncode
? getPresetVariableFromValue(
mergedConfig.settings,
blockName,
path,
newValue
)
: newValue
);
return newUserConfig;
} );
};

let result;
let rawResult, result;
switch ( source ) {
case 'all':
result = getValueFromVariable(
mergedConfig,
blockName,
rawResult =
// The stlyes.css path is allowed to be empty, so don't revert to base if undefined.
finalPath === 'styles.css'
? get( userConfig, finalPath )
: get( userConfig, finalPath ) ??
get( baseConfig, finalPath )
);
: get( mergedConfig, finalPath );
result = shouldDecodeEncode
? getValueFromVariable( mergedConfig, blockName, rawResult )
: rawResult;
break;
case 'user':
result = getValueFromVariable(
mergedConfig,
blockName,
get( userConfig, finalPath )
);
rawResult = get( userConfig, finalPath );
result = shouldDecodeEncode
? getValueFromVariable( mergedConfig, blockName, rawResult )
: rawResult;
break;
case 'base':
result = getValueFromVariable(
baseConfig,
blockName,
get( baseConfig, finalPath )
);
rawResult = get( baseConfig, finalPath );
result = shouldDecodeEncode
? getValueFromVariable( baseConfig, blockName, rawResult )
: rawResult;
break;
default:
throw 'Unsupported source';
}

return [ result, setStyle ];
}

export function useSupportedStyles( name, element ) {
const { supportedPanels } = useSelect(
( select ) => {
return {
supportedPanels: unlock(
select( blocksStore )
).getSupportedStyles( name, element ),
};
},
[ name, element ]
);

return supportedPanels;
}
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/global-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export {
} from './hooks';
export { useGlobalStylesOutput } from './use-global-styles-output';
export { GlobalStylesContext } from './context';
export {
default as TypographyPanel,
useHasTypographyPanel,
} from './typography-panel';
Loading