utils.ts
3.8 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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],
control: [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: 自定义节点 - 挂载额外属性到model上
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
}
}
/**
* 自定义节点 Control - 挂载额外属性到model上
*
* @param {any} model - The node model to format.
* @param {object} controlConfig - The configuration for the control.
*/
export function formatNodeModel_control(model: any, controlConfig: object) {
let {shape, size, control} = model
/**
* {
"id": "123456",
"x": 590,
"y": 100,
"text": "广告宣传",
"desc": "通过广告短频宣传",
"control": "advertisement", // 自定义节点的类型
}
*/
if (!!control) {
shape = 'control'
}
model.size = size || DEFAULT_SIZE[shape]
/**
* advertisement: {
text: "广告宣传1",
desc: "通过广告宣传新品",
color: "#9283ed",
img: "https://cdn.ipadbiz.cn/oa/advertisement-node.svg"
}
*/
if (!!control && !!controlConfig[control]) {
// 把 control 的配置挂到model上
model.shape = 'control'
model.img = controlConfig[control].img
model.color = controlConfig[control].color
}
}