UNPKG

@gechiui/block-editor

Version:
69 lines (56 loc) 1.84 kB
/** * External dependencies */ import { first, last } from 'lodash'; /** * GeChiUI dependencies */ import { isEntirelySelected } from '@gechiui/dom'; import { useSelect, useDispatch } from '@gechiui/data'; import { __unstableUseShortcutEventMatch as useShortcutEventMatch } from '@gechiui/keyboard-shortcuts'; import { useRefEffect } from '@gechiui/compose'; /** * Internal dependencies */ import { store as blockEditorStore } from '../../store'; export default function useSelectAll() { const { getBlockOrder, getSelectedBlockClientIds, getBlockRootClientId, } = useSelect( blockEditorStore ); const { multiSelect } = useDispatch( blockEditorStore ); const isMatch = useShortcutEventMatch(); return useRefEffect( ( node ) => { function onKeyDown( event ) { if ( ! isMatch( 'core/block-editor/select-all', event ) ) { return; } if ( ! isEntirelySelected( event.target ) ) { return; } const selectedClientIds = getSelectedBlockClientIds(); const [ firstSelectedClientId ] = selectedClientIds; const rootClientId = getBlockRootClientId( firstSelectedClientId ); let blockClientIds = getBlockOrder( rootClientId ); // If we have selected all sibling nested blocks, try selecting up a // level. See: https://github.com/GeChiUI/gutenberg/pull/31859/ if ( selectedClientIds.length === blockClientIds.length ) { blockClientIds = getBlockOrder( getBlockRootClientId( rootClientId ) ); } const firstClientId = first( blockClientIds ); const lastClientId = last( blockClientIds ); if ( firstClientId === lastClientId ) { return; } multiSelect( firstClientId, lastClientId ); event.preventDefault(); } node.addEventListener( 'keydown', onKeyDown ); return () => { node.removeEventListener( 'keydown', onKeyDown ); }; }, [] ); }