From 35b8e8e1c75b323051914b22f5cf76eb05c9043d Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 29 Mar 2019 16:13:19 -0400 Subject: [PATCH] Editor: Create own sub-registry in default EditorProvider use --- packages/edit-post/src/editor.js | 1 + .../editor/src/components/provider/index.js | 2 + .../provider/with-registry-provider.js | 46 +++++++++++++++++++ packages/editor/src/store/index.js | 6 ++- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 packages/editor/src/components/provider/with-registry-provider.js diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index 28e55b08d6cbf6..ea48a3a1d8e358 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 54405f61247b8f..7add6cc349312e 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 transformStyles from '../../editor-styles'; import { mediaUpload } from '../../utils'; import ReusableBlocksButtons from '../reusable-blocks-buttons'; @@ -164,6 +165,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 00000000000000..367782a82b4a42 --- /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/store/index.js b/packages/editor/src/store/index.js index 42af629bcce0d3..bdbd1eff49455c 100644 --- a/packages/editor/src/store/index.js +++ b/packages/editor/src/store/index.js @@ -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 );