-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Patterns: Avoid mapping template parts objects to patterns #62927
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
Changes from 6 commits
b093266
7d1fa9b
c5e0ad9
2ff18f3
113c798
c66631d
b939f56
f0e4e0b
f57bb59
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 |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ import { usePrevious } from '@wordpress/compose'; | |
| import { useEntityRecords } from '@wordpress/core-data'; | ||
| import { privateApis as editorPrivateApis } from '@wordpress/editor'; | ||
| import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
| import { parse } from '@wordpress/blocks'; | ||
| import { decodeEntities } from '@wordpress/html-entities'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -53,6 +55,7 @@ import PatternsHeader from './header'; | |
| import { useLink } from '../routes/link'; | ||
| import { useAddedBy } from '../page-templates/hooks'; | ||
| import { useEditPostAction } from '../dataviews-actions'; | ||
| import { defaultGetTitle } from './search-items'; | ||
|
|
||
| const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock( | ||
| blockEditorPrivateApis | ||
|
|
@@ -116,14 +119,21 @@ function Preview( { item, viewType } ) { | |
| const descriptionId = useId(); | ||
| const isUserPattern = item.type === PATTERN_TYPES.user; | ||
| const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE; | ||
| const isEmpty = ! item.blocks?.length; | ||
|
|
||
| const [ backgroundColor ] = useGlobalStyle( 'color.background' ); | ||
| const { onClick } = useLink( { | ||
| postType: item.type, | ||
| postId: isUserPattern ? item.id : item.name, | ||
| postId: isUserPattern || isTemplatePart ? item.id : item.name, | ||
| canvas: 'edit', | ||
| } ); | ||
| const blocks = useMemo( () => { | ||
| return ( | ||
| item.blocks ?? | ||
| parse( item.content.raw, { | ||
| __unstableSkipMigrationLogs: true, | ||
| } ) | ||
| ); | ||
| }, [ item?.content?.raw, item.blocks ] ); | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const isEmpty = ! blocks?.length; | ||
|
|
||
| return ( | ||
| <div | ||
|
|
@@ -140,7 +150,7 @@ function Preview( { item, viewType } ) { | |
| { ! isEmpty && ( | ||
| <Async> | ||
| <BlockPreview | ||
| blocks={ item.blocks } | ||
| blocks={ blocks } | ||
| viewportWidth={ item.viewportWidth } | ||
| /> | ||
| </Async> | ||
|
|
@@ -187,11 +197,13 @@ function Author( { item, viewType } ) { | |
|
|
||
| function Title( { item } ) { | ||
| const isUserPattern = item.type === PATTERN_TYPES.user; | ||
| const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE; | ||
| const { onClick } = useLink( { | ||
| postType: item.type, | ||
| postId: isUserPattern ? item.id : item.name, | ||
| postId: isUserPattern || isTemplatePart ? item.id : item.name, | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| canvas: 'edit', | ||
| } ); | ||
| const title = decodeEntities( defaultGetTitle( item ) ); | ||
|
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. Nice catch 👍 Before: Gravacao.do.ecra.2024-06-28.as.13.34.54.movAfter: Gravacao.do.ecra.2024-06-28.as.13.32.12.mov |
||
| return ( | ||
| <HStack alignment="center" justify="flex-start" spacing={ 2 }> | ||
| <Flex | ||
|
|
@@ -201,7 +213,7 @@ function Title( { item } ) { | |
| className="edit-site-patterns__pattern-title" | ||
| > | ||
| { item.type === PATTERN_TYPES.theme ? ( | ||
| item.title | ||
| title | ||
| ) : ( | ||
| <Button | ||
| variant="link" | ||
|
|
@@ -210,7 +222,7 @@ function Title( { item } ) { | |
| // See https://github.com/WordPress/gutenberg/pull/51898#discussion_r1243399243. | ||
| tabIndex="-1" | ||
| > | ||
| { item.title || item.name } | ||
| { title || item.name } | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </Button> | ||
| ) } | ||
| </Flex> | ||
|
|
@@ -321,7 +333,7 @@ export default function DataviewsPatterns() { | |
| _fields.push( { | ||
| header: __( 'Author' ), | ||
| id: 'author', | ||
| getValue: ( { item } ) => item.templatePart.author_text, | ||
| getValue: ( { item } ) => item.author_text, | ||
| render: ( { item } ) => { | ||
| return <Author viewType={ view.type } item={ item } />; | ||
| }, | ||
|
|
@@ -406,7 +418,7 @@ export default function DataviewsPatterns() { | |
| fields={ fields } | ||
| actions={ actions } | ||
| data={ data || EMPTY_ARRAY } | ||
| getItemId={ ( item ) => item.name } | ||
| getItemId={ ( item ) => item.name ?? item.id } | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| isLoading={ isResolving } | ||
| view={ view } | ||
| onChangeView={ onChangeView } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ import { parse } from '@wordpress/blocks'; | |
| import { useSelect, createSelector } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { store as editorStore } from '@wordpress/editor'; | ||
| import { decodeEntities } from '@wordpress/html-entities'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -16,7 +15,6 @@ import { | |
| PATTERN_TYPES, | ||
| PATTERN_SYNC_TYPES, | ||
| TEMPLATE_PART_POST_TYPE, | ||
| TEMPLATE_ORIGINS, | ||
| TEMPLATE_PART_AREA_DEFAULT_CATEGORY, | ||
| } from '../../utils/constants'; | ||
| import { unlock } from '../../lock-unlock'; | ||
|
|
@@ -25,38 +23,16 @@ import { store as editSiteStore } from '../../store'; | |
|
|
||
| const EMPTY_PATTERN_LIST = []; | ||
|
|
||
| const createTemplatePartId = ( theme, slug ) => | ||
| theme && slug ? theme + '//' + slug : null; | ||
|
|
||
| const templatePartToPattern = ( templatePart ) => ( { | ||
| blocks: parse( templatePart.content.raw, { | ||
|
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. Reviewed how this (or whether) is being used in the consumer code that uses parts (page-patterns). Everything has been migrated:
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. I see actions were also a consumer of this (through dataviews's item). I've only found one case that requires updating ( |
||
| __unstableSkipMigrationLogs: true, | ||
| } ), | ||
| categories: [ templatePart.area ], | ||
| description: templatePart.description || '', | ||
| isCustom: templatePart.source === TEMPLATE_ORIGINS.custom, | ||
| keywords: templatePart.keywords || [], | ||
| id: createTemplatePartId( templatePart.theme, templatePart.slug ), | ||
| name: createTemplatePartId( templatePart.theme, templatePart.slug ), | ||
| title: decodeEntities( templatePart.title.rendered ), | ||
| type: templatePart.type, | ||
| _links: templatePart._links, | ||
| templatePart, | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } ); | ||
|
|
||
| const selectTemplatePartsAsPatterns = createSelector( | ||
| const selectTemplateParts = createSelector( | ||
| ( select, categoryId, search = '' ) => { | ||
| const { getEntityRecords, isResolving: isResolvingSelector } = | ||
| select( coreStore ); | ||
| const { __experimentalGetDefaultTemplatePartAreas } = | ||
| select( editorStore ); | ||
| const query = { per_page: -1 }; | ||
| const rawTemplateParts = | ||
| const templateParts = | ||
| getEntityRecords( 'postType', TEMPLATE_PART_POST_TYPE, query ) ?? | ||
| EMPTY_PATTERN_LIST; | ||
| const templateParts = rawTemplateParts.map( ( templatePart ) => | ||
| templatePartToPattern( templatePart ) | ||
| ); | ||
|
|
||
| // In the case where a custom template part area has been removed we need | ||
| // the current list of areas to cross check against so orphaned template | ||
|
|
@@ -66,12 +42,12 @@ const selectTemplatePartsAsPatterns = createSelector( | |
|
|
||
| const templatePartHasCategory = ( item, category ) => { | ||
| if ( category !== TEMPLATE_PART_AREA_DEFAULT_CATEGORY ) { | ||
| return item.templatePart.area === category; | ||
| return item.area === category; | ||
| } | ||
|
|
||
| return ( | ||
| item.templatePart.area === category || | ||
| ! templatePartAreas.includes( item.templatePart.area ) | ||
| item.area === category || | ||
| ! templatePartAreas.includes( item.area ) | ||
| ); | ||
| }; | ||
|
|
||
|
|
@@ -298,11 +274,7 @@ export const usePatterns = ( | |
| return useSelect( | ||
| ( select ) => { | ||
| if ( postType === TEMPLATE_PART_POST_TYPE ) { | ||
| return selectTemplatePartsAsPatterns( | ||
| select, | ||
| categoryId, | ||
| search | ||
| ); | ||
| return selectTemplateParts( select, categoryId, search ); | ||
|
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. I've only found two uses of the
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. I presume that, back in the days, the It'd be good to rename it to |
||
| } else if ( postType === PATTERN_TYPES.user && !! categoryId ) { | ||
| const appliedCategory = | ||
| categoryId === 'uncategorized' ? '' : categoryId; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.