-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
220 lines (206 loc) · 5.42 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, useMemo } from '@wordpress/element';
import {
InnerBlocks,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
InspectorControls,
JustifyToolbar,
BlockControls,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useDispatch, withSelect, withDispatch } from '@wordpress/data';
import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import useBlockNavigator from './use-block-navigator';
import NavigationPlaceholder from './placeholder';
import PlaceholderPreview from './placeholder-preview';
import ResponsiveWrapper from './responsive-wrapper';
const ALLOWED_BLOCKS = [
'core/navigation-link',
'core/search',
'core/social-links',
'core/page-list',
'core/spacer',
'core/home-link',
];
const LAYOUT = {
type: 'default',
alignments: [],
};
function Navigation( {
selectedBlockHasDescendants,
attributes,
setAttributes,
clientId,
hasExistingNavItems,
isImmediateParentOfSelectedBlock,
isSelected,
updateInnerBlocks,
className,
hasSubmenuIndicatorSetting = true,
hasItemJustificationControls = true,
} ) {
const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
! hasExistingNavItems
);
const [ isResponsiveMenuOpen, setResponsiveMenuVisibility ] = useState(
false
);
const { selectBlock } = useDispatch( blockEditorStore );
const blockProps = useBlockProps( {
className: classnames( className, {
[ `items-justified-${ attributes.itemsJustification }` ]: attributes.itemsJustification,
'is-vertical': attributes.orientation === 'vertical',
'is-responsive': attributes.isResponsive,
} ),
} );
const { navigatorToolbarButton, navigatorModal } = useBlockNavigator(
clientId
);
const placeholder = useMemo( () => <PlaceholderPreview />, [] );
const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-navigation__container',
},
{
allowedBlocks: ALLOWED_BLOCKS,
orientation: attributes.orientation || 'horizontal',
renderAppender:
( isImmediateParentOfSelectedBlock &&
! selectedBlockHasDescendants ) ||
isSelected
? InnerBlocks.DefaultAppender
: false,
__experimentalAppenderTagName: 'li',
__experimentalCaptureToolbars: true,
// Template lock set to false here so that the Nav
// Block on the experimental menus screen does not
// inherit templateLock={ 'all' }.
templateLock: false,
__experimentalLayout: LAYOUT,
placeholder,
}
);
if ( isPlaceholderShown ) {
return (
<div { ...blockProps }>
<NavigationPlaceholder
onCreate={ ( blocks, selectNavigationBlock ) => {
setIsPlaceholderShown( false );
updateInnerBlocks( blocks );
if ( selectNavigationBlock ) {
selectBlock( clientId );
}
} }
/>
</div>
);
}
const justifyAllowedControls =
attributes.orientation === 'vertical'
? [ 'left', 'center', 'right' ]
: [ 'left', 'center', 'right', 'space-between' ];
return (
<>
<BlockControls>
{ hasItemJustificationControls && (
<JustifyToolbar
value={ attributes.itemsJustification }
allowedControls={ justifyAllowedControls }
onChange={ ( value ) =>
setAttributes( { itemsJustification: value } )
}
popoverProps={ {
position: 'bottom right',
isAlternate: true,
} }
/>
) }
<ToolbarGroup>{ navigatorToolbarButton }</ToolbarGroup>
</BlockControls>
{ navigatorModal }
<InspectorControls>
{ hasSubmenuIndicatorSetting && (
<PanelBody title={ __( 'Display settings' ) }>
<ToggleControl
checked={ attributes.showSubmenuIcon }
onChange={ ( value ) => {
setAttributes( {
showSubmenuIcon: value,
} );
} }
label={ __( 'Show submenu indicator icons' ) }
/>
<ToggleControl
checked={ attributes.isResponsive }
onChange={ ( value ) => {
setAttributes( {
isResponsive: value,
} );
} }
label={ __( 'Enable responsive menu' ) }
/>
</PanelBody>
) }
</InspectorControls>
<nav { ...blockProps }>
<ResponsiveWrapper
id={ clientId }
onToggle={ setResponsiveMenuVisibility }
isOpen={ isResponsiveMenuOpen }
isResponsive={ attributes.isResponsive }
>
<ul { ...innerBlocksProps }></ul>
</ResponsiveWrapper>
</nav>
</>
);
}
export default compose( [
withSelect( ( select, { clientId } ) => {
const innerBlocks = select( blockEditorStore ).getBlocks( clientId );
const {
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
} = select( blockEditorStore );
const isImmediateParentOfSelectedBlock = hasSelectedInnerBlock(
clientId,
false
);
const selectedBlockId = getSelectedBlockClientId();
const selectedBlockHasDescendants = !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length;
return {
isImmediateParentOfSelectedBlock,
selectedBlockHasDescendants,
hasExistingNavItems: !! innerBlocks.length,
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
return {
updateInnerBlocks( blocks ) {
if ( blocks?.length === 0 ) {
return false;
}
dispatch( blockEditorStore ).replaceInnerBlocks(
clientId,
blocks,
true
);
},
};
} ),
] )( Navigation );