UNPKG

sketch-json-parse

Version:

sketch-json解析

114 lines (113 loc) 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); /** * 清理冗余结构处理 * 1.Container 子元素无背景,无边框属于无意义元素 -删除子元素处理 * 2.Container 子元素有背景或者边框,父元素无背景及边框-删除父元素 * 3.Image 父元素无背景及边框-删除父元素 * 4.Image 子元素与父元素位置大小完全相同 -删除父元素 * 5.父元素为artboard的情况下,不删除父元素 * @param {Array} item */ const handleSingleLayer = (item) => { var _a, _b; // TODO 判断阴影等其他样式情况不存在时,删除父元素 if ( // false && item.children && item.children.length == 1) { //Container 子元素无背景,无边框属于无意义元素 -删除子元素处理 if (item.children[0].componentName == "Container") { if ( // !item.children[0].textContainer && !(0, utils_1.hasBorderByLayer)(item.children[0]) && !(0, utils_1.hasBackgroundByLayer)(item.children[0]) && // !hasBorderRadiusByLayer(item.children[0]) && // !hasShadowByLayer(item.children[0]) && item.children[0]._class !== "artboard" && !item.children[0].style.transform) { if (item.children[0].children) { item.children = JSON.parse(JSON.stringify(item.children[0].children)); } else { item.children = []; } item = handleSingleLayer(item); } else if (item._class !== "artboard" && !item.style.transform) { //Container 子元素有背景或者边框 且父元素不为artboard //父元素无背景及边框-删除父元素 if (item.componentName === "Container" && !(0, utils_1.hasBorderByLayer)(item) && !(0, utils_1.hasBackgroundByLayer)(item) // && // !hasBorderRadiusByLayer(item) && // !hasShadowByLayer(item) ) { item = item.children[0]; item = handleSingleLayer(item); } else if (((_b = (_a = item.children[0].style.background) === null || _a === void 0 ? void 0 : _a.color) === null || _b === void 0 ? void 0 : _b.alpha) === 1 && // !hasBorderRadiusByLayer(item) && item.children[0].structure.height === item.structure.height && item.children[0].structure.width === item.structure.width && item.children[0].structure.x === item.structure.x && item.children[0].structure.y === item.structure.y) { //子元素有背景色,父子结构大小完全相同-删除父元素 item = item.children[0]; item = handleSingleLayer(item); } } } else if ( //Image item.children[0].componentName == "Image" && item._class != "artboard" && !item.style["transform"]) { //父元素无背景及边框-删除父元素 if (item.componentName == "Container") { if (!(0, utils_1.hasBorderByLayer)(item) && !(0, utils_1.hasBackgroundByLayer)(item) // && !hasShadowByLayer(item) ) { item = item.children[0]; item = handleSingleLayer(item); //子元素与父元素完全相同-删除父元素 } else if (item.children[0].structure.height === item.structure.height && item.children[0].structure.width === item.structure.width && item.children[0].structure.x === item.structure.x && item.children[0].structure.y === item.structure.y && !(0, utils_1.hasBackgroundByLayer)(item)) { item = item.children[0]; item = handleSingleLayer(item); } } } else if (item.children[0].componentName === "Text") { //Text -暂时不处理 TODO //父元素无背景及边框-删除父元素 if (item.componentName === "Container") { if (!(0, utils_1.hasBorderByLayer)(item) && !(0, utils_1.hasBackgroundByLayer)(item) // && !hasShadowByLayer(item) ) { item = item.children[0]; item = handleSingleLayer(item); } } } } return item; }; const handleLayer = (data) => { for (let i = 0; i < data.length; i++) { data[i] = handleSingleLayer(data[i]); if (Array.isArray(data[i].children)) { data[i].children = handleLayer(data[i].children); } } return data; }; exports.default = handleLayer;