@@ -6,7 +6,11 @@ import { isTextField } from '@wordpress/dom';
66import { Popover } from '@wordpress/components' ;
77import { __unstableUseShortcutEventMatch as useShortcutEventMatch } from '@wordpress/keyboard-shortcuts' ;
88import { useRef } from '@wordpress/element' ;
9- import { switchToBlockType , store as blocksStore } from '@wordpress/blocks' ;
9+ import {
10+ switchToBlockType ,
11+ store as blocksStore ,
12+ hasBlockSupport ,
13+ } from '@wordpress/blocks' ;
1014import { speak } from '@wordpress/a11y' ;
1115import { __ , sprintf , _n } from '@wordpress/i18n' ;
1216
@@ -24,6 +28,7 @@ import usePopoverScroll from '../block-popover/use-popover-scroll';
2428import ZoomOutModeInserters from './zoom-out-mode-inserters' ;
2529import { useShowBlockTools } from './use-show-block-tools' ;
2630import { unlock } from '../../lock-unlock' ;
31+ import { cleanEmptyObject } from '../../hooks/utils' ;
2732import usePasteStyles from '../use-paste-styles' ;
2833
2934function selector ( select ) {
@@ -71,6 +76,7 @@ export default function BlockTools( {
7176 getSelectedBlockClientIds,
7277 getBlockRootClientId,
7378 isGroupable,
79+ getBlockName,
7480 } = useSelect ( blockEditorStore ) ;
7581 const { getGroupingBlockName } = useSelect ( blocksStore ) ;
7682 const { showEmptyBlockSideInserter, showBlockToolbarPopover } =
@@ -87,6 +93,7 @@ export default function BlockTools( {
8793 moveBlocksUp,
8894 moveBlocksDown,
8995 expandBlock,
96+ updateBlockAttributes,
9097 } = unlock ( useDispatch ( blockEditorStore ) ) ;
9198
9299 function onKeyDown ( event ) {
@@ -200,6 +207,44 @@ export default function BlockTools( {
200207 replaceBlocks ( clientIds , newBlocks ) ;
201208 speak ( __ ( 'Selected blocks are grouped.' ) ) ;
202209 }
210+ } else if (
211+ isMatch ( 'core/block-editor/toggle-block-visibility' , event )
212+ ) {
213+ const clientIds = getSelectedBlockClientIds ( ) ;
214+ if ( clientIds . length ) {
215+ event . preventDefault ( ) ;
216+ const blocks = getBlocksByClientId ( clientIds ) ;
217+ const canToggleBlockVisibility = blocks . every ( ( block ) =>
218+ hasBlockSupport (
219+ getBlockName ( block . clientId ) ,
220+ 'blockVisibility' ,
221+ true
222+ )
223+ ) ;
224+ if ( ! canToggleBlockVisibility ) {
225+ return ;
226+ }
227+ const hasHiddenBlock = blocks . some (
228+ ( block ) =>
229+ block . attributes . metadata ?. blockVisibility === false
230+ ) ;
231+ const attributesByClientId = Object . fromEntries (
232+ blocks . map ( ( { clientId : mapClientId , attributes } ) => [
233+ mapClientId ,
234+ {
235+ metadata : cleanEmptyObject ( {
236+ ...attributes ?. metadata ,
237+ blockVisibility : hasHiddenBlock
238+ ? undefined
239+ : false ,
240+ } ) ,
241+ } ,
242+ ] )
243+ ) ;
244+ updateBlockAttributes ( clientIds , attributesByClientId , {
245+ uniqueByBlock : true ,
246+ } ) ;
247+ }
203248 }
204249 }
205250 const blockToolbarRef = usePopoverScroll ( __unstableContentRef ) ;
0 commit comments