Skip to content

Commit

Permalink
Merge pull request #3577 from WordPress/update/block-settings-allowed…
Browse files Browse the repository at this point in the history
…-types

Blocks: Filterable allowed inserter block types
  • Loading branch information
aduth authored Nov 21, 2017
2 parents 359f11f + e6642bc commit 7067bae
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 64 deletions.
9 changes: 2 additions & 7 deletions blocks/block-alignment-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Toolbar } from '@wordpress/components';

/**
* Internal dependencies
*/
import withEditorSettings from '../with-editor-settings';
import { Toolbar, withContext } from '@wordpress/components';

const BLOCK_ALIGNMENTS_CONTROLS = {
left: {
Expand Down Expand Up @@ -59,7 +54,7 @@ function BlockAlignmentToolbar( { value, onChange, controls = DEFAULT_CONTROLS,
);
}

export default withEditorSettings(
export default withContext( 'editor' )(
( settings ) => ( {
wideControlsEnabled: settings.wideImages,
} )
Expand Down
5 changes: 2 additions & 3 deletions blocks/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import { ChromePicker } from 'react-color';
/**
* WordPress dependencies
*/
import { Dropdown } from '@wordpress/components';
import { Dropdown, withContext } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import './style.scss';
import withEditorSettings from '../with-editor-settings';

function ColorPalette( { colors, value, onChange } ) {
return (
Expand Down Expand Up @@ -72,7 +71,7 @@ function ColorPalette( { colors, value, onChange } ) {
);
}

export default withEditorSettings(
export default withContext( 'editor' )(
( settings ) => ( {
colors: settings.colors,
} )
Expand Down
4 changes: 2 additions & 2 deletions blocks/library/image/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import {
DropZone,
FormFileUpload,
withAPIData,
withContext,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import withEditorSettings from '../../with-editor-settings';
import Editable from '../../editable';
import MediaUploadButton from '../../media-upload-button';
import InspectorControls from '../../inspector-controls';
Expand Down Expand Up @@ -282,7 +282,7 @@ class ImageBlock extends Component {
}

export default flowRight( [
withEditorSettings(),
withContext( 'editor' ),
withAPIData( ( props ) => {
const { id } = props.attributes;
if ( ! id ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import { noop } from 'lodash';
*/
import { Component } from '@wordpress/element';

const withEditorSettings = ( mapSettingsToProps ) => ( OriginalComponent ) => {
const withContext = ( contextName ) => ( mapSettingsToProps ) => ( OriginalComponent ) => {
// Allow call without explicit `mapSettingsToProps`
if ( mapSettingsToProps instanceof Component ) {
return withContext( contextName )()( mapSettingsToProps );
}

class WrappedComponent extends Component {
render() {
const extraProps = mapSettingsToProps ?
mapSettingsToProps( this.context.editor, this.props ) :
{ settings: this.context.editor };
mapSettingsToProps( this.context[ contextName ], this.props ) :
this.context[ contextName ];

return (
<OriginalComponent
Expand All @@ -25,10 +30,10 @@ const withEditorSettings = ( mapSettingsToProps ) => ( OriginalComponent ) => {
}

WrappedComponent.contextTypes = {
editor: noop,
[ contextName ]: noop,
};

return WrappedComponent;
};

export default withEditorSettings;
export default withContext;
1 change: 1 addition & 0 deletions components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { Slot, Fill, Provider as SlotFillProvider } from './slot-fill';
// Higher-Order Components
export { default as navigateRegions } from './higher-order/navigate-regions';
export { default as withAPIData } from './higher-order/with-api-data';
export { default as withContext } from './higher-order/with-context';
export { default as withFocusOutside } from './higher-order/with-focus-outside';
export { default as withFocusReturn } from './higher-order/with-focus-return';
export { default as withInstanceId } from './higher-order/with-instance-id';
Expand Down
55 changes: 35 additions & 20 deletions editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { flowRight, isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Dropdown, IconButton } from '@wordpress/components';
import { Dropdown, IconButton, withContext } from '@wordpress/components';
import { createBlock } from '@wordpress/blocks';
import { Component } from '@wordpress/element';

Expand Down Expand Up @@ -61,8 +62,13 @@ class Inserter extends Component {
children,
onInsertBlock,
insertionPoint,
hasSupportedBlocks,
} = this.props;

if ( ! hasSupportedBlocks ) {
return null;
}

return (
<Dropdown
className="editor-inserter"
Expand Down Expand Up @@ -97,24 +103,33 @@ class Inserter extends Component {
}
}

export default connect(
( state ) => {
export default flowRight( [
connect(
( state ) => {
return {
insertionPoint: getBlockInsertionPoint( state ),
mode: getEditorMode( state ),
};
},
( dispatch ) => ( {
onInsertBlock( name, position ) {
dispatch( hideInsertionPoint() );
dispatch( insertBlock(
createBlock( name ),
position
) );
},
...bindActionCreators( {
setInsertionPoint: setBlockInsertionPoint,
clearInsertionPoint: clearBlockInsertionPoint,
}, dispatch ),
} )
),
withContext( 'editor' )( ( settings ) => {
const { blockTypes } = settings;

return {
insertionPoint: getBlockInsertionPoint( state ),
mode: getEditorMode( state ),
hasSupportedBlocks: true === blockTypes || ! isEmpty( blockTypes ),
};
},
( dispatch ) => ( {
onInsertBlock( name, position ) {
dispatch( hideInsertionPoint() );
dispatch( insertBlock(
createBlock( name ),
position
) );
},
...bindActionCreators( {
setInsertionPoint: setBlockInsertionPoint,
clearInsertionPoint: clearBlockInsertionPoint,
}, dispatch ),
} )
)( Inserter );
} ),
] )( Inserter );
79 changes: 52 additions & 27 deletions editor/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TabbableContainer,
withInstanceId,
withSpokenMessages,
withContext,
} from '@wordpress/components';
import { getCategories, getBlockTypes } from '@wordpress/blocks';
import { keycodes } from '@wordpress/utils';
Expand Down Expand Up @@ -112,27 +113,56 @@ export class InserterMenu extends Component {
}

getBlockTypes() {
const { blockTypes } = this.props;

// If all block types disabled, return empty set
if ( ! blockTypes ) {
return [];
}

// Block types that are marked as private should not appear in the inserter
return getBlockTypes().filter( ( block ) => ! block.isPrivate );
return getBlockTypes().filter( ( block ) => {
if ( block.isPrivate ) {
return false;
}

// Block types defined as either `true` or array:
// - True: Allow
// - Array: Check block name within whitelist
return (
! Array.isArray( blockTypes ) ||
includes( blockTypes, block.name )
);
} );
}

searchBlocks( blockTypes ) {
return searchBlocks( blockTypes, this.state.filterValue );
}

getBlocksForTab( tab ) {
const blockTypes = this.getBlockTypes();
// if we're searching, use everything, otherwise just get the blocks visible in this tab
if ( this.state.filterValue ) {
return this.getBlockTypes();
return blockTypes;
}

let predicate;
switch ( tab ) {
case 'recent':
return this.props.recentlyUsedBlocks;
predicate = ( block ) => find( this.props.recentlyUsedBlocks, { name: block.name } );
break;

case 'blocks':
return filter( this.getBlockTypes(), ( block ) => block.category !== 'embed' );
predicate = ( block ) => block.category !== 'embed';
break;

case 'embeds':
return filter( this.getBlockTypes(), ( block ) => block.category === 'embed' );
predicate = ( block ) => block.category === 'embed';
break;
}

return filter( blockTypes, predicate );
}

sortBlocks( blockTypes ) {
Expand Down Expand Up @@ -201,17 +231,18 @@ export class InserterMenu extends Component {
this.setState( { tab } );
}

renderTabView( tab, visibleBlocks ) {
switch ( tab ) {
case 'recent':
return this.renderBlocks( this.props.recentlyUsedBlocks, undefined );

case 'embed':
return this.renderBlocks( visibleBlocks.embed, undefined );
renderTabView( tab ) {
const blocksForTab = this.getBlocksForTab( tab );
if ( 'recent' === tab ) {
return this.renderBlocks( blocksForTab );
}

default:
return this.renderCategories( visibleBlocks, undefined );
const visibleBlocks = this.getVisibleBlocksByCategory( blocksForTab );
if ( 'embed' === tab ) {
return this.renderBlocks( visibleBlocks.embed );
}

return this.renderCategories( visibleBlocks );
}

interceptArrows( event ) {
Expand Down Expand Up @@ -266,19 +297,12 @@ export class InserterMenu extends Component {
},
] }
>
{
( tabKey ) => {
const blocksForTab = this.getBlocksForTab( tabKey );
const visibleBlocks = this.getVisibleBlocksByCategory( blocksForTab );

return (
<div ref={ ( ref ) => this.tabContainer = ref }
className="editor-inserter__content">
{ this.renderTabView( tabKey, visibleBlocks ) }
</div>
);
}
}
{ ( tabKey ) => (
<div ref={ ( ref ) => this.tabContainer = ref }
className="editor-inserter__content">
{ this.renderTabView( tabKey ) }
</div>
) }
</TabPanel>
}
{ isSearching &&
Expand All @@ -304,5 +328,6 @@ const connectComponent = connect(
export default flow(
withInstanceId,
withSpokenMessages,
withContext( 'editor' )( ( settings ) => pick( settings, 'blockTypes' ) ),
connectComponent
)( InserterMenu );
Loading

0 comments on commit 7067bae

Please sign in to comment.