@wordpress/block-library
Version:
Block library for the WordPress editor.
84 lines (83 loc) • 3.81 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_data = require("@wordpress/data");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_blocks = require("@wordpress/blocks");
let ___utils_js = require("../utils.cjs");
// packages/block-library/src/list-item/hooks/use-merge.js
function useMerge(clientId, onMerge) {
const registry = (0, _wordpress_data.useRegistry)();
return (forward) => {
const select = registry.select(_wordpress_block_editor.store);
const { getPreviousBlockClientId, getNextBlockClientId, getBlockOrder, getBlockIndex, getBlockRootClientId, getBlockName, getBlock } = select;
const { mergeBlocks, moveBlocksToPosition, removeBlock, insertBlocks } = registry.dispatch(_wordpress_block_editor.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 ((0, ___utils_js.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 = (0, ___utils_js.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 = (0, _wordpress_blocks.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 = (0, ___utils_js.getOutdentTarget)(select, clientId);
if (parentListItemId) {
mergeWithNested(parentListItemId, clientId);
return;
}
const blockOrder = getBlockOrder(clientId);
if ((0, _wordpress_blocks.isUnmodifiedBlock)(getBlock(clientId), "content") && blockOrder.length > 0) registry.batch(() => {
(0, ___utils_js.outdentListItems)(registry, getBlockOrder(blockOrder[0]));
removeBlock(clientId, true);
});
else onMerge(forward);
}
};
}
exports.default = useMerge;
//# sourceMappingURL=use-merge.cjs.map