forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
BlockManager
component reusable (WordPress#67052)
* Make `BlockManager` component reusable * Convert a prop to an array * Revert "Convert a prop to an array" This reverts commit 242ba0e. Co-authored-by: t-hamano <[email protected]> Co-authored-by: ramonjd <[email protected]> Co-authored-by: youknowriad <[email protected]>
- Loading branch information
1 parent
6b42879
commit 180211c
Showing
5 changed files
with
168 additions
and
86 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
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
94 changes: 94 additions & 0 deletions
94
packages/editor/src/components/preferences-modal/block-visibility.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,94 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
import { hasBlockSupport, store as blocksStore } from '@wordpress/blocks'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
import BlockManager from '../block-manager'; | ||
|
||
export default function BlockVisibility() { | ||
const { showBlockTypes, hideBlockTypes } = unlock( | ||
useDispatch( editorStore ) | ||
); | ||
|
||
const { | ||
blockTypes, | ||
allowedBlockTypes: _allowedBlockTypes, | ||
hiddenBlockTypes: _hiddenBlockTypes, | ||
} = useSelect( ( select ) => { | ||
return { | ||
blockTypes: select( blocksStore ).getBlockTypes(), | ||
allowedBlockTypes: | ||
select( editorStore ).getEditorSettings().allowedBlockTypes, | ||
hiddenBlockTypes: | ||
select( preferencesStore ).get( 'core', 'hiddenBlockTypes' ) ?? | ||
[], | ||
}; | ||
}, [] ); | ||
|
||
const allowedBlockTypes = useMemo( () => { | ||
if ( _allowedBlockTypes === true ) { | ||
return blockTypes; | ||
} | ||
return blockTypes.filter( ( { name } ) => { | ||
return _allowedBlockTypes?.includes( name ); | ||
} ); | ||
}, [ _allowedBlockTypes, blockTypes ] ); | ||
|
||
const filteredBlockTypes = allowedBlockTypes.filter( | ||
( blockType ) => | ||
hasBlockSupport( blockType, 'inserter', true ) && | ||
( ! blockType.parent || | ||
blockType.parent.includes( 'core/post-content' ) ) | ||
); | ||
|
||
// Some hidden blocks become unregistered | ||
// by removing for instance the plugin that registered them, yet | ||
// they're still remain as hidden by the user's action. | ||
// We consider "hidden", blocks which were hidden and | ||
// are still registered. | ||
const hiddenBlockTypes = _hiddenBlockTypes.filter( ( hiddenBlock ) => { | ||
return filteredBlockTypes.some( | ||
( registeredBlock ) => registeredBlock.name === hiddenBlock | ||
); | ||
} ); | ||
|
||
const selectedBlockTypes = filteredBlockTypes.filter( | ||
( blockType ) => ! hiddenBlockTypes.includes( blockType.name ) | ||
); | ||
|
||
const onChangeSelectedBlockTypes = ( newSelectedBlockTypes ) => { | ||
if ( selectedBlockTypes.length > newSelectedBlockTypes.length ) { | ||
const blockTypesToHide = selectedBlockTypes.filter( | ||
( blockType ) => | ||
! newSelectedBlockTypes.find( | ||
( { name } ) => name === blockType.name | ||
) | ||
); | ||
hideBlockTypes( blockTypesToHide.map( ( { name } ) => name ) ); | ||
} else if ( selectedBlockTypes.length < newSelectedBlockTypes.length ) { | ||
const blockTypesToShow = newSelectedBlockTypes.filter( | ||
( blockType ) => | ||
! selectedBlockTypes.find( | ||
( { name } ) => name === blockType.name | ||
) | ||
); | ||
showBlockTypes( blockTypesToShow.map( ( { name } ) => name ) ); | ||
} | ||
}; | ||
|
||
return ( | ||
<BlockManager | ||
blockTypes={ filteredBlockTypes } | ||
selectedBlockTypes={ selectedBlockTypes } | ||
onChange={ onChangeSelectedBlockTypes } | ||
/> | ||
); | ||
} |
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