Skip to content

Commit

Permalink
Editor: Bring back Inspector Advnaced Controls
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 3, 2018
1 parent 3d143d0 commit eecc528
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 64 deletions.
38 changes: 21 additions & 17 deletions blocks/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { assign } from 'lodash';
/**
* WordPress dependencies
*/
import { createHigherOrderComponent } from '@wordpress/element';
import { createHigherOrderComponent, Fragment } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -15,7 +15,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { hasBlockSupport } from '../api';
import InspectorControls from '../inspector-controls';
import InspectorAdvancedControls from '../inspector-advanced-controls';

/**
* Regular expression matching invalid anchor characters for replacement.
Expand Down Expand Up @@ -59,21 +59,25 @@ export function addAttribute( settings ) {
export const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
const hasAnchor = hasBlockSupport( props.name, 'anchor' ) && props.isSelected;
return [
<BlockEdit key="block-edit-anchor" { ...props } />,
hasAnchor && <InspectorControls key="inspector-anchor">
<TextControl
label={ __( 'HTML Anchor' ) }
help={ __( 'Anchors lets you link directly to a section on a page.' ) }
value={ props.attributes.anchor || '' }
onChange={ ( nextValue ) => {
nextValue = nextValue.replace( ANCHOR_REGEX, '-' );
props.setAttributes( {
anchor: nextValue,
} );
} } />
</InspectorControls>,
];
return (
<Fragment>
<BlockEdit { ...props } />
{ hasAnchor && (
<InspectorAdvancedControls>
<TextControl
label={ __( 'HTML Anchor' ) }
help={ __( 'Anchors lets you link directly to a section on a page.' ) }
value={ props.attributes.anchor || '' }
onChange={ ( nextValue ) => {
nextValue = nextValue.replace( ANCHOR_REGEX, '-' );
props.setAttributes( {
anchor: nextValue,
} );
} } />
</InspectorAdvancedControls>
) }
</Fragment>
);
};
}, 'withInspectorControl' );

Expand Down
36 changes: 20 additions & 16 deletions blocks/hooks/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createHigherOrderComponent } from '@wordpress/element';
import { createHigherOrderComponent, Fragment } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -16,7 +16,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { hasBlockSupport } from '../api';
import InspectorControls from '../inspector-controls';
import InspectorAdvancedControls from '../inspector-advanced-controls';

/**
* Filters registered block settings, extending attributes with anchor using ID
Expand Down Expand Up @@ -51,20 +51,24 @@ export const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) =>
return ( props ) => {
const hasCustomClassName = hasBlockSupport( props.name, 'customClassName', true ) && props.isSelected;

return [
<BlockEdit key="block-edit-custom-class-name" { ...props } />,
hasCustomClassName && <InspectorControls key="inspector-custom-class-name">
<TextControl
label={ __( 'Additional CSS Class' ) }
value={ props.attributes.className || '' }
onChange={ ( nextValue ) => {
props.setAttributes( {
className: nextValue,
} );
} }
/>
</InspectorControls>,
];
return (
<Fragment>
<BlockEdit { ...props } />
{ hasCustomClassName && (
<InspectorAdvancedControls>
<TextControl
label={ __( 'Additional CSS Class' ) }
value={ props.attributes.className || '' }
onChange={ ( nextValue ) => {
props.setAttributes( {
className: nextValue,
} );
} }
/>
</InspectorAdvancedControls>
) }
</Fragment>
);
};
}, 'withInspectorControl' );

Expand Down
1 change: 1 addition & 0 deletions blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { default as Editable } from './rich-text/editable';
export { default as ImagePlaceholder } from './image-placeholder';
export { default as InnerBlocks } from './inner-blocks';
export { default as InspectorControls } from './inspector-controls';
export { default as InspectorAdvancedControls } from './inspector-advanced-controls';
export { default as PlainText } from './plain-text';
export { default as MediaUpload } from './media-upload';
export { default as RichText } from './rich-text';
Expand Down
6 changes: 6 additions & 0 deletions blocks/inspector-advanced-controls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* WordPress dependencies
*/
import { createSlotFill } from '@wordpress/components';

export default createSlotFill( 'InspectorAdvancedControls' );
10 changes: 2 additions & 8 deletions blocks/inspector-controls/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/**
* WordPress dependencies
*/
import { Fill } from '@wordpress/components';
import { createSlotFill } from '@wordpress/components';

export default function InspectorControls( { children } ) {
return (
<Fill name="Inspector.Controls">
{ children }
</Fill>
);
}
export default createSlotFill( 'InspectorControls' );
2 changes: 1 addition & 1 deletion components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export { default as ToggleControl } from './toggle-control';
export { default as Toolbar } from './toolbar';
export { default as Tooltip } from './tooltip';
export { default as TreeSelect } from './tree-select';
export { Slot, Fill, Provider as SlotFillProvider } from './slot-fill';
export { createSlotFill, Slot, Fill, Provider as SlotFillProvider } from './slot-fill';

// Higher-Order Components
export { default as ifCondition } from './higher-order/if-condition';
Expand Down
17 changes: 16 additions & 1 deletion components/slot-fill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,19 @@ export { Slot };
export { Fill };
export { Provider };

export default { Slot, Fill, Provider };
export function createSlotFill( name ) {
const Component = ( { children, ...props } ) => (
<Fill name={ name } { ...props }>
{ children }
</Fill>
);
Component.displayName = name;

Component.Slot = ( { children, ...props } ) => (
<Slot name={ name } { ...props }>
{ children }
</Slot>
);

return Component;
}
38 changes: 20 additions & 18 deletions components/slot-fill/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,34 @@ class Slot extends Component {
}

render() {
const { name, bubblesVirtually = false, fillProps = {} } = this.props;
const { children, name, bubblesVirtually = false, fillProps = {} } = this.props;
const { getFills = noop } = this.context;

if ( bubblesVirtually ) {
return <div ref={ this.bindNode } />;
}

const fills = map( getFills( name ), ( fill ) => {
const fillKey = fill.occurrence;

// If a function is passed as a child, render it with the fillProps.
if ( isFunction( fill.props.children ) ) {
return cloneElement( fill.props.children( fillProps ), { key: fillKey } );
}

return Children.map( fill.props.children, ( child, childIndex ) => {
if ( ! child || isString( child ) ) {
return child;
}

const childKey = `${ fillKey }---${ child.key || childIndex }`;
return cloneElement( child, { key: childKey } );
} );
} );

return (
<div ref={ this.bindNode }>
{ map( getFills( name ), ( fill ) => {
const fillKey = fill.occurrence;

// If a function is passed as a child, render it with the fillProps.
if ( isFunction( fill.props.children ) ) {
return cloneElement( fill.props.children( fillProps ), { key: fillKey } );
}

return Children.map( fill.props.children, ( child, childIndex ) => {
if ( ! child || isString( child ) ) {
return child;
}

const childKey = `${ fillKey }---${ child.key || childIndex }`;
return cloneElement( child, { key: childKey } );
} );
} ) }
{ isFunction( children ) ? children( fills.filter( Boolean ) ) : fills }
</div>
);
}
Expand Down
4 changes: 4 additions & 0 deletions edit-post/components/sidebar/block-inspector-panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
.components-panel__body-toggle {
color: $dark-gray-500;
}

&.editor-block-inspector__advanced {
margin-bottom: -16px;
}
}
21 changes: 18 additions & 3 deletions editor/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { connect } from 'react-redux';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Slot } from '@wordpress/components';
import { getBlockType, BlockIcon } from '@wordpress/blocks';
import {
BlockIcon,
getBlockType,
InspectorControls,
InspectorAdvancedControls,
} from '@wordpress/blocks';
import { PanelBody } from '@wordpress/components';

/**
* Internal Dependencies
Expand Down Expand Up @@ -37,7 +42,17 @@ const BlockInspector = ( { selectedBlock, count } ) => {
<div className="editor-block-inspector__card-description">{ blockType.description }</div>
</div>
</div>,
<Slot name="Inspector.Controls" key="inspector-controls" />,
<InspectorControls.Slot key="inspector-controls" />,
<InspectorAdvancedControls.Slot key="inspector-advanced-controls">
{ ( fills ) => fills.length && (
<PanelBody
className="editor-block-inspector__advanced"
title={ __( 'Advanced' ) }
>
{ fills }
</PanelBody>
) }
</InspectorAdvancedControls.Slot>,
];
};

Expand Down

0 comments on commit eecc528

Please sign in to comment.