Skip to content

Commit 6b5377b

Browse files
committed
DataViews: Removing mapping of user patterns to temporary object
1 parent 99a30cb commit 6b5377b

File tree

7 files changed

+84
-75
lines changed

7 files changed

+84
-75
lines changed

packages/edit-site/src/components/page-patterns/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function PreviewWrapper( { item, onClick, ariaDescribedBy, children } ) {
117117

118118
function Preview( { item, viewType } ) {
119119
const descriptionId = useId();
120+
const description = item.description || item?.excerpt?.raw;
120121
const isUserPattern = item.type === PATTERN_TYPES.user;
121122
const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;
122123
const [ backgroundColor ] = useGlobalStyle( 'color.background' );
@@ -143,7 +144,7 @@ function Preview( { item, viewType } ) {
143144
<PreviewWrapper
144145
item={ item }
145146
onClick={ onClick }
146-
ariaDescribedBy={ item.description ? descriptionId : undefined }
147+
ariaDescribedBy={ !! description ? descriptionId : undefined }
147148
>
148149
{ isEmpty && isTemplatePart && __( 'Empty template part' ) }
149150
{ isEmpty && ! isTemplatePart && __( 'Empty pattern' ) }
@@ -156,9 +157,9 @@ function Preview( { item, viewType } ) {
156157
</Async>
157158
) }
158159
</PreviewWrapper>
159-
{ item.description && (
160+
{ !! description && (
160161
<div hidden id={ descriptionId }>
161-
{ item.description }
162+
{ description }
162163
</div>
163164
) }
164165
</div>
@@ -222,7 +223,7 @@ function Title( { item } ) {
222223
// See https://github.com/WordPress/gutenberg/pull/51898#discussion_r1243399243.
223224
tabIndex="-1"
224225
>
225-
{ title || item.name }
226+
{ title }
226227
</Button>
227228
) }
228229
</Flex>
@@ -300,6 +301,8 @@ export default function DataviewsPatterns() {
300301
header: __( 'Sync status' ),
301302
id: 'sync-status',
302303
render: ( { item } ) => {
304+
const syncStatus =
305+
item.wp_pattern_sync_status || PATTERN_SYNC_TYPES.full;
303306
// User patterns can have their sync statuses checked directly.
304307
// Non-user patterns are all unsynced for the time being.
305308
return (
@@ -309,8 +312,7 @@ export default function DataviewsPatterns() {
309312
{
310313
(
311314
SYNC_FILTERS.find(
312-
( { value } ) =>
313-
value === item.syncStatus
315+
( { value } ) => value === syncStatus
314316
) ||
315317
SYNC_FILTERS.find(
316318
( { value } ) =>

packages/edit-site/src/components/page-patterns/search-items.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,42 @@ import {
2424
} from '../../utils/constants';
2525

2626
// Default search helpers.
27-
const defaultGetName = ( item ) =>
28-
item.type !== TEMPLATE_PART_POST_TYPE ? item.name || '' : '';
29-
export const defaultGetTitle = ( item ) =>
30-
typeof item.title === 'string' ? item.title : item.title.rendered;
31-
const defaultGetDescription = ( item ) => item.description || '';
27+
const defaultGetName = ( item ) => {
28+
if ( item.type === PATTERN_TYPES.user ) {
29+
return item.slug;
30+
}
31+
32+
if ( item.type === TEMPLATE_PART_POST_TYPE ) {
33+
return '';
34+
}
35+
36+
return item.name || '';
37+
};
38+
39+
export const defaultGetTitle = ( item ) => {
40+
if ( typeof item.title === 'string' ) {
41+
return item.title;
42+
}
43+
44+
if ( item.title && item.title.rendered ) {
45+
return item.title.rendered;
46+
}
47+
48+
if ( item.title && item.title.raw ) {
49+
return item.title.raw;
50+
}
51+
52+
return '';
53+
};
54+
55+
const defaultGetDescription = ( item ) => {
56+
if ( item.type === PATTERN_TYPES.user ) {
57+
return item.excerpt.raw;
58+
}
59+
60+
return item.description || '';
61+
};
62+
3263
const defaultGetKeywords = ( item ) => item.keywords || [];
3364
const defaultHasCategory = () => false;
3465

packages/edit-site/src/components/page-patterns/use-patterns.js

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ const selectPatterns = createSelector(
128128
patterns: themePatterns,
129129
isResolving: isResolvingThemePatterns,
130130
} = selectThemePatterns( select );
131-
const { patterns: userPatterns, isResolving: isResolvingUserPatterns } =
132-
selectUserPatterns( select );
131+
const {
132+
patterns: userPatterns,
133+
isResolving: isResolvingUserPatterns,
134+
categories: userPatternCategories,
135+
} = selectUserPatterns( select );
133136

134137
let patterns = [
135138
...( themePatterns || [] ),
@@ -141,16 +144,26 @@ const selectPatterns = createSelector(
141144
// Non-user patterns are all unsynced for the time being.
142145
patterns = patterns.filter( ( pattern ) => {
143146
return pattern.type === PATTERN_TYPES.user
144-
? pattern.syncStatus === syncStatus
147+
? ( pattern.wp_pattern_sync_status ||
148+
PATTERN_SYNC_TYPES.full ) === syncStatus
145149
: syncStatus === PATTERN_SYNC_TYPES.unsynced;
146150
} );
147151
}
148152

149153
if ( categoryId ) {
150154
patterns = searchItems( patterns, search, {
151155
categoryId,
152-
hasCategory: ( item, currentCategory ) =>
153-
item.categories?.includes( currentCategory ),
156+
hasCategory: ( item, currentCategory ) => {
157+
if ( item.type === PATTERN_TYPES.user ) {
158+
return item.wp_pattern_category.some(
159+
( catId ) =>
160+
userPatternCategories.find(
161+
( cat ) => cat.id === catId
162+
)?.slug === currentCategory
163+
);
164+
}
165+
return item.categories?.includes( currentCategory );
166+
},
154167
} );
155168
} else {
156169
patterns = searchItems( patterns, search, {
@@ -168,41 +181,6 @@ const selectPatterns = createSelector(
168181
]
169182
);
170183

171-
/**
172-
* Converts a post of type `wp_block` to a 'pattern item' that more closely
173-
* matches the structure of theme provided patterns.
174-
*
175-
* @param {Object} patternPost The `wp_block` record being normalized.
176-
* @param {Map} categories A Map of user created categories.
177-
*
178-
* @return {Object} The normalized item.
179-
*/
180-
const convertPatternPostToItem = ( patternPost, categories ) => ( {
181-
blocks: parse( patternPost.content.raw, {
182-
__unstableSkipMigrationLogs: true,
183-
} ),
184-
...( patternPost.wp_pattern_category.length > 0 && {
185-
categories: patternPost.wp_pattern_category.map(
186-
( patternCategoryId ) =>
187-
categories && categories.get( patternCategoryId )
188-
? categories.get( patternCategoryId ).slug
189-
: patternCategoryId
190-
),
191-
} ),
192-
termLabels: patternPost.wp_pattern_category.map( ( patternCategoryId ) =>
193-
categories?.get( patternCategoryId )
194-
? categories.get( patternCategoryId ).label
195-
: patternCategoryId
196-
),
197-
id: patternPost.id,
198-
name: patternPost.slug,
199-
syncStatus: patternPost.wp_pattern_sync_status || PATTERN_SYNC_TYPES.full,
200-
title: patternPost.title.raw,
201-
type: patternPost.type,
202-
description: patternPost.excerpt.raw,
203-
patternPost,
204-
} );
205-
206184
const selectUserPatterns = createSelector(
207185
( select, syncStatus, search = '' ) => {
208186
const {
@@ -222,12 +200,7 @@ const selectUserPatterns = createSelector(
222200
userPatternCategories.forEach( ( userCategory ) =>
223201
categories.set( userCategory.id, userCategory )
224202
);
225-
let patterns = patternPosts
226-
? patternPosts.map( ( record ) =>
227-
convertPatternPostToItem( record, categories )
228-
)
229-
: EMPTY_PATTERN_LIST;
230-
203+
let patterns = patternPosts ?? EMPTY_PATTERN_LIST;
231204
const isResolving = isResolvingSelector( 'getEntityRecords', [
232205
'postType',
233206
PATTERN_TYPES.user,
@@ -236,7 +209,9 @@ const selectUserPatterns = createSelector(
236209

237210
if ( syncStatus ) {
238211
patterns = patterns.filter(
239-
( pattern ) => pattern.syncStatus === syncStatus
212+
( pattern ) =>
213+
pattern.wp_pattern_sync_status ||
214+
PATTERN_SYNC_TYPES.full === syncStatus
240215
);
241216
}
242217

packages/editor/src/components/post-actions/actions.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { store as editorStore } from '../../store';
3333
import { unlock } from '../../lock-unlock';
3434
import { exportPatternAsJSONAction } from './export-pattern-action';
3535
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
36+
import { getItemTitle } from '../../dataviews/actions/utils';
3637

3738
// Patterns.
3839
const { PATTERN_TYPES, CreatePatternModalContents, useDuplicatePatternProps } =
@@ -57,13 +58,6 @@ function isTemplateRemovable( template ) {
5758
);
5859
}
5960

60-
function getItemTitle( item ) {
61-
if ( typeof item.title === 'string' ) {
62-
return decodeEntities( item.title );
63-
}
64-
return decodeEntities( item.title?.rendered || '' );
65-
}
66-
6761
const trashPostAction = {
6862
id: 'move-to-trash',
6963
label: __( 'Move to Trash' ),
@@ -793,10 +787,8 @@ export const duplicatePatternAction = {
793787
modalHeader: _x( 'Duplicate pattern', 'action label' ),
794788
RenderModal: ( { items, closeModal } ) => {
795789
const [ item ] = items;
796-
const isThemePattern = item.type === PATTERN_TYPES.theme;
797790
const duplicatedProps = useDuplicatePatternProps( {
798-
pattern:
799-
isThemePattern || ! item.patternPost ? item : item.patternPost,
791+
pattern: item,
800792
onSuccess: () => closeModal(),
801793
} );
802794
return (

packages/editor/src/components/post-actions/export-pattern-action.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
1515
* Internal dependencies
1616
*/
1717
import { unlock } from '../../lock-unlock';
18+
import { getItemTitle } from '../../dataviews/actions/utils';
1819

1920
// Patterns.
2021
const { PATTERN_TYPES } = unlock( patternsPrivateApis );
@@ -23,9 +24,9 @@ function getJsonFromItem( item ) {
2324
return JSON.stringify(
2425
{
2526
__file: item.type,
26-
title: item.title || item.name,
27-
content: item.patternPost.content.raw,
28-
syncStatus: item.patternPost.wp_pattern_sync_status,
27+
title: getItemTitle( item ),
28+
content: item.content.raw,
29+
syncStatus: item.wp_pattern_sync_status,
2930
},
3031
null,
3132
2
@@ -45,14 +46,16 @@ export const exportPatternAsJSONAction = {
4546
callback: async ( items ) => {
4647
if ( items.length === 1 ) {
4748
return downloadBlob(
48-
`${ kebabCase( items[ 0 ].title || items[ 0 ].name ) }.json`,
49+
`${ kebabCase(
50+
getItemTitle( items[ 0 ] ) || items[ 0 ].slug
51+
) }.json`,
4952
getJsonFromItem( items[ 0 ] ),
5053
'application/json'
5154
);
5255
}
5356
const nameCount = {};
5457
const filesToZip = items.map( ( item ) => {
55-
const name = kebabCase( item.title || item.name );
58+
const name = kebabCase( getItemTitle( item ) || item.slug );
5659
nameCount[ name ] = ( nameCount[ name ] || 0 ) + 1;
5760
return {
5861
name: `${

packages/editor/src/dataviews/actions/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ export function getItemTitle( item: Post ) {
2424
if ( typeof item.title === 'string' ) {
2525
return decodeEntities( item.title );
2626
}
27-
return decodeEntities( item.title?.rendered || '' );
27+
if ( 'rendered' in item.title ) {
28+
return decodeEntities( item.title.rendered );
29+
}
30+
if ( 'raw' in item.title ) {
31+
return decodeEntities( item.title.raw );
32+
}
33+
return '';
2834
}
2935

3036
/**

packages/editor/src/dataviews/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type PostStatus =
99

1010
export interface BasePost {
1111
status?: PostStatus;
12-
title: string | { rendered: string };
12+
title: string | { rendered: string } | { raw: string };
1313
type: string;
1414
id: string | number;
1515
}

0 commit comments

Comments
 (0)