-
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 block settings menu (#7895)
- Loading branch information
Showing
8 changed files
with
186 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
33 changes: 33 additions & 0 deletions
33
edit-post/components/block-settings-menu/plugin-block-settings-menu-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: PluginBlockSettingsMenuGroup, Slot } = createSlotFill( 'PluginBlockSettingsMenuGroup' ); | ||
|
||
const PluginBlockSettingsMenuGroupSlot = ( { 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> | ||
); | ||
}; | ||
|
||
PluginBlockSettingsMenuGroup.Slot = withSelect( ( select, { fillProps: { clientIds } } ) => ( { | ||
selectedBlocks: select( 'core/editor' ).getBlocksByUID( clientIds ), | ||
} ) )( PluginBlockSettingsMenuGroupSlot ); | ||
|
||
export default PluginBlockSettingsMenuGroup; |
52 changes: 52 additions & 0 deletions
52
edit-post/components/block-settings-menu/plugin-block-settings-menu-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 PluginBlockSettingsMenuGroup from './plugin-block-settings-menu-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 PluginBlockSettingsMenuItem = ( { allowedBlocks, icon, label, onClick, small, role } ) => ( | ||
<PluginBlockSettingsMenuGroup> | ||
{ ( { 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> ); | ||
} } | ||
</PluginBlockSettingsMenuGroup> | ||
); | ||
|
||
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
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
10 changes: 10 additions & 0 deletions
10
packages/editor/src/components/block-settings-menu/block-settings-menu-plugins-extension.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,10 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill } from '@wordpress/components'; | ||
|
||
const { Fill: _BlockSettingsMenuPluginsExtension, Slot } = createSlotFill( '_BlockSettingsMenuPluginsExtension' ); | ||
|
||
_BlockSettingsMenuPluginsExtension.Slot = Slot; | ||
|
||
export default _BlockSettingsMenuPluginsExtension; |
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