-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: useBlockTools hook (#58979)
There are times where a block is selected but no tools are available. If we don't have any tools, we don't want to show a toolbar. This commit helps unify the logic so there is a source of truth for if it will be showing or not. * Adds a useHasBlockToolbar hook that has the logic for if the block-toolbar will render or not. * Adds a useBlockTools hook that privately exports which block tools will be showing (block toolbar popover, fixed block toolbar, block inserter, block breadcrumb). * Checks if any block tools are available before adding the is-collapsed class to the central toolbar area * Removes selected block tools wrapper from DOM if no block tools available. I'm not sure if this was intentional to leave it in the DOM before, or if it was because there wasn't a reliable way of checking for it. --------- Co-authored-by: jeryj <jeryj@git.wordpress.org> Co-authored-by: draganescu <andraganescu@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org>
- Loading branch information
1 parent
cca1165
commit 226b2ba
Showing
10 changed files
with
188 additions
and
155 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
49 changes: 49 additions & 0 deletions
49
packages/block-editor/src/components/block-toolbar/use-has-block-toolbar.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,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { getBlockType, hasBlockSupport } from '@wordpress/blocks'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import { useHasAnyBlockControls } from '../block-controls/use-has-block-controls'; | ||
|
||
/** | ||
* Returns true if the block toolbar should be shown. | ||
* | ||
* @return {boolean} Whether the block toolbar component will be rendered. | ||
*/ | ||
export function useHasBlockToolbar() { | ||
const hasAnyBlockControls = useHasAnyBlockControls(); | ||
return useSelect( | ||
( select ) => { | ||
const { | ||
getBlockEditingMode, | ||
getBlockName, | ||
getSelectedBlockClientIds, | ||
} = select( blockEditorStore ); | ||
|
||
const selectedBlockClientIds = getSelectedBlockClientIds(); | ||
const selectedBlockClientId = selectedBlockClientIds[ 0 ]; | ||
const isDefaultEditingMode = | ||
getBlockEditingMode( selectedBlockClientId ) === 'default'; | ||
const blockType = | ||
selectedBlockClientId && | ||
getBlockType( getBlockName( selectedBlockClientId ) ); | ||
const isToolbarEnabled = | ||
blockType && | ||
hasBlockSupport( blockType, '__experimentalToolbar', true ); | ||
|
||
if ( | ||
! isToolbarEnabled || | ||
( ! isDefaultEditingMode && ! hasAnyBlockControls ) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}, | ||
[ hasAnyBlockControls ] | ||
); | ||
} |
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
73 changes: 73 additions & 0 deletions
73
packages/block-editor/src/components/block-tools/use-show-block-tools.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,73 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import { useHasBlockToolbar } from '../block-toolbar/use-has-block-toolbar'; | ||
|
||
/** | ||
* Source of truth for which block tools are showing in the block editor. | ||
* | ||
* @return {Object} Object of which block tools will be shown. | ||
*/ | ||
export function useShowBlockTools() { | ||
const hasBlockToolbar = useHasBlockToolbar(); | ||
|
||
return useSelect( | ||
( select ) => { | ||
const { | ||
getSelectedBlockClientId, | ||
getFirstMultiSelectedBlockClientId, | ||
getBlock, | ||
getSettings, | ||
hasMultiSelection, | ||
__unstableGetEditorMode, | ||
isTyping, | ||
} = select( blockEditorStore ); | ||
|
||
const clientId = | ||
getSelectedBlockClientId() || | ||
getFirstMultiSelectedBlockClientId(); | ||
|
||
const { name = '', attributes = {} } = getBlock( clientId ) || {}; | ||
const editorMode = __unstableGetEditorMode(); | ||
const hasSelectedBlock = clientId && name; | ||
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock( { | ||
name, | ||
attributes, | ||
} ); | ||
const _showEmptyBlockSideInserter = | ||
clientId && | ||
! isTyping() && | ||
editorMode === 'edit' && | ||
isUnmodifiedDefaultBlock( { name, attributes } ); | ||
const maybeShowBreadcrumb = | ||
hasSelectedBlock && | ||
! hasMultiSelection() && | ||
( editorMode === 'navigation' || editorMode === 'zoom-out' ); | ||
|
||
return { | ||
showEmptyBlockSideInserter: _showEmptyBlockSideInserter, | ||
showBreadcrumb: | ||
! _showEmptyBlockSideInserter && maybeShowBreadcrumb, | ||
showBlockToolbarPopover: | ||
hasBlockToolbar && | ||
! getSettings().hasFixedToolbar && | ||
! _showEmptyBlockSideInserter && | ||
hasSelectedBlock && | ||
! isEmptyDefaultBlock && | ||
! maybeShowBreadcrumb, | ||
showFixedToolbar: | ||
editorMode !== 'zoom-out' && | ||
hasBlockToolbar && | ||
getSettings().hasFixedToolbar, | ||
}; | ||
}, | ||
[ hasBlockToolbar ] | ||
); | ||
} |
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
48 changes: 0 additions & 48 deletions
48
packages/block-editor/src/utils/use-can-block-toolbar-be-focused.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.