UNPKG

@gechiui/block-editor

Version:
78 lines (61 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useSelectAll; var _lodash = require("lodash"); var _dom = require("@gechiui/dom"); var _data = require("@gechiui/data"); var _keyboardShortcuts = require("@gechiui/keyboard-shortcuts"); var _compose = require("@gechiui/compose"); var _store = require("../../store"); /** * External dependencies */ /** * GeChiUI dependencies */ /** * Internal dependencies */ function useSelectAll() { const { getBlockOrder, getSelectedBlockClientIds, getBlockRootClientId } = (0, _data.useSelect)(_store.store); const { multiSelect } = (0, _data.useDispatch)(_store.store); const isMatch = (0, _keyboardShortcuts.__unstableUseShortcutEventMatch)(); return (0, _compose.useRefEffect)(node => { function onKeyDown(event) { if (!isMatch('core/block-editor/select-all', event)) { return; } if (!(0, _dom.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 = (0, _lodash.first)(blockClientIds); const lastClientId = (0, _lodash.last)(blockClientIds); if (firstClientId === lastClientId) { return; } multiSelect(firstClientId, lastClientId); event.preventDefault(); } node.addEventListener('keydown', onKeyDown); return () => { node.removeEventListener('keydown', onKeyDown); }; }, []); } //# sourceMappingURL=use-select-all.js.map