diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index b80703dcc67b18..9f328c955a5891 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -374,6 +374,21 @@ _Returns_ - `any`: The entity record's save error. +### getLinkSuggestions + +Fetch link suggestions for a given search term. + +_Parameters_ + +- _state_ Data state. +- _search_ Search term. +- _searchOptions_ +- _settings_ + +_Returns_ + +- Link suggestions. + ### getRawEntityRecord Returns the entity's record object by key, with its attributes mapped to their raw values. diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 6677a32df08dc9..78606839a5c298 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -695,6 +695,21 @@ _Returns_ - `any`: The entity record's save error. +### getLinkSuggestions + +Fetch link suggestions for a given search term. + +_Parameters_ + +- _state_ Data state. +- _search_ Search term. +- _searchOptions_ +- _settings_ + +_Returns_ + +- Link suggestions. + ### getRawEntityRecord Returns the entity's record object by key, with its attributes mapped to their raw values. diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 8e6be425244687..45be3f1f262629 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -623,6 +623,18 @@ export function defaultTemplates( state = {}, action ) { return state; } +export function linkSuggestions( state = {}, action ) { + switch ( action.type ) { + case 'RECEIVE_LINK_SUGGESTIONS': + return { + ...state, + [ action.search ]: action.suggestions, + }; + } + + return state; +} + export default combineReducers( { terms, users, @@ -644,4 +656,5 @@ export default combineReducers( { userPatternCategories, navigationFallbackId, defaultTemplates, + linkSuggestions, } ); diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 807005ec4a6e8d..ea5def4d46a447 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -17,6 +17,7 @@ import { STORE_NAME } from './name'; import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities'; import { forwardResolver, getNormalizedCommaSeparable } from './utils'; import { getSyncProvider } from './sync'; +import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from './fetch'; /** * Requests authors from the REST API. @@ -668,6 +669,17 @@ export const getUserPatternCategories = } ); }; +export const getLinkSuggestions = + ( search, searchOptions, settings ) => + async ( { dispatch } ) => { + const suggestions = await fetchLinkSuggestions( + search, + searchOptions, + settings + ); + dispatch( { type: 'RECEIVE_LINK_SUGGESTIONS', search, suggestions } ); + }; + export const getNavigationFallbackId = () => async ( { dispatch, select } ) => { diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 5751a80b9106cd..e439f58e23dcf2 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -1491,3 +1491,21 @@ export const getRevision = createSelector( ]; } ); + +/** + * Fetch link suggestions for a given search term. + * + * @param state Data state. + * @param search Search term. + * @param searchOptions + * @param settings + * @return Link suggestions. + */ +export function getLinkSuggestions( + state, + search = '', + searchOptions, + settings +) { + return state.linkSuggestions[ search ]; +} 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 9d60ef9cc6c487..68e104e3037336 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -2,10 +2,9 @@ * WordPress dependencies */ import { Platform, useMemo, useCallback } from '@wordpress/element'; -import { useDispatch, useSelect } from '@wordpress/data'; +import { useDispatch, useSelect, resolveSelect } from '@wordpress/data'; import { store as coreStore, - __experimentalFetchLinkSuggestions as fetchLinkSuggestions, __experimentalFetchUrlData as fetchUrlData, } from '@wordpress/core-data'; import { __ } from '@wordpress/i18n'; @@ -215,6 +214,10 @@ function useBlockEditorSettings( settings, postType, postId ) { [ saveEntityRecord, userCanCreatePages ] ); + const syncGetLinkSuggestions = useCallback( async ( ...args ) => { + return await resolveSelect( coreStore ).getLinkSuggestions( ...args ); + }, [] ); + const forceDisableFocusMode = settings.focusMode === false; return useMemo( @@ -232,8 +235,7 @@ function useBlockEditorSettings( settings, postType, postId ) { __experimentalBlockPatterns: blockPatterns, __experimentalBlockPatternCategories: blockPatternCategories, __experimentalUserPatternCategories: userPatternCategories, - __experimentalFetchLinkSuggestions: ( search, searchOptions ) => - fetchLinkSuggestions( search, searchOptions, settings ), + __experimentalFetchLinkSuggestions: syncGetLinkSuggestions, inserterMediaCategories, __experimentalFetchRichUrlData: fetchUrlData, // Todo: This only checks the top level post, not the post within a template or any other entity that can be edited. @@ -279,6 +281,7 @@ function useBlockEditorSettings( settings, postType, postId ) { postType, setIsInserterOpened, getPostLinkProps, + syncGetLinkSuggestions, ] ); }