Skip to content

Commit 3cd9a76

Browse files
committed
Create types for utility functions and constants
1 parent 648394e commit 3cd9a76

File tree

5 files changed

+107
-100
lines changed

5 files changed

+107
-100
lines changed

packages/edit-site/src/components/style-book/categories.js

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type {
5+
BlockExample,
6+
StyleBookCategory,
7+
CategoryExamples,
8+
} from './types';
9+
10+
/**
11+
* Returns category examples for a given category definition and list of examples.
12+
* @param {StyleBookCategory} categoryDefinition The category definition.
13+
* @param {BlockExample[]} examples An array of block examples.
14+
* @return {CategoryExamples|undefined} An object containing the category examples.
15+
*/
16+
export function getCategoryExamples(
17+
categoryDefinition: StyleBookCategory,
18+
examples: BlockExample[]
19+
): CategoryExamples | undefined {
20+
if ( ! categoryDefinition?.slug || ! examples?.length ) {
21+
return;
22+
}
23+
24+
if ( categoryDefinition?.subcategories?.length ) {
25+
return categoryDefinition.subcategories.reduce(
26+
( acc, subcategoryDefinition ) => {
27+
const subcategoryExamples = getCategoryExamples(
28+
subcategoryDefinition,
29+
examples
30+
);
31+
if ( subcategoryExamples ) {
32+
acc.subcategories = [
33+
...acc.subcategories,
34+
subcategoryExamples,
35+
];
36+
}
37+
return acc;
38+
},
39+
{
40+
title: categoryDefinition.title,
41+
slug: categoryDefinition.slug,
42+
subcategories: [],
43+
}
44+
);
45+
}
46+
47+
const blocksToInclude = categoryDefinition?.blocks || [];
48+
const blocksToExclude = categoryDefinition?.exclude || [];
49+
const categoryExamples = examples.filter( ( example ) => {
50+
return (
51+
! blocksToExclude.includes( example.name ) &&
52+
( example.category === categoryDefinition.slug ||
53+
blocksToInclude.includes( example.name ) )
54+
);
55+
} );
56+
57+
if ( ! categoryExamples.length ) {
58+
return;
59+
}
60+
61+
return {
62+
title: categoryDefinition.title,
63+
slug: categoryDefinition.slug,
64+
examples: categoryExamples,
65+
};
66+
}

packages/edit-site/src/components/style-book/constants.js renamed to packages/edit-site/src/components/style-book/constants.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
*/
44
import { __ } from '@wordpress/i18n';
55

6-
export const STYLE_BOOK_THEME_SUBCATEGORIES = [
6+
/**
7+
* Internal dependencies
8+
*/
9+
import type { StyleBookCategory } from './types';
10+
11+
export const STYLE_BOOK_THEME_SUBCATEGORIES: Omit<
12+
StyleBookCategory,
13+
'subcategories'
14+
>[] = [
715
{
816
slug: 'site-identity',
917
title: __( 'Site Identity' ),
@@ -53,7 +61,7 @@ export const STYLE_BOOK_THEME_SUBCATEGORIES = [
5361
},
5462
];
5563

56-
export const STYLE_BOOK_CATEGORIES = [
64+
export const STYLE_BOOK_CATEGORIES: StyleBookCategory[] = [
5765
{
5866
slug: 'text',
5967
title: __( 'Text' ),

packages/edit-site/src/components/style-book/examples.js renamed to packages/edit-site/src/components/style-book/examples.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,16 @@ import {
1010
} from '@wordpress/blocks';
1111

1212
/**
13-
* blockExamples object.
14-
*
15-
* @typedef {Object} blockExamples
16-
*
17-
* @property {string} name Block name, e.g., "core/paragraph".
18-
* @property {string} title Block title/label.
19-
* @property {string} category Block category.
20-
* @property {Object} blocks Block object.
13+
* Internal dependencies
2114
*/
15+
import type { BlockExample } from './types';
2216

2317
/**
2418
* Returns a list of examples for registered block types.
2519
*
26-
* @return {Array<blockExamples>} An array of block examples.
20+
* @return {BlockExample[]} An array of block examples.
2721
*/
28-
export function getExamples() {
22+
export function getExamples(): BlockExample[] {
2923
const nonHeadingBlockExamples = getBlockTypes()
3024
.filter( ( blockType ) => {
3125
const { name, example, supports } = blockType;
@@ -41,7 +35,6 @@ export function getExamples() {
4135
category: blockType.category,
4236
blocks: getBlockFromExample( blockType.name, blockType.example ),
4337
} ) );
44-
4538
const isHeadingBlockRegistered = !! getBlockType( 'core/heading' );
4639

4740
if ( ! isHeadingBlockRegistered ) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
type Block = {
2+
name: string;
3+
attributes: Record< string, unknown >;
4+
innerBlocks: Block[];
5+
};
6+
7+
export type StyleBookCategory = {
8+
title: string;
9+
slug: string;
10+
blocks?: string[];
11+
exclude?: string[];
12+
subcategories?: StyleBookCategory[];
13+
};
14+
15+
export type BlockExample = {
16+
name: string;
17+
title: string;
18+
category: string;
19+
blocks: Block;
20+
};
21+
22+
export type CategoryExamples = {
23+
title: string;
24+
slug: string;
25+
examples?: BlockExample[];
26+
subcategories?: CategoryExamples[];
27+
};

0 commit comments

Comments
 (0)