Skip to content

Commit

Permalink
Extensibility: Refactor hooks to make them reusable for editor and co…
Browse files Browse the repository at this point in the history
…mponents
  • Loading branch information
gziolo committed Nov 17, 2017
1 parent 98982b0 commit c7ce65a
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 50 deletions.
4 changes: 2 additions & 2 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { get, isFunction, some } from 'lodash';
/**
* WordPress dependencies
*/
import { getCategories } from './categories';
import { applyFilters } from '@wordpress/utils';

/**
* Internal dependencies
*/
import { applyFilters } from '../hooks';
import { getCategories } from './categories';

/**
* Block settings keyed by block name.
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Component, createElement, renderToString, cloneElement, Children } from '@wordpress/element';
import { applyFilters } from '@wordpress/utils';

/**
* Internal dependencies
*/
import { getBlockType, getUnknownTypeHandlerName } from './registration';
import { applyFilters } from '../hooks';

/**
* Returns the block's default classname from its name
Expand Down
5 changes: 5 additions & 0 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe( 'block factory', () => {
title: 'block title',
};

beforeAll( () => {
// Load all hooks that modify blocks
require( 'blocks/hooks' );
} );

afterEach( () => {
setUnknownTypeHandlerName( undefined );
getBlockTypes().forEach( ( block ) => {
Expand Down
5 changes: 5 additions & 0 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ describe( 'blocks', () => {
const error = console.error;
const defaultBlockSettings = { save: noop, category: 'common', title: 'block title' };

beforeAll( () => {
// Load all hooks that modify blocks
require( 'blocks/hooks' );
} );

// Reset block state before each test.
beforeEach( () => {
console.error = jest.fn();
Expand Down
5 changes: 5 additions & 0 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import {
import { createBlock } from '../';

describe( 'block serializer', () => {
beforeAll( () => {
// Load all hooks that modify blocks
require( 'blocks/hooks' );
} );

afterEach( () => {
setUnknownTypeHandlerName( undefined );
getBlockTypes().forEach( block => {
Expand Down
11 changes: 6 additions & 5 deletions blocks/block-edit/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/utils';

/**
* Internal dependencies
*/
import { getBlockType } from '../api';
import { applyFilters } from '../hooks';

function BlockEdit( props ) {
const { name, ...editProps } = props;
Expand All @@ -15,10 +19,7 @@ function BlockEdit( props ) {
// `edit` and `save` are functions or components describing the markup
// with which a block is displayed. If `blockType` is valid, assign
// them preferencially as the render value for the block.
let Edit;
if ( blockType ) {
Edit = blockType.edit || blockType.save;
}
const Edit = blockType.edit || blockType.save;

return applyFilters( 'BlockEdit', <Edit key="edit" { ...editProps } />, props );
}
Expand Down
38 changes: 2 additions & 36 deletions blocks/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,15 @@
/**
* WordPress dependencies
*/
import createHooks from '@wordpress/hooks';
import { addFilter } from '@wordpress/utils';

/**
* Internal dependencies
*/
import anchor from './anchor';
import customClassName from './custom-class-name';

const hooks = createHooks();

const {
addAction,
addFilter,
removeAction,
removeFilter,
removeAllActions,
removeAllFilters,
doAction,
applyFilters,
doingAction,
doingFilter,
didAction,
didFilter,
hasAction,
hasFilter,
} = hooks;

export {
addAction,
addFilter,
removeAction,
removeFilter,
removeAllActions,
removeAllFilters,
doAction,
applyFilters,
doingAction,
doingFilter,
didAction,
didFilter,
hasAction,
hasFilter,
};
const hooks = { addFilter };

anchor( hooks );
customClassName( hooks );
2 changes: 1 addition & 1 deletion blocks/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import './hooks';
import './library';

// A "block" is the abstract term used to describe units of markup that,
Expand All @@ -13,7 +14,6 @@ import './library';
// Blocks are inferred from the HTML source of a post through a parsing mechanism
// and then stored as objects in state, from which it is then rendered for editing.
export * from './api';
export * from './hooks';
export { default as AlignmentToolbar } from './alignment-toolbar';
export { default as BlockAlignmentToolbar } from './block-alignment-toolbar';
export { default as BlockControls } from './block-controls';
Expand Down
7 changes: 2 additions & 5 deletions components/higher-order/with-filters/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/**
* WordPress dependencies
*/
import createHooks from '@wordpress/hooks';
import { applyFilters } from '@wordpress/utils';

/**
* Internal dependencies
*/
import wrapperDisplayName from '../wrapper-display-name';

// TODO: Use withHooks HOC from https://github.com/WordPress/gutenberg/pull/3321.
const hooks = createHooks();

export default function withFilters( hookName ) {
return ( WrappedComponent ) => {
const FiltersComponent = ( props ) => {
return hooks.applyFilters( hookName, <WrappedComponent { ...props } />, props );
return applyFilters( hookName, <WrappedComponent { ...props } />, props );
};
FiltersComponent.displayName = wrapperDisplayName( 'filters', WrappedComponent );

Expand Down
40 changes: 40 additions & 0 deletions utils/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import createHooks from '@wordpress/hooks';

const hooks = createHooks();

const {
addAction,
addFilter,
removeAction,
removeFilter,
removeAllActions,
removeAllFilters,
doAction,
applyFilters,
doingAction,
doingFilter,
didAction,
didFilter,
hasAction,
hasFilter,
} = hooks;

export {
addAction,
addFilter,
removeAction,
removeFilter,
removeAllActions,
removeAllFilters,
doAction,
applyFilters,
doingAction,
doingFilter,
didAction,
didFilter,
hasAction,
hasFilter,
};
1 change: 1 addition & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { keycodes };
export { decodeEntities };

export * from './blob-cache';
export * from './hooks';
export * from './mediaupload';

export { viewPort };

0 comments on commit c7ce65a

Please sign in to comment.