-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow plugins to extend the BlockSettingsMenu
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { _BlockSettingsMenuPluginsItem } from '@wordpress/editor'; | ||
|
||
const PluginBlockSettingsMenuItem = _BlockSettingsMenuPluginsItem; | ||
export default PluginBlockSettingsMenuItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
editor/components/block-settings-menu/block-settings-menu-plugins-group.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEmpty, map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill } from '@wordpress/components'; | ||
import { Fragment } from '@wordpress/element'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
const { Fill: BlockSettingsMenuPluginsGroup, Slot } = createSlotFill( 'BlockSettingsMenuPluginsGroup' ); | ||
|
||
const BlockSettingsMenuPluginsGroupSlot = ( { fillProps, selectedBlocks } ) => { | ||
selectedBlocks = map( selectedBlocks, ( block ) => block.name ); | ||
return ( | ||
<Slot fillProps={ { ...fillProps, selectedBlocks } } > | ||
{ ( fills ) => ! isEmpty( fills ) && ( | ||
<Fragment> | ||
<div className="editor-block-settings-menu__separator" /> | ||
{ fills } | ||
</Fragment> | ||
) } | ||
</Slot> | ||
); | ||
}; | ||
|
||
BlockSettingsMenuPluginsGroup.Slot = withSelect( ( select, { fillProps: { uids } } ) => ( { | ||
selectedBlocks: select( 'core/editor' ).getBlocksByUID( uids ), | ||
} ) )( BlockSettingsMenuPluginsGroupSlot ); | ||
|
||
export default BlockSettingsMenuPluginsGroup; |
52 changes: 52 additions & 0 deletions
52
editor/components/block-settings-menu/block-settings-menu-plugins-item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { difference } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { IconButton } from '@wordpress/components'; | ||
import { compose } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockSettingsMenuPluginsGroup from './block-settings-menu-plugins-group'; | ||
|
||
const isEverySelectedBlockAllowed = ( selected, allowed ) => difference( selected, allowed ).length === 0; | ||
|
||
/** | ||
* Plugins may want to add an item to the menu either for every block | ||
* or only for the specific ones provided in the `allowedBlocks` component property. | ||
* | ||
* If there are multiple blocks selected the item will be rendered if every block | ||
* is of one allowed type (not necesarily the same). | ||
* | ||
* @param {string[]} selectedBlockNames Array containing the names of the blocks selected | ||
* @param {string[]} allowedBlockNames Array containing the names of the blocks allowed | ||
* @return {boolean} Whether the item will be rendered or not. | ||
*/ | ||
const shouldRenderItem = ( selectedBlockNames, allowedBlockNames ) => ! Array.isArray( allowedBlockNames ) || | ||
isEverySelectedBlockAllowed( selectedBlockNames, allowedBlockNames ); | ||
|
||
const BlockSettingsMenuPluginsItem = ( { allowedBlocks, icon, label, onClick, small, role } ) => ( | ||
<BlockSettingsMenuPluginsGroup> | ||
{ ( { selectedBlocks, onClose } ) => { | ||
if ( ! shouldRenderItem( selectedBlocks, allowedBlocks ) ) { | ||
return null; | ||
} | ||
return ( <IconButton | ||
className="editor-block-settings-menu__control" | ||
onClick={ compose( onClick, onClose ) } | ||
icon={ icon || 'admin-plugins' } | ||
label={ small ? label : undefined } | ||
role={ role } | ||
> | ||
{ ! small && label } | ||
</IconButton> ); | ||
} } | ||
</BlockSettingsMenuPluginsGroup> | ||
); | ||
|
||
export default BlockSettingsMenuPluginsItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters