@wordpress/block-library
Version:
Block library for the WordPress editor.
161 lines (159 loc) • 5.63 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-library/src/list-item/hooks/use-merge.js
var use_merge_exports = {};
__export(use_merge_exports, {
default: () => useMerge
});
module.exports = __toCommonJS(use_merge_exports);
var import_data = require("@wordpress/data");
var import_block_editor = require("@wordpress/block-editor");
var import_blocks = require("@wordpress/blocks");
var import_utils = require("../utils.cjs");
function useMerge(clientId, onMerge) {
const registry = (0, import_data.useRegistry)();
return (forward) => {
const select = registry.select(import_block_editor.store);
const {
getPreviousBlockClientId,
getNextBlockClientId,
getBlockOrder,
getBlockIndex,
getBlockRootClientId,
getBlockName,
getBlock
} = select;
const { mergeBlocks, moveBlocksToPosition, removeBlock, insertBlocks } = registry.dispatch(import_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, import_utils.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, import_utils.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 transformed = (0, import_blocks.switchToBlockType)(
getBlock(followingBlockId),
"core/list"
);
const newInnerBlocks = transformed?.[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) {
const trailingId = getTrailingId(previousBlockClientId);
mergeWithNested(trailingId, clientId);
return;
}
const parentListItemId = (0, import_utils.getOutdentTarget)(select, clientId);
if (parentListItemId) {
mergeWithNested(parentListItemId, clientId);
return;
}
const blockOrder = getBlockOrder(clientId);
if ((0, import_blocks.isUnmodifiedBlock)(getBlock(clientId), "content") && blockOrder.length > 0) {
registry.batch(() => {
(0, import_utils.outdentListItems)(
registry,
getBlockOrder(blockOrder[0])
);
removeBlock(clientId, true);
});
} else {
onMerge(forward);
}
}
};
}
//# sourceMappingURL=use-merge.cjs.map