UNPKG

@wordpress/block-library

Version:
26 lines (25 loc) • 1.29 kB
import { useRefEffect } from "@wordpress/compose"; import { privateApis } from "@wordpress/rich-text"; import { SPACE, TAB } from "@wordpress/keycodes"; import { store } from "@wordpress/block-editor"; import { useRegistry } from "@wordpress/data"; import { isEntirelySelected } from "@wordpress/dom"; import { indentListItems, outdentListItems } from "../utils.mjs"; import { unlock } from "../../lock-unlock.mjs"; // packages/block-library/src/list-item/hooks/use-tab.js const { subscribeOwnedListener } = unlock(privateApis); function useTab() { const registry = useRegistry(); return useRefEffect((element) => { function onKeyDown(event) { const { keyCode, shiftKey, altKey, metaKey, ctrlKey } = event; if (event.defaultPrevented || keyCode !== SPACE && keyCode !== TAB || altKey || metaKey || ctrlKey) return; const { getSelectionStart, getSelectionEnd } = registry.select(store); if (!(getSelectionStart().offset === 0 && getSelectionEnd().offset === 0) && !(keyCode === TAB && isEntirelySelected(element))) return; if ((shiftKey ? outdentListItems : indentListItems)(registry)) event.preventDefault(); } return subscribeOwnedListener(element, "keydown", onKeyDown, true); }, [registry]); } export { useTab as default }; //# sourceMappingURL=use-tab.mjs.map