Skip to content

Commit

Permalink
Extract nav editor component in Nav in Browse mode (WordPress#51436)
Browse files Browse the repository at this point in the history
* Fix path bug

* Extract component
  • Loading branch information
getdave authored and sethrubenstein committed Jul 13, 2023
1 parent f2a28d7 commit 95965fd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,19 @@ import {
Spinner,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useCallback, useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { BlockEditorProvider } from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities';

import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import {
isPreviewingTheme,
currentlyPreviewingTheme,
} from '../../utils/is-previewing-theme';
import { SidebarNavigationScreenWrapper } from '../sidebar-navigation-screen-navigation-menus';
import NavigationMenuContent from '../sidebar-navigation-screen-navigation-menus/navigation-menu-content';
import ScreenNavigationMoreMenu from './more-menu';
import NavigationMenuEditor from './navigation-menu-editor';

const { useHistory } = unlock( routerPrivateApis );
const noop = () => {};
export const noop = () => {};

export default function SidebarNavigationScreenNavigationMenu() {
const {
Expand Down Expand Up @@ -244,75 +233,3 @@ export default function SidebarNavigationScreenNavigationMenu() {
</SidebarNavigationScreenWrapper>
);
}

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 ( ! NavigationMenuEditor ) {
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>
);
}
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>
);
}

0 comments on commit 95965fd

Please sign in to comment.