Skip to content

Commit

Permalink
Editor: Create own sub-registry in default EditorProvider use
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 29, 2019
1 parent 2c09d2c commit 35b8e8e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Editor extends Component {
settings={ editorSettings }
post={ post }
initialEdits={ initialEdits }
useSubRegistry={ false }
{ ...props }
>
<ErrorBoundary onError={ onError }>
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import withRegistryProvider from './with-registry-provider';
import transformStyles from '../../editor-styles';
import { mediaUpload } from '../../utils';
import ReusableBlocksButtons from '../reusable-blocks-buttons';
Expand Down Expand Up @@ -164,6 +165,7 @@ class EditorProvider extends Component {
}

export default compose( [
withRegistryProvider,
withSelect( ( select ) => {
const {
__unstableIsEditorReady: isEditorReady,
Expand Down
46 changes: 46 additions & 0 deletions packages/editor/src/components/provider/with-registry-provider.js
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;
6 changes: 5 additions & 1 deletion packages/editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import * as actions from './actions';
import controls from './controls';
import { STORE_KEY } from './constants';

const store = registerStore( STORE_KEY, {
export const storeConfig = {
reducer,
selectors,
actions,
controls,
};

const store = registerStore( STORE_KEY, {
...storeConfig,
persist: [ 'preferences' ],
} );
applyMiddlewares( store );
Expand Down

0 comments on commit 35b8e8e

Please sign in to comment.