Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: Extensibility: Enable hook-based Slot/Fill registration #3321

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions components/higher-order/with-hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

export default ( WrappedComponent ) => {
function HooksComponent( props, context ) {
return (
<WrappedComponent
{ ...props }
{ ...context } />
);
}

// Derive display name from original component
const { displayName = WrappedComponent.name || 'Component' } = WrappedComponent;
HooksComponent.displayName = `hooks(${ displayName })`;

HooksComponent.contextTypes = {
hooks: noop,
};

return HooksComponent;
};
31 changes: 31 additions & 0 deletions components/higher-order/with-hooks/provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* WordPress dependencies
*/
import createHooks from '@wordpress/hooks';
import { Component } from '@wordpress/element';

export default class HooksProvider extends Component {
constructor() {
super( ...arguments );

this.hooks = createHooks();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it tested with the recently added recreate editor instance feature? createHooks() makes me think that there is going to be a new object created with a fresh state, so we might lose all registered hooks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2nd thing we should figure out pretty soon is how it relates to createHooks() call for blocks: https://github.com/WordPress/gutenberg/blob/master/blocks/hooks/index.js#L11.

Unless createHooks uses singletone pattern and it isn't an issue at all :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it does @gziolo - did you ever look into this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't use singletone . It's fine as it is, we took a completely different approach anyways :)

}

getChildContext() {
const { hooks } = this;
return { hooks };
}

render() {
return this.props.children;
}
}

HooksProvider.childContextTypes = {
hooks: noop,
};
2 changes: 2 additions & 0 deletions components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { default as ExternalLink } from './external-link';
export { default as FormFileUpload } from './form-file-upload';
export { default as FormToggle } from './form-toggle';
export { default as FormTokenField } from './form-token-field';
export { default as HooksProvider } from './higher-order/with-hooks/provider';
export { default as IconButton } from './icon-button';
export { default as KeyboardShortcuts } from './keyboard-shortcuts';
export { NavigableMenu, TabbableContainer } from './navigable-container';
Expand All @@ -35,5 +36,6 @@ export { default as navigateRegions } from './higher-order/navigate-regions';
export { default as withAPIData } from './higher-order/with-api-data';
export { default as withFocusOutside } from './higher-order/with-focus-outside';
export { default as withFocusReturn } from './higher-order/with-focus-return';
export { default as withHooks } from './higher-order/with-hooks';
export { default as withInstanceId } from './higher-order/with-instance-id';
export { default as withSpokenMessages } from './higher-order/with-spoken-messages';
1 change: 1 addition & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ export { default as Warning } from './warning';

// State Related Components
export { default as EditorProvider } from './provider';
export { default as PluginFills } from './plugin-fills';
58 changes: 58 additions & 0 deletions editor/components/plugin-fills/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* WordPress dependencies
*/
import { Fill, withHooks } from '@wordpress/components';
import { Component } from '@wordpress/element';

class PluginFills extends Component {
constructor() {
super( ...arguments );

this.registerFill = this.registerFill.bind( this );

this.state = {
fills: {},
};
}

componentDidMount() {
this.props.hooks.addAction( 'registerFill', 'core\plugin-fills-register', this.registerFill );
}

componentWillUnmount() {
this.props.hooks.removeAction( 'registerFill', 'core\plugin-fills-register' );
}

registerFill( name, FillComponent ) {
this.setState( ( prevState ) => {
const { fills } = prevState;

return {
fills: {
...fills,
[ name ]: [
...( fills[ name ] || [] ),
FillComponent,
],
},
};
} );
}

render() {
return map( this.state.fills, ( components, name ) => (
<Fill key={ name } name={ name }>
{ map( components, ( FillComponent, index ) => (
<FillComponent key={ index } />
) ) }
</Fill>
) );
}
}

export default withHooks( PluginFills );
12 changes: 12 additions & 0 deletions editor/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
APIProvider,
DropZoneProvider,
SlotFillProvider,
HooksProvider,
} from '@wordpress/components';

/**
Expand Down Expand Up @@ -42,6 +43,8 @@ class EditorProvider extends Component {
constructor( props ) {
super( ...arguments );

this.setHooks = this.setHooks.bind( this );

const store = createReduxStore( props.initialState );

// If initial state is passed, assume that we don't need to initialize,
Expand Down Expand Up @@ -73,6 +76,10 @@ class EditorProvider extends Component {
}
}

setHooks( node ) {
this.hooks = node.hooks;
}

render() {
const { children } = this.props;
const providers = [
Expand Down Expand Up @@ -103,6 +110,11 @@ class EditorProvider extends Component {
SlotFillProvider,
],

[
HooksProvider,
{ ref: this.setHooks },
],

// APIProvider
//
// - context.getAPISchema
Expand Down
3 changes: 2 additions & 1 deletion editor/header/publish-dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress Dependencies
*/
import { __ } from '@wordpress/i18n';
import { withAPIData, PanelBody } from '@wordpress/components';
import { withAPIData, PanelBody, Slot } from '@wordpress/components';

/**
* Internal Dependencies
Expand Down Expand Up @@ -44,6 +44,7 @@ function PublishDropdown( { user, onSubmit } ) {
<PostSchedule />
</PanelBody>
}
<Slot name="publish-dropdown" />
<div className="editor-publish-dropdown__publish-button-container">
<PostPublishButton onSubmit={ onSubmit } />
</div>
Expand Down
3 changes: 3 additions & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@ export function createEditorInstance( id, post, settings ) {
initializeMetaBoxes( metaBoxes ) {
provider.store.dispatch( initializeMetaBoxState( metaBoxes ) );
},
registerFill( fill, component ) {
provider.hooks.doAction( 'registerFill', fill, component );
},
};
}
8 changes: 7 additions & 1 deletion editor/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import TextEditor from '../modes/text-editor';
import VisualEditor from '../modes/visual-editor';
import DocumentTitle from '../document-title';
import { removeNotice } from '../actions';
import { MetaBoxes, AutosaveMonitor, UnsavedChangesWarning } from '../components';
import {
MetaBoxes,
AutosaveMonitor,
UnsavedChangesWarning,
PluginFills,
} from '../components';
import {
getEditorMode,
isEditorSidebarOpened,
Expand Down Expand Up @@ -50,6 +55,7 @@ function Layout( { mode, isSidebarOpened, notices, ...props } ) {
</div>
{ isSidebarOpened && <Sidebar /> }
<Popover.Slot />
<PluginFills />
</div>
);
}
Expand Down