utils.ts
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
export function suffixSize(val){
if (typeof val === 'number') {
return `${val}px`
} else {
val = String(val)
if (/^[\d]+$/.test(val)) {
return `${val}px`
}
if (val.indexOf('px') > 0) {
return val
}
return val
}
}
export function flatten(array) {
let flattend = [];
(function flat(array) {
array.forEach(function (el) {
if (Array.isArray(el)) flat(el);
else flattend.push(el);
});
})(array);
return flattend;
}
export const DEFAULT_SIZE = { // 不同类型的默认宽高
rect: [130, 40],
ellipse: [100, 50],
diamond: [140, 50],
circle: [80, 80],
triangle: [80, 80],
star: [80, 80],
// TAG: 自定义节点
activity: [200, 80],
}
export function formatPos(option: { x: number, y: number, size: [number, number], shape: string }): { x: number, y: number, size: [number, number], shape } {
const {x, y} = option
option = {...option}
option.size = option.size || DEFAULT_SIZE[option.shape]
let [width, height] = option.size
let originPos = {
x: width / 2,
y: height / 2,
}
let targetPos = {
x, y
}
const grid = 5
let durx = targetPos.x - originPos.x
let dury = targetPos.y - originPos.y
if (durx % grid > grid / 2) {
durx = Math.ceil(durx / grid) * grid
} else {
durx = Math.floor(durx / grid) * grid
}
if (dury % grid > grid / 2) {
dury = Math.ceil(dury / grid) * grid
} else {
dury = Math.floor(dury / grid) * grid
}
targetPos.x = durx + originPos.x
targetPos.y = dury + originPos.y
return {
...targetPos,
size: option.size,
shape: option.shape,
}
}
// TAG: 自定义节点
export function formatNodeModel(model, activityConfig) {
let {shape, size, activity} = model
/**
* {
"id": "123456",
"x": 590,
"y": 100,
"text": "广告宣传",
"desc": "通过广告短频宣传",
"activity": "advertisement", // 自定义节点的类型
}
*/
if (!!activity) {
shape = 'activity'
}
model.size = size || DEFAULT_SIZE[shape]
/**
* advertisement: {
text: "广告宣传1",
desc: "通过广告宣传新品",
color: "#9283ed",
img: "https://cdn.ipadbiz.cn/oa/advertisement-node.svg"
}
*/
if (!!activity && !!activityConfig[activity]) {
// 把activity的配置挂到model上
model.shape = 'activity'
model.img = activityConfig[activity].img
model.color = activityConfig[activity].color
}
}