-
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
Block editor settings: delay selecting pageOnFront, pageForPosts #57290
Open
ellatrix
wants to merge
10
commits into
trunk
Choose a base branch
from
try/untangle-block-editor-settings
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
99f955b
Block editor settings: delay selecting pageOnFront, pageForPosts
ellatrix 8d05001
Check if usePageSettings exists
ellatrix a9813b6
Use Symbol instead of experimental prefix
ellatrix 3991a70
Add private keys for native
ellatrix ac45b44
use new key for all editors
ellatrix 80a3e54
Extract useHandleEntitySearch
ellatrix cc80345
Absorb fetchLinkSuggestions
ellatrix 15f78d9
Move hook to core-data
ellatrix 17857cd
Move back creation result
ellatrix 91dc709
Restore __experimentalFetchLinkSuggestions setting
ellatrix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
useLinkControlEntitySearch: Symbol( 'useLinkControlEntitySearch' ), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,2 +1,93 @@ | ||||
export { default as __experimentalFetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; | ||||
/** | ||||
* WordPress dependencies | ||||
*/ | ||||
import { useCallback } from '@wordpress/element'; | ||||
import { useSelect } from '@wordpress/data'; | ||||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||||
|
||||
/** | ||||
* Internal dependencies | ||||
*/ | ||||
import { store as coreStore } from '../'; | ||||
import { default as fetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; | ||||
|
||||
export const __experimentalFetchLinkSuggestions = fetchLinkSuggestions; | ||||
export { default as __experimentalFetchUrlData } from './__experimental-fetch-url-data'; | ||||
|
||||
export function __experimentalUseLinkControlEntitySearch() { | ||||
const settings = useSelect( | ||||
( select ) => select( blockEditorStore ).getSettings(), | ||||
[] | ||||
); | ||||
// The function should either be undefined or a stable function reference | ||||
// throughout the editor lifetime, much like importing a function from a | ||||
// module. | ||||
const { pageOnFront, pageForPosts } = useSelect( ( select ) => { | ||||
const { canUser, getEntityRecord } = select( coreStore ); | ||||
|
||||
const siteSettings = canUser( 'read', 'settings' ) | ||||
? getEntityRecord( 'root', 'site' ) | ||||
: undefined; | ||||
|
||||
return { | ||||
pageOnFront: siteSettings?.page_on_front, | ||||
pageForPosts: siteSettings?.page_for_posts, | ||||
}; | ||||
}, [] ); | ||||
|
||||
return useCallback( | ||||
async ( val, suggestionsQuery, withCreateSuggestion ) => { | ||||
const { isInitialSuggestions } = suggestionsQuery; | ||||
|
||||
const results = await fetchLinkSuggestions( | ||||
val, | ||||
suggestionsQuery, | ||||
settings | ||||
); | ||||
|
||||
// Identify front page and update type to match. | ||||
results.map( ( result ) => { | ||||
if ( Number( result.id ) === pageOnFront ) { | ||||
result.isFrontPage = true; | ||||
return result; | ||||
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.
Suggested change
I think we can remove these early |
||||
} else if ( Number( result.id ) === pageForPosts ) { | ||||
result.isBlogHome = true; | ||||
return result; | ||||
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. Don't think we need these early
Suggested change
|
||||
} | ||||
|
||||
return result; | ||||
} ); | ||||
|
||||
// If displaying initial suggestions just return plain results. | ||||
if ( isInitialSuggestions ) { | ||||
return results; | ||||
} | ||||
|
||||
// Here we append a faux suggestion to represent a "CREATE" option. This | ||||
// is detected in the rendering of the search results and handled as a | ||||
// special case. This is currently necessary because the suggestions | ||||
// dropdown will only appear if there are valid suggestions and | ||||
// therefore unless the create option is a suggestion it will not | ||||
// display in scenarios where there are no results returned from the | ||||
// API. In addition promoting CREATE to a first class suggestion affords | ||||
// the a11y benefits afforded by `URLInput` to all suggestions (eg: | ||||
// keyboard handling, ARIA roles...etc). | ||||
// | ||||
// Note also that the value of the `title` and `url` properties must correspond | ||||
// to the text value of the `<input>`. This is because `title` is used | ||||
// when creating the suggestion. Similarly `url` is used when using keyboard to select | ||||
// the suggestion (the <form> `onSubmit` handler falls-back to `url`). | ||||
return ! withCreateSuggestion | ||||
? results | ||||
: results.concat( { | ||||
// the `id` prop is intentionally ommitted here because it | ||||
// is never exposed as part of the component's public API. | ||||
// see: https://github.com/WordPress/gutenberg/pull/19775#discussion_r378931316. | ||||
title: val, // Must match the existing `<input>`s text value. | ||||
url: val, // Must match the existing `<input>`s text value. | ||||
type: '__CREATE__', | ||||
} ); | ||||
}, | ||||
[ pageOnFront, pageForPosts, settings ] | ||||
); | ||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this be a private API instead of a prefixed one.
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 am working on a plan to move this fetching to become a full selector in Core Data in order to take advantage of caching...etc. So making it private would be good.
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.
See #57582