-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically use a subregistry when using the block editor provider (#…
- Loading branch information
1 parent
fa7549e
commit 7431011
Showing
4 changed files
with
54 additions
and
3 deletions.
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
41 changes: 41 additions & 0 deletions
41
packages/block-editor/src/components/provider/with-registry-provider.js
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,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 ); | ||
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; |
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