sketch-json-parse
Version:
sketch-json解析
68 lines (67 loc) • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAbsoluteBlock = exports.absoluteBlock = void 0;
/**
* 相对定位
*
* @param {*} data
* @param {*} parent
* @returns
*/
const absoluteBlock = (data, parent) => {
var _a, _b, _c, _d, _e, _f;
if (parent) {
if (!parent.style) {
parent.style = {};
}
if (!parent.style.position) {
parent.style.position = 'relative';
}
//边框处理
const leftBorderWidth = ((_c = (_b = (_a = parent.structure) === null || _a === void 0 ? void 0 : _a.border) === null || _b === void 0 ? void 0 : _b.left) === null || _c === void 0 ? void 0 : _c.width) || 0;
const topBorderWidth = ((_f = (_e = (_d = parent.structure) === null || _d === void 0 ? void 0 : _d.border) === null || _e === void 0 ? void 0 : _e.top) === null || _f === void 0 ? void 0 : _f.width) || 0;
for (let i = 0; i < data.length; i++) {
if (!data[i].style) {
data[i].style = {};
}
data[i].style = Object.assign(Object.assign({}, data[i].style), { position: 'absolute', left: data[i].structure.x - (parent.structure.x + leftBorderWidth), top: data[i].structure.y - (parent.structure.y + topBorderWidth) });
}
}
return data;
};
exports.absoluteBlock = absoluteBlock;
// 判断元素是否存在部分重叠
const isAbsoluteBlock = (data, absoluteMatchNames) => {
let flag = false;
if (data.find((t) => absoluteMatchNames.find((key) => t.name.includes(key)))) {
return true;
}
if (Array.isArray(data) && data.length > 1) {
let dataTemp = JSON.parse(JSON.stringify(data));
for (let i = 0; i < dataTemp.length; i++) {
const item = dataTemp[i];
for (let j = 0; j < dataTemp.length; j++) {
if (i === j) {
continue;
}
const itemJ = dataTemp[j];
const isBlockFlag = (item.structure.x <= itemJ.structure.x &&
itemJ.structure.x < item.structure.x + item.structure.width) ||
(itemJ.structure.x <= item.structure.x &&
item.structure.x < itemJ.structure.x + itemJ.structure.width);
const isRowFlag = (item.structure.y <= itemJ.structure.y &&
itemJ.structure.y < item.structure.y + item.structure.height) ||
(itemJ.structure.y <= item.structure.y &&
item.structure.y < itemJ.structure.y + itemJ.structure.height);
if (isBlockFlag && isRowFlag) {
flag = true;
}
}
if (flag) {
break;
}
}
}
return flag;
};
exports.isAbsoluteBlock = isAbsoluteBlock;