react-layouts-builder
Version:
Lightweight and powerfull react layouts drag and drop
52 lines (40 loc) • 1.86 kB
JavaScript
import { __spreadArray, __assign } from '../../node_modules/tslib/tslib.es6.js';
import { createLayoutBlock } from '../helpers/create.js';
function insertLayout(options) {
var layouts = options.layouts,
targetBlockId = options.targetBlockId,
block = options.block;
function findParentAndInsert(layout) {
var _a, _b, _c;
var targetLayout = (_a = layout.childrens) === null || _a === void 0 ? void 0 : _a.find(function (child) {
var _a, _b;
var blockId = ((_a = child.block) === null || _a === void 0 ? void 0 : _a['@id']) || ((_b = child.block) === null || _b === void 0 ? void 0 : _b.id);
return child.block && blockId === targetBlockId;
});
if (targetLayout && ((_b = layout.childrens) === null || _b === void 0 ? void 0 : _b.length)) {
var childrens = layout.childrens.reduce(function (childs, nextChild) {
var _a, _b;
var nextBlockId = ((_a = nextChild.block) === null || _a === void 0 ? void 0 : _a['@id']) || ((_b = nextChild.block) === null || _b === void 0 ? void 0 : _b.id);
if (nextChild.block && nextBlockId === targetBlockId) {
return __spreadArray(__spreadArray([], childs, true), [nextChild, createLayoutBlock(block)], false);
}
return childs.concat(nextChild);
}, []);
return __assign(__assign({}, layout), {
childrens: childrens
});
} else if (layout.childrens && ((_c = layout.childrens) === null || _c === void 0 ? void 0 : _c.length)) {
var newLayout = layout.childrens.map(function (child) {
return findParentAndInsert(child);
}).filter(function (child) {
return child !== null;
});
return __assign(__assign({}, layout), {
childrens: newLayout
});
}
return layout;
}
return findParentAndInsert(layouts);
}
export { insertLayout };