-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block settings extensibility #7895
Merged
+186
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59c9b19
Allow plugins to extend the BlockSettingsMenu
oandregal 360dc9d
Change uids to clientIds
oandregal 3d216ad
Move components to edit-post
oandregal 04b8b82
Decouple editor and edit-post
oandregal 0e6d2f8
Adapt to rebase
oandregal 740f68e
Add docs
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, |
||
|
||
/** | ||
* 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getBlocksByUID
was deprecated in Gutenberg v3.3 and is slated to be removed in the next release. A warning would have been logged to your console on its use.