Skip to content

Commit

Permalink
Select nearest *inner block* of Post Content
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Mar 20, 2024
1 parent 2a9bc68 commit 9a8db3f
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function useFlashEditableBlocks( {
rootClientId = '',
isEnabled = true,
} = {} ) {
const { getEnabledClientIdsTree } = unlock( useSelect( blockEditorStore ) );
const { getEnabledClientIdsTree, getBlockName, getBlockOrder } = unlock(
useSelect( blockEditorStore )
);
const { selectBlock } = useDispatch( blockEditorStore );

return useRefEffect(
Expand Down Expand Up @@ -54,12 +56,21 @@ export function useFlashEditableBlocks( {
const selectClosestEditableBlock = ( x, y ) => {
const editableBlockClientIds = getEnabledClientIdsTree(
rootClientId
).map( ( { clientId } ) => clientId );
).flatMap( ( { clientId } ) => {
// TODO: We shouldn't be referencing a particular block in @wordpress/block-editor
if ( getBlockName( clientId ) === 'core/post-content' ) {
const innerBlocks = getBlockOrder( clientId );
if ( innerBlocks.length ) {
return innerBlocks;
}
}
return [ clientId ];
} );
let closestDistance = Infinity,
closestClientId = null;
for ( const id of editableBlockClientIds ) {
for ( const clientId of editableBlockClientIds ) {
const block = element.querySelector(
`[data-block="${ id }"]`
`[data-block="${ clientId }"]`
);
if ( ! block ) {
continue;
Expand All @@ -68,7 +79,7 @@ export function useFlashEditableBlocks( {
const distance = distanceFromRect( x, y, rect );
if ( distance < closestDistance ) {
closestDistance = distance;
closestClientId = id;
closestClientId = clientId;
}
}
if ( closestClientId ) {
Expand Down

0 comments on commit 9a8db3f

Please sign in to comment.