-
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.
Editor: Create own sub-registry in default EditorProvider use (#15989)
- Loading branch information
Showing
8 changed files
with
82 additions
and
2 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
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
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
46 changes: 46 additions & 0 deletions
46
packages/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,46 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect } from '@wordpress/element'; | ||
import { withRegistry, createRegistry, RegistryProvider } from '@wordpress/data'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { storeConfig as blockEditorStoreConfig } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { storeConfig } from '../../store'; | ||
import applyMiddlewares from '../../store/middlewares'; | ||
|
||
const withRegistryProvider = createHigherOrderComponent( | ||
( WrappedComponent ) => withRegistry( ( props ) => { | ||
const { useSubRegistry = true, registry, ...additionalProps } = props; | ||
if ( ! useSubRegistry ) { | ||
return <WrappedComponent { ...additionalProps } />; | ||
} | ||
|
||
const [ subRegistry, setSubRegistry ] = useState( null ); | ||
useEffect( () => { | ||
const newRegistry = createRegistry( { | ||
'core/block-editor': blockEditorStoreConfig, | ||
}, registry ); | ||
const store = newRegistry.registerStore( 'core/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 { ...additionalProps } /> | ||
</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