Skip to content

Commit 25c9dc3

Browse files
author
fanyang
committed
refactor: 重构自定义style逻辑
1 parent c24e575 commit 25c9dc3

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

packages/core/src/model/node/CircleNodeModel.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ export class CircleNodeModel extends BaseNodeModel {
4141
setAttributes() {
4242
super.setAttributes()
4343

44-
const { r, style } = this.properties
44+
const { r } = this.properties
4545
if (r) {
4646
this.r = r
4747
}
48-
// style 需挂载到实例上: 可以通过properties设置每个节点样式属性
49-
if (style) this.style = cloneDeep(style)
5048
}
5149

5250
getNodeStyle() {
@@ -56,11 +54,11 @@ export class CircleNodeModel extends BaseNodeModel {
5654
theme: { circle },
5755
},
5856
} = this
59-
const { style: customStyle } = this.properties
57+
const { style: customStyle = {} } = this.properties
6058
return {
6159
...style,
62-
...(customStyle ?? {}),
6360
...cloneDeep(circle),
61+
...cloneDeep(customStyle),
6462
}
6563
}
6664

packages/core/src/model/node/DiamondNodeModel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ export class DiamondNodeModel extends BaseNodeModel {
3737
setAttributes() {
3838
super.setAttributes()
3939

40-
const { rx, ry, style } = this.properties
40+
const { rx, ry } = this.properties
4141
if (rx) {
4242
this.rx = rx
4343
}
4444
if (ry) {
4545
this.ry = ry
4646
}
47-
// style 需挂载到实例上: 可以通过properties设置每个节点样式属性
48-
if (style) this.style = cloneDeep(style)
4947
}
5048

5149
getNodeStyle() {
@@ -55,9 +53,11 @@ export class DiamondNodeModel extends BaseNodeModel {
5553
theme: { diamond },
5654
},
5755
} = this
56+
const { style: customStyle = {} } = this.properties
5857
return {
5958
...style,
6059
...cloneDeep(diamond),
60+
...cloneDeep(customStyle),
6161
}
6262
}
6363

packages/core/src/model/node/EllipseNodeModel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ export class EllipseNodeModel extends BaseNodeModel {
3535
setAttributes() {
3636
super.setAttributes()
3737

38-
const { rx, ry, style } = this.properties
38+
const { rx, ry } = this.properties
3939
if (rx) {
4040
this.rx = rx
4141
}
4242
if (ry) {
4343
this.ry = ry
4444
}
45-
// style 需挂载到实例上: 可以通过properties设置每个节点样式属性
46-
if (style) this.style = cloneDeep(style)
4745
}
4846

4947
getNodeStyle() {
@@ -53,9 +51,11 @@ export class EllipseNodeModel extends BaseNodeModel {
5351
theme: { ellipse },
5452
},
5553
} = this
54+
const { style: customStyle = {} } = this.properties
5655
return {
5756
...style,
5857
...cloneDeep(ellipse),
58+
...cloneDeep(customStyle),
5959
}
6060
}
6161

packages/core/src/model/node/PolygonNodeModel.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class PolygonNodeModel extends BaseNodeModel {
4848
setAttributes() {
4949
super.setAttributes()
5050

51-
const { points, width, height, style } = this.properties
51+
const { points, width, height } = this.properties
5252
// DONE: 如果设置了 points,又设置了节点的宽高,则需要做如下操作
5353
// 1. 将 points 的位置置零,即将图形向原点移动(找到 points 中 x,y 的最小值,所有坐标值减掉该值)
5454
// 2. 按宽高的比例重新计算最新的 points
@@ -57,8 +57,6 @@ export class PolygonNodeModel extends BaseNodeModel {
5757
// }
5858
const nextPoints = points || this.points
5959
this.points = normalizePolygon(nextPoints, width, height)
60-
// style 需挂载到实例上: 可以通过properties设置每个节点样式属性
61-
if (style) this.style = cloneDeep(style)
6260
}
6361

6462
getNodeStyle() {
@@ -68,11 +66,11 @@ export class PolygonNodeModel extends BaseNodeModel {
6866
theme: { polygon },
6967
},
7068
} = this
71-
const { style: customStyle } = this.properties
69+
const { style: customStyle = {} } = this.properties
7270
return {
7371
...style,
7472
...cloneDeep(polygon),
75-
...(cloneDeep(customStyle) || {}),
73+
...cloneDeep(customStyle),
7674
}
7775
}
7876

packages/core/src/model/node/RectNodeModel.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ModelType } from '../../constant'
88
export type IRectNodeModel = {
99
width?: number
1010
height?: number
11+
radius?: number
1112
style?: LogicFlow.CommonTheme
1213
textStyle?: LogicFlow.CommonTheme
1314

@@ -30,13 +31,11 @@ export class RectNodeModel extends BaseNodeModel {
3031
setAttributes() {
3132
super.setAttributes()
3233

33-
const { width, height, style } = this.properties
34+
const { width, height, radius } = this.properties
3435
if (width) this.width = width
3536
if (height) this.height = height
36-
// style 需挂载到实例上: 可以通过properties设置每个节点样式属性
37-
if (style) this.style = cloneDeep(style)
3837
// 矩形特有
39-
if (style?.radius) this.radius = style.radius
38+
if (radius) this.radius = radius
4039
}
4140

4241
getDefaultAnchor() {
@@ -52,9 +51,11 @@ export class RectNodeModel extends BaseNodeModel {
5251
getNodeStyle() {
5352
const style = super.getNodeStyle()
5453
const { rect } = this.graphModel.theme
54+
const { style: customStyle = {} } = this.properties
5555
return {
5656
...style,
5757
...cloneDeep(rect),
58+
...cloneDeep(customStyle),
5859
}
5960
}
6061
}

sites/docs/docs/tutorial/basic-node.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ class customRectModel extends RectNodeModel {
9898
super.initNodeData(data);
9999
this.width = 200;
100100
this.height = 80;
101+
this.radius = 20; // 矩形特有
101102
}
102103
// or
103104
setAttributes() {
104105
this.width = 200;
105106
this.height = 80;
107+
this.radius = 20; // 矩形特有
106108
}
107109
}
108110
```

sites/docs/src/tutorial/basic/node/properties/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export default {
2121
statu: 'pass', // 业务属性
2222
width: 100, // 形状属性
2323
height: 100,
24+
radius: 20,
2425
style: {
2526
// 样式属性
26-
radius: 20,
2727
strokeWidth: 3,
2828
},
2929
},

0 commit comments

Comments
 (0)