Skip to content

Commit

Permalink
Try: hide rich text toolbar if no selection
Browse files Browse the repository at this point in the history
Limit the content only toolbar for headings and paragraphs to only show if there is a text selection
  • Loading branch information
jeryj committed Aug 30, 2024
1 parent 70b257a commit 30c4aa6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ export default function BlockToolbarPopover( {
isTyping,
__unstableContentRef,
} ) {
const { capturingClientId, isInsertionPointVisible, lastClientId } =
useSelectedBlockToolProps( clientId );
const {
capturingClientId,
isInsertionPointVisible,
lastClientId,
isContentOnlyRichTextBlock,
hasTextSelection,
} = useSelectedBlockToolProps( clientId );

// Stores the active toolbar item index so the block toolbar can return focus
// to it when re-mounting.
Expand Down Expand Up @@ -56,8 +61,12 @@ export default function BlockToolbarPopover( {
clientId: clientIdToPositionOver,
} );

const showBlockToolbar =
( ! isTyping && ! isContentOnlyRichTextBlock ) ||
( isContentOnlyRichTextBlock && hasTextSelection );

return (
! isTyping && (
showBlockToolbar && (
<BlockPopover
clientId={ clientIdToPositionOver }
bottomClientId={ lastClientId }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { useSelect } from '@wordpress/data';
*/
import { store as blockEditorStore } from '../../store';

const CONTENT_ONLY_RICH_TEXT_BLOCKS = [ 'core/paragraph', 'core/heading' ];

/**
* Returns props for the selected block tools and empty block inserter.
*
Expand All @@ -24,7 +26,11 @@ export default function useSelectedBlockToolProps( clientId ) {
getBlockInsertionPoint,
getBlockOrder,
hasMultiSelection,
getSelectionStart,
getSelectionEnd,
getLastMultiSelectedBlockClientId,
getBlockEditingMode,
getBlockName,
} = select( blockEditorStore );

const blockParentsClientIds = getBlockParents( clientId );
Expand All @@ -50,13 +56,24 @@ export default function useSelectedBlockToolProps( clientId ) {
order[ insertionPoint.index ] === clientId;
}

const _hasMultiSelection = hasMultiSelection();

return {
capturingClientId,
isInsertionPointVisible,
lastClientId: hasMultiSelection()
lastClientId: _hasMultiSelection
? getLastMultiSelectedBlockClientId()
: null,
rootClientId: getBlockRootClientId( clientId ),
isContentOnlyRichTextBlock:
getBlockEditingMode( clientId ) === 'contentOnly' &&
CONTENT_ONLY_RICH_TEXT_BLOCKS.includes(
getBlockName( clientId )
),
// Maybe rely on documentHasTextSelection instead?
hasTextSelection:
! _hasMultiSelection &&
getSelectionStart().offset !== getSelectionEnd().offset,
};
},
[ clientId ]
Expand Down

0 comments on commit 30c4aa6

Please sign in to comment.