Skip to content

Commit

Permalink
Merge branch 'trunk' into font-library/improve-font-variant-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
mikachan authored Nov 30, 2023
2 parents 0cca9f2 + 482a507 commit ebd9b91
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 239 deletions.
4 changes: 0 additions & 4 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,6 @@ Returns state object prior to a specified optimist transaction ID, or `null` if

Returns a suggested post format for the current post, inferred only if there is a single block within the post and it is of a type known to match a default post format. Returns null if the format cannot be determined.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_

- `?string`: Suggested post format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { mergeOrigins, hasMergedOrigins } from '../use-settings';
import FontFamilyControl from '../font-family';
import FontAppearanceControl from '../font-appearance-control';
import LineHeightControl from '../line-height-control';
Expand Down Expand Up @@ -51,44 +52,29 @@ export function useHasTypographyPanel( settings ) {
}

function useHasFontSizeControl( settings ) {
const disableCustomFontSizes = ! settings?.typography?.customFontSize;
const fontSizesPerOrigin = settings?.typography?.fontSizes ?? {};
const fontSizes = []
.concat( fontSizesPerOrigin?.custom ?? [] )
.concat( fontSizesPerOrigin?.theme ?? [] )
.concat( fontSizesPerOrigin.default ?? [] );
return !! fontSizes?.length || ! disableCustomFontSizes;
return (
hasMergedOrigins( settings?.typography?.fontSizes ) ||
settings?.typography?.customFontSize
);
}

function useHasFontFamilyControl( settings ) {
const fontFamiliesPerOrigin = settings?.typography?.fontFamilies;
const fontFamilies = []
.concat( fontFamiliesPerOrigin?.custom ?? [] )
.concat( fontFamiliesPerOrigin?.theme ?? [] )
.concat( fontFamiliesPerOrigin?.default ?? [] )
.sort( ( a, b ) =>
( a?.name || a?.slug )?.localeCompare( b?.name || a?.slug )
);
return !! fontFamilies?.length;
return hasMergedOrigins( settings?.typography?.fontFamilies );
}

function useHasLineHeightControl( settings ) {
return settings?.typography?.lineHeight;
}

function useHasAppearanceControl( settings ) {
const hasFontStyles = settings?.typography?.fontStyle;
const hasFontWeights = settings?.typography?.fontWeight;
return hasFontStyles || hasFontWeights;
return settings?.typography?.fontStyle || settings?.typography?.fontWeight;
}

