Skip to content

Commit e2b1a7a

Browse files
CWsuperchengweiwboyongjiong
authored
feat: 补充自定义折线、圆角折线、节点选择示例 (#1680)
Co-authored-by: chengweiw <[email protected]> Co-authored-by: 你说呢? <[email protected]>
1 parent 53fd618 commit e2b1a7a

File tree

12 files changed

+1022
-72
lines changed

12 files changed

+1022
-72
lines changed

examples/feature-examples/.umirc.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ export default defineConfig({
7474
},
7575
],
7676
},
77+
{
78+
path: '/custom-edges',
79+
name: 'custom edges',
80+
routes: [
81+
{
82+
path: 'custom-edges',
83+
redirect: 'custom-edges/polyline',
84+
},
85+
{
86+
path: '/custom-edges/polyline',
87+
name: '折线',
88+
component: './edges/custom/polyline',
89+
},
90+
{
91+
path: '/custom-edges/curved-polyline',
92+
name: '圆角折线',
93+
component: './edges/custom/curved-polyline',
94+
},
95+
],
96+
},
7797
{
7898
name: 'official extensions',
7999
path: '/extension',
@@ -117,6 +137,11 @@ export default defineConfig({
117137
name: 'MiniMap 插件',
118138
component: './extensions/mini-map',
119139
},
140+
{
141+
path: '/extension/node-selection',
142+
name: 'NodeSelection 插件',
143+
component: './extensions/node-selection',
144+
},
120145
{
121146
path: '/extension/snapshot',
122147
name: 'Snapshot 插件',
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { LogicFlow } from '@logicflow/core'
2+
import { CurvedEdge, CurvedEdgeModel } from '@logicflow/extension'
3+
4+
class CustomCurvedEdge extends CurvedEdge {}
5+
6+
class CustomCurvedEdgeModel extends CurvedEdgeModel {
7+
initEdgeData(data: LogicFlow.EdgeData) {
8+
super.initEdgeData(data)
9+
this.radius = 20
10+
}
11+
getEdgeStyle() {
12+
const style = super.getEdgeStyle()
13+
style.strokeWidth = 3
14+
return style
15+
}
16+
}
17+
18+
export default {
19+
type: 'customCurvedEdge',
20+
model: CustomCurvedEdgeModel,
21+
view: CustomCurvedEdge,
22+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import LogicFlow, { h, PolylineEdge, PolylineEdgeModel } from '@logicflow/core'
2+
3+
class CustomPolyline extends PolylineEdge {
4+
// 自定义边箭头
5+
// 在自定义连线 view 的时候,可以重写getEndArrow和getStartArrow方法来实现自定义连线两端的图形,这两个方法可以返回的任意svg图形。
6+
// 这里以通过连线properties属性中的 arrowType 来控制连线不同的外观为例。
7+
getEndArrow() {
8+
const { model } = this.props
9+
const {
10+
properties: { arrowType },
11+
} = model
12+
const { stroke, strokeWidth } = model.getArrowStyle()
13+
const pathAttr = {
14+
stroke,
15+
strokeWidth,
16+
}
17+
// 空心箭头
18+
if (arrowType === 'empty') {
19+
return h('path', {
20+
...pathAttr,
21+
fill: '#FFF',
22+
d: 'M -10 0 -20 -5 -30 0 -20 5 z',
23+
})
24+
} else if (arrowType === 'half') {
25+
// 半箭头
26+
return h('path', {
27+
...pathAttr,
28+
d: 'M 0 0 -10 5',
29+
})
30+
}
31+
return h('path', {
32+
...pathAttr,
33+
fill: stroke,
34+
d: 'M 0 0 -10 -5 -10 5 z',
35+
})
36+
}
37+
}
38+
39+
class CustomPolylineModel extends PolylineEdgeModel {
40+
initEdgeData(data: LogicFlow.EdgeConfig) {
41+
super.initEdgeData(data)
42+
this.customTextPosition = true
43+
}
44+
// 自定义边文本位置
45+
getTextPosition(): LogicFlow.Point {
46+
const { textPosition = 'center' } = this.properties
47+
const position = super.getTextPosition()
48+
const currentPositionList = this.points.split(' ')
49+
const pointsList: LogicFlow.Position[] = []
50+
currentPositionList &&
51+
currentPositionList.forEach((item) => {
52+
const [x, y] = item.split(',')
53+
pointsList.push({ x: Number(x), y: Number(y) })
54+
})
55+
if (textPosition === 'center') {
56+
return position
57+
}
58+
if (textPosition === 'start') {
59+
if (pointsList.length > 1) {
60+
const { x: x1, y: y1 } = pointsList[0]
61+
const { x: x2, y: y2 } = pointsList[1]
62+
let distance = 50
63+
if (x1 === x2) {
64+
// 垂直
65+
if (y2 < y1) {
66+
distance = -50
67+
}
68+
position.y = y1 + distance
69+
position.x = x1
70+
} else {
71+
if (x2 < x1) {
72+
distance = -50
73+
}
74+
position.x = x1 + distance
75+
position.y = y1 - 10
76+
}
77+
}
78+
return position
79+
}
80+
if (textPosition === 'end') {
81+
if (pointsList.length > 1) {
82+
const { x: x1, y: y1 } = pointsList[pointsList.length - 2]
83+
const { x: x2, y: y2 } = pointsList[pointsList.length - 1]
84+
let distance = 50
85+
if (x1 === x2) {
86+
// 垂直
87+
if (y2 > y1) {
88+
distance = -50
89+
}
90+
position.y = y2 + distance
91+
position.x = x2
92+
} else {
93+
if (x2 < x1) {
94+
distance = -50
95+
}
96+
position.x = x2 - distance
97+
position.y = y2 - 10
98+
}
99+
}
100+
return position
101+
}
102+
return position
103+
}
104+
// 自定义动画
105+
setAttributes() {
106+
const { openAnimation } = this.properties
107+
this.isAnimation = !!openAnimation
108+
}
109+
// 自定义动画
110+
getEdgeAnimationStyle() {
111+
const style = super.getEdgeAnimationStyle()
112+
style.strokeDasharray = '15 5'
113+
style.animationDuration = '10s'
114+
style.stroke = 'rgb(130, 179, 102)'
115+
return style
116+
}
117+
// 自定义边样式:颜色、宽度、线段类型等
118+
getEdgeStyle() {
119+
const style = super.getEdgeStyle()
120+
const { edgeWeight, highlight } = this.properties
121+
style.strokeWidth = edgeWeight ? 5 : 3
122+
style.stroke = highlight ? 'red' : 'black'
123+
return style
124+
}
125+
}
126+
127+
export default {
128+
type: 'customPolyline',
129+
model: CustomPolylineModel,
130+
view: CustomPolyline,
131+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.viewport {
2+
position: relative;
3+
height: 90vh;
4+
overflow: hidden;
5+
}
6+
7+
.btn {
8+
margin-right: 10px;
9+
}

0 commit comments

Comments
 (0)