-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add layout and block spacing support to Latest Posts block #77989
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
92b61a7
70f9ffb
3927e08
7baaa3f
601548d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| export const MIN_EXCERPT_LENGTH = 10; | ||
| export const MAX_EXCERPT_LENGTH = 100; | ||
| export const MAX_POSTS_COLUMNS = 6; | ||
| export const DEFAULT_EXCERPT_LENGTH = 55; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,27 +3,84 @@ | |
| */ | ||
| import metadata from './block.json'; | ||
|
|
||
| const { attributes } = metadata; | ||
| const attributes = { | ||
| ...metadata.attributes, | ||
| }; | ||
|
|
||
| const legacyLayoutAttributes = { | ||
| postLayout: { | ||
| type: 'string', | ||
| default: 'list', | ||
| }, | ||
| columns: { | ||
| type: 'number', | ||
| default: 3, | ||
| }, | ||
| }; | ||
|
|
||
| const migrateCategories = ( oldAttributes ) => { | ||
| if ( | ||
| ! oldAttributes.categories || | ||
| 'string' !== typeof oldAttributes.categories | ||
| ) { | ||
| return oldAttributes; | ||
| } | ||
|
|
||
| // This needs the full category object, not just the ID. | ||
| return { | ||
| ...oldAttributes, | ||
| categories: [ { id: Number( oldAttributes.categories ) } ], | ||
| }; | ||
| }; | ||
|
|
||
| const migratePostLayout = ( oldAttributes ) => { | ||
| const { postLayout, columns, ...attributesWithoutLegacyLayout } = | ||
| oldAttributes; | ||
|
|
||
| if ( ! postLayout ) { | ||
| return oldAttributes; | ||
| } | ||
|
|
||
| return { | ||
| ...attributesWithoutLegacyLayout, | ||
| layout: { | ||
| type: postLayout === 'grid' ? 'grid' : 'default', | ||
| ...( postLayout === 'grid' && columns && { columnCount: columns } ), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does there need to be a default here, or does it flow down from block.json? (column: 3) E.g.,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll be whatever is set on the legacy block, which might be that default from block.json or something else if user has changed it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah actually I think we should ensure the legacy attribs are present in the deprecation, then they can be removed from the block.json |
||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export default [ | ||
| { | ||
| attributes: { | ||
| ...attributes, | ||
| ...legacyLayoutAttributes, | ||
| categories: { | ||
| type: 'string', | ||
| type: [ 'array', 'string' ], | ||
| }, | ||
| }, | ||
| supports: { | ||
| align: true, | ||
| html: false, | ||
| layout: true, | ||
| }, | ||
| migrate: ( oldAttributes ) => | ||
| migratePostLayout( migrateCategories( oldAttributes ) ), | ||
| isEligible: ( { postLayout } ) => postLayout, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the migration have to run just for 'grid' or all layouts?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all layouts because we're also converting the "list" layout to the "default" type. |
||
| save: () => null, | ||
| }, | ||
| { | ||
| attributes: { | ||
| ...attributes, | ||
| categories: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| migrate: ( oldAttributes ) => { | ||
| // This needs the full category object, not just the ID. | ||
| return { | ||
| ...oldAttributes, | ||
| categories: [ { id: Number( oldAttributes.categories ) } ], | ||
| }; | ||
| supports: { | ||
| align: true, | ||
| html: false, | ||
| }, | ||
| migrate: migrateCategories, | ||
| isEligible: ( { categories } ) => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies, I haven't had my nose in deprecations for a long time. Does this deprecation see the original raw attributes or the already-migrated attributes? Just wondering if this will run on the original attributes and replace the migration result from before. If so maybe a I don't know! 😄
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I understand it the migrations always run in sequence, so if the block is eligible for the categories migration it'll run that, and if it's eligible for the layout one it'll run categories and then layout on whatever categories spat out. I tested this on blocks with legacy layouts and it seemed to work fine. |
||
| categories && 'string' === typeof categories, | ||
| save: () => null, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,6 @@ import { createInterpolateElement } from '@wordpress/element'; | |
| import { | ||
| MIN_EXCERPT_LENGTH, | ||
| MAX_EXCERPT_LENGTH, | ||
| MAX_POSTS_COLUMNS, | ||
| DEFAULT_EXCERPT_LENGTH, | ||
| } from './constants'; | ||
| import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; | ||
|
|
@@ -105,7 +104,7 @@ function getCurrentAuthor( post ) { | |
| return post._embedded?.author?.[ 0 ]; | ||
| } | ||
|
|
||
| function Controls( { attributes, setAttributes, postCount } ) { | ||
| function Controls( { attributes, setAttributes } ) { | ||
| const { | ||
| postsToShow, | ||
| order, | ||
|
|
@@ -117,8 +116,6 @@ function Controls( { attributes, setAttributes, postCount } ) { | |
| displayPostContent, | ||
| displayPostDate, | ||
| displayAuthor, | ||
| postLayout, | ||
| columns, | ||
| excerptLength, | ||
| featuredImageAlign, | ||
| featuredImageSizeSlug, | ||
|
|
@@ -473,7 +470,6 @@ function Controls( { attributes, setAttributes, postCount } ) { | |
| postsToShow: 5, | ||
| categories: undefined, | ||
| selectedAuthor: undefined, | ||
| columns: 3, | ||
| } ) | ||
| } | ||
| dropdownMenuProps={ dropdownMenuProps } | ||
|
|
@@ -523,41 +519,16 @@ function Controls( { attributes, setAttributes, postCount } ) { | |
| selectedAuthorId={ selectedAuthor } | ||
| /> | ||
| </ToolsPanelItem> | ||
|
|
||
| { postLayout === 'grid' && ( | ||
| <ToolsPanelItem | ||
| hasValue={ () => columns !== 3 } | ||
| label={ __( 'Columns' ) } | ||
| onDeselect={ () => | ||
| setAttributes( { | ||
| columns: 3, | ||
| } ) | ||
| } | ||
| isShownByDefault | ||
| > | ||
| <RangeControl | ||
| __next40pxDefaultSize | ||
| label={ __( 'Columns' ) } | ||
| value={ columns } | ||
| onChange={ ( value ) => | ||
| setAttributes( { columns: value } ) | ||
| } | ||
| min={ 2 } | ||
| max={ | ||
| ! postCount | ||
| ? MAX_POSTS_COLUMNS | ||
| : Math.min( MAX_POSTS_COLUMNS, postCount ) | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the old max here so it matches the Post Template controls. |
||
| required | ||
| /> | ||
| </ToolsPanelItem> | ||
| ) } | ||
| </ToolsPanel> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default function LatestPostsEdit( { attributes, setAttributes } ) { | ||
| export default function LatestPostsEdit( { | ||
| attributes, | ||
| setAttributes, | ||
| __unstableLayoutClassNames, | ||
| } ) { | ||
| const instanceId = useInstanceId( LatestPostsEdit ); | ||
|
|
||
| const { | ||
|
|
@@ -571,6 +542,7 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) { | |
| displayPostContent, | ||
| displayPostDate, | ||
| displayAuthor, | ||
| layout, | ||
| postLayout, | ||
| columns, | ||
| excerptLength, | ||
|
|
@@ -580,6 +552,11 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) { | |
| featuredImageSizeHeight, | ||
| addLinkToFeaturedImage, | ||
| } = attributes; | ||
| const { type: savedLayoutType, minimumColumnWidth } = layout || {}; | ||
| const layoutType = | ||
| savedLayoutType || ( postLayout === 'grid' ? 'grid' : 'default' ); | ||
| const columnCount = | ||
| layout?.columnCount ?? ( ! savedLayoutType ? columns : undefined ) ?? 3; | ||
| const { latestPosts } = useSelect( | ||
| ( select ) => { | ||
| const { getEntityRecords } = select( coreStore ); | ||
|
|
@@ -626,18 +603,20 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) { | |
| <Controls | ||
| attributes={ attributes } | ||
| setAttributes={ setAttributes } | ||
| postCount={ latestPosts?.length ?? 0 } | ||
| /> | ||
| </InspectorControls> | ||
| ); | ||
|
|
||
| const blockProps = useBlockProps( { | ||
| className: clsx( { | ||
| className: clsx( __unstableLayoutClassNames, { | ||
| 'wp-block-latest-posts__list': true, | ||
| 'is-grid': postLayout === 'grid', | ||
| 'is-grid': layoutType === 'grid', | ||
| 'has-dates': displayPostDate, | ||
| 'has-author': displayAuthor, | ||
| [ `columns-${ columns }` ]: postLayout === 'grid', | ||
| [ `columns-${ columnCount }` ]: | ||
| layoutType === 'grid' && columnCount, | ||
| 'has-native-responsive-grid': | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block has the same responsive fallback as Post Template so we're adding the same back compat workaround. |
||
| layoutType === 'grid' && columnCount && minimumColumnWidth, | ||
| } ), | ||
| } ); | ||
|
|
||
|
|
@@ -662,18 +641,29 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) { | |
| ? latestPosts.slice( 0, postsToShow ) | ||
| : latestPosts; | ||
|
|
||
| const setDisplayLayout = ( newDisplayLayout ) => | ||
| setAttributes( { | ||
| layout: { ...layout, ...newDisplayLayout }, | ||
| postLayout: undefined, | ||
| columns: undefined, | ||
| } ); | ||
|
|
||
| const layoutControls = [ | ||
| { | ||
| icon: list, | ||
| title: _x( 'List view', 'Latest posts block display setting' ), | ||
| onClick: () => setAttributes( { postLayout: 'list' } ), | ||
| isActive: postLayout === 'list', | ||
| onClick: () => setDisplayLayout( { type: 'default' } ), | ||
| isActive: layoutType === 'default' || layoutType === 'constrained', | ||
| }, | ||
| { | ||
| icon: grid, | ||
| title: _x( 'Grid view', 'Latest posts block display setting' ), | ||
| onClick: () => setAttributes( { postLayout: 'grid' } ), | ||
| isActive: postLayout === 'grid', | ||
| onClick: () => | ||
| setDisplayLayout( { | ||
| type: 'grid', | ||
| columnCount, | ||
| } ), | ||
| isActive: layoutType === 'grid', | ||
| }, | ||
| ]; | ||
|
|
||
|
|
||
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.
had to add this check because we need to run this before the layout migration, and if we don't check whether categories actually exists it just goes and adds an array of them anyway