-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create types for utility functions and constants
- Loading branch information
Showing
5 changed files
with
107 additions
and
100 deletions.
There are no files selected for viewing
87 changes: 0 additions & 87 deletions
87
packages/edit-site/src/components/style-book/categories.js
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
packages/edit-site/src/components/style-book/categories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
}; |