Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to open the editor on the selected post on navigation. #47387

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const BLOCK_LIST_ITEM_HEIGHT = 36;
* @param {boolean} props.showBlockMovers Flag to enable block movers
* @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
* @param {Object} props.LeafMoreMenu Optional more menu substitution.
* @param {string} props.description Optional accessible description for the tree grid component.
* @param {string} props.description Optional accessible description for the tree grid component.
* @param {string} props.onSelect Optional callback to be invoked when a block is selected.
* @param {Object} ref Forwarded ref
*/
function OffCanvasEditor(
Expand All @@ -71,9 +72,11 @@ function OffCanvasEditor(
isExpanded = false,
LeafMoreMenu,
description = __( 'Block navigation structure' ),
onSelect,
},
ref
) {
const { getBlock } = useSelect( blockEditorStore );
const { clientIdsTree, draggedClientIds, selectedClientIds } =
useListViewClientIds( blocks );

Expand Down Expand Up @@ -113,8 +116,11 @@ function OffCanvasEditor(
( event, blockClientId ) => {
updateBlockSelection( event, blockClientId );
setSelectedTreeId( blockClientId );
if ( onSelect ) {
onSelect( getBlock( blockClientId ) );
}
},
[ setSelectedTreeId, updateBlockSelection ]
[ setSelectedTreeId, updateBlockSelection, onSelect, getBlock ]
);
useEffect( () => {
isMounted.current = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import NavigationMenu from './navigation-menu';

const NAVIGATION_MENUS_QUERY = [ { per_page: -1, status: 'publish' } ];

export default function NavigationInspector() {
export default function NavigationInspector( { onSelect } ) {
const {
selectedNavigationBlockId,
clientIdToRef,
Expand Down Expand Up @@ -206,7 +206,10 @@ export default function NavigationInspector() {
onChange={ onChange }
onInput={ onInput }
>
<NavigationMenu innerBlocks={ publishedInnerBlocks } />
<NavigationMenu
innerBlocks={ publishedInnerBlocks }
onSelect={ onSelect }
/>
</BlockEditorProvider>
) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ALLOWED_BLOCKS = {
],
};

export default function NavigationMenu( { innerBlocks } ) {
export default function NavigationMenu( { innerBlocks, onSelect } ) {
const { updateBlockListSettings } = useDispatch( blockEditorStore );

const { OffCanvasEditor } = unlock( blockEditorExperiments );
Expand All @@ -56,5 +56,5 @@ export default function NavigationMenu( { innerBlocks } ) {
} );
}, [ updateBlockListSettings, innerBlocks ] );

return <OffCanvasEditor blocks={ innerBlocks } />;
return <OffCanvasEditor blocks={ innerBlocks } onSelect={ onSelect } />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import NavigationInspector from '../navigation-inspector';
import { useHistory } from '../routes';

export default function SidebarNavigationScreenNavigationMenus() {
const history = useHistory();
const onSelect = useCallback(
( selectedBlock ) => {
const { attributes } = selectedBlock;
if (
attributes.kind === 'post-type' &&
attributes.id &&
attributes.type &&
history
) {
history.push( {
postType: attributes.type,
postId: attributes.id,
} );
}
},
[ history ]
);
return (
<SidebarNavigationScreen
path="/navigation"
parentTitle={ __( 'Design' ) }
title={ __( 'Navigation' ) }
content={
<div className="edit-site-sidebar-navigation-screen-navigation-menus">
<NavigationInspector />
<NavigationInspector onSelect={ onSelect } />
</div>
}
/>
Expand Down