forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-inspector-controls.js
178 lines (162 loc) · 4.57 KB
/
menu-inspector-controls.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
/**
* WordPress dependencies
*/
import {
privateApis as blockEditorPrivateApis,
InspectorControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
PanelBody,
__experimentalHStack as HStack,
__experimentalHeading as Heading,
Spinner,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import NavigationMenuSelector from './navigation-menu-selector';
import { unlock } from '../../lock-unlock';
import DeletedNavigationWarning from './deleted-navigation-warning';
import useNavigationMenu from '../use-navigation-menu';
import LeafMoreMenu from './leaf-more-menu';
import { updateAttributes } from '../../navigation-link/update-attributes';
import { LinkUI } from '../../navigation-link/link-ui';
/* translators: %s: The name of a menu. */
const actionLabel = __( "Switch to '%s'" );
const BLOCKS_WITH_LINK_UI_SUPPORT = [
'core/navigation-link',
'core/navigation-submenu',
];
const { PrivateListView } = unlock( blockEditorPrivateApis );
function AdditionalBlockContent( { block, insertedBlock, setInsertedBlock } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const supportsLinkControls = BLOCKS_WITH_LINK_UI_SUPPORT?.includes(
insertedBlock?.name
);
const blockWasJustInserted = insertedBlock?.clientId === block.clientId;
const showLinkControls = supportsLinkControls && blockWasJustInserted;
if ( ! showLinkControls ) {
return null;
}
const setInsertedBlockAttributes =
( _insertedBlockClientId ) => ( _updatedAttributes ) => {
if ( ! _insertedBlockClientId ) return;
updateBlockAttributes( _insertedBlockClientId, _updatedAttributes );
};
return (
<LinkUI
clientId={ insertedBlock?.clientId }
link={ insertedBlock?.attributes }
onClose={ () => {
setInsertedBlock( null );
} }
onChange={ ( updatedValue ) => {
updateAttributes(
updatedValue,
setInsertedBlockAttributes( insertedBlock?.clientId ),
insertedBlock?.attributes
);
setInsertedBlock( null );
} }
onCancel={ () => {
setInsertedBlock( null );
} }
/>
);
}
const MainContent = ( {
clientId,
currentMenuId,
isLoading,
isNavigationMenuMissing,
onCreateNew,
} ) => {
const hasChildren = useSelect(
( select ) => {
return !! select( blockEditorStore ).getBlockCount( clientId );
},
[ clientId ]
);
const { navigationMenu } = useNavigationMenu( currentMenuId );
if ( currentMenuId && isNavigationMenuMissing ) {
return <DeletedNavigationWarning onCreateNew={ onCreateNew } />;
}
if ( isLoading ) {
return <Spinner />;
}
const description = navigationMenu
? sprintf(
/* translators: %s: The name of a menu. */
__( 'Structure for Navigation Menu: %s' ),
navigationMenu?.title || __( 'Untitled menu' )
)
: __(
'You have not yet created any menus. Displaying a list of your Pages'
);
return (
<div className="wp-block-navigation__menu-inspector-controls">
{ ! hasChildren && (
<p className="wp-block-navigation__menu-inspector-controls__empty-message">
{ __( 'This Navigation Menu is empty.' ) }
</p>
) }
<PrivateListView
rootClientId={ clientId }
isExpanded
description={ description }
showAppender
blockSettingsMenu={ LeafMoreMenu }
additionalBlockContent={ AdditionalBlockContent }
/>
</div>
);
};
const MenuInspectorControls = ( props ) => {
const {
createNavigationMenuIsSuccess,
createNavigationMenuIsError,
currentMenuId = null,
onCreateNew,
onSelectClassicMenu,
onSelectNavigationMenu,
isManageMenusButtonDisabled,
blockEditingMode,
} = props;
return (
<InspectorControls group="list">
<PanelBody title={ null }>
<HStack className="wp-block-navigation-off-canvas-editor__header">
<Heading
className="wp-block-navigation-off-canvas-editor__title"
level={ 2 }
>
{ __( 'Menu' ) }
</Heading>
{ blockEditingMode === 'default' && (
<NavigationMenuSelector
currentMenuId={ currentMenuId }
onSelectClassicMenu={ onSelectClassicMenu }
onSelectNavigationMenu={ onSelectNavigationMenu }
onCreateNew={ onCreateNew }
createNavigationMenuIsSuccess={
createNavigationMenuIsSuccess
}
createNavigationMenuIsError={
createNavigationMenuIsError
}
actionLabel={ actionLabel }
isManageMenusButtonDisabled={
isManageMenusButtonDisabled
}
/>
) }
</HStack>
<MainContent { ...props } />
</PanelBody>
</InspectorControls>
);
};
export default MenuInspectorControls;