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

Page List: Render the children correctly in the editor #46165

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Changes from 1 commit
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
59 changes: 40 additions & 19 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import {
InspectorControls,
BlockControls,
useBlockProps,
useInnerBlocksProps,
getColorClassName,
store as blockEditorStore,
Warning,
} from '@wordpress/block-editor';
import {
PanelBody,
Expand All @@ -20,9 +23,10 @@ import {
Notice,
ComboboxControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useMemo, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { useMemo, useState, useEffect } from '@wordpress/element';
import { useEntityRecords } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -40,7 +44,10 @@ export default function PageListEdit( {
setAttributes,
} ) {
const { parentPageID } = attributes;
const [ pages ] = useGetPages();
const { pagesByParentId, totalPages, hasResolvedPages } = usePageData();
const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const isNavigationChild = 'showSubmenuIcon' in context;
const allowConvertToLinks =
Expand All @@ -64,14 +71,14 @@ export default function PageListEdit( {
style: { ...context.style?.color },
} );

const makeBlockTemplate = ( parentId = 0 ) => {
const pages = pagesByParentId.get( parentId );
const getBlockList = ( parentId = parentPageID ) => {
const childPages = pagesByParentId.get( parentId );

if ( ! pages?.length ) {
if ( ! childPages?.length ) {
return [];
}

return pages.reduce( ( template, page ) => {
return childPages.reduce( ( template, page ) => {
const hasChildren = pagesByParentId.has( page.id );
const pageProps = {
id: page.id,
Expand All @@ -81,21 +88,17 @@ export default function PageListEdit( {
hasChildren,
};
let item = null;
const children = makeBlockTemplate( page.id );
item = [ 'core/page-list-item', pageProps, children ];

const children = getBlockList( page.id );
item = createBlock( 'core/page-list-item', pageProps, children );
template.push( item );

return template;
}, [] );
};

const pagesTemplate = useMemo( makeBlockTemplate, [ pagesByParentId ] );
const blockList = getBlockList();
draganescu marked this conversation as resolved.
Show resolved Hide resolved

const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: pagesTemplate,
templateLock: 'all',
} );
const innerBlocksProps = useInnerBlocksProps( blockProps );

const getBlockContent = () => {
if ( ! hasResolvedPages ) {
Expand Down Expand Up @@ -126,13 +129,28 @@ export default function PageListEdit( {
);
}

if ( blockList.length === 0 ) {
const parentPageDetails =
pages && pages.find( ( page ) => page.id === parentPageID );
return (
<div { ...blockProps }>
<Warning>
{ sprintf(
// translators: %s: Page title.
__( '"%s" page has no children.' ),
parentPageDetails.title.rendered
) }
</Warning>
</div>
);
}

if ( totalPages > 0 ) {
return <ul { ...innerBlocksProps }></ul>;
}
};

const useParentOptions = () => {
const [ pages ] = useGetPages();
return pages?.reduce( ( accumulator, page ) => {
accumulator.push( {
value: page.id,
Expand All @@ -142,6 +160,13 @@ export default function PageListEdit( {
}, [] );
};

useEffect( () => {
__unstableMarkNextChangeAsNotPersistent();
if ( blockList ) {
replaceInnerBlocks( clientId, blockList );
}
}, [ clientId, parentPageID, hasResolvedPages ] );
draganescu marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
<InspectorControls>
Expand Down Expand Up @@ -203,10 +228,6 @@ function usePageData( pageId = 0 ) {
// 'orderby', this can be removed.
// https://core.trac.wordpress.org/ticket/39037

if ( pageId !== 0 ) {
return pages.find( ( page ) => page.id === pageId );
}

const sortedPages = [ ...( pages ?? [] ) ].sort( ( a, b ) => {
if ( a.menu_order === b.menu_order ) {
return a.title.rendered.localeCompare( b.title.rendered );
Expand Down