@wordpress/block-library
Version:
Block library for the WordPress editor.
141 lines (140 loc) • 4.37 kB
JavaScript
// packages/block-library/src/list-item/utils.js
import { store as blockEditorStore } from "@wordpress/block-editor";
import { cloneBlock } from "@wordpress/blocks";
function getIndentTarget(select, clientId) {
return select.getPreviousBlockClientId(clientId);
}
function getOutdentTarget(select, clientId) {
const listId = select.getBlockRootClientId(clientId);
const parentListItemId = select.getBlockRootClientId(listId);
if (!parentListItemId || select.getBlockName(parentListItemId) !== "core/list-item") {
return void 0;
}
return parentListItemId;
}
function moveBlocksToNestedList(registry, clientIds, sourceListId, listItemId) {
const { getBlockOrder, getBlock } = registry.select(blockEditorStore);
const { moveBlocksToPosition, removeBlocks, insertBlock } = registry.dispatch(blockEditorStore);
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);
});
}
function indentListItems(registry, clientId) {
const select = registry.select(blockEditorStore);
const {
getBlockRootClientId,
getSelectedBlockClientIds,
getSelectionStart,
getSelectionEnd,
hasMultiSelection,
getMultiSelectedBlockClientIds
} = select;
const { selectionChange, multiSelect } = registry.dispatch(blockEditorStore);
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;
}
function outdentListItems(registry, clientIds) {
const select = registry.select(blockEditorStore);
const {
getBlockRootClientId,
getBlockName,
getBlockOrder,
getBlockIndex,
getSelectedBlockClientIds
} = select;
const { moveBlocksToPosition, removeBlock, multiSelect } = registry.dispatch(blockEditorStore);
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 order = getBlockOrder(parentListId);
const followingListItems = order.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) {
const shouldSelectParent = false;
removeBlock(parentListId, shouldSelectParent);
}
if (clientIds.length > 1) {
multiSelect(firstClientId, lastClientId);
}
});
return true;
}
export {
getIndentTarget,
getOutdentTarget,
indentListItems,
moveBlocksToNestedList,
outdentListItems
};
//# sourceMappingURL=utils.mjs.map