Skip to content

Commit

Permalink
Plugins: Refactor plugin context implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed May 14, 2018
1 parent ba1ffe5 commit bde94b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ const PluginSidebarMoreMenuItem = ( { children, icon, isSelected, onClick } ) =>
);

export default compose(
withPluginContext,
withSelect( ( select, { icon, pluginContext, target } ) => {
withPluginContext( ( context, ownProps ) => {
return {
icon: ownProps.icon || context.icon,
sidebarName: `${ context.name }/${ ownProps.target }`,
};
} ),
withSelect( ( select, { sidebarName } ) => {
const {
getActiveGeneralSidebarName,
} = select( 'core/edit-post' );
const sidebarName = `${ pluginContext.name }/${ target }`;

return {
icon: icon || pluginContext.icon,
isSelected: getActiveGeneralSidebarName() === sidebarName,
sidebarName,
};
} ),
withDispatch( ( dispatch, { isSelected, sidebarName } ) => {
Expand Down
12 changes: 7 additions & 5 deletions edit-post/components/sidebar/plugin-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ function PluginSidebar( props ) {
}

export default compose(
withPluginContext,
withSelect( ( select, { icon, name, pluginContext } ) => {
withPluginContext( ( context, ownProps ) => {
return {
icon: ownProps.icon || context.icon,
sidebarName: `${ context.name }/${ ownProps.name }`,
};
} ),
withSelect( ( select, { sidebarName } ) => {
const {
getActiveGeneralSidebarName,
isPluginItemPinned,
} = select( 'core/edit-post' );
const sidebarName = `${ pluginContext.name }/${ name }`;

return {
icon: icon || pluginContext.icon,
isActive: getActiveGeneralSidebarName() === sidebarName,
isPinned: isPluginItemPinned( sidebarName ),
sidebarName,
};
} ),
withDispatch( ( dispatch, { isActive, sidebarName } ) => {
Expand Down
5 changes: 2 additions & 3 deletions plugins/components/plugin-area/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class PluginArea extends Component {
return {
plugins: map( getPlugins(), ( { icon, name, render } ) => {
return {
name,
Plugin: render,
context: {
name,
Expand Down Expand Up @@ -60,9 +59,9 @@ class PluginArea extends Component {
render() {
return (
<div style={ { display: 'none' } }>
{ map( this.state.plugins, ( { context, name, Plugin } ) => (
{ map( this.state.plugins, ( { context, Plugin } ) => (
<PluginContextProvider
key={ name }
key={ context.name }
value={ context }
>
<Plugin />
Expand Down
23 changes: 12 additions & 11 deletions plugins/components/plugin-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ const { Consumer, Provider } = createContext( {
export { Provider as PluginContextProvider };

/**
* A Higher-order Component used to inject Plugin context into the wrapped
* component.
* A Higher Order Component used to inject Plugin context to the
* wrapped component.
*
* @param {Component} OriginalComponent Component to wrap.
* @param {Function} mapContextToProps Function called on every context change,
* expected to return object of props to
* merge with the component's own props.
*
* @return {Component} Component with Plugin context injected.
* @return {Component} Enhanced component with injected context as props.
*/
export const withPluginContext = createHigherOrderComponent(
( OriginalComponent ) => ( props ) => (
export const withPluginContext = ( mapContextToProps ) => createHigherOrderComponent( ( OriginalComponent ) => {
return ( props ) => (
<Consumer>
{ ( pluginContext ) => (
{ ( context ) => (
<OriginalComponent
{ ...props }
pluginContext={ pluginContext }
{ ...mapContextToProps( context, props ) }
/>
) }
</Consumer>
),
'withPluginContext'
);
);
}, 'withPluginContext' );

0 comments on commit bde94b0

Please sign in to comment.