Skip to content

Commit

Permalink
Editor: Create own sub-registry in default EditorProvider use (#15989)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored Jun 10, 2019
1 parent f46d6f4 commit a3ef960
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ The default editor settings

Undocumented declaration.

<a name="storeConfig" href="#storeConfig">#</a> **storeConfig**

Block editor data store configuration.

_Related_

- <https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#registerStore>

_Type_

- `Object`

<a name="URLInput" href="#URLInput">#</a> **URLInput**

_Related_
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ import './hooks';

export * from './components';
export * from './utils';

export { storeConfig } from './store';
export { SETTINGS_DEFAULTS } from './store/defaults';
7 changes: 7 additions & 0 deletions packages/block-editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import controls from './controls';
*/
const MODULE_KEY = 'core/block-editor';

/**
* Block editor data store configuration.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#registerStore
*
* @type {Object}
*/
export const storeConfig = {
reducer,
selectors,
Expand Down
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 { mediaUpload } from '../../utils';
import ReusableBlocksButtons from '../reusable-blocks-buttons';
import ConvertToGroupButtons from '../convert-to-group-buttons';
Expand Down Expand Up @@ -167,6 +168,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;
1 change: 1 addition & 0 deletions packages/editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import './hooks';

export * from './components';
export * from './utils';
export { storeConfig } from './store';

/*
* Backward compatibility
Expand Down
13 changes: 12 additions & 1 deletion packages/editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ import * as selectors from './selectors';
import * as actions from './actions';
import { STORE_KEY } from './constants';

const store = registerStore( STORE_KEY, {
/**
* Post editor data store configuration.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#registerStore
*
* @type {Object}
*/
export const storeConfig = {
reducer,
selectors,
actions,
controls,
};

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

0 comments on commit a3ef960

Please sign in to comment.