-
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
Fix widget insertion from sidebar block library by using a declared insertionPoint prop #25763
Fix widget insertion from sidebar block library by using a declared insertionPoint prop #25763
Conversation
…nsertionPoint prop
const blockSelectionEnd = getBlockSelectionEnd(); | ||
const blockRootClientId = getBlockRootClientId( blockSelectionEnd ); | ||
// getBlockRootClientId returns an empty string for top-level blocks, in which case just return the block id. | ||
return blockRootClientId === '' ? blockSelectionEnd : blockRootClientId; | ||
const blockSelectionEndClientId = getBlockSelectionEnd(); | ||
|
||
if ( | ||
getBlockName( blockSelectionEndClientId ) === 'core/widget-area' | ||
) { | ||
return blockSelectionEndClientId; | ||
} | ||
|
||
const blockParents = getBlockParents( blockSelectionEndClientId ); | ||
const rootWidgetAreaClientId = blockParents.find( | ||
( clientId ) => getBlockName( clientId ) === 'core/widget-area' | ||
); | ||
|
||
if ( rootWidgetAreaClientId ) { | ||
return rootWidgetAreaClientId; | ||
} |
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 made some changes here, as this is used with the setIsWidgetAreaOpen
function, but previously could return a rootClientId for a block that's not a widget area (e.g. a group block).
@@ -46,6 +47,7 @@ function InserterMenu( { | |||
clientId, | |||
isAppender, | |||
selectBlockOnInsert: __experimentalSelectBlockOnInsert, | |||
insertionIndex: __experimentalInsertionIndex, |
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'm concerned about the proliferation of inserter props that all have the same intent, chose where to insert block: isAppender
, __experimentalSelectBlockOnInsert
, __experimentalInsertionIndex
, rootClientId
and clientId
are all related to that. I wonder if it's time to take a step back here and figure out something consistent.
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.
Yeah, I get that it's adding more along the same lines, especially clientId
and __experimentalInsertionIndex
, which are very similarly used.
We could continue making the insertion index more deterministic (more logic inside of useInsertionPoint
). An option there could be to consider that the root block list in widgets it essentially locked, so maybe useInsertionPoint
could detect that and instead insert inside the selected block when possible. I'm not sure how that would feel in terms of user experience.
Or alternatively continue on the path of making it configurable, but in a nicer way, make the entire useInsertionPoint
hook replaceable?
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.
Or alternatively continue on the path of making it configurable, but in a nicer way, make the entire useInsertionPoint hook replaceable?
Yes, something like that might make sense.
I also wonder whether we should remove the responsibility of choosing the position from the inserter component and just have a prop like { rootClientId + index }
with index
being optional and equal to the last position?
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.
That sounds good. I'll do some exploration in a separate PR (this one just merges into #25727).
Size Change: +189 B (0%) Total Size: 1.17 MB
ℹ️ View Unchanged
|
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.
LGTM 👍! Just a small nitpick
const { getBlockSelectionEnd, getBlockParents, getBlockName } = select( | ||
'core/block-editor' | ||
); | ||
const blockSelectionEnd = getBlockSelectionEnd(); | ||
const blockRootClientId = getBlockRootClientId( blockSelectionEnd ); | ||
// getBlockRootClientId returns an empty string for top-level blocks, in which case just return the block id. | ||
return blockRootClientId === '' ? blockSelectionEnd : blockRootClientId; | ||
const blockSelectionEndClientId = getBlockSelectionEnd(); | ||
|
||
if ( | ||
getBlockName( blockSelectionEndClientId ) === 'core/widget-area' | ||
) { | ||
return blockSelectionEndClientId; | ||
} | ||
|
||
const blockParents = getBlockParents( blockSelectionEndClientId ); | ||
const rootWidgetAreaClientId = blockParents.find( | ||
( clientId ) => getBlockName( clientId ) === 'core/widget-area' | ||
); | ||
|
||
if ( rootWidgetAreaClientId ) { | ||
return rootWidgetAreaClientId; | ||
} |
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 think we can use some comments here :)
Thanks for the review @kevin940726. I added the comments in a6bf31a, and also decided to reorder some of the code in 5f8c61b. Merging into your PR now. |
* Move hooks inside the newly created Interface component * Fix insertion at the top of the widget area * Fix widget insertion from sidebar block library by using a declared insertionPoint prop (#25763) * Fix widget insertion from sidebar block library by using a declared insertionPoint prop * Add comments * Optimize by avoiding `getEntityRecord` call unless needed Co-authored-by: Daniel Richards <[email protected]>
* Move hooks inside the newly created Interface component * Fix insertion at the top of the widget area * Fix widget insertion from sidebar block library by using a declared insertionPoint prop (#25763) * Fix widget insertion from sidebar block library by using a declared insertionPoint prop * Add comments * Optimize by avoiding `getEntityRecord` call unless needed Co-authored-by: Daniel Richards <[email protected]>
* Include edit-widgets php files in build (#25792) * Fix PHP warining in widget utils controller (#25797) * Fix PHP warning in WP_REST_Widget_Utils_Controller The `WP_REST_Widget_Utils_Controller::is_valid_widget` method needs to be public in order to be accessible as a callback (since it's being called from outside the class, via `call_user_func`). This commit fixes the warning by changing the method's visibility from `private` to `public`. * Ammend the inline documentation. Add `* @access public` * [Widgets Editor] Fix insertion point in widget areas (#25727) * Move hooks inside the newly created Interface component * Fix insertion at the top of the widget area * Fix widget insertion from sidebar block library by using a declared insertionPoint prop (#25763) * Fix widget insertion from sidebar block library by using a declared insertionPoint prop * Add comments * Optimize by avoiding `getEntityRecord` call unless needed Co-authored-by: Daniel Richards <[email protected]> * Initialize the state before rendering widgets editor (#25736) * Initialize the state before rendering widgets editor * Replace empty div with null * Document persistStubPost * Document persistStubPost further * Bump version to 9.1.1 * Update changelog * Fix spaces in changelog.txt * Adjust spaces in changelog.txt * Fix link formatting in the changelog Co-authored-by: Jon Surrell <[email protected]> Co-authored-by: David Biňovec <[email protected]> Co-authored-by: Kai Hao <[email protected]> Co-authored-by: Daniel Richards <[email protected]>
Description
An alternative way to handle #25727 (this PR merges into that one).
Primer
Insertion on the widgets page is somewhat unique in comparison to the post editor. There are multiple
widget-area
blocks that act as roots for block insertion.The trouble is that when inserting from the library, the default behavior is to insert after the selected block, but for these widget areas, we always want to insert inside of them.
This PR allows for the definition of an
insertionIndex
for the blockLibrary
component. When defined it overrides the behavior the library has where it defaults to inserting after the selection.How has this been tested?
With a widget area selected
With a block inside a widget area selected
Screenshots
Types of changes
Bug fix
Checklist: