Skip to content

Commit

Permalink
Widgets editor: Add basic options for extensibility (#25758)
Browse files Browse the repository at this point in the history
* Widgets editor: Add basic options for extensibility

- Adds a WidgetAreas.Sidebar Fill component so that third party plugins
  can add custom UI to the widget editor's sidebar.

- Sets the widget editor's block editor provider to not use a
  sub-registry, so that third party plugins can add custom blocks
  (including custom UI) to the block editor used in this screen.

* Remove the WidgetAreas.Sidebar slot for now
  • Loading branch information
noisysocks authored Oct 5, 2020
1 parent 117f09e commit 5283628
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
51 changes: 23 additions & 28 deletions packages/edit-widgets/src/components/sidebar/block-areas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,25 @@ import { BlockIcon } from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function BlockArea( { clientId } ) {
const { name, selectedBlock } = useSelect(
( select ) => {
const { getBlockAttributes, getBlockSelectionStart } = select(
'core/block-editor'
);
return {
name: getBlockAttributes( clientId ).name,
selectedBlock: getBlockSelectionStart(),
};
},
[ clientId ]
function BlockArea( { block } ) {
const selectedBlock = useSelect(
( select ) => select( 'core/block-editor' ).getBlockSelectionStart(),
[]
);

const { selectBlock } = useDispatch( 'core/block-editor' );
const isSelected = selectedBlock === clientId;

if ( block.name !== 'core/widget-area' ) {
return null;
}

return (
<li>
<Button
aria-expanded={ isSelected }
onClick={
isSelected
? undefined
: () => {
selectBlock( clientId );
}
}
aria-expanded={ selectedBlock === block.clientId }
onClick={ () => selectBlock( block.clientId ) }
>
{ name }
{ block.attributes.name }
<span className="edit-widgets-block-areas__edit">
{ __( 'Edit' ) }
</span>
Expand All @@ -44,10 +35,13 @@ function BlockArea( { clientId } ) {
}

export default function BlockAreas() {
const blockOrder = useSelect( ( select ) => {
return select( 'core/block-editor' ).getBlockOrder();
} );
const hasBlockAreas = blockOrder.length > 0;
const blocks = useSelect(
( select ) => select( 'core/block-editor' ).getBlocks(),
[]
);

const hasBlockAreas = blocks.length > 0;

return (
<>
<div className="edit-widgets-block-areas">
Expand All @@ -68,10 +62,11 @@ export default function BlockAreas() {
) }
</div>
</div>

{ hasBlockAreas && (
<ul>
{ blockOrder.map( ( clientId ) => (
<BlockArea key={ clientId } clientId={ clientId } />
{ blocks.map( ( block ) => (
<BlockArea key={ block.clientId } block={ block } />
) ) }
</ul>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function WidgetAreasBlockEditorProvider( {
onInput={ onInput }
onChange={ onChange }
settings={ settings }
useSubRegistry={ false }
{ ...props }
/>
</FocusReturnProvider>
Expand Down

0 comments on commit 5283628

Please sign in to comment.