@wordpress/block-library 6.0.8 → 6.0.12

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.
Files changed (214) hide show
  1. package/build/cover/edit.js +9 -3
  2. package/build/cover/edit.js.map +1 -1
  3. package/build/gallery/deprecated.js +314 -26
  4. package/build/gallery/deprecated.js.map +1 -1
  5. package/build/gallery/edit-wrapper.js +10 -27
  6. package/build/gallery/edit-wrapper.js.map +1 -1
  7. package/build/gallery/edit.js +11 -13
  8. package/build/gallery/edit.js.map +1 -1
  9. package/build/gallery/save.js +3 -3
  10. package/build/gallery/save.js.map +1 -1
  11. package/build/gallery/shared.js +24 -0
  12. package/build/gallery/shared.js.map +1 -1
  13. package/build/gallery/transforms.js +8 -22
  14. package/build/gallery/transforms.js.map +1 -1
  15. package/build/gallery/use-mobile-warning.js +1 -1
  16. package/build/gallery/use-mobile-warning.js.map +1 -1
  17. package/build/gallery/v1/edit.js +2 -21
  18. package/build/gallery/v1/edit.js.map +1 -1
  19. package/build/index.js +4 -3
  20. package/build/index.js.map +1 -1
  21. package/build/navigation/deprecated.js +127 -19
  22. package/build/navigation/deprecated.js.map +1 -1
  23. package/build/navigation/edit/index.js +46 -31
  24. package/build/navigation/edit/index.js.map +1 -1
  25. package/build/navigation/edit/inner-blocks.js +0 -4
  26. package/build/navigation/edit/inner-blocks.js.map +1 -1
  27. package/build/navigation/edit/navigation-menu-selector.js +2 -2
  28. package/build/navigation/edit/navigation-menu-selector.js.map +1 -1
  29. package/build/navigation/edit/placeholder/index.js +16 -48
  30. package/build/navigation/edit/placeholder/index.js.map +1 -1
  31. package/build/navigation/edit/unsaved-inner-blocks.js +4 -35
  32. package/build/navigation/edit/unsaved-inner-blocks.js.map +1 -1
  33. package/build/navigation/edit/use-create-navigation-menu.js +50 -0
  34. package/build/navigation/edit/use-create-navigation-menu.js.map +1 -0
  35. package/build/navigation/edit/use-generate-default-navigation-title.js +73 -0
  36. package/build/navigation/edit/use-generate-default-navigation-title.js.map +1 -0
  37. package/build/navigation/index.js +1 -1
  38. package/build/navigation/save.js +2 -2
  39. package/build/navigation/save.js.map +1 -1
  40. package/build/navigation/use-navigation-menu.js +19 -8
  41. package/build/navigation/use-navigation-menu.js.map +1 -1
  42. package/build/navigation-area/edit.js +5 -0
  43. package/build/navigation-area/edit.js.map +1 -1
  44. package/build/navigation-submenu/edit.js +4 -0
  45. package/build/navigation-submenu/edit.js.map +1 -1
  46. package/build/pattern/index.js +1 -1
  47. package/build/post-featured-image/edit.js +2 -3
  48. package/build/post-featured-image/edit.js.map +1 -1
  49. package/build/post-terms/index.js +1 -1
  50. package/build/post-title/edit.js +2 -2
  51. package/build/post-title/edit.js.map +1 -1
  52. package/build/query-pagination/index.js +1 -1
  53. package/build/query-pagination-next/index.js +2 -2
  54. package/build/query-pagination-numbers/index.js +1 -1
  55. package/build/query-pagination-previous/index.js +2 -2
  56. package/build/query-title/index.js +1 -1
  57. package/build/separator/separator-settings.js +1 -0
  58. package/build/separator/separator-settings.js.map +1 -1
  59. package/build/site-logo/index.js +1 -1
  60. package/build/site-tagline/index.js +1 -1
  61. package/build/site-title/index.js +1 -1
  62. package/build/social-links/edit.js +1 -0
  63. package/build/social-links/edit.js.map +1 -1
  64. package/build/template-part/edit/placeholder/index.js +1 -1
  65. package/build/template-part/edit/placeholder/index.js.map +1 -1
  66. package/build/template-part/index.js +2 -1
  67. package/build/template-part/index.js.map +1 -1
  68. package/build-module/cover/edit.js +9 -3
  69. package/build-module/cover/edit.js.map +1 -1
  70. package/build-module/gallery/deprecated.js +309 -27
  71. package/build-module/gallery/deprecated.js.map +1 -1
  72. package/build-module/gallery/edit-wrapper.js +7 -27
  73. package/build-module/gallery/edit-wrapper.js.map +1 -1
  74. package/build-module/gallery/edit.js +11 -13
  75. package/build-module/gallery/edit.js.map +1 -1
  76. package/build-module/gallery/save.js +2 -3
  77. package/build-module/gallery/save.js.map +1 -1
  78. package/build-module/gallery/shared.js +22 -0
  79. package/build-module/gallery/shared.js.map +1 -1
  80. package/build-module/gallery/transforms.js +9 -21
  81. package/build-module/gallery/transforms.js.map +1 -1
  82. package/build-module/gallery/use-mobile-warning.js +1 -1
  83. package/build-module/gallery/use-mobile-warning.js.map +1 -1
  84. package/build-module/gallery/v1/edit.js +4 -22
  85. package/build-module/gallery/v1/edit.js.map +1 -1
  86. package/build-module/index.js +4 -3
  87. package/build-module/index.js.map +1 -1
  88. package/build-module/navigation/deprecated.js +127 -19
  89. package/build-module/navigation/deprecated.js.map +1 -1
  90. package/build-module/navigation/edit/index.js +47 -31
  91. package/build-module/navigation/edit/index.js.map +1 -1
  92. package/build-module/navigation/edit/inner-blocks.js +0 -4
  93. package/build-module/navigation/edit/inner-blocks.js.map +1 -1
  94. package/build-module/navigation/edit/navigation-menu-selector.js +2 -2
  95. package/build-module/navigation/edit/navigation-menu-selector.js.map +1 -1
  96. package/build-module/navigation/edit/placeholder/index.js +15 -45
  97. package/build-module/navigation/edit/placeholder/index.js.map +1 -1
  98. package/build-module/navigation/edit/unsaved-inner-blocks.js +6 -35
  99. package/build-module/navigation/edit/unsaved-inner-blocks.js.map +1 -1
  100. package/build-module/navigation/edit/use-create-navigation-menu.js +36 -0
  101. package/build-module/navigation/edit/use-create-navigation-menu.js.map +1 -0
  102. package/build-module/navigation/edit/use-generate-default-navigation-title.js +57 -0
  103. package/build-module/navigation/edit/use-generate-default-navigation-title.js.map +1 -0
  104. package/build-module/navigation/index.js +1 -1
  105. package/build-module/navigation/save.js +2 -2
  106. package/build-module/navigation/save.js.map +1 -1
  107. package/build-module/navigation/use-navigation-menu.js +19 -8
  108. package/build-module/navigation/use-navigation-menu.js.map +1 -1
  109. package/build-module/navigation-area/edit.js +4 -0
  110. package/build-module/navigation-area/edit.js.map +1 -1
  111. package/build-module/navigation-submenu/edit.js +4 -0
  112. package/build-module/navigation-submenu/edit.js.map +1 -1
  113. package/build-module/pattern/index.js +1 -1
  114. package/build-module/post-featured-image/edit.js +2 -3
  115. package/build-module/post-featured-image/edit.js.map +1 -1
  116. package/build-module/post-terms/index.js +1 -1
  117. package/build-module/post-title/edit.js +2 -2
  118. package/build-module/post-title/edit.js.map +1 -1
  119. package/build-module/query-pagination/index.js +1 -1
  120. package/build-module/query-pagination-next/index.js +2 -2
  121. package/build-module/query-pagination-numbers/index.js +1 -1
  122. package/build-module/query-pagination-previous/index.js +2 -2
  123. package/build-module/query-title/index.js +1 -1
  124. package/build-module/separator/separator-settings.js +1 -0
  125. package/build-module/separator/separator-settings.js.map +1 -1
  126. package/build-module/site-logo/index.js +1 -1
  127. package/build-module/site-tagline/index.js +1 -1
  128. package/build-module/site-title/index.js +1 -1
  129. package/build-module/social-links/edit.js +1 -0
  130. package/build-module/social-links/edit.js.map +1 -1
  131. package/build-module/template-part/edit/placeholder/index.js +2 -2
  132. package/build-module/template-part/edit/placeholder/index.js.map +1 -1
  133. package/build-module/template-part/index.js +2 -1
  134. package/build-module/template-part/index.js.map +1 -1
  135. package/build-style/columns/editor-rtl.css +1 -1
  136. package/build-style/columns/editor.css +1 -1
  137. package/build-style/editor-rtl.css +6 -5
  138. package/build-style/editor.css +6 -5
  139. package/build-style/gallery/editor-rtl.css +1 -0
  140. package/build-style/gallery/editor.css +1 -0
  141. package/build-style/navigation/style-rtl.css +12 -3
  142. package/build-style/navigation/style.css +12 -3
  143. package/build-style/post-featured-image/editor-rtl.css +2 -2
  144. package/build-style/post-featured-image/editor.css +2 -2
  145. package/build-style/site-logo/editor-rtl.css +2 -2
  146. package/build-style/site-logo/editor.css +2 -2
  147. package/build-style/style-rtl.css +12 -3
  148. package/build-style/style.css +12 -3
  149. package/package.json +9 -9
  150. package/src/calendar/index.php +2 -2
  151. package/src/columns/editor.scss +3 -2
  152. package/src/cover/edit.js +9 -1
  153. package/src/gallery/deprecated.js +831 -559
  154. package/src/gallery/edit-wrapper.js +7 -27
  155. package/src/gallery/edit.js +11 -13
  156. package/src/gallery/editor.scss +1 -0
  157. package/src/gallery/save.js +2 -1
  158. package/src/gallery/shared.js +24 -0
  159. package/src/gallery/transforms.js +9 -27
  160. package/src/gallery/use-mobile-warning.js +1 -1
  161. package/src/gallery/v1/edit.js +1 -28
  162. package/src/index.js +28 -26
  163. package/src/navigation/block.json +1 -1
  164. package/src/navigation/deprecated.js +115 -13
  165. package/src/navigation/edit/index.js +44 -36
  166. package/src/navigation/edit/inner-blocks.js +0 -5
  167. package/src/navigation/edit/navigation-menu-selector.js +2 -2
  168. package/src/navigation/edit/placeholder/index.js +16 -62
  169. package/src/navigation/edit/unsaved-inner-blocks.js +6 -57
  170. package/src/navigation/edit/use-create-navigation-menu.js +39 -0
  171. package/src/navigation/edit/use-generate-default-navigation-title.js +79 -0
  172. package/src/navigation/index.php +263 -26
  173. package/src/navigation/save.js +2 -2
  174. package/src/navigation/style.scss +15 -4
  175. package/src/navigation/use-navigation-menu.js +20 -9
  176. package/src/navigation-area/edit.js +5 -0
  177. package/src/navigation-area/index.php +1 -0
  178. package/src/navigation-submenu/edit.js +6 -0
  179. package/src/navigation-submenu/index.php +30 -45
  180. package/src/page-list/index.php +14 -13
  181. package/src/pattern/block.json +1 -1
  182. package/src/post-featured-image/edit.js +2 -5
  183. package/src/post-featured-image/editor.scss +2 -2
  184. package/src/post-terms/block.json +1 -1
  185. package/src/post-title/edit.js +2 -2
  186. package/src/query-pagination/block.json +1 -1
  187. package/src/query-pagination-next/block.json +2 -2
  188. package/src/query-pagination-numbers/block.json +1 -1
  189. package/src/query-pagination-previous/block.json +2 -2
  190. package/src/query-title/block.json +1 -1
  191. package/src/separator/separator-settings.js +1 -0
  192. package/src/site-logo/block.json +1 -1
  193. package/src/site-logo/editor.scss +2 -2
  194. package/src/site-tagline/block.json +1 -1
  195. package/src/site-title/block.json +1 -1
  196. package/src/social-links/edit.js +1 -0
  197. package/src/template-part/block.json +2 -1
  198. package/src/template-part/edit/placeholder/index.js +2 -2
  199. package/src/template-part/index.php +38 -1
  200. package/build/gallery/v1/update-gallery-modal.js +0 -114
  201. package/build/gallery/v1/update-gallery-modal.js.map +0 -1
  202. package/build/navigation/edit/navigation-menu-name-modal.js +0 -55
  203. package/build/navigation/edit/navigation-menu-name-modal.js.map +0 -1
  204. package/build/navigation/edit/navigation-menu-publish-button.js +0 -53
  205. package/build/navigation/edit/navigation-menu-publish-button.js.map +0 -1
  206. package/build-module/gallery/v1/update-gallery-modal.js +0 -97
  207. package/build-module/gallery/v1/update-gallery-modal.js.map +0 -1
  208. package/build-module/navigation/edit/navigation-menu-name-modal.js +0 -47
  209. package/build-module/navigation/edit/navigation-menu-name-modal.js.map +0 -1
  210. package/build-module/navigation/edit/navigation-menu-publish-button.js +0 -40
  211. package/build-module/navigation/edit/navigation-menu-publish-button.js.map +0 -1
  212. package/src/gallery/v1/update-gallery-modal.js +0 -97
  213. package/src/navigation/edit/navigation-menu-name-modal.js +0 -69
  214. 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 navigationMenuId = navigationArea
