Skip to content

Commit

Permalink
More semantic code
Browse files Browse the repository at this point in the history
Improve the logic for when an item should be rendered and add comments.
  • Loading branch information
oandregal committed Jul 16, 2018
1 parent f70c526 commit bada6cc
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ import { compose } from '@wordpress/element';
*/
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 ) ||
difference( selectedBlockNames, allowedBlockNames ).length === 0;
isEverySelectedBlockAllowed( selectedBlockNames, allowedBlockNames );

const BlockSettingsMenuPluginsItem = ( { allowedBlocks, icon, label, onClick, small, role } ) => (
<BlockSettingsMenuPluginsGroup>
Expand Down

0 comments on commit bada6cc

Please sign in to comment.