forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract nav editor component in Nav in Browse mode (WordPress#51436)
* Fix path bug * Extract component
- Loading branch information
1 parent
f2a28d7
commit 95965fd
Showing
2 changed files
with
96 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...t-site/src/components/sidebar-navigation-screen-navigation-menu/navigation-menu-editor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useMemo } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { BlockEditorProvider } from '@wordpress/block-editor'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
import { store as editSiteStore } from '../../store'; | ||
import { | ||
isPreviewingTheme, | ||
currentlyPreviewingTheme, | ||
} from '../../utils/is-previewing-theme'; | ||
import NavigationMenuContent from '../sidebar-navigation-screen-navigation-menus/navigation-menu-content'; | ||
import { noop } from '.'; | ||
|
||
const { useHistory } = unlock( routerPrivateApis ); | ||
|
||
export default function NavigationMenuEditor( { navigationMenu } ) { | ||
const history = useHistory(); | ||
|
||
const onSelect = useCallback( | ||
( selectedBlock ) => { | ||
const { attributes, name } = selectedBlock; | ||
if ( | ||
attributes.kind === 'post-type' && | ||
attributes.id && | ||
attributes.type && | ||
history | ||
) { | ||
history.push( { | ||
postType: attributes.type, | ||
postId: attributes.id, | ||
...( isPreviewingTheme() && { | ||
gutenberg_theme_preview: currentlyPreviewingTheme(), | ||
} ), | ||
} ); | ||
} | ||
if ( name === 'core/page-list-item' && attributes.id && history ) { | ||
history.push( { | ||
postType: 'page', | ||
postId: attributes.id, | ||
...( isPreviewingTheme() && { | ||
gutenberg_theme_preview: currentlyPreviewingTheme(), | ||
} ), | ||
} ); | ||
} | ||
}, | ||
[ history ] | ||
); | ||
|
||
const { storedSettings } = useSelect( ( select ) => { | ||
const { getSettings } = unlock( select( editSiteStore ) ); | ||
|
||
return { | ||
storedSettings: getSettings( false ), | ||
}; | ||
}, [] ); | ||
|
||
const blocks = useMemo( () => { | ||
if ( ! navigationMenu ) { | ||
return []; | ||
} | ||
|
||
return [ | ||
createBlock( 'core/navigation', { ref: navigationMenu?.id } ), | ||
]; | ||
}, [ navigationMenu ] ); | ||
|
||
if ( ! navigationMenu || ! blocks?.length ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockEditorProvider | ||
settings={ storedSettings } | ||
value={ blocks } | ||
onChange={ noop } | ||
onInput={ noop } | ||
> | ||
<div className="edit-site-sidebar-navigation-screen-navigation-menus__content"> | ||
<NavigationMenuContent | ||
rootClientId={ blocks[ 0 ].clientId } | ||
onSelect={ onSelect } | ||
/> | ||
</div> | ||
</BlockEditorProvider> | ||
); | ||
} |