122
- ? navigationAreaMenu
123
- : attributes.navigationMenuId;
121
+ const ref = navigationArea ? navigationAreaMenu : attributes.ref;
124
122
 
125
- const setNavigationMenuId = useCallback(
123
+ const setRef = useCallback(
126
124
  ( postId ) => {
127
- setAttributes( { navigationMenuId: postId } );
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/${ navigationMenuId }`
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 && ! navigationMenuId;
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( navigationMenuId );
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
- setNavigationMenuId( post.id );
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 ( navigationMenuId && isNavigationMenuMissing ) {
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
- setNavigationMenuId( id );
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>
@@ -432,6 +437,7 @@ function Navigation( {
432
437
  { hasColorSettings && (
433
438
  <PanelColorSettings
434
439
  __experimentalHasMultipleOrigins
440
+ __experimentalIsRenderedInSidebar
435
441
  title={ __( 'Color' ) }
436
442
  initialOpen={ false }
437
443
  colorSettings={ [
@@ -481,12 +487,11 @@ function Navigation( {
481
487
  <NavigationMenuNameControl />
482
488
  <NavigationMenuDeleteControl
483
489
  onDelete={ () => {
484
- replaceInnerBlocks( clientId, [] );
485
490
  if ( navigationArea ) {
486
491
  setAreaMenu( 0 );
487
492
  }
488
493
  setAttributes( {
489
- navigationMenuId: undefined,
494
+ ref: undefined,
490
495
  } );
491
496
  setIsPlaceholderShown( true );
492
497
  } }
@@ -494,17 +499,20 @@ function Navigation( {
494
499
  </InspectorControls>
495
500
  ) }
496
501
  <nav { ...blockProps }>
497
- { ! isEntityAvailable && isPlaceholderShown && (
502
+ { isPlaceholderShown && (
498
503
  <PlaceholderComponent
499
504
  onFinish={ ( post ) => {
500
505
  setIsPlaceholderShown( false );
501
- setNavigationMenuId( post.id );
506
+ if ( post ) {
507
+ setRef( post.id );
508
+ }
502
509
  selectBlock( clientId );
503
510
  } }
504
511
  canSwitchNavigationMenu={ canSwitchNavigationMenu }
505
512
  hasResolvedNavigationMenus={
506
513
  hasResolvedNavigationMenus
507
514
  }
515
+ clientId={ clientId }
508
516
  />
509
517
  ) }
510
518
  { ! isEntityAvailable && ! isPlaceholderShown && (
@@ -110,11 +110,6 @@ export default function NavigationInnerBlocks( {
110
110
  __experimentalDirectInsert: shouldDirectInsert,
111
111
  orientation,
112
112
  renderAppender: CustomAppender || appender,
113
-
114
- // Ensure block toolbar is not too far removed from item
115
- // being edited when in vertical mode.
116
- // see: https://github.com/WordPress/gutenberg/pull/34615.
117
- __experimentalCaptureToolbars: orientation !== 'vertical',
118
113
  // Template lock set to false here so that the Nav
119
114
  // Block on the experimental menus screen does not
120
115
  // inherit templateLock={ 'all' }.
@@ -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 navigationMenuId = useEntityId( 'postType', 'wp_navigation' );
16
+ const ref = useEntityId( 'postType', 'wp_navigation' );
17
17
 
18
18
  return (
19
19
  <>
20
20
  <MenuGroup>
21
21
  <MenuItemsChoice
22
- value={ navigationMenuId }
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 { serialize, createBlock } from '@wordpress/blocks';
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 [ isNewMenuModalVisible, setIsNewMenuModalVisible ] = useState(
104
- false
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( name, blocks );
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 = ( name ) => {
174
- onFinishMenuCreation( name, [] );
145
+ const onCreateEmptyMenu = () => {
146
+ onFinishMenuCreation( [] );
175
147
  };
176
148
 
177
- const onCreateAllPages = ( name ) => {
149
+ const onCreateAllPages = () => {
178
150
  const block = [ createBlock( 'core/page-list' ) ];
179
- onFinishMenuCreation( name, block );
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 { useDispatch, useSelect } from '@wordpress/data';
14
- import { useCallback, useContext, useEffect, useRef } from '@wordpress/element';
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 useTemplatePartAreaLabel from '../use-template-part-area-label';
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 = useCallback(
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 title = area
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
- area,
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
+ }