-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
90 lines (80 loc) · 2.05 KB
/
index.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
/**
* External dependencies
*/
import { capitalCase } from 'change-case';
/**
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { select } from '@wordpress/data';
import { symbolFilled } from '@wordpress/icons';
import { addFilter } from '@wordpress/hooks';
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';
import { enhanceTemplatePartVariations } from './variations';
const { name } = metadata;
export { metadata, name };
export const settings = {
icon: symbolFilled,
__experimentalLabel: ( { slug, theme } ) => {
// Attempt to find entity title if block is a template part.
// Require slug to request, otherwise entity is uncreated and will throw 404.
if ( ! slug ) {
return;
}
const entity = select( coreDataStore ).getEntityRecord(
'postType',
'wp_template_part',
theme + '//' + slug
);
if ( ! entity ) {
return;
}
return (
decodeEntities( entity.title?.rendered ) ||
capitalCase( entity.slug )
);
},
edit,
};
export const init = () => {
addFilter(
'blocks.registerBlockType',
'core/template-part',
enhanceTemplatePartVariations
);
// Prevent adding template parts inside post templates.
const DISALLOWED_PARENTS = [ 'core/post-template', 'core/post-content' ];
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'removeTemplatePartsFromPostTemplates',
(
can,
blockType,
rootClientId,
{ getBlock, getBlockParentsByBlockName }
) => {
if ( blockType.name !== 'core/template-part' ) {
return can;
}
for ( const disallowedParentType of DISALLOWED_PARENTS ) {
const hasDisallowedParent =
getBlock( rootClientId )?.name === disallowedParentType ||
getBlockParentsByBlockName(
rootClientId,
disallowedParentType
).length;
if ( hasDisallowedParent ) {
return false;
}
}
return true;
}
);
return initBlock( { name, metadata, settings } );
};