-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Move sectionRootClientId to block editor state #64544
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Size Change: +78 B (0%) Total Size: 1.78 MB
ℹ️ View Unchanged
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
export function getSectionRootClientId( state = '' ) { | ||
return state.sectionRootClientId; | ||
} |
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.
export function getSectionRootClientId( state = '' ) { | |
return state.sectionRootClientId; | |
} | |
export function getSectionRootClientId( state = '' ) { | |
let sectionRootClientId = state.sectionRootClientId; | |
// Default to the main block if no section root is set. | |
if ( ! sectionRootClientId ) { | |
const blocks = getBlocksByName( state, 'core/group' ); | |
sectionRootClientId = | |
blocks.find( | |
( clientId ) => | |
getBlockAttributes( state, clientId )?.tagName === 'main' | |
) ?? ''; | |
} | |
return sectionRootClientId; | |
} |
I wonder if we can default the the algorithm which prefers the <main>
tag?
Then if an individual editor needs to customise the behaviour it can dispatch the setSectionRootClientId()
action.
Bear in the mind we'd need to account for this part of the existing algorithm in the editor
package:
if ( mode === 'template-locked' ) {
_sectionRootClientId =
getBlocksByName( 'core/post-content' )?.[ 0 ] ?? '';
}
Thanks for working on this @getdave!
Good question. Maybe in ExperimentalEditorProvider, as that seems close to where it happened before (the editor settings pass through there), and there are some similar effects that dispatch the block rendering mode and other things. Another option is setting it right before zoom out mode is initiated, but I see that currently happens in the block editor package. There's a question of whether initiating zoom out mode like that should be a built-in part of the block editor package, or configurable on a per editor basis (e.g. it could be set by a callback like I think generally though I'd ask @youknowriad this question! |
56292e8
to
2052259
Compare
const { computedSectionRootClientId } = useSelect( | ||
( select ) => { | ||
const { getBlockAttributes, getBlocksByName } = | ||
select( blockEditorStore ); | ||
|
||
let _sectionRootClientId; | ||
|
||
if ( mode === 'template-locked' ) { | ||
_sectionRootClientId = | ||
getBlocksByName( 'core/post-content' )?.[ 0 ] ?? ''; | ||
} else { | ||
_sectionRootClientId = | ||
getBlocksByName( 'core/group' ).find( | ||
( clientId ) => | ||
getBlockAttributes( clientId )?.tagName === | ||
'main' | ||
) ?? ''; | ||
} | ||
|
||
return { | ||
computedSectionRootClientId: _sectionRootClientId, | ||
}; | ||
}, | ||
[ mode ] | ||
); |
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.
Essentially this is the algorithm that determines the section "root" based on things that are specific to WordPress. For example the core/post-content
block is likely WP-specific and so we can't embed this logic in the block editor package.
We could move this into a hook to better name the concept and make the code easier to reason about.
What I'm really interested to agree on is whether this is the best location to be doing such setup logic?
This one is very quiet. I suggest we move forward with this implementation as it's better at keeping the APIs private. If we need to reverse out in the future it should be relatively easy. Do you concur @talldan? Let's try rebasing this branch and check whether the current test failures are legit. |
@scruffian @jeryj would be interested in your opinions on this one. Some background in PR description and also in #64141. |
For now I've gone with #65000. We can circle back here if/when we need to. |
What?
Moves the source of truth for
sectionRootClientId
to the block-editor store.Related to #65001
Resolves #64141
Co-authored-by: getdave [email protected]
Co-authored-by: talldan [email protected]
Why?
Currently on
trunk
thesectionRootClientId
is provided to the block editor via block editor settings. This is necessary because the algorithm which determines the block to be designated as the "section root" must consider blocks that may not be present in every editor (e.g.core/post-content
). Therefore to make this setting "editor agnostic" it must be passed to the block editor.However, at this stage it's important this feature is kept private as we are not yet confident in it as a public API. Therefore efforts were made to "lock" the setting. However, this seems to be non-standard and is causing errors as described in #64141.
How?
This PR seeks to explore an alternative method of providing the "section root" by moving it to block editor state with private selectors / actions to set/set the state.
Then the consuming editor instance (in WP this is the
@wordpress/editor
package) implements it's own algorithm to determine the "section root" and dispatches an action with theclientId
of that block element it wants to use as the root.This has two advantages:
Note: I am not fully invested in this method and would be keen to understand from others whether this method is actually preferable to passing the data via block editor settings.
Testing Instructions
The aim is to verify that when in Zoom Out mode the behaviour is the same as that currently on
trunk
.<main>
tag.Testing Instructions for Keyboard
Screenshots or screencast