function useAppearanceControlLabel( settings ) {
const hasFontStyles = settings?.typography?.fontStyle;
const hasFontWeights = settings?.typography?.fontWeight;
if ( ! hasFontStyles ) {
if ( ! settings?.typography?.fontStyle ) {
return __( 'Font weight' );
}
if ( ! hasFontWeights ) {
if ( ! settings?.typography?.fontWeight ) {
return __( 'Font style' );
}
return __( 'Appearance' );
Expand All @@ -115,18 +101,15 @@ function useHasTextColumnsControl( settings ) {
}

function getUniqueFontSizesBySlug( settings ) {
const fontSizesPerOrigin = settings?.typography?.fontSizes ?? {};
const fontSizes = []
.concat( fontSizesPerOrigin?.custom ?? [] )
.concat( fontSizesPerOrigin?.theme ?? [] )
.concat( fontSizesPerOrigin.default ?? [] );

return fontSizes.reduce( ( acc, currentSize ) => {
if ( ! acc.some( ( { slug } ) => slug === currentSize.slug ) ) {
acc.push( currentSize );
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 acc;
}, [] );
}
return uniqueSizes;
}

function TypographyToolsPanel( {
Expand Down Expand Up @@ -178,14 +161,11 @@ export default function TypographyPanel( {

// Font Family
const hasFontFamilyEnabled = useHasFontFamilyControl( settings );
const fontFamiliesPerOrigin = settings?.typography?.fontFamilies;
const fontFamilies = []
.concat( fontFamiliesPerOrigin?.custom ?? [] )
.concat( fontFamiliesPerOrigin?.theme ?? [] )
.concat( fontFamiliesPerOrigin?.default ?? [] );
const fontFamilies = settings?.typography?.fontFamilies;
const mergedFontFamilies = fontFamilies ? mergeOrigins( fontFamilies ) : [];
const fontFamily = decodeValue( inheritedValue?.typography?.fontFamily );
const setFontFamily = ( newValue ) => {
const slug = fontFamilies?.find(
const slug = mergedFontFamilies?.find(
( { fontFamily: f } ) => f === newValue
)?.slug;
onChange(
Expand All @@ -204,7 +184,7 @@ export default function TypographyPanel( {
// Font Size
const hasFontSizeEnabled = useHasFontSizeControl( settings );
const disableCustomFontSizes = ! settings?.typography?.customFontSize;
const fontSizes = getUniqueFontSizesBySlug( settings );
const mergedFontSizes = getUniqueFontSizesBySlug( settings );

const fontSize = decodeValue( inheritedValue?.typography?.fontSize );
const setFontSize = ( newValue, metadata ) => {
Expand Down Expand Up @@ -368,7 +348,7 @@ export default function TypographyPanel( {
panelId={ panelId }
>
<FontFamilyControl
fontFamilies={ fontFamilies }
fontFamilies={ mergedFontFamilies }
value={ fontFamily }
onChange={ setFontFamily }
size="__unstable-large"
Expand All @@ -387,7 +367,7 @@ export default function TypographyPanel( {
<FontSizePicker
value={ fontSize }
onChange={ setFontSize }
fontSizes={ fontSizes }
fontSizes={ mergedFontSizes }
disableCustomFontSizes={ disableCustomFontSizes }
withReset={ false }
withSlider
Expand Down
16 changes: 15 additions & 1 deletion packages/block-editor/src/components/use-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const removeCustomPrefixes = ( path ) => {
* @param {Object} value Object to merge
* @return {Array} Array of merged items
*/
function mergeOrigins( value ) {
export function mergeOrigins( value ) {
let result = mergeCache.get( value );
if ( ! result ) {
result = [ 'default', 'theme', 'custom' ].flatMap(
Expand All @@ -115,6 +115,20 @@ function mergeOrigins( value ) {
}
const mergeCache = new WeakMap();

/**
* For settings like `color.palette`, which have a value that is an object
* with `default`, `theme`, `custom`, with field values that are arrays of
* items, see if any of the three origins have values.
*
* @param {Object} value Object to check
* @return {boolean} Whether the object has values in any of the three origins
*/
export function hasMergedOrigins( value ) {
return [ 'default', 'theme', 'custom' ].some(
( key ) => value?.[ key ]?.length
);
}

/**
* Hook that retrieves the given settings for the block instance in use.
*
Expand Down
3 changes: 2 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- `CheckboxControl`: Add option to not render label ([#56158](https://github.com/WordPress/gutenberg/pull/56158)).
- `QueryControls`: Add opt-in prop for 40px default size ([#56576](https://github.com/WordPress/gutenberg/pull/56576)).

## 25.13.0 (2023-11-29)

Expand All @@ -27,7 +28,7 @@

### Documentation

- `Text` and `Heading`: improve docs around default values and truncation logic ([#56518](https://github.com/WordPress/gutenberg/pull/56518))
- `Text` and `Heading`: improve docs around default values and truncation logic ([#56518](https://github.com/WordPress/gutenberg/pull/56518))

### Internal

Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/query-controls/author-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TreeSelect from '../tree-select';
import type { AuthorSelectProps } from './types';

export default function AuthorSelect( {
__next40pxDefaultSize,
label,
noOptionLabel,
authorList,
Expand All @@ -28,6 +29,7 @@ export default function AuthorSelect( {
: undefined
}
__nextHasNoMarginBottom
__next40pxDefaultSize={ __next40pxDefaultSize }
/>
);
}
2 changes: 2 additions & 0 deletions packages/components/src/query-controls/category-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useMemo } from '@wordpress/element';
import type { CategorySelectProps } from './types';

export default function CategorySelect( {
__next40pxDefaultSize,
label,
noOptionLabel,
categoriesList,
Expand All @@ -37,6 +38,7 @@ export default function CategorySelect( {
}
{ ...props }
__nextHasNoMarginBottom
__next40pxDefaultSize={ __next40pxDefaultSize }
/>
);
}
7 changes: 6 additions & 1 deletion packages/components/src/query-controls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function isMultipleCategorySelection(
* ```
*/
export function QueryControls( {
__next40pxDefaultSize = false,
authorList,
selectedAuthorId,
numberOfItems,
Expand All @@ -81,6 +82,7 @@ export function QueryControls( {
onOrderChange && onOrderByChange && (
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize={ __next40pxDefaultSize }
key="query-controls-order-select"
label={ __( 'Order by' ) }
value={ `${ orderBy }/${ order }` }
Expand Down Expand Up @@ -131,6 +133,7 @@ export function QueryControls( {
props.categoriesList &&
props.onCategoryChange && (
<CategorySelect
__next40pxDefaultSize={ __next40pxDefaultSize }
key="query-controls-category-select"
categoriesList={ props.categoriesList }
label={ __( 'Category' ) }
Expand All @@ -143,6 +146,7 @@ export function QueryControls( {
props.categorySuggestions &&
props.onCategoryChange && (
<FormTokenField
__next40pxDefaultSize={ __next40pxDefaultSize }
__nextHasNoMarginBottom
key="query-controls-categories-select"
label={ __( 'Categories' ) }
Expand All @@ -166,6 +170,7 @@ export function QueryControls( {
),
onAuthorChange && (
<AuthorSelect
__next40pxDefaultSize={ __next40pxDefaultSize }
key="query-controls-author-select"
authorList={ authorList }
label={ __( 'Author' ) }
Expand All @@ -177,7 +182,7 @@ export function QueryControls( {
onNumberOfItemsChange && (
<RangeControl
__nextHasNoMarginBottom
__next40pxDefaultSize
__next40pxDefaultSize={ __next40pxDefaultSize }
key="query-controls-range-control"
label={ __( 'Number of items' ) }
value={ numberOfItems }
Expand Down
9 changes: 9 additions & 0 deletions packages/components/src/query-controls/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type CategorySelectProps = Pick<
categoriesList: Category[];
onChange: ( newCategory: string ) => void;
selectedCategoryId?: Category[ 'id' ];
__next40pxDefaultSize: boolean;
};

export type AuthorSelectProps = Pick<
Expand All @@ -40,6 +41,7 @@ export type AuthorSelectProps = Pick<
authorList?: Author[];
onChange: ( newAuthor: string ) => void;
selectedAuthorId?: Author[ 'id' ];
__next40pxDefaultSize: boolean;
};

type Order = 'asc' | 'desc';
Expand Down Expand Up @@ -101,6 +103,13 @@ type BaseQueryControlsProps = {
* The selected author ID.
*/
selectedAuthorId?: AuthorSelectProps[ 'selectedAuthorId' ];
/**
* Start opting into the larger default height that will become the
* default size in a future version.
*
* @default false
*/
__next40pxDefaultSize?: boolean;
};

export type QueryControlsWithSingleCategorySelectionProps =
Expand Down
82 changes: 41 additions & 41 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,54 +819,54 @@ export function getEditedPostPreviewLink( state ) {
* is a single block within the post and it is of a type known to match a
* default post format. Returns null if the format cannot be determined.
*
* @param {Object} state Global application state.
*
* @return {?string} Suggested post format.
*/
export function getSuggestedPostFormat( state ) {
const blocks = getEditorBlocks( state );

if ( blocks.length > 2 ) return null;

let name;
// If there is only one block in the content of the post grab its name
// so we can derive a suitable post format from it.
if ( blocks.length === 1 ) {
name = blocks[ 0 ].name;
// Check for core/embed `video` and `audio` eligible suggestions.
if ( name === 'core/embed' ) {
const provider = blocks[ 0 ].attributes?.providerNameSlug;
if ( [ 'youtube', 'vimeo' ].includes( provider ) ) {
name = 'core/video';
} else if ( [ 'spotify', 'soundcloud' ].includes( provider ) ) {
name = 'core/audio';
export const getSuggestedPostFormat = createRegistrySelector(
( select ) => () => {
const blocks = select( blockEditorStore ).getBlocks();

if ( blocks.length > 2 ) return null;

let name;
// If there is only one block in the content of the post grab its name
// so we can derive a suitable post format from it.
if ( blocks.length === 1 ) {
name = blocks[ 0 ].name;
// Check for core/embed `video` and `audio` eligible suggestions.
if ( name === 'core/embed' ) {
const provider = blocks[ 0 ].attributes?.providerNameSlug;
if ( [ 'youtube', 'vimeo' ].includes( provider ) ) {
name = 'core/video';
} else if ( [ 'spotify', 'soundcloud' ].includes( provider ) ) {
name = 'core/audio';
}
}
}
}

// If there are two blocks in the content and the last one is a text blocks
// grab the name of the first one to also suggest a post format from it.
if ( blocks.length === 2 && blocks[ 1 ].name === 'core/paragraph' ) {
name = blocks[ 0 ].name;
}
// If there are two blocks in the content and the last one is a text blocks
// grab the name of the first one to also suggest a post format from it.
if ( blocks.length === 2 && blocks[ 1 ].name === 'core/paragraph' ) {
name = blocks[ 0 ].name;
}

// We only convert to default post formats in core.
switch ( name ) {
case 'core/image':
return 'image';
case 'core/quote':
case 'core/pullquote':
return 'quote';
case 'core/gallery':
return 'gallery';
case 'core/video':
return 'video';
case 'core/audio':
return 'audio';
default:
return null;
// We only convert to default post formats in core.
switch ( name ) {
case 'core/image':
return 'image';
case 'core/quote':
case 'core/pullquote':
return 'quote';
case 'core/gallery':
return 'gallery';
case 'core/video':
return 'video';
case 'core/audio':
return 'audio';
default:
return null;
}
}
}
);

/**
* Returns the content of the post being edited.
Expand Down
Loading

0 comments on commit ebd9b91

Please sign in to comment.