Skip to content

Commit

Permalink
Fix: Make navigation page list load its items on navigation sidebar.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Feb 16, 2023
1 parent 11525f7 commit 5320c77
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,7 @@ export default function NavigationInspector( { onSelect } ) {
onChange={ onChange }
onInput={ onInput }
>
<NavigationMenu
innerBlocks={ publishedInnerBlocks }
onSelect={ onSelect }
/>
<NavigationMenu onSelect={ onSelect } />
</BlockEditorProvider>
) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import {
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
BlockList,
BlockTools,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,9 +36,18 @@ const ALLOWED_BLOCKS = {
'core/navigation-link',
'core/navigation-submenu',
],
'core/page-list': [ 'core/page-list-item' ],
};

export default function NavigationMenu( { innerBlocks, onSelect } ) {
export default function NavigationMenu( { onSelect } ) {
const { clientIdsTree, innerBlocks } = useSelect( ( select ) => {
const { __unstableGetClientIdsTree, getBlocks } =
select( blockEditorStore );
return {
clientIdsTree: __unstableGetClientIdsTree(),
innerBlocks: getBlocks(),
};
} );
const { updateBlockListSettings } = useDispatch( blockEditorStore );

const { OffCanvasEditor, LeafMoreMenu } = unlock( blockEditorPrivateApis );
Expand All @@ -56,11 +67,20 @@ export default function NavigationMenu( { innerBlocks, onSelect } ) {
} );
}, [ updateBlockListSettings, innerBlocks ] );

// The hidden block is needed because it makes block edit side effects trigger.
// For example a navigation page list load its items has an effect on edit to load its items.
return (
<OffCanvasEditor
blocks={ innerBlocks }
onSelect={ onSelect }
LeafMoreMenu={ LeafMoreMenu }
/>
<>
<OffCanvasEditor
blocks={ clientIdsTree }
onSelect={ onSelect }
LeafMoreMenu={ LeafMoreMenu }
/>
<div style={ { display: 'none' } }>
<BlockTools>
<BlockList />
</BlockTools>
</div>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function SidebarNavigationScreenNavigationMenus() {
const history = useHistory();
const onSelect = useCallback(
( selectedBlock ) => {
const { attributes } = selectedBlock;
const { attributes, name } = selectedBlock;
if (
attributes.kind === 'post-type' &&
attributes.id &&
Expand All @@ -27,6 +27,13 @@ export default function SidebarNavigationScreenNavigationMenus() {
postId: attributes.id,
} );
}
if ( name === 'core/page-list-item' && attributes.id && history ) {
history.push( {
postType: 'page',
postId: attributes.id,
path: '/navigation/single',
} );
}
},
[ history ]
);
Expand Down

0 comments on commit 5320c77

Please sign in to comment.