sketch-json-parse
Version:
sketch-json解析
70 lines (69 loc) • 3.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const isCover_1 = require("./isCover");
const utils_1 = require("../utils");
/**
* 反向覆盖的元素则进行定位处理
* 前面图层应该都在后面图层的下面,如果前面图层zIndex大,则说明上面图层对下面图层进行了覆盖(例如:画板中有矩形完全填充作为背景)
* zIndex大即上面,上面的图层对下面的图层进行完全覆盖,则删除下面的图层
*/
const handleDelete = (data) => {
var _a, _b, _c, _d, _e, _f, _g;
let isDelete = false;
for (let i = 0; i < data.length; i++) {
const currItemI = data[i];
for (let j = i; j < data.length; j++) {
const currItemJ = data[j];
// zIndex大说明显示在上面,容器覆盖了原来的zIndex小的图层,删除原来的底层图层
if (currItemI.structure.zIndex > currItemJ.structure.zIndex &&
(0, isCover_1.default)(currItemI, currItemJ)) {
if (((_c = (_b = (_a = currItemI.style) === null || _a === void 0 ? void 0 : _a.background) === null || _b === void 0 ? void 0 : _b.color) === null || _c === void 0 ? void 0 : _c.alpha) === 1) {
currItemJ.isDelete = true;
}
}
else if (
// 容器顺序虽然没变,但上层图层能完全覆盖底层图层,上层图层会被当作新的容器,删除冗余的底层容器
currItemI.structure.zIndex < currItemJ.structure.zIndex &&
(0, isCover_1.default)(currItemJ, currItemI)) {
if (((_f = (_e = (_d = currItemJ.style) === null || _d === void 0 ? void 0 : _d.background) === null || _e === void 0 ? void 0 : _e.color) === null || _f === void 0 ? void 0 : _f.alpha) === 1) {
currItemI.isDelete = true;
}
}
}
// 容器无边框,无背景,无子元素,则删除
if (currItemI.componentName === "Container" &&
!(0, utils_1.hasBorderByLayer)(currItemI) &&
!(0, utils_1.hasBackgroundByLayer)(currItemI) &&
// !hasBorderRadiusByLayer(currItemI) &&
!((_g = currItemI.children) === null || _g === void 0 ? void 0 : _g.length)) {
currItemI.isDelete = true;
}
}
// 去掉需要删除的
const oldLength = data.length;
data = data.filter((item) => !item.isDelete);
// 判断是否存在删除的元素,存在删除,则让上级重新处理
if (oldLength !== data.length) {
isDelete = true;
}
let isChildrenDelete = false;
//递归处理children
for (let i = 0; i < data.length; i++) {
if (Array.isArray(data[i].children) && data[i].children.length > 0) {
const { data: childrenData, isDelete: childrenIsDelete } = handleDelete(data[i].children);
data[i].children = childrenData;
isChildrenDelete = isChildrenDelete || childrenIsDelete;
}
}
// 子节点有改动,则递归处理,删除原来children不为空的元素
if (isChildrenDelete) {
const result = handleDelete(data);
data = result.data;
isDelete = isDelete || result.isDelete;
}
return {
data,
isDelete,
};
};
exports.default = handleDelete;