diff --git a/packages/edit-site/src/components/style-book/categories.js b/packages/edit-site/src/components/style-book/categories.js deleted file mode 100644 index dc564960a3c0c9..00000000000000 --- a/packages/edit-site/src/components/style-book/categories.js +++ /dev/null @@ -1,87 +0,0 @@ -/** - * categoryDefinition object. - * - * @typedef {Object} StyleBookCategory - * - * @property {string} slug Category identifier. - * @property {string} title Category title/label. - * @property {Array?} blocks Array of block names to include in the category. Used when blocks are not included in the category by default. - * @property {Array?} excludes Array of blocks to exclude from the category. Used when blocks are included in the category by default. - */ - -/** - * 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. - */ - -/** - * getCategoryExamples return value. - * - * @typedef {Object} CategoryExamples - * - * @property {string} slug Category identifier. - * @property {string} title Category title/label. - * @property {Array?} examples Array of block examples. - * @property {Array?} subcategories Array of subcategory examples. - */ - -/** - * Returns category examples for a given category definition and list of examples. - * @param {StyleBookCategory} categoryDefinition The category definition. - * @param {Array} examples An array of block examples. - * @return {CategoryExamples|undefined} An object containing the category examples. - */ -export function getCategoryExamples( categoryDefinition, examples ) { - 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, - }; -} diff --git a/packages/edit-site/src/components/style-book/categories.ts b/packages/edit-site/src/components/style-book/categories.ts new file mode 100644 index 00000000000000..e5ed8fb6d4d255 --- /dev/null +++ b/packages/edit-site/src/components/style-book/categories.ts @@ -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, + }; +} diff --git a/packages/edit-site/src/components/style-book/constants.js b/packages/edit-site/src/components/style-book/constants.ts similarity index 95% rename from packages/edit-site/src/components/style-book/constants.js rename to packages/edit-site/src/components/style-book/constants.ts index 926d062b1e7614..fc06d8f1409f0d 100644 --- a/packages/edit-site/src/components/style-book/constants.js +++ b/packages/edit-site/src/components/style-book/constants.ts @@ -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' ), @@ -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' ), diff --git a/packages/edit-site/src/components/style-book/examples.js b/packages/edit-site/src/components/style-book/examples.ts similarity index 78% rename from packages/edit-site/src/components/style-book/examples.js rename to packages/edit-site/src/components/style-book/examples.ts index 2cd4385dca9fe9..80807b10374c68 100644 --- a/packages/edit-site/src/components/style-book/examples.js +++ b/packages/edit-site/src/components/style-book/examples.ts @@ -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} 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; @@ -41,7 +35,6 @@ export function getExamples() { category: blockType.category, blocks: getBlockFromExample( blockType.name, blockType.example ), } ) ); - const isHeadingBlockRegistered = !! getBlockType( 'core/heading' ); if ( ! isHeadingBlockRegistered ) { diff --git a/packages/edit-site/src/components/style-book/types.ts b/packages/edit-site/src/components/style-book/types.ts new file mode 100644 index 00000000000000..99c18dca06eebc --- /dev/null +++ b/packages/edit-site/src/components/style-book/types.ts @@ -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[]; +};