@wordpress/block-library 6.0.9 → 6.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cover/edit.js +8 -3
- package/build/cover/edit.js.map +1 -1
- package/build/gallery/edit.js +1 -1
- package/build/gallery/edit.js.map +1 -1
- package/build/gallery/v1/update-gallery-modal.js +1 -1
- package/build/gallery/v1/update-gallery-modal.js.map +1 -1
- package/build/index.js +3 -2
- package/build/index.js.map +1 -1
- package/build/navigation/deprecated.js +118 -12
- package/build/navigation/deprecated.js.map +1 -1
- package/build/navigation/edit/index.js +45 -31
- package/build/navigation/edit/index.js.map +1 -1
- package/build/navigation/edit/navigation-menu-selector.js +2 -2
- package/build/navigation/edit/navigation-menu-selector.js.map +1 -1
- package/build/navigation/edit/placeholder/index.js +16 -48
- package/build/navigation/edit/placeholder/index.js.map +1 -1
- package/build/navigation/edit/unsaved-inner-blocks.js +4 -35
- package/build/navigation/edit/unsaved-inner-blocks.js.map +1 -1
- package/build/navigation/edit/use-create-navigation-menu.js +50 -0
- package/build/navigation/edit/use-create-navigation-menu.js.map +1 -0
- package/build/navigation/edit/use-generate-default-navigation-title.js +73 -0
- package/build/navigation/edit/use-generate-default-navigation-title.js.map +1 -0
- package/build/navigation/index.js +1 -1
- package/build/navigation/save.js +2 -2
- package/build/navigation/save.js.map +1 -1
- package/build/navigation/use-navigation-menu.js +19 -8
- package/build/navigation/use-navigation-menu.js.map +1 -1
- package/build/pattern/index.js +1 -1
- package/build/post-featured-image/edit.js +2 -3
- package/build/post-featured-image/edit.js.map +1 -1
- package/build/post-terms/index.js +1 -1
- package/build/post-title/edit.js +2 -2
- package/build/post-title/edit.js.map +1 -1
- package/build/query-title/index.js +1 -1
- package/build/site-logo/index.js +1 -1
- package/build/site-tagline/index.js +1 -1
- package/build/site-title/index.js +1 -1
- package/build/template-part/edit/placeholder/index.js +1 -1
- package/build/template-part/edit/placeholder/index.js.map +1 -1
- package/build/template-part/index.js +2 -1
- package/build/template-part/index.js.map +1 -1
- package/build-module/cover/edit.js +8 -3
- package/build-module/cover/edit.js.map +1 -1
- package/build-module/gallery/edit.js +1 -1
- package/build-module/gallery/edit.js.map +1 -1
- package/build-module/gallery/v1/update-gallery-modal.js +1 -1
- package/build-module/gallery/v1/update-gallery-modal.js.map +1 -1
- package/build-module/index.js +3 -2
- package/build-module/index.js.map +1 -1
- package/build-module/navigation/deprecated.js +118 -12
- package/build-module/navigation/deprecated.js.map +1 -1
- package/build-module/navigation/edit/index.js +46 -31
- package/build-module/navigation/edit/index.js.map +1 -1
- package/build-module/navigation/edit/navigation-menu-selector.js +2 -2
- package/build-module/navigation/edit/navigation-menu-selector.js.map +1 -1
- package/build-module/navigation/edit/placeholder/index.js +15 -45
- package/build-module/navigation/edit/placeholder/index.js.map +1 -1
- package/build-module/navigation/edit/unsaved-inner-blocks.js +6 -35
- package/build-module/navigation/edit/unsaved-inner-blocks.js.map +1 -1
- package/build-module/navigation/edit/use-create-navigation-menu.js +36 -0
- package/build-module/navigation/edit/use-create-navigation-menu.js.map +1 -0
- package/build-module/navigation/edit/use-generate-default-navigation-title.js +57 -0
- package/build-module/navigation/edit/use-generate-default-navigation-title.js.map +1 -0
- package/build-module/navigation/index.js +1 -1
- package/build-module/navigation/save.js +2 -2
- package/build-module/navigation/save.js.map +1 -1
- package/build-module/navigation/use-navigation-menu.js +19 -8
- package/build-module/navigation/use-navigation-menu.js.map +1 -1
- package/build-module/pattern/index.js +1 -1
- package/build-module/post-featured-image/edit.js +2 -3
- package/build-module/post-featured-image/edit.js.map +1 -1
- package/build-module/post-terms/index.js +1 -1
- package/build-module/post-title/edit.js +2 -2
- package/build-module/post-title/edit.js.map +1 -1
- package/build-module/query-title/index.js +1 -1
- package/build-module/site-logo/index.js +1 -1
- package/build-module/site-tagline/index.js +1 -1
- package/build-module/site-title/index.js +1 -1
- package/build-module/template-part/edit/placeholder/index.js +2 -2
- package/build-module/template-part/edit/placeholder/index.js.map +1 -1
- package/build-module/template-part/index.js +2 -1
- package/build-module/template-part/index.js.map +1 -1
- package/build-style/editor-rtl.css +5 -4
- package/build-style/editor.css +5 -4
- package/build-style/gallery/editor-rtl.css +1 -0
- package/build-style/gallery/editor.css +1 -0
- package/build-style/post-featured-image/editor-rtl.css +2 -2
- package/build-style/post-featured-image/editor.css +2 -2
- package/build-style/site-logo/editor-rtl.css +2 -2
- package/build-style/site-logo/editor.css +2 -2
- package/package.json +8 -8
- package/src/calendar/index.php +2 -2
- package/src/cover/edit.js +8 -1
- package/src/gallery/edit.js +1 -1
- package/src/gallery/editor.scss +1 -0
- package/src/gallery/v1/update-gallery-modal.js +1 -1
- package/src/index.js +27 -25
- package/src/navigation/block.json +1 -1
- package/src/navigation/deprecated.js +105 -4
- package/src/navigation/edit/index.js +43 -36
- package/src/navigation/edit/navigation-menu-selector.js +2 -2
- package/src/navigation/edit/placeholder/index.js +16 -62
- package/src/navigation/edit/unsaved-inner-blocks.js +6 -57
- package/src/navigation/edit/use-create-navigation-menu.js +39 -0
- package/src/navigation/edit/use-generate-default-navigation-title.js +79 -0
- package/src/navigation/index.php +128 -14
- package/src/navigation/save.js +2 -2
- package/src/navigation/use-navigation-menu.js +20 -9
- package/src/page-list/index.php +5 -0
- package/src/pattern/block.json +1 -1
- package/src/post-featured-image/edit.js +2 -5
- package/src/post-featured-image/editor.scss +2 -2
- package/src/post-terms/block.json +1 -1
- package/src/post-title/edit.js +2 -2
- package/src/query-title/block.json +1 -1
- package/src/site-logo/block.json +1 -1
- package/src/site-logo/editor.scss +2 -2
- package/src/site-tagline/block.json +1 -1
- package/src/site-title/block.json +1 -1
- package/src/template-part/block.json +2 -1
- package/src/template-part/edit/placeholder/index.js +2 -2
- package/src/template-part/index.php +2 -1
- package/build/navigation/edit/navigation-menu-name-modal.js +0 -55
- package/build/navigation/edit/navigation-menu-name-modal.js.map +0 -1
- package/build/navigation/edit/navigation-menu-publish-button.js +0 -53
- package/build/navigation/edit/navigation-menu-publish-button.js.map +0 -1
- package/build-module/navigation/edit/navigation-menu-name-modal.js +0 -47
- package/build-module/navigation/edit/navigation-menu-name-modal.js.map +0 -1
- package/build-module/navigation/edit/navigation-menu-publish-button.js +0 -40
- package/build-module/navigation/edit/navigation-menu-publish-button.js.map +0 -1
- package/src/navigation/edit/navigation-menu-name-modal.js +0 -69
- package/src/navigation/edit/navigation-menu-publish-button.js +0 -57
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
|
|
35
35
|
ToolbarGroup,
|
|
36
36
|
ToolbarDropdownMenu,
|
|
37
|
+
Button,
|
|
37
38
|
} from '@wordpress/components';
|
|
38
39
|
import { __ } from '@wordpress/i18n';
|
|
39
40
|
|
|
@@ -48,7 +49,6 @@ import ResponsiveWrapper from './responsive-wrapper';
|
|
|
48
49
|
import NavigationInnerBlocks from './inner-blocks';
|
|
49
50
|
import NavigationMenuSelector from './navigation-menu-selector';
|
|
50
51
|
import NavigationMenuNameControl from './navigation-menu-name-control';
|
|
51
|
-
import NavigationMenuPublishButton from './navigation-menu-publish-button';
|
|
52
52
|
import UnsavedInnerBlocks from './unsaved-inner-blocks';
|
|
53
53
|
import NavigationMenuDeleteControl from './navigation-menu-delete-control';
|
|
54
54
|
|
|
@@ -118,13 +118,11 @@ function Navigation( {
|
|
|
118
118
|
|
|
119
119
|
const navigationAreaMenu = areaMenu === 0 ? undefined : areaMenu;
|
|
120
120
|
|
|
121
|
-
const
|
|
122
|
-
? navigationAreaMenu
|
|
123
|
-
: attributes.navigationMenuId;
|
|
121
|
+
const ref = navigationArea ? navigationAreaMenu : attributes.ref;
|
|
124
122
|
|
|
125
|
-
const
|
|
123
|
+
const setRef = useCallback(
|
|
126
124
|
( postId ) => {
|
|
127
|
-
setAttributes( {
|
|
125
|
+
setAttributes( { ref: postId } );
|
|
128
126
|
if ( navigationArea ) {
|
|
129
127
|
setAreaMenu( postId );
|
|
130
128
|
}
|
|
@@ -133,7 +131,7 @@ function Navigation( {
|
|
|
133
131
|
);
|
|
134
132
|
|
|
135
133
|
const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
|
|
136
|
-
`navigationMenu/${
|
|
134
|
+
`navigationMenu/${ ref }`
|
|
137
135
|
);
|
|
138
136
|
|
|
139
137
|
const { innerBlocks, isInnerBlockSelected } = useSelect(
|
|
@@ -160,7 +158,7 @@ function Navigation( {
|
|
|
160
158
|
setHasSavedUnsavedInnerBlocks,
|
|
161
159
|
] = useState( false );
|
|
162
160
|
|
|
163
|
-
const isWithinUnassignedArea = navigationArea && !
|
|
161
|
+
const isWithinUnassignedArea = navigationArea && ! ref;
|
|
164
162
|
|
|
165
163
|
const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
|
|
166
164
|
! hasExistingNavItems || isWithinUnassignedArea
|
|
@@ -177,7 +175,7 @@ function Navigation( {
|
|
|
177
175
|
hasResolvedNavigationMenus,
|
|
178
176
|
navigationMenus,
|
|
179
177
|
navigationMenu,
|
|
180
|
-
} = useNavigationMenu(
|
|
178
|
+
} = useNavigationMenu( ref );
|
|
181
179
|
|
|
182
180
|
const navRef = useRef();
|
|
183
181
|
const isDraftNavigationMenu = navigationMenu?.status === 'draft';
|
|
@@ -281,6 +279,27 @@ function Navigation( {
|
|
|
281
279
|
setIsPlaceholderShown( ! isEntityAvailable );
|
|
282
280
|
}, [ isEntityAvailable ] );
|
|
283
281
|
|
|
282
|
+
// If the ref no longer exists the reset the inner blocks
|
|
283
|
+
// to provide a clean slate.
|
|
284
|
+
useEffect( () => {
|
|
285
|
+
if ( ref === undefined && innerBlocks.length > 0 ) {
|
|
286
|
+
replaceInnerBlocks( clientId, [] );
|
|
287
|
+
}
|
|
288
|
+
// innerBlocks are intentionally not listed as deps. This function is only concerned
|
|
289
|
+
// with the snapshot from the time when ref became undefined.
|
|
290
|
+
}, [ clientId, ref, innerBlocks ] );
|
|
291
|
+
|
|
292
|
+
const startWithEmptyMenu = useCallback( () => {
|
|
293
|
+
if ( navigationArea ) {
|
|
294
|
+
setAreaMenu( 0 );
|
|
295
|
+
}
|
|
296
|
+
setAttributes( {
|
|
297
|
+
ref: undefined,
|
|
298
|
+
} );
|
|
299
|
+
|
|
300
|
+
setIsPlaceholderShown( true );
|
|
301
|
+
}, [ clientId ] );
|
|
302
|
+
|
|
284
303
|
// If the block has inner blocks, but no menu id, this was an older
|
|
285
304
|
// navigation block added before the block used a wp_navigation entity.
|
|
286
305
|
// Either this block was saved in the content or inserted by a pattern.
|
|
@@ -300,7 +319,7 @@ function Navigation( {
|
|
|
300
319
|
onSave={ ( post ) => {
|
|
301
320
|
setHasSavedUnsavedInnerBlocks( true );
|
|
302
321
|
// Switch to using the wp_navigation entity.
|
|
303
|
-
|
|
322
|
+
setRef( post.id );
|
|
304
323
|
} }
|
|
305
324
|
/>
|
|
306
325
|
);
|
|
@@ -308,13 +327,16 @@ function Navigation( {
|
|
|
308
327
|
|
|
309
328
|
// Show a warning if the selected menu is no longer available.
|
|
310
329
|
// TODO - the user should be able to select a new one?
|
|
311
|
-
if (
|
|
330
|
+
if ( ref && isNavigationMenuMissing ) {
|
|
312
331
|
return (
|
|
313
332
|
<div { ...blockProps }>
|
|
314
333
|
<Warning>
|
|
315
334
|
{ __(
|
|
316
|
-
'Navigation menu has been deleted or is unavailable'
|
|
335
|
+
'Navigation menu has been deleted or is unavailable. '
|
|
317
336
|
) }
|
|
337
|
+
<Button onClick={ startWithEmptyMenu } variant="link">
|
|
338
|
+
{ __( 'Create a new menu?' ) }
|
|
339
|
+
</Button>
|
|
318
340
|
</Warning>
|
|
319
341
|
</div>
|
|
320
342
|
);
|
|
@@ -335,11 +357,7 @@ function Navigation( {
|
|
|
335
357
|
: Placeholder;
|
|
336
358
|
|
|
337
359
|
return (
|
|
338
|
-
<EntityProvider
|
|
339
|
-
kind="postType"
|
|
340
|
-
type="wp_navigation"
|
|
341
|
-
id={ navigationMenuId }
|
|
342
|
-
>
|
|
360
|
+
<EntityProvider kind="postType" type="wp_navigation" id={ ref }>
|
|
343
361
|
<RecursionProvider>
|
|
344
362
|
<BlockControls>
|
|
345
363
|
{ ! isDraftNavigationMenu && isEntityAvailable && (
|
|
@@ -352,29 +370,16 @@ function Navigation( {
|
|
|
352
370
|
{ ( { onClose } ) => (
|
|
353
371
|
<NavigationMenuSelector
|
|
354
372
|
onSelect={ ( { id } ) => {
|
|
355
|
-
|
|
373
|
+
setRef( id );
|
|
356
374
|
onClose();
|
|
357
375
|
} }
|
|
358
|
-
onCreateNew={
|
|
359
|
-
if ( navigationArea ) {
|
|
360
|
-
setAreaMenu( 0 );
|
|
361
|
-
}
|
|
362
|
-
setAttributes( {
|
|
363
|
-
navigationMenuId: undefined,
|
|
364
|
-
} );
|
|
365
|
-
setIsPlaceholderShown( true );
|
|
366
|
-
} }
|
|
376
|
+
onCreateNew={ startWithEmptyMenu }
|
|
367
377
|
/>
|
|
368
378
|
) }
|
|
369
379
|
</ToolbarDropdownMenu>
|
|
370
380
|
</ToolbarGroup>
|
|
371
381
|
) }
|
|
372
382
|
<ToolbarGroup>{ listViewToolbarButton }</ToolbarGroup>
|
|
373
|
-
{ isDraftNavigationMenu && (
|
|
374
|
-
<ToolbarGroup>
|
|
375
|
-
<NavigationMenuPublishButton />
|
|
376
|
-
</ToolbarGroup>
|
|
377
|
-
) }
|
|
378
383
|
</BlockControls>
|
|
379
384
|
{ listViewModal }
|
|
380
385
|
<InspectorControls>
|
|
@@ -481,12 +486,11 @@ function Navigation( {
|
|
|
481
486
|
<NavigationMenuNameControl />
|
|
482
487
|
<NavigationMenuDeleteControl
|
|
483
488
|
onDelete={ () => {
|
|
484
|
-
replaceInnerBlocks( clientId, [] );
|
|
485
489
|
if ( navigationArea ) {
|
|
486
490
|
setAreaMenu( 0 );
|
|
487
491
|
}
|
|
488
492
|
setAttributes( {
|
|
489
|
-
|
|
493
|
+
ref: undefined,
|
|
490
494
|
} );
|
|
491
495
|
setIsPlaceholderShown( true );
|
|
492
496
|
} }
|
|
@@ -494,17 +498,20 @@ function Navigation( {
|
|
|
494
498
|
</InspectorControls>
|
|
495
499
|
) }
|
|
496
500
|
<nav { ...blockProps }>
|
|
497
|
-
{
|
|
501
|
+
{ isPlaceholderShown && (
|
|
498
502
|
<PlaceholderComponent
|
|
499
503
|
onFinish={ ( post ) => {
|
|
500
504
|
setIsPlaceholderShown( false );
|
|
501
|
-
|
|
505
|
+
if ( post ) {
|
|
506
|
+
setRef( post.id );
|
|
507
|
+
}
|
|
502
508
|
selectBlock( clientId );
|
|
503
509
|
} }
|
|
504
510
|
canSwitchNavigationMenu={ canSwitchNavigationMenu }
|
|
505
511
|
hasResolvedNavigationMenus={
|
|
506
512
|
hasResolvedNavigationMenus
|
|
507
513
|
}
|
|
514
|
+
clientId={ clientId }
|
|
508
515
|
/>
|
|
509
516
|
) }
|
|
510
517
|
{ ! isEntityAvailable && ! isPlaceholderShown && (
|
|
@@ -13,13 +13,13 @@ import useNavigationMenu from '../use-navigation-menu';
|
|
|
13
13
|
|
|
14
14
|
export default function NavigationMenuSelector( { onSelect, onCreateNew } ) {
|
|
15
15
|
const { navigationMenus } = useNavigationMenu();
|
|
16
|
-
const
|
|
16
|
+
const ref = useEntityId( 'postType', 'wp_navigation' );
|
|
17
17
|
|
|
18
18
|
return (
|
|
19
19
|
<>
|
|
20
20
|
<MenuGroup>
|
|
21
21
|
<MenuItemsChoice
|
|
22
|
-
value={
|
|
22
|
+
value={ ref }
|
|
23
23
|
onSelect={ ( selectedId ) =>
|
|
24
24
|
onSelect(
|
|
25
25
|
navigationMenus.find(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { createBlock } from '@wordpress/blocks';
|
|
5
5
|
import {
|
|
6
6
|
Placeholder,
|
|
7
7
|
Button,
|
|
@@ -9,8 +9,6 @@ import {
|
|
|
9
9
|
MenuGroup,
|
|
10
10
|
MenuItem,
|
|
11
11
|
} from '@wordpress/components';
|
|
12
|
-
import { store as coreStore } from '@wordpress/core-data';
|
|
13
|
-
import { useDispatch } from '@wordpress/data';
|
|
14
12
|
import { useCallback, useState, useEffect } from '@wordpress/element';
|
|
15
13
|
import { __ } from '@wordpress/i18n';
|
|
16
14
|
import { navigation, Icon } from '@wordpress/icons';
|
|
@@ -23,8 +21,8 @@ import { decodeEntities } from '@wordpress/html-entities';
|
|
|
23
21
|
import useNavigationEntities from '../../use-navigation-entities';
|
|
24
22
|
import PlaceholderPreview from './placeholder-preview';
|
|
25
23
|
import menuItemsToBlocks from '../../menu-items-to-blocks';
|
|
26
|
-
import NavigationMenuNameModal from '../navigation-menu-name-modal';
|
|
27
24
|
import useNavigationMenu from '../../use-navigation-menu';
|
|
25
|
+
import useCreateNavigationMenu from '../use-create-navigation-menu';
|
|
28
26
|
|
|
29
27
|
const ExistingMenusDropdown = ( {
|
|
30
28
|
canSwitchNavigationMenu,
|
|
@@ -90,51 +88,25 @@ const ExistingMenusDropdown = ( {
|
|
|
90
88
|
};
|
|
91
89
|
|
|
92
90
|
export default function NavigationPlaceholder( {
|
|
91
|
+
clientId,
|
|
93
92
|
onFinish,
|
|
94
93
|
canSwitchNavigationMenu,
|
|
95
94
|
hasResolvedNavigationMenus,
|
|
96
95
|
} ) {
|
|
97
96
|
const [ selectedMenu, setSelectedMenu ] = useState();
|
|
98
|
-
|
|
99
97
|
const [ isCreatingFromMenu, setIsCreatingFromMenu ] = useState( false );
|
|
100
|
-
|
|
101
98
|
const [ menuName, setMenuName ] = useState( '' );
|
|
99
|
+
const createNavigationMenu = useCreateNavigationMenu( clientId );
|
|
102
100
|
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const [ createEmpty, setCreateEmpty ] = useState( false );
|
|
108
|
-
|
|
109
|
-
const { saveEntityRecord } = useDispatch( coreStore );
|
|
110
|
-
|
|
111
|
-
// This callback uses data from the two placeholder steps and only creates
|
|
112
|
-
// a new navigation menu when the user completes the final step.
|
|
113
|
-
const createNavigationMenu = useCallback(
|
|
114
|
-
async ( title = __( 'Untitled Navigation Menu' ), blocks = [] ) => {
|
|
115
|
-
const record = {
|
|
116
|
-
title,
|
|
117
|
-
content: serialize( blocks ),
|
|
118
|
-
status: 'publish',
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const navigationMenu = await saveEntityRecord(
|
|
122
|
-
'postType',
|
|
123
|
-
'wp_navigation',
|
|
124
|
-
record
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
return navigationMenu;
|
|
128
|
-
},
|
|
129
|
-
[ serialize, saveEntityRecord ]
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
const onFinishMenuCreation = async ( navigationMenuTitle, blocks ) => {
|
|
101
|
+
const onFinishMenuCreation = async (
|
|
102
|
+
blocks,
|
|
103
|
+
navigationMenuTitle = null
|
|
104
|
+
) => {
|
|
133
105
|
const navigationMenu = await createNavigationMenu(
|
|
134
106
|
navigationMenuTitle,
|
|
135
107
|
blocks
|
|
136
108
|
);
|
|
137
|
-
onFinish( navigationMenu );
|
|
109
|
+
onFinish( navigationMenu, blocks );
|
|
138
110
|
};
|
|
139
111
|
|
|
140
112
|
const {
|
|
@@ -152,7 +124,7 @@ export default function NavigationPlaceholder( {
|
|
|
152
124
|
const createFromMenu = useCallback(
|
|
153
125
|
( name ) => {
|
|
154
126
|
const { innerBlocks: blocks } = menuItemsToBlocks( menuItems );
|
|
155
|
-
onFinishMenuCreation(
|
|
127
|
+
onFinishMenuCreation( blocks, name );
|
|
156
128
|
},
|
|
157
129
|
[ menuItems, menuItemsToBlocks, onFinish ]
|
|
158
130
|
);
|
|
@@ -170,14 +142,13 @@ export default function NavigationPlaceholder( {
|
|
|
170
142
|
setMenuName( name );
|
|
171
143
|
};
|
|
172
144
|
|
|
173
|
-
const onCreateEmptyMenu = (
|
|
174
|
-
onFinishMenuCreation(
|
|
145
|
+
const onCreateEmptyMenu = () => {
|
|
146
|
+
onFinishMenuCreation( [] );
|
|
175
147
|
};
|
|
176
148
|
|
|
177
|
-
const onCreateAllPages = (
|
|
149
|
+
const onCreateAllPages = () => {
|
|
178
150
|
const block = [ createBlock( 'core/page-list' ) ];
|
|
179
|
-
onFinishMenuCreation(
|
|
180
|
-
setIsNewMenuModalVisible( true );
|
|
151
|
+
onFinishMenuCreation( block );
|
|
181
152
|
};
|
|
182
153
|
|
|
183
154
|
useEffect( () => {
|
|
@@ -225,10 +196,7 @@ export default function NavigationPlaceholder( {
|
|
|
225
196
|
<>
|
|
226
197
|
<Button
|
|
227
198
|
variant="tertiary"
|
|
228
|
-
onClick={
|
|
229
|
-
setIsNewMenuModalVisible( true );
|
|
230
|
-
setCreateEmpty( false );
|
|
231
|
-
} }
|
|
199
|
+
onClick={ onCreateAllPages }
|
|
232
200
|
>
|
|
233
201
|
{ __( 'Add all pages' ) }
|
|
234
202
|
</Button>
|
|
@@ -237,10 +205,7 @@ export default function NavigationPlaceholder( {
|
|
|
237
205
|
) : undefined }
|
|
238
206
|
<Button
|
|
239
207
|
variant="tertiary"
|
|
240
|
-
onClick={
|
|
241
|
-
setIsNewMenuModalVisible( true );
|
|
242
|
-
setCreateEmpty( true );
|
|
243
|
-
} }
|
|
208
|
+
onClick={ onCreateEmptyMenu }
|
|
244
209
|
>
|
|
245
210
|
{ __( 'Start empty' ) }
|
|
246
211
|
</Button>
|
|
@@ -248,17 +213,6 @@ export default function NavigationPlaceholder( {
|
|
|
248
213
|
</div>
|
|
249
214
|
</Placeholder>
|
|
250
215
|
) }
|
|
251
|
-
{ isNewMenuModalVisible && (
|
|
252
|
-
<NavigationMenuNameModal
|
|
253
|
-
title={ __( 'Create your new navigation menu' ) }
|
|
254
|
-
onRequestClose={ () => {
|
|
255
|
-
setIsNewMenuModalVisible( false );
|
|
256
|
-
} }
|
|
257
|
-
onFinish={
|
|
258
|
-
createEmpty ? onCreateEmptyMenu : onCreateAllPages
|
|
259
|
-
}
|
|
260
|
-
/>
|
|
261
|
-
) }
|
|
262
216
|
</>
|
|
263
217
|
);
|
|
264
218
|
}
|
|
@@ -7,18 +7,16 @@ import classnames from 'classnames';
|
|
|
7
7
|
* WordPress dependencies
|
|
8
8
|
*/
|
|
9
9
|
import { useInnerBlocksProps } from '@wordpress/block-editor';
|
|
10
|
-
import { serialize } from '@wordpress/blocks';
|
|
11
10
|
import { Disabled, Spinner } from '@wordpress/components';
|
|
12
11
|
import { store as coreStore } from '@wordpress/core-data';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import { __, sprintf } from '@wordpress/i18n';
|
|
12
|
+
import { useSelect } from '@wordpress/data';
|
|
13
|
+
import { useContext, useEffect, useRef } from '@wordpress/element';
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
16
|
* Internal dependencies
|
|
19
17
|
*/
|
|
20
18
|
import useNavigationMenu from '../use-navigation-menu';
|
|
21
|
-
import
|
|
19
|
+
import useCreateNavigationMenu from './use-create-navigation-menu';
|
|
22
20
|
|
|
23
21
|
const NOOP = () => {};
|
|
24
22
|
const EMPTY_OBJECT = {};
|
|
@@ -51,7 +49,6 @@ export default function UnsavedInnerBlocks( {
|
|
|
51
49
|
onChange: NOOP,
|
|
52
50
|
onInput: NOOP,
|
|
53
51
|
} );
|
|
54
|
-
const { saveEntityRecord } = useDispatch( coreStore );
|
|
55
52
|
|
|
56
53
|
const {
|
|
57
54
|
isSaving,
|
|
@@ -83,29 +80,7 @@ export default function UnsavedInnerBlocks( {
|
|
|
83
80
|
|
|
84
81
|
const { hasResolvedNavigationMenus, navigationMenus } = useNavigationMenu();
|
|
85
82
|
|
|
86
|
-
const createNavigationMenu =
|
|
87
|
-
async ( title ) => {
|
|
88
|
-
const record = {
|
|
89
|
-
title,
|
|
90
|
-
content: serialize( blocks ),
|
|
91
|
-
status: 'draft',
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const navigationMenu = await saveEntityRecord(
|
|
95
|
-
'postType',
|
|
96
|
-
'wp_navigation',
|
|
97
|
-
record
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
return navigationMenu;
|
|
101
|
-
},
|
|
102
|
-
[ blocks, serialize, saveEntityRecord ]
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
// Because we can't conditionally call hooks, pass an undefined client id
|
|
106
|
-
// arg to bypass the expensive `useTemplateArea` code. The hook will return
|
|
107
|
-
// early.
|
|
108
|
-
const area = useTemplatePartAreaLabel( isDisabled ? undefined : clientId );
|
|
83
|
+
const createNavigationMenu = useCreateNavigationMenu( clientId );
|
|
109
84
|
|
|
110
85
|
// Automatically save the uncontrolled blocks.
|
|
111
86
|
useEffect( async () => {
|
|
@@ -134,33 +109,7 @@ export default function UnsavedInnerBlocks( {
|
|
|
134
109
|
}
|
|
135
110
|
|
|
136
111
|
savingLock.current = true;
|
|
137
|
-
const
|
|
138
|
-
? sprintf(
|
|
139
|
-
// translators: %s: the name of a menu (e.g. Header navigation).
|
|
140
|
-
__( '%s navigation' ),
|
|
141
|
-
area
|
|
142
|
-
)
|
|
143
|
-
: // translators: 'navigation' as in website navigation.
|
|
144
|
-
__( 'Navigation' );
|
|
145
|
-
|
|
146
|
-
// Determine how many menus start with the automatic title.
|
|
147
|
-
const matchingMenuTitleCount = [
|
|
148
|
-
...draftNavigationMenus,
|
|
149
|
-
...navigationMenus,
|
|
150
|
-
].reduce(
|
|
151
|
-
( count, menu ) =>
|
|
152
|
-
menu?.title?.raw?.startsWith( title ) ? count + 1 : count,
|
|
153
|
-
0
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
// Append a number to the end of the title if a menu with
|
|
157
|
-
// the same name exists.
|
|
158
|
-
const titleWithCount =
|
|
159
|
-
matchingMenuTitleCount > 0
|
|
160
|
-
? `${ title } ${ matchingMenuTitleCount + 1 }`
|
|
161
|
-
: title;
|
|
162
|
-
|
|
163
|
-
const menu = await createNavigationMenu( titleWithCount );
|
|
112
|
+
const menu = await createNavigationMenu( null, blocks );
|
|
164
113
|
onSave( menu );
|
|
165
114
|
savingLock.current = false;
|
|
166
115
|
}, [
|
|
@@ -172,7 +121,7 @@ export default function UnsavedInnerBlocks( {
|
|
|
172
121
|
navigationMenus,
|
|
173
122
|
hasSelection,
|
|
174
123
|
createNavigationMenu,
|
|
175
|
-
|
|
124
|
+
blocks,
|
|
176
125
|
] );
|
|
177
126
|
|
|
178
127
|
return (
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { serialize } from '@wordpress/blocks';
|
|
5
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
6
|
+
import { useDispatch } from '@wordpress/data';
|
|
7
|
+
import { useCallback } from '@wordpress/element';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Internal dependencies
|
|
11
|
+
*/
|
|
12
|
+
import useGenerateDefaultNavigationTitle from './use-generate-default-navigation-title';
|
|
13
|
+
|
|
14
|
+
export default function useCreateNavigationMenu( clientId ) {
|
|
15
|
+
const { saveEntityRecord } = useDispatch( coreStore );
|
|
16
|
+
const generateDefaultTitle = useGenerateDefaultNavigationTitle( clientId );
|
|
17
|
+
|
|
18
|
+
// This callback uses data from the two placeholder steps and only creates
|
|
19
|
+
// a new navigation menu when the user completes the final step.
|
|
20
|
+
return useCallback(
|
|
21
|
+
async ( title = null, blocks = [] ) => {
|
|
22
|
+
if ( ! title ) {
|
|
23
|
+
title = await generateDefaultTitle();
|
|
24
|
+
}
|
|
25
|
+
const record = {
|
|
26
|
+
title,
|
|
27
|
+
content: serialize( blocks ),
|
|
28
|
+
status: 'publish',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
return await saveEntityRecord(
|
|
32
|
+
'postType',
|
|
33
|
+
'wp_navigation',
|
|
34
|
+
record
|
|
35
|
+
);
|
|
36
|
+
},
|
|
37
|
+
[ serialize, saveEntityRecord ]
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { Disabled } from '@wordpress/components';
|
|
5
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
6
|
+
import { useRegistry } from '@wordpress/data';
|
|
7
|
+
import { useContext, useCallback } from '@wordpress/element';
|
|
8
|
+
import { __, sprintf } from '@wordpress/i18n';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Internal dependencies
|
|
12
|
+
*/
|
|
13
|
+
import useTemplatePartAreaLabel from '../use-template-part-area-label';
|
|
14
|
+
|
|
15
|
+
const DRAFT_MENU_PARAMS = [
|
|
16
|
+
'postType',
|
|
17
|
+
'wp_navigation',
|
|
18
|
+
{ status: 'draft', per_page: -1 },
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const PUBLISHED_MENU_PARAMS = [
|
|
22
|
+
'postType',
|
|
23
|
+
'wp_navigation',
|
|
24
|
+
{ per_page: -1, status: 'publish' },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
export default function useGenerateDefaultNavigationTitle( clientId ) {
|
|
28
|
+
// The block will be disabled in a block preview, use this as a way of
|
|
29
|
+
// avoiding the side-effects of this component for block previews.
|
|
30
|
+
const isDisabled = useContext( Disabled.Context );
|
|
31
|
+
|
|
32
|
+
// Because we can't conditionally call hooks, pass an undefined client id
|
|
33
|
+
// arg to bypass the expensive `useTemplateArea` code. The hook will return
|
|
34
|
+
// early.
|
|
35
|
+
const area = useTemplatePartAreaLabel( isDisabled ? undefined : clientId );
|
|
36
|
+
|
|
37
|
+
const registry = useRegistry();
|
|
38
|
+
return useCallback( async () => {
|
|
39
|
+
// Ensure other navigation menus have loaded so an
|
|
40
|
+
// accurate name can be created.
|
|
41
|
+
if ( isDisabled ) {
|
|
42
|
+
return '';
|
|
43
|
+
}
|
|
44
|
+
const { getEntityRecords } = registry.resolveSelect( coreStore );
|
|
45
|
+
|
|
46
|
+
const [ draftNavigationMenus, navigationMenus ] = await Promise.all( [
|
|
47
|
+
getEntityRecords( ...DRAFT_MENU_PARAMS ),
|
|
48
|
+
getEntityRecords( ...PUBLISHED_MENU_PARAMS ),
|
|
49
|
+
] );
|
|
50
|
+
|
|
51
|
+
const title = area
|
|
52
|
+
? sprintf(
|
|
53
|
+
// translators: %s: the name of a menu (e.g. Header navigation).
|
|
54
|
+
__( '%s navigation' ),
|
|
55
|
+
area
|
|
56
|
+
)
|
|
57
|
+
: // translators: 'navigation' as in website navigation.
|
|
58
|
+
__( 'Navigation' );
|
|
59
|
+
|
|
60
|
+
// Determine how many menus start with the automatic title.
|
|
61
|
+
const matchingMenuTitleCount = [
|
|
62
|
+
...draftNavigationMenus,
|
|
63
|
+
...navigationMenus,
|
|
64
|
+
].reduce(
|
|
65
|
+
( count, menu ) =>
|
|
66
|
+
menu?.title?.raw?.startsWith( title ) ? count + 1 : count,
|
|
67
|
+
0
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// Append a number to the end of the title if a menu with
|
|
71
|
+
// the same name exists.
|
|
72
|
+
const titleWithCount =
|
|
73
|
+
matchingMenuTitleCount > 0
|
|
74
|
+
? `${ title } ${ matchingMenuTitleCount + 1 }`
|
|
75
|
+
: title;
|
|
76
|
+
|
|
77
|
+
return titleWithCount || '';
|
|
78
|
+
}, [ isDisabled, area ] );
|
|
79
|
+
}
|