-
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
Changes from all commits
9a3b51d
dbbb2c1
71cdce0
6357d29
2052259
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
BlockEditorProvider, | ||
BlockContextProvider, | ||
privateApis as blockEditorPrivateApis, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { privateApis as editPatternsPrivateApis } from '@wordpress/patterns'; | ||
|
@@ -222,6 +223,37 @@ export const ExperimentalEditorProvider = withRegistryProvider( | |
setEditedPost, | ||
setRenderingMode, | ||
} = unlock( useDispatch( editorStore ) ); | ||
|
||
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 ] | ||
); | ||
Comment on lines
+227
to
+251
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. Essentially this is the algorithm that determines the section "root" based on things that are specific to WordPress. For example the 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? |
||
|
||
const { setSectionRootClientId } = unlock( | ||
useDispatch( blockEditorStore ) | ||
); | ||
|
||
const { createWarningNotice } = useDispatch( noticesStore ); | ||
|
||
// Ideally this should be synced on each change and not just something you do once. | ||
|
@@ -271,6 +303,13 @@ export const ExperimentalEditorProvider = withRegistryProvider( | |
setRenderingMode( settings.defaultRenderingMode ?? 'post-only' ); | ||
}, [ settings.defaultRenderingMode, setRenderingMode ] ); | ||
|
||
// Sets the section root client id once it is computed. | ||
useEffect( () => { | ||
if ( computedSectionRootClientId ) { | ||
setSectionRootClientId( computedSectionRootClientId ); | ||
} | ||
}, [ computedSectionRootClientId, setSectionRootClientId ] ); | ||
|
||
useHideBlocksFromInserter( post.type, mode ); | ||
|
||
// Register the editor commands. | ||
|
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 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: