UNPKG

@wordpress/block-library

Version:
34 lines (33 loc) • 1.51 kB
import { useRefEffect } from "@wordpress/compose"; import { TAB } from "@wordpress/keycodes"; import { store } from "@wordpress/block-editor"; import { useRegistry, useSelect } from "@wordpress/data"; import { indentListItems, outdentListItems } from "../utils.mjs"; // packages/block-library/src/list-item/hooks/use-multi-select-tab.js function useMultiSelectTab(clientId) { const registry = useRegistry(); const isActive = useSelect((select) => { const { hasMultiSelection, getMultiSelectedBlockClientIds, getBlockName } = select(store); if (!hasMultiSelection()) return false; const clientIds = getMultiSelectedBlockClientIds(); return clientIds[0] === clientId && clientIds.every((id) => getBlockName(id) === "core/list-item"); }, [clientId]); return useRefEffect((element) => { if (!isActive) return; function onKeyDown(event) { const { keyCode, shiftKey, altKey, metaKey, ctrlKey } = event; if (keyCode !== TAB || event.defaultPrevented || altKey || metaKey || ctrlKey) return; if (!event.target.contains(element)) return; if (shiftKey) { if (outdentListItems(registry)) event.preventDefault(); } else if (indentListItems(registry)) event.preventDefault(); } const { ownerDocument } = element; ownerDocument.addEventListener("keydown", onKeyDown, true); return () => { ownerDocument.removeEventListener("keydown", onKeyDown, true); }; }, [isActive, registry]); } export { useMultiSelectTab as default }; //# sourceMappingURL=use-multi-select-tab.mjs.map