UNPKG

@wordpress/block-library

Version:
133 lines (114 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useOutdentListItem; var _lodash = require("lodash"); var _element = require("@wordpress/element"); var _data = require("@wordpress/data"); var _blockEditor = require("@wordpress/block-editor"); var _blocks = require("@wordpress/blocks"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const { name: listItemName } = { $schema: "https://schemas.wp.org/trunk/block.json", apiVersion: 2, name: "core/list-item", title: "List item", category: "text", parent: ["core/list"], description: "Create a list item.", textdomain: "default", attributes: { placeholder: { type: "string" }, content: { type: "string", source: "html", selector: "li", "default": "", __experimentalRole: "content" } }, supports: { className: false, __experimentalSelector: "li" } }; function useOutdentListItem(clientId) { const registry = (0, _data.useRegistry)(); const { canOutdent } = (0, _data.useSelect)(innerSelect => { const { getBlockRootClientId } = innerSelect(_blockEditor.store); const grandParentId = getBlockRootClientId(getBlockRootClientId(clientId)); return { canOutdent: !!grandParentId }; }, [clientId]); const { moveBlocksToPosition, removeBlock, insertBlock, updateBlockListSettings } = (0, _data.useDispatch)(_blockEditor.store); const { getBlockRootClientId, getBlockName, getBlockOrder, getBlockIndex, getSelectedBlockClientIds, getBlock, getBlockListSettings } = (0, _data.useSelect)(_blockEditor.store); function getParentListItemId(id) { const listId = getBlockRootClientId(id); const parentListItemId = getBlockRootClientId(listId); if (!parentListItemId) return; if (getBlockName(parentListItemId) !== listItemName) return; return parentListItemId; } return [canOutdent, (0, _element.useCallback)(function () { let clientIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getSelectedBlockClientIds(); clientIds = (0, _lodash.castArray)(clientIds); if (!clientIds.length) return; const firstClientId = clientIds[0]; // Can't outdent if it's not a list item. if (getBlockName(firstClientId) !== listItemName) return; const parentListItemId = getParentListItemId(firstClientId); // Can't outdent if it's at the top level. if (!parentListItemId) return; const parentListId = getBlockRootClientId(firstClientId); const lastClientId = clientIds[clientIds.length - 1]; const order = getBlockOrder(parentListId); const followingListItems = order.slice(getBlockIndex(lastClientId) + 1); registry.batch(() => { if (followingListItems.length) { let nestedListId = getBlockOrder(firstClientId)[0]; if (!nestedListId) { const nestedListBlock = (0, _blocks.cloneBlock)(getBlock(parentListId), {}, []); nestedListId = nestedListBlock.clientId; insertBlock(nestedListBlock, 0, firstClientId, false); // Immediately update the block list settings, otherwise // blocks can't be moved here due to canInsert checks. updateBlockListSettings(nestedListId, getBlockListSettings(parentListId)); } moveBlocksToPosition(followingListItems, parentListId, nestedListId); } moveBlocksToPosition(clientIds, parentListId, getBlockRootClientId(parentListItemId), getBlockIndex(parentListItemId) + 1); if (!getBlockOrder(parentListId).length) { removeBlock(parentListId); } }); }, [])]; } //# sourceMappingURL=use-outdent-list-item.js.map