forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try selecting closest editable block when clicking on a disabled block (
WordPress#60016) * Try selecting closest editable block when clicking on a disabled block * Remove 'Edit template' notification * Select nearest *inner block* of Post Content * Move to own hook in @wordpress/editor * Revert use-block-props/index.js * Don't select elements that are too far away Co-authored-by: noisysocks <[email protected]> Co-authored-by: jasmussen <[email protected]> Co-authored-by: andrewserong <[email protected]> Co-authored-by: jameskoster <[email protected]> Co-authored-by: richtabor <[email protected]> Co-authored-by: SaxonF <[email protected]>
- Loading branch information
1 parent
b80cfa8
commit eb435ea
Showing
3 changed files
with
105 additions
and
56 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
95 changes: 95 additions & 0 deletions
95
packages/editor/src/hooks/use-select-nearest-editable-block.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,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRefEffect } from '@wordpress/compose'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../lock-unlock'; | ||
|
||
const DISTANCE_THRESHOLD = 500; | ||
|
||
function clamp( value, min, max ) { | ||
return Math.min( Math.max( value, min ), max ); | ||
} | ||
|
||
function distanceFromRect( x, y, rect ) { | ||
const dx = x - clamp( x, rect.left, rect.right ); | ||
const dy = y - clamp( y, rect.top, rect.bottom ); | ||
return Math.sqrt( dx * dx + dy * dy ); | ||
} | ||
|
||
export default function useSelectNearestEditableBlock( { | ||
isEnabled = true, | ||
} = {} ) { | ||
const { getEnabledClientIdsTree, getBlockName, getBlockOrder } = unlock( | ||
useSelect( blockEditorStore ) | ||
); | ||
const { selectBlock } = useDispatch( blockEditorStore ); | ||
|
||
return useRefEffect( | ||
( element ) => { | ||
if ( ! isEnabled ) { | ||
return; | ||
} | ||
|
||
const selectNearestEditableBlock = ( x, y ) => { | ||
const editableBlockClientIds = | ||
getEnabledClientIdsTree().flatMap( ( { clientId } ) => { | ||
const blockName = getBlockName( clientId ); | ||
if ( blockName === 'core/template-part' ) { | ||
return []; | ||
} | ||
if ( blockName === 'core/post-content' ) { | ||
const innerBlocks = getBlockOrder( clientId ); | ||
if ( innerBlocks.length ) { | ||
return innerBlocks; | ||
} | ||
} | ||
return [ clientId ]; | ||
} ); | ||
|
||
let nearestDistance = Infinity, | ||
nearestClientId = null; | ||
|
||
for ( const clientId of editableBlockClientIds ) { | ||
const block = element.querySelector( | ||
`[data-block="${ clientId }"]` | ||
); | ||
if ( ! block ) { | ||
continue; | ||
} | ||
const rect = block.getBoundingClientRect(); | ||
const distance = distanceFromRect( x, y, rect ); | ||
if ( | ||
distance < nearestDistance && | ||
distance < DISTANCE_THRESHOLD | ||
) { | ||
nearestDistance = distance; | ||
nearestClientId = clientId; | ||
} | ||
} | ||
|
||
if ( nearestClientId ) { | ||
selectBlock( nearestClientId ); | ||
} | ||
}; | ||
|
||
const handleClick = ( event ) => { | ||
const shouldSelect = | ||
event.target === element || | ||
event.target.classList.contains( 'is-root-container' ); | ||
if ( shouldSelect ) { | ||
selectNearestEditableBlock( event.clientX, event.clientY ); | ||
} | ||
}; | ||
|
||
element.addEventListener( 'click', handleClick ); | ||
return () => element.removeEventListener( 'click', handleClick ); | ||
}, | ||
[ isEnabled ] | ||
); | ||
} |