-
Notifications
You must be signed in to change notification settings - Fork 4k
/
use-patterns.js
170 lines (147 loc) · 4.6 KB
/
use-patterns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* WordPress dependencies
*/
import { parse } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import {
CORE_PATTERN_SOURCES,
PATTERNS,
SYNC_TYPES,
TEMPLATE_PARTS,
USER_PATTERNS,
filterOutDuplicatesByName,
} from './utils';
import { unlock } from '../../lock-unlock';
import { searchItems } from './search-items';
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 ),
categories: [ templatePart.area ],
description: templatePart.description || '',
isCustom: templatePart.source === 'custom',
keywords: templatePart.keywords || [],
id: createTemplatePartId( templatePart.theme, templatePart.slug ),
name: createTemplatePartId( templatePart.theme, templatePart.slug ),
title: decodeEntities( templatePart.title.rendered ),
type: templatePart.type,
templatePart,
} );
const templatePartHasCategory = ( item, category ) =>
item.templatePart.area === category;
const selectTemplatePartsAsPatterns = (
select,
{ categoryId, search = '' } = {}
) => {
const { getEntityRecords, getIsResolving } = select( coreStore );
const query = { per_page: -1 };
const rawTemplateParts =
getEntityRecords( 'postType', TEMPLATE_PARTS, query ) ??
EMPTY_PATTERN_LIST;
const templateParts = rawTemplateParts.map( ( templatePart ) =>
templatePartToPattern( templatePart )
);
const isResolving = getIsResolving( 'getEntityRecords', [
'postType',
'wp_template_part',
query,
] );
const patterns = searchItems( templateParts, search, {
categoryId,
hasCategory: templatePartHasCategory,
} );
return { patterns, isResolving };
};
const selectThemePatterns = ( select, { categoryId, search = '' } = {} ) => {
const { getSettings } = unlock( select( editSiteStore ) );
const settings = getSettings();
const blockPatterns =
settings.__experimentalAdditionalBlockPatterns ??
settings.__experimentalBlockPatterns;
const restBlockPatterns = select( coreStore ).getBlockPatterns();
let patterns = [
...( blockPatterns || [] ),
...( restBlockPatterns || [] ),
]
.filter(
( pattern ) => ! CORE_PATTERN_SOURCES.includes( pattern.source )
)
.filter( filterOutDuplicatesByName )
.map( ( pattern ) => ( {
...pattern,
keywords: pattern.keywords || [],
type: 'pattern',
blocks: parse( pattern.content ),
} ) );
patterns = searchItems( patterns, search, {
categoryId,
hasCategory: ( item, currentCategory ) =>
item.categories?.includes( currentCategory ),
} );
return { patterns, isResolving: false };
};
const reusableBlockToPattern = ( reusableBlock ) => ( {
blocks: parse( reusableBlock.content.raw ),
categories: reusableBlock.wp_pattern,
id: reusableBlock.id,
name: reusableBlock.slug,
syncStatus: reusableBlock.wp_pattern_sync_status || SYNC_TYPES.full,
title: reusableBlock.title.raw,
type: reusableBlock.type,
reusableBlock,
} );
const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => {
const { getEntityRecords, getIsResolving } = select( coreStore );
const query = { per_page: -1 };
const records = getEntityRecords( 'postType', USER_PATTERNS, query );
let patterns = records
? records.map( ( record ) => reusableBlockToPattern( record ) )
: EMPTY_PATTERN_LIST;
const isResolving = getIsResolving( 'getEntityRecords', [
'postType',
USER_PATTERNS,
query,
] );
if ( syncStatus ) {
patterns = patterns.filter(
( pattern ) => pattern.syncStatus === syncStatus
);
}
patterns = searchItems( patterns, search, {
// We exit user pattern retrieval early if we aren't in the
// catch-all category for user created patterns, so it has
// to be in the category.
hasCategory: () => true,
} );
return { patterns, isResolving };
};
export const usePatterns = (
categoryType,
categoryId,
{ search = '', syncStatus }
) => {
return useSelect(
( select ) => {
if ( categoryType === TEMPLATE_PARTS ) {
return selectTemplatePartsAsPatterns( select, {
categoryId,
search,
} );
} else if ( categoryType === PATTERNS ) {
return selectThemePatterns( select, { categoryId, search } );
} else if ( categoryType === USER_PATTERNS ) {
return selectUserPatterns( select, { search, syncStatus } );
}
return { patterns: EMPTY_PATTERN_LIST, isResolving: false };
},
[ categoryId, categoryType, search, syncStatus ]
);
};
export default usePatterns;