@wordpress/block-library
Version:
Block library for the WordPress editor.
79 lines (78 loc) • 3.53 kB
JavaScript
import { useRegistry } from "@wordpress/data";
import { store } from "@wordpress/block-editor";
import { isUnmodifiedBlock, switchToBlockType } from "@wordpress/blocks";
import { getOutdentTarget, outdentListItems } from "../utils.mjs";
// packages/block-library/src/list-item/hooks/use-merge.js
function useMerge(clientId, onMerge) {
const registry = useRegistry();
return (forward) => {
const select = registry.select(store);
const { getPreviousBlockClientId, getNextBlockClientId, getBlockOrder, getBlockIndex, getBlockRootClientId, getBlockName, getBlock } = select;
const { mergeBlocks, moveBlocksToPosition, removeBlock, insertBlocks } = registry.dispatch(store);
function getTrailingId(id) {
const order = getBlockOrder(id);
if (!order.length) return id;
return getTrailingId(order[order.length - 1]);
}
function mergeWithNested(clientIdA, clientIdB) {
registry.batch(() => {
const [nestedListClientId] = getBlockOrder(clientIdB);
if (nestedListClientId) {
if (getPreviousBlockClientId(clientIdB) === clientIdA && !getBlockOrder(clientIdA).length) moveBlocksToPosition([nestedListClientId], clientIdB, clientIdA);
else if (getOutdentTarget(select, clientIdB) === clientIdA) moveBlocksToPosition(getBlockOrder(nestedListClientId), nestedListClientId, getBlockRootClientId(clientIdB), getBlockIndex(clientIdB) + 1);
else moveBlocksToPosition(getBlockOrder(nestedListClientId), nestedListClientId, getBlockRootClientId(clientIdA));
}
const listId = getBlockRootClientId(clientIdB);
mergeBlocks(clientIdA, clientIdB);
if (!getBlockOrder(listId).length) removeBlock(listId, false);
});
}
if (forward) {
const innerListId = getBlockOrder(clientId)[0];
let nextBlockClientId;
let listItemId = clientId;
if (innerListId) nextBlockClientId = getBlockOrder(innerListId)[0];
else while (!(nextBlockClientId = getNextBlockClientId(listItemId))) {
const parentLi = getOutdentTarget(select, listItemId);
if (!parentLi) break;
listItemId = parentLi;
}
if (!nextBlockClientId) {
const outerListId = getBlockRootClientId(listItemId);
const followingBlockId = getNextBlockClientId(outerListId);
if (followingBlockId) {
if (getBlockName(followingBlockId) === "core/list") registry.batch(() => {
moveBlocksToPosition(getBlockOrder(followingBlockId), followingBlockId, outerListId);
removeBlock(followingBlockId, false);
});
else {
const newInnerBlocks = switchToBlockType(getBlock(followingBlockId), "core/list")?.[0]?.innerBlocks;
if (newInnerBlocks?.length) registry.batch(() => {
insertBlocks(newInnerBlocks, void 0, outerListId, false);
removeBlock(followingBlockId, false);
});
}
}
} else mergeWithNested(clientId, nextBlockClientId);
} else {
const previousBlockClientId = getPreviousBlockClientId(clientId);
if (previousBlockClientId) {
mergeWithNested(getTrailingId(previousBlockClientId), clientId);
return;
}
const parentListItemId = getOutdentTarget(select, clientId);
if (parentListItemId) {
mergeWithNested(parentListItemId, clientId);
return;
}
const blockOrder = getBlockOrder(clientId);
if (isUnmodifiedBlock(getBlock(clientId), "content") && blockOrder.length > 0) registry.batch(() => {
outdentListItems(registry, getBlockOrder(blockOrder[0]));
removeBlock(clientId, true);
});
else onMerge(forward);
}
};
}
export { useMerge as default };
//# sourceMappingURL=use-merge.mjs.map