Skip to content

Commit 72443b0

Browse files
committed
fix: 清理空图层的解析
1 parent e1e128c commit 72443b0

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

src/function/nodeToSketchLayers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ export const nodeToSketchLayers = (node: Element): AnyLayer[] => {
8282
if (isImage || isShape) {
8383
// 添加后继续执行,不终止
8484
const shape = transferToShape(node);
85-
console.info('[nodeToSketchLayers]转换为 Rectangle: ', shape);
86-
layers.push(shape);
85+
86+
if (shape) {
87+
console.info('[nodeToSketchLayers]转换为 Rectangle: ', shape);
88+
layers.push(shape);
89+
}
8790
}
8891

8992
// 判断一下是否有伪类

src/helpers/shape.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pseudoText from '../parser/pseudoText';
22
import pseudoShape from '../parser/pseudoShape';
3+
import Rectangle from '../model/Layer/Rectangle';
34

45
/**
56
* 判断是否存在伪类
@@ -11,3 +12,30 @@ export const isExistPseudoText = (node: Element) =>
1112
*/
1213
export const isExistPseudoShape = (node: Element) =>
1314
!!(pseudoShape(node, 'after') || pseudoShape(node, 'before'));
15+
16+
export const isVisibleShape = (shape: Rectangle) => {
17+
const isInvalidFills =
18+
shape.style.fills.length === 0 ||
19+
shape.style.fills.every((fill) => fill.opacity.toString() === '0');
20+
const isInvalidBorders =
21+
shape.style.borders.length === 0 ||
22+
shape.style.borders.every(
23+
(border) => border.opacity === 0 || border.thickness === 0
24+
);
25+
26+
console.log(
27+
shape.name,
28+
'isInvalidFills',
29+
isInvalidFills,
30+
'isInvalidBorders',
31+
isInvalidBorders
32+
);
33+
const isInvisible = shape.style.opacity === 0;
34+
35+
if (isInvisible) return false;
36+
37+
if (isInvalidFills && isInvalidBorders) {
38+
return false;
39+
}
40+
return true;
41+
};

src/model/Style/Border.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class Border extends StyleBase {
4545
this.image = new Image(image);
4646
}
4747
}
48+
get opacity() {
49+
return this.color.alpha;
50+
}
4851

4952
/**
5053
* 颜色填充类型

src/model/Style/Fill.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class Fill extends StyleBase {
5656
*/
5757
stops: Color[];
5858

59+
get opacity() {
60+
return this.color.alpha;
61+
}
5962
/**
6063
* 终点
6164
*/

src/parser/pseudoShape.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Style, Rectangle, Shadow } from '../model';
22
import { defaultNodeStyle } from '../model/utils';
3+
import { isVisibleShape } from '../helpers/shape';
34

45
/**
56
* 解析图形类伪类
@@ -155,7 +156,7 @@ const parsePseudo = (node: Element, pseudoElt: 'before' | 'after') => {
155156

156157
rect.cornerRadius = cornerRadius;
157158

158-
return rect;
159+
if (isVisibleShape(rect)) return rect;
159160
};
160161

161162
export default parsePseudo;

src/parser/shape.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
parseBackgroundImage,
55
} from '../helpers/background';
66
import { defaultNodeStyle } from '../model/utils';
7+
import { isVisibleShape } from '../helpers/shape';
78

89
const transferToShape = (node: Element): Group | Rectangle => {
910
const bcr = node.getBoundingClientRect();
@@ -236,7 +237,7 @@ const transferToShape = (node: Element): Group | Rectangle => {
236237
break;
237238
}
238239
}
239-
return rect;
240+
if (isVisibleShape(rect)) return rect;
240241
};
241242

242243
export default transferToShape;

0 commit comments

Comments
 (0)