-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
151 lines (142 loc) · 4.09 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
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
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
BlockControls,
InspectorAdvancedControls,
useBlockProps,
Warning,
} from '@wordpress/block-editor';
import {
SelectControl,
Dropdown,
ToolbarGroup,
ToolbarButton,
Spinner,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { chevronUp, chevronDown } from '@wordpress/icons';
/**
* Internal dependencies
*/
import TemplatePartNamePanel from './name-panel';
import TemplatePartInnerBlocks from './inner-blocks';
import TemplatePartPlaceholder from './placeholder';
import TemplatePartSelection from './selection';
export default function TemplatePartEdit( {
attributes: { slug, theme, tagName: TagName = 'div' },
setAttributes,
clientId,
} ) {
const templatePartId = theme && slug ? theme + '//' + slug : null;
// Set the postId block attribute if it did not exist,
// but wait until the inner blocks have loaded to allow
// new edits to trigger this.
const { isResolved, innerBlocks, isMissing } = useSelect(
( select ) => {
const { getEntityRecord, hasFinishedResolution } = select( 'core' );
const { getBlocks } = select( 'core/block-editor' );
const getEntityArgs = [
'postType',
'wp_template_part',
templatePartId,
];
const entityRecord = templatePartId
? getEntityRecord( ...getEntityArgs )
: null;
const hasResolvedEntity = templatePartId
? hasFinishedResolution( 'getEntityRecord', getEntityArgs )
: false;
return {
innerBlocks: getBlocks( clientId ),
isResolved: hasResolvedEntity,
isMissing: hasResolvedEntity && ! entityRecord,
};
},
[ templatePartId, clientId ]
);
const blockProps = useBlockProps();
const isPlaceholder = ! slug;
const isEntityAvailable = ! isPlaceholder && ! isMissing;
if ( ! isPlaceholder && isMissing ) {
return (
<TagName { ...blockProps }>
<Warning>
{ __(
'Template part has been deleted or is unavailable.'
) }
</Warning>
</TagName>
);
}
const inspectorAdvancedControls = (
<InspectorAdvancedControls>
<SelectControl
label={ __( 'HTML element' ) }
options={ [
{ label: __( 'Default (<div>)' ), value: 'div' },
{ label: '<header>', value: 'header' },
{ label: '<main>', value: 'main' },
{ label: '<section>', value: 'section' },
{ label: '<article>', value: 'article' },
{ label: '<aside>', value: 'aside' },
{ label: '<footer>', value: 'footer' },
] }
value={ TagName }
onChange={ ( value ) => setAttributes( { tagName: value } ) }
/>
</InspectorAdvancedControls>
);
return (
<>
{ inspectorAdvancedControls }
<TagName { ...blockProps }>
{ isPlaceholder && (
<TemplatePartPlaceholder
setAttributes={ setAttributes }
innerBlocks={ innerBlocks }
/>
) }
{ isEntityAvailable && (
<BlockControls>
<ToolbarGroup className="wp-block-template-part__block-control-group">
<TemplatePartNamePanel postId={ templatePartId } />
<Dropdown
className="wp-block-template-part__preview-dropdown-button"
contentClassName="wp-block-template-part__preview-dropdown-content"
position="bottom right left"
renderToggle={ ( { isOpen, onToggle } ) => (
<ToolbarButton
aria-expanded={ isOpen }
icon={
isOpen ? chevronUp : chevronDown
}
label={ __( 'Choose another' ) }
onClick={ onToggle }
// Disable when open to prevent odd FireFox bug causing reopening.
// As noted in https://github.com/WordPress/gutenberg/pull/24990#issuecomment-689094119 .
disabled={ isOpen }
/>
) }
renderContent={ ( { onClose } ) => (
<TemplatePartSelection
setAttributes={ setAttributes }
onClose={ onClose }
/>
) }
/>
</ToolbarGroup>
</BlockControls>
) }
{ isEntityAvailable && (
<TemplatePartInnerBlocks
postId={ templatePartId }
hasInnerBlocks={ innerBlocks.length > 0 }
/>
) }
{ ! isPlaceholder && ! isResolved && <Spinner /> }
</TagName>
</>
);
}