diff --git a/packages/block-editor/src/components/off-canvas-editor/block-edit-button.js b/packages/block-editor/src/components/off-canvas-editor/block-edit-button.js deleted file mode 100644 index 6281134e8c95e..0000000000000 --- a/packages/block-editor/src/components/off-canvas-editor/block-edit-button.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * WordPress dependencies - */ -import { edit } from '@wordpress/icons'; -import { Button } from '@wordpress/components'; -import { useDispatch } from '@wordpress/data'; -import { forwardRef } from '@wordpress/element'; - -/** - * Internal dependencies - */ -import { store as blockEditorStore } from '../../store'; - -export default forwardRef( function BlockEditButton( - { clientId, ...props }, - ref -) { - const { selectBlock } = useDispatch( blockEditorStore ); - - const onClick = () => { - selectBlock( clientId ); - }; - - return ( - + + + + ); +}; diff --git a/packages/block-editor/src/components/off-canvas-editor/use-page-data.js b/packages/block-editor/src/components/off-canvas-editor/use-page-data.js new file mode 100644 index 0000000000000..df868cd6d212d --- /dev/null +++ b/packages/block-editor/src/components/off-canvas-editor/use-page-data.js @@ -0,0 +1,57 @@ +/** + * WordPress dependencies + */ +import { useSelect } from '@wordpress/data'; +import { useMemo } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import { store as blockEditorStore } from '../../store'; + +// copied from packages/block-library/src/page-list/edit.js + +export default () => { + // 1. Grab editor settings + // 2. Call the selector when we need it + const { pages } = useSelect( ( select ) => { + const { getSettings } = select( blockEditorStore ); + + return { + pages: getSettings().__experimentalFetchPageEntities( { + orderby: 'menu_order', + order: 'asc', + _fields: [ 'id', 'link', 'parent', 'title', 'menu_order' ], + per_page: -1, + context: 'view', + } ), + }; + }, [] ); + + return useMemo( () => { + // TODO: Once the REST API supports passing multiple values to + // 'orderby', this can be removed. + // https://core.trac.wordpress.org/ticket/39037 + const sortedPages = [ ...( pages ?? [] ) ].sort( ( a, b ) => { + if ( a.menu_order === b.menu_order ) { + return a.title.rendered.localeCompare( b.title.rendered ); + } + return a.menu_order - b.menu_order; + } ); + const pagesByParentId = sortedPages.reduce( ( accumulator, page ) => { + const { parent } = page; + if ( accumulator.has( parent ) ) { + accumulator.get( parent ).push( page ); + } else { + accumulator.set( parent, [ page ] ); + } + return accumulator; + }, new Map() ); + + return { + pages, // necessary for access outside the hook + pagesByParentId, + totalPages: pages?.length ?? null, + }; + }, [ pages ] ); +}; diff --git a/packages/edit-site/src/components/block-editor/index.js b/packages/edit-site/src/components/block-editor/index.js index f990ee7f15f69..e17d81bb3e7e9 100644 --- a/packages/edit-site/src/components/block-editor/index.js +++ b/packages/edit-site/src/components/block-editor/index.js @@ -126,6 +126,16 @@ export default function BlockEditor( { setIsInserterOpen } ) { [ settingsBlockPatternCategories, restBlockPatternCategories ] ); + const { fetchPagesEntities } = useSelect( ( select ) => { + const { getEntityRecords } = select( coreStore ); + + return { + fetchPagesEntities: ( options = {} ) => { + return getEntityRecords( 'postType', 'page', options ); + }, + }; + }, [] ); + const settings = useMemo( () => { const { __experimentalAdditionalBlockPatterns, @@ -138,8 +148,14 @@ export default function BlockEditor( { setIsInserterOpen } ) { __unstableFetchMedia: fetchMedia, __experimentalBlockPatterns: blockPatterns, __experimentalBlockPatternCategories: blockPatternCategories, + __experimentalFetchPageEntities: fetchPagesEntities, }; - }, [ storedSettings, blockPatterns, blockPatternCategories ] ); + }, [ + storedSettings, + blockPatterns, + blockPatternCategories, + fetchPagesEntities, + ] ); const [ blocks, onInput, onChange ] = useEntityBlockEditor( 'postType', diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index 3f4ed5a15fe37..b77bf34c3e119 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -113,6 +113,16 @@ function useBlockEditorSettings( settings, hasTemplate ) { const { saveEntityRecord } = useDispatch( coreStore ); + const { fetchPagesEntities } = useSelect( ( select ) => { + const { getEntityRecords } = select( coreStore ); + + return { + fetchPagesEntities: ( options = {} ) => { + return getEntityRecords( 'postType', 'page', options ); + }, + }; + }, [] ); + /** * Creates a Post entity. * This is utilised by the Link UI to allow for on-the-fly creation of Posts/Pages. @@ -176,6 +186,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { 'supportsLayout', 'widgetTypesToHideFromLegacyWidgetBlock', '__unstableResolvedAssets', + '__experimentalFetchPageEntities', ].includes( key ) ) ), @@ -196,6 +207,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { __experimentalUserCanCreatePages: userCanCreatePages, pageOnFront, __experimentalPreferPatternsOnRoot: hasTemplate, + __experimentalFetchPageEntities: fetchPagesEntities, } ), [ settings, @@ -208,6 +220,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { hasTemplate, userCanCreatePages, pageOnFront, + fetchPagesEntities, ] ); }