-
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
Automatically use a subregistry when using the block editor provider #14678
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,41 @@ | ||||||
/** | ||||||
* WordPress dependencies | ||||||
*/ | ||||||
import { useState, useEffect } from '@wordpress/element'; | ||||||
import { withRegistry, createRegistry, RegistryProvider } from '@wordpress/data'; | ||||||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||||||
|
||||||
/** | ||||||
* Internal dependencies | ||||||
*/ | ||||||
import { storeConfig } from '../../store'; | ||||||
import applyMiddlewares from '../../store/middlewares'; | ||||||
|
||||||
const withRegistryProvider = createHigherOrderComponent( ( WrappedComponent ) => { | ||||||
return withRegistry( ( { useSubRegistry = true, registry, ...props } ) => { | ||||||
if ( ! useSubRegistry ) { | ||||||
return <WrappedComponent registry={ registry } { ...props } />; | ||||||
} | ||||||
|
||||||
const [ subRegistry, setSubRegistry ] = useState( null ); | ||||||
useEffect( () => { | ||||||
const newRegistry = createRegistry( {}, registry ); | ||||||
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. Would it work as well?
Suggested change
This is what I read from: 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. Yes, previously this was not possible because the controls plugins was not built-in but we can do this now. Good suggestion. 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. Oh I guess we still can't do that yet because we need the returned "store" object to apply middlewares. 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. |
||||||
const store = newRegistry.registerStore( 'core/block-editor', storeConfig ); | ||||||
// This should be removed after the refactoring of the effects to controls. | ||||||
applyMiddlewares( store ); | ||||||
setSubRegistry( newRegistry ); | ||||||
}, [ registry ] ); | ||||||
|
||||||
if ( ! subRegistry ) { | ||||||
return null; | ||||||
} | ||||||
|
||||||
return ( | ||||||
<RegistryProvider value={ subRegistry }> | ||||||
<WrappedComponent registry={ subRegistry } { ...props } /> | ||||||
</RegistryProvider> | ||||||
); | ||||||
} ); | ||||||
}, 'withRegistryProvider' ); | ||||||
|
||||||
export default withRegistryProvider; |
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.
It took me a while to understand what's happening here:
core/block-editor