From 523a2e75a5527966563393d63598d87824b8bf01 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/block-editor/README.md | 12 +++++ packages/block-editor/src/index.js | 2 +- packages/block-editor/src/store/index.js | 7 +++ packages/edit-post/src/editor.js | 1 + .../editor/src/components/provider/index.js | 2 + .../provider/with-registry-provider.js | 46 +++++++++++++++++++ packages/editor/src/index.js | 1 + packages/editor/src/store/index.js | 13 +++++- 8 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 packages/editor/src/components/provider/with-registry-provider.js diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index 6467b59284706f..ff1a8579a0b016 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 1d8827e2a33b49..d554e6e577a385 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 485238f46f606d..bfc7766a508762 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 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 601a44c0260cfe..e689c892104803 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 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/index.js b/packages/editor/src/index.js index a55a7b1c0bfc1b..eb54a38859b135 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 1ba136aaab7226..33c5686396097e 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 );