-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Style book: create static categories #65430
Open
ramonjd
wants to merge
15
commits into
trunk
Choose a base branch
from
try/style-book-refactor-categories-and-blocks
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+533
−158
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cbcdb14
Initial commit - working out category model
ramonjd e308cc7
More stuff
ramonjd 74f0c3a
Working out the category model
ramonjd 0e963d8
Adding test
ramonjd 3918f40
Start integration
ramonjd df49128
Groups
ramonjd 55af6f3
Clean up experiments and unused code for MVP
ramonjd 8ef64f5
Remove comments
ramonjd ee9f6f2
Tinkering around the edges with subcategory reducer
ramonjd 25721a4
I found the custom link! :)
ramonjd 8f9423d
Tweaking subcategory headings. Guard against empty category examples.
ramonjd 159eea7
Add default category
ramonjd 30bed1b
update e2e
ramonjd 7beae63
Split files
ramonjd 8ea8273
Rename cat.name to cat.slug to match categories store object in the b…
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
packages/edit-site/src/components/style-book/categories.js
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,87 @@ | ||
/** | ||
* 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<blockExamples>?} examples Array of block examples. | ||
* @property {Array<CategoryExamples>?} 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<blockExamples>} 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, | ||
}; | ||
} |
183 changes: 183 additions & 0 deletions
183
packages/edit-site/src/components/style-book/constants.js
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,183 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export const STYLE_BOOK_THEME_SUBCATEGORIES = [ | ||
{ | ||
slug: 'site-identity', | ||
title: __( 'Site Identity' ), | ||
blocks: [ 'core/site-logo', 'core/site-title', 'core/site-tagline' ], | ||
}, | ||
{ | ||
slug: 'design', | ||
title: __( 'Design' ), | ||
blocks: [ 'core/navigation', 'core/avatar', 'core/post-time-to-read' ], | ||
exclude: [ 'core/home-link', 'core/navigation-link' ], | ||
}, | ||
{ | ||
slug: 'posts', | ||
title: __( 'Posts' ), | ||
blocks: [ | ||
'core/post-title', | ||
'core/post-excerpt', | ||
'core/post-author', | ||
'core/post-author-name', | ||
'core/post-author-biography', | ||
'core/post-date', | ||
'core/post-terms', | ||
'core/term-description', | ||
'core/query-title', | ||
'core/query-no-results', | ||
'core/query-pagination', | ||
'core/query-numbers', | ||
], | ||
}, | ||
{ | ||
slug: 'comments', | ||
title: __( 'Comments' ), | ||
blocks: [ | ||
'core/comments-title', | ||
'core/comments-pagination', | ||
'core/comments-pagination-numbers', | ||
'core/comments', | ||
'core/comments-author-name', | ||
'core/comment-content', | ||
'core/comment-date', | ||
'core/comment-edit-link', | ||
'core/comment-reply-link', | ||
'core/comment-template', | ||
'core/post-comments-count', | ||
'core/post-comments-link', | ||
], | ||
}, | ||
]; | ||
|
||
export const STYLE_BOOK_CATEGORIES = [ | ||
{ | ||
slug: 'text', | ||
title: __( 'Text' ), | ||
blocks: [ | ||
'core/post-content', | ||
'core/home-link', | ||
'core/navigation-link', | ||
], | ||
}, | ||
{ | ||
slug: 'colors', | ||
title: __( 'Colors' ), | ||
blocks: [ 'custom/colors' ], | ||
}, | ||
{ | ||
slug: 'theme', | ||
title: __( 'Theme' ), | ||
subcategories: STYLE_BOOK_THEME_SUBCATEGORIES, | ||
}, | ||
{ | ||
slug: 'media', | ||
title: __( 'Media' ), | ||
blocks: [ 'core/post-featured-image' ], | ||
}, | ||
{ | ||
slug: 'widgets', | ||
title: __( 'Widgets' ), | ||
blocks: [], | ||
}, | ||
{ | ||
slug: 'embed', | ||
title: __( 'Embeds' ), | ||
include: [], | ||
}, | ||
]; | ||
|
||
// The content area of the Style Book is rendered within an iframe so that global styles | ||
// are applied to elements within the entire content area. To support elements that are | ||
// not part of the block previews, such as headings and layout for the block previews, | ||
// additional CSS rules need to be passed into the iframe. These are hard-coded below. | ||
// Note that button styles are unset, and then focus rules from the `Button` component are | ||
// applied to the `button` element, targeted via `.edit-site-style-book__example`. | ||
// This is to ensure that browser default styles for buttons are not applied to the previews. | ||
export const STYLE_BOOK_IFRAME_STYLES = ` | ||
// Forming a "block formatting context" to prevent margin collapsing. | ||
// @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context | ||
.is-root-container { | ||
display: flow-root; | ||
} | ||
|
||
body { | ||
position: relative; | ||
padding: 32px !important; | ||
} | ||
|
||
.edit-site-style-book__examples { | ||
max-width: 1200px; | ||
margin: 0 auto; | ||
} | ||
|
||
.edit-site-style-book__example { | ||
max-width: 900px; | ||
border-radius: 2px; | ||
cursor: pointer; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 40px; | ||
padding: 16px; | ||
width: 100%; | ||
box-sizing: border-box; | ||
scroll-margin-top: 32px; | ||
scroll-margin-bottom: 32px; | ||
margin: 0 auto 40px auto; | ||
} | ||
|
||
.edit-site-style-book__example.is-selected { | ||
box-shadow: 0 0 0 1px var(--wp-components-color-accent, var(--wp-admin-theme-color, #007cba)); | ||
} | ||
|
||
.edit-site-style-book__example:focus:not(:disabled) { | ||
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-components-color-accent, var(--wp-admin-theme-color, #007cba)); | ||
outline: 3px solid transparent; | ||
} | ||
|
||
.edit-site-style-book__examples.is-wide .edit-site-style-book__example { | ||
flex-direction: row; | ||
} | ||
|
||
.edit-site-style-book__subcategory-title, | ||
.edit-site-style-book__example-title { | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; | ||
font-size: 11px; | ||
font-weight: 500; | ||
line-height: normal; | ||
margin: 0; | ||
text-align: left; | ||
text-transform: uppercase; | ||
} | ||
|
||
.edit-site-style-book__subcategory-title { | ||
font-size: 16px; | ||
margin-bottom: 40px; | ||
border-bottom: 1px solid #ddd; | ||
padding-bottom: 8px; | ||
} | ||
|
||
.edit-site-style-book__examples.is-wide .edit-site-style-book__example-title { | ||
text-align: right; | ||
width: 120px; | ||
} | ||
|
||
.edit-site-style-book__example-preview { | ||
width: 100%; | ||
} | ||
|
||
.edit-site-style-book__example-preview .block-editor-block-list__insertion-point, | ||
.edit-site-style-book__example-preview .block-list-appender { | ||
display: none; | ||
} | ||
|
||
.edit-site-style-book__example-preview .is-root-container > .wp-block:first-child { | ||
margin-top: 0; | ||
} | ||
.edit-site-style-book__example-preview .is-root-container > .wp-block:last-child { | ||
margin-bottom: 0; | ||
} | ||
`; |
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,70 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { | ||
getBlockType, | ||
getBlockTypes, | ||
getBlockFromExample, | ||
createBlock, | ||
} 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. | ||
*/ | ||
|
||
/** | ||
* Returns a list of examples for registered block types. | ||
* | ||
* @return {Array<blockExamples>} An array of block examples. | ||
*/ | ||
export function getExamples() { | ||
const nonHeadingBlockExamples = getBlockTypes() | ||
.filter( ( blockType ) => { | ||
const { name, example, supports } = blockType; | ||
return ( | ||
name !== 'core/heading' && | ||
!! example && | ||
supports.inserter !== false | ||
); | ||
} ) | ||
.map( ( blockType ) => ( { | ||
name: blockType.name, | ||
title: blockType.title, | ||
category: blockType.category, | ||
blocks: getBlockFromExample( blockType.name, blockType.example ), | ||
} ) ); | ||
|
||
const isHeadingBlockRegistered = !! getBlockType( 'core/heading' ); | ||
|
||
if ( ! isHeadingBlockRegistered ) { | ||
return nonHeadingBlockExamples; | ||
} | ||
|
||
// Use our own example for the Heading block so that we can show multiple | ||
// heading levels. | ||
const headingsExample = { | ||
name: 'core/heading', | ||
title: __( 'Headings' ), | ||
category: 'text', | ||
blocks: [ 1, 2, 3, 4, 5, 6 ].map( ( level ) => { | ||
return createBlock( 'core/heading', { | ||
content: sprintf( | ||
// translators: %d: heading level e.g: "1", "2", "3" | ||
__( 'Heading %d' ), | ||
level | ||
), | ||
level, | ||
} ); | ||
} ), | ||
}; | ||
|
||
return [ headingsExample, ...nonHeadingBlockExamples ]; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if I should just convert this all to TS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One step at a time?
It could easily be a follow-up but if you're going to pull the trigger on it, maybe before we get too deep into the weeds iterating on then Style Book?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good call. That was a 🧠 🌬️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't see your reply before leaving my review which sort of flip-flopped. I think eventually we'll end up doing it. Now, or in a follow-up, I don't think it matters greatly.