diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index 6467b59284706..ff1a8579a0b01 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -375,6 +375,18 @@ The default editor settings Undocumented declaration. +# **storeConfig** + +Block editor data store configuration. + +_Related_ + +- + +_Type_ + +- `Object` + # **URLInput** _Related_ diff --git a/packages/block-editor/src/index.js b/packages/block-editor/src/index.js index 1d8827e2a33b4..d554e6e577a38 100644 --- a/packages/block-editor/src/index.js +++ b/packages/block-editor/src/index.js @@ -14,5 +14,5 @@ import './hooks'; export * from './components'; export * from './utils'; - +export { storeConfig } from './store'; export { SETTINGS_DEFAULTS } from './store/defaults'; diff --git a/packages/block-editor/src/store/index.js b/packages/block-editor/src/store/index.js index 485238f46f606..bfc7766a50876 100644 --- a/packages/block-editor/src/store/index.js +++ b/packages/block-editor/src/store/index.js @@ -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, diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index 28e55b08d6cbf..ea48a3a1d8e35 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -91,6 +91,7 @@ class Editor extends Component { settings={ editorSettings } post={ post } initialEdits={ initialEdits } + useSubRegistry={ false } { ...props } > diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 601a44c0260cf..e689c89210480 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -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'; @@ -165,6 +166,7 @@ class EditorProvider extends Component { } export default compose( [ + withRegistryProvider, withSelect( ( select ) => { const { __unstableIsEditorReady: isEditorReady, diff --git a/packages/editor/src/components/provider/with-registry-provider.js b/packages/editor/src/components/provider/with-registry-provider.js new file mode 100644 index 0000000000000..367782a82b4a4 --- /dev/null +++ b/packages/editor/src/components/provider/with-registry-provider.js @@ -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 ; + } + + 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 ( + + + + ); + } ), + 'withRegistryProvider' +); + +export default withRegistryProvider; diff --git a/packages/editor/src/index.js b/packages/editor/src/index.js index a55a7b1c0bfc1..eb54a38859b13 100644 --- a/packages/editor/src/index.js +++ b/packages/editor/src/index.js @@ -17,6 +17,7 @@ import './hooks'; export * from './components'; export * from './utils'; +export { storeConfig } from './store'; /* * Backward compatibility diff --git a/packages/editor/src/store/index.js b/packages/editor/src/store/index.js index 1ba136aaab722..33c5686396097 100644 --- a/packages/editor/src/store/index.js +++ b/packages/editor/src/store/index.js @@ -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 );