-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Page List: Respect the selected parent page when converting to a list of navigation links #47651
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,14 @@ import { createBlock } from '@wordpress/blocks'; | |
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
export function convertToNavigationLinks( pages = [] ) { | ||
/** | ||
* Converts an array of pages into a nested array of navigation link blocks. | ||
* | ||
* @param {Array} pages An array of pages. | ||
* | ||
* @return {Array} A nested array of navigation link blocks. | ||
*/ | ||
function createNavigationLinks( pages = [] ) { | ||
const linkMap = {}; | ||
const navigationLinks = []; | ||
pages.forEach( ( { id, title, link: url, type, parent } ) => { | ||
|
@@ -30,11 +37,61 @@ export function convertToNavigationLinks( pages = [] ) { | |
// Use a placeholder if the child appears before parent in list. | ||
linkMap[ parent ] = { innerBlocks: [] }; | ||
} | ||
// Although these variables are not referenced, they are needed to store the innerBlocks in memory. | ||
const parentLinkInnerBlocks = linkMap[ parent ].innerBlocks; | ||
parentLinkInnerBlocks.push( linkMap[ id ] ); | ||
} | ||
} ); | ||
|
||
return navigationLinks; | ||
} | ||
|
||
/** | ||
* Finds a navigation link block by id, recursively. | ||
* It might be possible to make this a more generic helper function. | ||
* | ||
* @param {Array} navigationLinks An array of navigation link blocks. | ||
* @param {number} id The id of the navigation link to find. | ||
* | ||
* @return {Object|null} The navigation link block with the given id. | ||
*/ | ||
function findNavigationLinkById( navigationLinks, id ) { | ||
for ( const navigationLink of navigationLinks ) { | ||
// Is this the link we're looking for? | ||
if ( navigationLink.attributes.id === id ) { | ||
return navigationLink; | ||
} | ||
|
||
// If not does it have innerBlocks? | ||
if ( navigationLink.innerBlocks && navigationLink.innerBlocks.length ) { | ||
const foundNavigationLink = findNavigationLinkById( | ||
navigationLink.innerBlocks, | ||
id | ||
); | ||
|
||
if ( foundNavigationLink ) { | ||
return foundNavigationLink; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function convertToNavigationLinks( pages = [], parentPageID = null ) { | ||
let navigationLinks = createNavigationLinks( pages ); | ||
|
||
// If a parent page ID is provided, only return the children of that page. | ||
if ( parentPageID ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I wonder if we should not instead filter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that, but it's technically more complex as |
||
const parentPage = findNavigationLinkById( | ||
navigationLinks, | ||
parentPageID | ||
); | ||
if ( parentPage && parentPage.innerBlocks ) { | ||
navigationLinks = parentPage.innerBlocks; | ||
} | ||
} | ||
|
||
// Transform all links with innerBlocks into Submenus. This can't be done | ||
// sooner because page objects have no information on their children. | ||
const transformSubmenus = ( listOfLinks ) => { | ||
|
@@ -53,11 +110,14 @@ export function convertToNavigationLinks( pages = [] ) { | |
}; | ||
|
||
transformSubmenus( navigationLinks ); | ||
|
||
return navigationLinks; | ||
} | ||
|
||
export function useConvertToNavigationLinks( { clientId, pages } ) { | ||
export function useConvertToNavigationLinks( { | ||
clientId, | ||
pages, | ||
parentPageID, | ||
} ) { | ||
const { replaceBlock, selectBlock } = useDispatch( blockEditorStore ); | ||
|
||
const { parentNavBlockClientId } = useSelect( | ||
|
@@ -79,7 +139,7 @@ export function useConvertToNavigationLinks( { clientId, pages } ) { | |
); | ||
|
||
return () => { | ||
const navigationLinks = convertToNavigationLinks( pages ); | ||
const navigationLinks = convertToNavigationLinks( pages, parentPageID ); | ||
|
||
// Replace the Page List block with the Navigation Links. | ||
replaceBlock( clientId, navigationLinks ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand: how are they useful if nobody reads them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't 100% get it either. I think it means they are stored in memory, so that they don't get lost when a mutation happens. This is old code that might be possible to write in a clearer way, but I think that's outside of the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you remove these lines it breaks :)