@wordpress/block-library
Version:
Block library for the WordPress editor.
133 lines (131 loc) • 4.6 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
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_use_outdent_list_item = __toESM(require("./use-outdent-list-item"));
function useMerge(clientId, onMerge) {
const registry = (0, import_data.useRegistry)();
const {
getPreviousBlockClientId,
getNextBlockClientId,
getBlockOrder,
getBlockRootClientId,
getBlockName
} = (0, import_data.useSelect)(import_block_editor.store);
const { mergeBlocks, moveBlocksToPosition } = (0, import_data.useDispatch)(import_block_editor.store);
const outdentListItem = (0, import_use_outdent_list_item.default)();
function getTrailingId(id) {
const order = getBlockOrder(id);
if (!order.length) {
return id;
}
return getTrailingId(order[order.length - 1]);
}
function getParentListItemId(id) {
const listId = getBlockRootClientId(id);
const parentListItemId = getBlockRootClientId(listId);
if (!parentListItemId) {
return;
}
if (getBlockName(parentListItemId) !== "core/list-item") {
return;
}
return parentListItemId;
}
function _getNextId(id) {
const next = getNextBlockClientId(id);
if (next) {
return next;
}
const parentListItemId = getParentListItemId(id);
if (!parentListItemId) {
return;
}
return _getNextId(parentListItemId);
}
function getNextId(id) {
const order = getBlockOrder(id);
if (!order.length) {
return _getNextId(id);
}
return getBlockOrder(order[0])[0];
}
return (forward) => {
function mergeWithNested(clientIdA, clientIdB) {
registry.batch(() => {
const [nestedListClientId] = getBlockOrder(clientIdB);
if (nestedListClientId) {
if (getPreviousBlockClientId(clientIdB) === clientIdA && !getBlockOrder(clientIdA).length) {
moveBlocksToPosition(
[nestedListClientId],
clientIdB,
clientIdA
);
} else {
moveBlocksToPosition(
getBlockOrder(nestedListClientId),
nestedListClientId,
getBlockRootClientId(clientIdA)
);
}
}
mergeBlocks(clientIdA, clientIdB);
});
}
if (forward) {
const nextBlockClientId = getNextId(clientId);
if (!nextBlockClientId) {
onMerge(forward);
return;
}
if (getParentListItemId(nextBlockClientId)) {
outdentListItem(nextBlockClientId);
} else {
mergeWithNested(clientId, nextBlockClientId);
}
} else {
const previousBlockClientId = getPreviousBlockClientId(clientId);
if (getParentListItemId(clientId)) {
outdentListItem(clientId);
} else if (previousBlockClientId) {
const trailingId = getTrailingId(previousBlockClientId);
mergeWithNested(trailingId, clientId);
} else {
onMerge(forward);
}
}
};
}
//# sourceMappingURL=use-merge.js.map