@wordpress/block-library
Version:
Block library for the WordPress editor.
118 lines (117 loc) • 5.63 kB
JavaScript
import { store } from "@wordpress/block-editor";
import { cloneBlock } from "@wordpress/blocks";
// packages/block-library/src/list-item/utils.js
/**
* The previous sibling to nest a list item under when indenting, or undefined
* for the first item, which has no sibling to nest under.
*
* @param {Object} select The block editor store's selectors.
* @param {string} clientId The list item's client ID.
*
* @return {string|undefined} The client ID to nest the item under.
*/
function getIndentTarget(select, clientId) {
return select.getPreviousBlockClientId(clientId);
}
/**
* The ancestor list item the given item's list is nested in when outdenting, or
* undefined at the top level, where there is nothing to outdent to.
*
* @param {Object} select The block editor store's selectors.
* @param {string} clientId The list item's client ID.
*
* @return {string|undefined} The ancestor list item's client ID.
*/
function getOutdentTarget(select, clientId) {
const listId = select.getBlockRootClientId(clientId);
const parentListItemId = select.getBlockRootClientId(listId);
if (!parentListItemId || select.getBlockName(parentListItemId) !== "core/list-item") return;
return parentListItemId;
}
/**
* Moves blocks into a list item's nested list, keeping their client IDs. If the
* list item has no nested list yet, one is created from the source list so it
* inherits its attributes.
*
* @param {Object} registry The data registry.
* @param {string[]} clientIds The blocks to move.
* @param {string} sourceListId The list the blocks currently live in, also
* cloned when a nested list has to be created.
* @param {string} listItemId The list item to nest the blocks under.
*/
function moveBlocksToNestedList(registry, clientIds, sourceListId, listItemId) {
const { getBlockOrder, getBlock } = registry.select(store);
const { moveBlocksToPosition, removeBlocks, insertBlock } = registry.dispatch(store);
const nestedLists = getBlockOrder(listItemId);
const nestedListId = nestedLists[nestedLists.length - 1];
if (nestedListId) {
moveBlocksToPosition(clientIds, sourceListId, nestedListId);
return;
}
const nestedList = cloneBlock(getBlock(sourceListId), {}, clientIds.map((id) => getBlock(id)));
registry.batch(() => {
removeBlocks(clientIds, false);
insertBlock(nestedList, 0, listItemId, false);
});
}
/**
* Indents the selected list items, nesting them under the previous sibling of
* the given item. Defaults to the first selected item.
*
* @param {Object} registry The data registry.
* @param {string} [clientId] The list item providing the sibling to nest under.
*
* @return {boolean} Whether the items were indented.
*/
function indentListItems(registry, clientId) {
const select = registry.select(store);
const { getBlockRootClientId, getSelectedBlockClientIds, getSelectionStart, getSelectionEnd, hasMultiSelection, getMultiSelectedBlockClientIds } = select;
const { selectionChange, multiSelect } = registry.dispatch(store);
const _hasMultiSelection = hasMultiSelection();
const clientIds = _hasMultiSelection ? getMultiSelectedBlockClientIds() : getSelectedBlockClientIds();
if (clientId === void 0) clientId = clientIds[0];
const previousSiblingId = getIndentTarget(select, clientId);
if (!previousSiblingId) return false;
const rootClientId = getBlockRootClientId(clientId);
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();
registry.batch(() => {
moveBlocksToNestedList(registry, clientIds, rootClientId, previousSiblingId);
if (!_hasMultiSelection) selectionChange(clientIds[0], selectionEnd.attributeKey, selectionEnd.clientId === selectionStart.clientId ? selectionStart.offset : selectionEnd.offset, selectionEnd.offset);
else multiSelect(clientIds[0], clientIds[clientIds.length - 1]);
});
return true;
}
/**
* Outdents list items, moving them up a level. Defaults to the selected items;
* a specific set can be passed, e.g. to outdent an item's children.
*
* @param {Object} registry The data registry.
* @param {string[]|string} [clientIds] The list items to outdent.
*
* @return {boolean} Whether the items were outdented.
*/
function outdentListItems(registry, clientIds) {
const select = registry.select(store);
const { getBlockRootClientId, getBlockName, getBlockOrder, getBlockIndex, getSelectedBlockClientIds } = select;
const { moveBlocksToPosition, removeBlock, multiSelect } = registry.dispatch(store);
if (clientIds === void 0) clientIds = getSelectedBlockClientIds();
if (!Array.isArray(clientIds)) clientIds = [clientIds];
if (!clientIds.length) return false;
const firstClientId = clientIds[0];
if (getBlockName(firstClientId) !== "core/list-item") return false;
const parentListItemId = getOutdentTarget(select, firstClientId);
if (!parentListItemId) return false;
const parentListId = getBlockRootClientId(firstClientId);
const lastClientId = clientIds[clientIds.length - 1];
const followingListItems = getBlockOrder(parentListId).slice(getBlockIndex(lastClientId) + 1);
registry.batch(() => {
if (followingListItems.length) moveBlocksToNestedList(registry, followingListItems, parentListId, firstClientId);
moveBlocksToPosition(clientIds, parentListId, getBlockRootClientId(parentListItemId), getBlockIndex(parentListItemId) + 1);
if (!getBlockOrder(parentListId).length) removeBlock(parentListId, false);
if (clientIds.length > 1) multiSelect(firstClientId, lastClientId);
});
return true;
}
export { getIndentTarget, getOutdentTarget, indentListItems, moveBlocksToNestedList, outdentListItems };
//# sourceMappingURL=utils.mjs.map