-
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
Refactor: useBlockTools hook #58979
Refactor: useBlockTools hook #58979
Changes from all commits
5a2b603
55a9e7d
2cf7dea
d9427e9
96335d7
b6b7544
2c4a758
9f8bc36
41466cb
551d1a0
d5bac66
161454f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ]; | ||
Comment on lines
+27
to
+28
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. Suggestion: We can probably use 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. I do think that would be a good change because
So, as it would be a functional change, I think we should do it in a follow-up. 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. Follow-up at #59450 |
||
const isDefaultEditingMode = | ||
getBlockEditingMode( selectedBlockClientId ) === 'default'; | ||
const blockType = | ||
selectedBlockClientId && | ||
getBlockType( getBlockName( selectedBlockClientId ) ); | ||
const isToolbarEnabled = | ||
blockType && | ||
hasBlockSupport( blockType, '__experimentalToolbar', true ); | ||
|
||
if ( | ||
! isToolbarEnabled || | ||
( ! isDefaultEditingMode && ! hasAnyBlockControls ) | ||
) { | ||
return false; | ||
} | ||
Comment on lines
+38
to
+43
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. This is the same check that |
||
|
||
return true; | ||
}, | ||
[ hasAnyBlockControls ] | ||
); | ||
} |
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, | ||
Comment on lines
+55
to
+68
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. This hook subscribes to four values. Three of these are only used by I'm unsure if moving the selection of values used only by 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. I had concerns about that too. However, the information we need for 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. This is probably fine. We can always refactor if CodeVitals catches any perf regression. 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. It's fine, it's not worth trying to optimise performance here because there's only one block toolbar visible at any point in time. 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. Or maybe not, looking at the comments below. Not sure if this is the cause of the regression though. |
||
}; | ||
}, | ||
[ hasBlockToolbar ] | ||
); | ||
} |
This file was deleted.
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.
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.
This isn't a check to know if it will show or not, but rather if it can be shown. Like, does the current selection have a block toolbar it can render?
useShowBlockToolbar
makes me think it will do something to display the toolbar.