Skip to content

Commit

Permalink
Create types for utility functions and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 27, 2024
1 parent 648394e commit 3cd9a76
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 100 deletions.
87 changes: 0 additions & 87 deletions packages/edit-site/src/components/style-book/categories.js

This file was deleted.

66 changes: 66 additions & 0 deletions packages/edit-site/src/components/style-book/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Internal dependencies
*/
import type {
BlockExample,
StyleBookCategory,
CategoryExamples,
} from './types';

/**
* Returns category examples for a given category definition and list of examples.
* @param {StyleBookCategory} categoryDefinition The category definition.
* @param {BlockExample[]} examples An array of block examples.
* @return {CategoryExamples|undefined} An object containing the category examples.
*/
export function getCategoryExamples(
categoryDefinition: StyleBookCategory,
examples: BlockExample[]
): CategoryExamples | undefined {
if ( ! categoryDefinition?.slug || ! examples?.length ) {
return;
}

if ( categoryDefinition?.subcategories?.length ) {
return categoryDefinition.subcategories.reduce(
( acc, subcategoryDefinition ) => {
const subcategoryExamples = getCategoryExamples(
subcategoryDefinition,
examples
);
if ( subcategoryExamples ) {
acc.subcategories = [
...acc.subcategories,
subcategoryExamples,
];
}
return acc;
},
{
title: categoryDefinition.title,
slug: categoryDefinition.slug,
subcategories: [],
}
);
}

const blocksToInclude = categoryDefinition?.blocks || [];
const blocksToExclude = categoryDefinition?.exclude || [];
const categoryExamples = examples.filter( ( example ) => {
return (
! blocksToExclude.includes( example.name ) &&
( example.category === categoryDefinition.slug ||
blocksToInclude.includes( example.name ) )
);
} );

if ( ! categoryExamples.length ) {
return;
}

return {
title: categoryDefinition.title,
slug: categoryDefinition.slug,
examples: categoryExamples,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
*/
import { __ } from '@wordpress/i18n';

export const STYLE_BOOK_THEME_SUBCATEGORIES = [
/**
* Internal dependencies
*/
import type { StyleBookCategory } from './types';

export const STYLE_BOOK_THEME_SUBCATEGORIES: Omit<
StyleBookCategory,
'subcategories'
>[] = [
{
slug: 'site-identity',
title: __( 'Site Identity' ),
Expand Down Expand Up @@ -53,7 +61,7 @@ export const STYLE_BOOK_THEME_SUBCATEGORIES = [
},
];

export const STYLE_BOOK_CATEGORIES = [
export const STYLE_BOOK_CATEGORIES: StyleBookCategory[] = [
{
slug: 'text',
title: __( 'Text' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ import {
} from '@wordpress/blocks';

/**
* blockExamples object.
*
* @typedef {Object} blockExamples
*
* @property {string} name Block name, e.g., "core/paragraph".
* @property {string} title Block title/label.
* @property {string} category Block category.
* @property {Object} blocks Block object.
* Internal dependencies
*/
import type { BlockExample } from './types';

/**
* Returns a list of examples for registered block types.
*
* @return {Array<blockExamples>} An array of block examples.
* @return {BlockExample[]} An array of block examples.
*/
export function getExamples() {
export function getExamples(): BlockExample[] {
const nonHeadingBlockExamples = getBlockTypes()
.filter( ( blockType ) => {
const { name, example, supports } = blockType;
Expand All @@ -41,7 +35,6 @@ export function getExamples() {
category: blockType.category,
blocks: getBlockFromExample( blockType.name, blockType.example ),
} ) );

const isHeadingBlockRegistered = !! getBlockType( 'core/heading' );

if ( ! isHeadingBlockRegistered ) {
Expand Down
27 changes: 27 additions & 0 deletions packages/edit-site/src/components/style-book/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type Block = {
name: string;
attributes: Record< string, unknown >;
innerBlocks: Block[];
};

export type StyleBookCategory = {
title: string;
slug: string;
blocks?: string[];
exclude?: string[];
subcategories?: StyleBookCategory[];
};

export type BlockExample = {
name: string;
title: string;
category: string;
blocks: Block;
};

export type CategoryExamples = {
title: string;
slug: string;
examples?: BlockExample[];
subcategories?: CategoryExamples[];
};

0 comments on commit 3cd9a76

Please sign in to comment.