-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
175 lines (162 loc) · 4.99 KB
/
edit.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
171
172
173
174
175
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import {
InnerBlocks,
useBlockProps,
InspectorControls,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { SelectControl } from '@wordpress/components';
import { useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { View } from '@wordpress/primitives';
/**
* Internal dependencies
*/
import GroupPlaceHolder, { useShouldShowPlaceHolder } from './placeholder';
/**
* Render inspector controls for the Group block.
*
* @param {Object} props Component props.
* @param {string} props.tagName The HTML tag name.
* @param {Function} props.onSelectTagName onChange function for the SelectControl.
*
* @return {JSX.Element} The control group.
*/
function GroupEditControls( { tagName, onSelectTagName } ) {
const htmlElementMessages = {
header: __(
'The <header> element should represent introductory content, typically a group of introductory or navigational aids.'
),
main: __(
'The <main> element should be used for the primary content of your document only.'
),
section: __(
"The <section> element should represent a standalone portion of the document that can't be better represented by another element."
),
article: __(
'The <article> element should represent a self-contained, syndicatable portion of the document.'
),
aside: __(
"The <aside> element should represent a portion of a document whose content is only indirectly related to the document's main content."
),
footer: __(
'The <footer> element should represent a footer for its nearest sectioning element (e.g.: <section>, <article>, <main> etc.).'
),
};
return (
<InspectorControls group="advanced">
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize
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={ onSelectTagName }
help={ htmlElementMessages[ tagName ] }
/>
</InspectorControls>
);
}
function GroupEdit( { attributes, name, setAttributes, clientId } ) {
const { hasInnerBlocks, themeSupportsLayout } = useSelect(
( select ) => {
const { getBlock, getSettings } = select( blockEditorStore );
const block = getBlock( clientId );
return {
hasInnerBlocks: !! ( block && block.innerBlocks.length ),
themeSupportsLayout: getSettings()?.supportsLayout,
};
},
[ clientId ]
);
const {
tagName: TagName = 'div',
templateLock,
allowedBlocks,
layout = {},
} = attributes;
// Layout settings.
const { type = 'default' } = layout;
const layoutSupportEnabled =
themeSupportsLayout || type === 'flex' || type === 'grid';
// Hooks.
const ref = useRef();
const blockProps = useBlockProps( { ref } );
const [ showPlaceholder, setShowPlaceholder ] = useShouldShowPlaceHolder( {
attributes,
usedLayoutType: type,
hasInnerBlocks,
} );
// Default to the regular appender being rendered.
let renderAppender;
if ( showPlaceholder ) {
// In the placeholder state, ensure the appender is not rendered.
// This is needed because `...innerBlocksProps` is used in the placeholder
// state so that blocks can dragged onto the placeholder area
// from both the list view and in the editor canvas.
renderAppender = false;
} else if ( ! hasInnerBlocks ) {
// When there is no placeholder, but the block is also empty,
// use the larger button appender.
renderAppender = InnerBlocks.ButtonBlockAppender;
}
const innerBlocksProps = useInnerBlocksProps(
layoutSupportEnabled
? blockProps
: { className: 'wp-block-group__inner-container' },
{
dropZoneElement: ref.current,
templateLock,
allowedBlocks,
renderAppender,
}
);
const { selectBlock } = useDispatch( blockEditorStore );
const selectVariation = ( nextVariation ) => {
setAttributes( nextVariation.attributes );
selectBlock( clientId, -1 );
setShowPlaceholder( false );
};
return (
<>
<GroupEditControls
tagName={ TagName }
onSelectTagName={ ( value ) =>
setAttributes( { tagName: value } )
}
/>
{ showPlaceholder && (
<View>
{ innerBlocksProps.children }
<GroupPlaceHolder
name={ name }
onSelect={ selectVariation }
/>
</View>
) }
{ layoutSupportEnabled && ! showPlaceholder && (
<TagName { ...innerBlocksProps } />
) }
{ /* Ideally this is not needed but it's there for backward compatibility reason
to keep this div for themes that might rely on its presence */ }
{ ! layoutSupportEnabled && ! showPlaceholder && (
<TagName { ...blockProps }>
<div { ...innerBlocksProps } />
</TagName>
) }
</>
);
}
export default GroupEdit;