-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathapp-node.ts
More file actions
432 lines (415 loc) · 14.6 KB
/
app-node.ts
File metadata and controls
432 lines (415 loc) · 14.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import Components from '@/components'
import ElementPlus from 'element-plus'
import * as ElementPlusIcons from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { HtmlResize } from '@logicflow/extension'
import { h as lh } from '@logicflow/core'
import { createApp, h } from 'vue'
import directives from '@/directives'
import i18n from '@/locales'
import { WorkflowType } from '@/enums/application'
import { nodeDict } from '@/workflow/common/data'
import { isActive, connect, disconnect } from './teleport'
import { t } from '@/locales'
import { type Dict } from '@/api/type/common'
const getNodeName = (nodes: Array<any>, baseName: string) => {
let index = 0
let name = baseName
while (true) {
if (index > 0) {
name = baseName + index
}
if (!nodes.some((node: any) => node.properties.stepName === name.trim())) {
return name
}
index++
}
}
class AppNode extends HtmlResize.view {
isMounted
r?: any
component: any
app: any
root?: any
VueNode: any
up_node_field_dict?: Dict<Array<any>>
constructor(props: any, VueNode: any) {
super(props)
this.component = VueNode
this.isMounted = false
props.model.clear_next_node_field = this.clear_next_node_field.bind(this)
props.model.get_up_node_field_dict = this.get_up_node_field_dict.bind(this)
props.model.get_node_field_list = this.get_node_field_list.bind(this)
props.model.get_up_node_field_list = this.get_up_node_field_list.bind(this)
if (props.model.properties.noRender) {
delete props.model.properties.noRender
} else {
props.model.properties.stepName = getNodeName(
props.graphModel.nodes.filter((node: any) => node.id !== props.model.id),
props.model.properties.stepName,
)
}
props.model.properties.config = nodeDict[props.model.type].properties.config
if (props.model.properties.height) {
props.model.height = props.model.properties.height
}
}
get_node_field_list() {
const result = []
if (this.props.model.type === 'start-node') {
result.push({
value: 'global',
label: t('views.applicationWorkflow.variable.global'),
type: 'global',
children: this.props.model.properties?.config?.globalFields || [],
})
result.push({
value: 'chat',
label: t('views.applicationWorkflow.variable.chat'),
type: 'chat',
children: this.props.model.properties?.config?.chatFields || [],
})
}
result.push({
value: this.props.model.id,
label: this.props.model.properties.stepName,
type: this.props.model.type,
children: this.props.model.properties?.config?.fields || [],
})
return result
}
get_up_node_field_dict(contain_self: boolean, use_cache: boolean) {
if (!this.up_node_field_dict || !use_cache) {
const up_node_list = this.props.graphModel.getNodeIncomingNode(this.props.model.id)
this.up_node_field_dict = up_node_list
.filter((node) => node.id != 'start-node' && node.id != 'loop-start-node')
.map((node) => node.get_up_node_field_dict(true, use_cache))
.reduce((pre, next) => ({ ...pre, ...next }), {})
}
if (contain_self) {
return {
...this.up_node_field_dict,
[this.props.model.id]: this.get_node_field_list(),
}
}
return this.up_node_field_dict ? this.up_node_field_dict : {}
}
get_up_node_field_list(contain_self: boolean, use_cache: boolean) {
const result = Object.values(this.get_up_node_field_dict(contain_self, use_cache)).reduce(
(pre, next) => [...pre, ...next],
[],
)
const start_node_field_list = (
this.props.graphModel.getNodeModelById('start-node') ||
this.props.graphModel.getNodeModelById('loop-start-node')
).get_node_field_list()
return [...start_node_field_list, ...result]
}
clear_next_node_field(contain_self: boolean) {
const next_node_list = this.props.graphModel.getNodeOutgoingNode(this.props.model.id)
next_node_list.forEach((node) => {
node.clear_next_node_field(true)
})
if (contain_self) {
this.up_node_field_dict = undefined
}
}
getAnchorShape(anchorData: any) {
const { x, y, type } = anchorData
let isConnect = false
if (type == 'left') {
isConnect = this.props.graphModel.edges.some((edge) => edge.targetAnchorId == anchorData.id)
} else {
isConnect = this.props.graphModel.edges.some((edge) => edge.sourceAnchorId == anchorData.id)
}
return lh(
'foreignObject',
{
...anchorData,
x: x - 10,
y: y - 12,
width: 30,
height: 30,
},
[
lh('div', {
style: { zindex: 0 },
onClick: () => {
if (type == 'right') {
this.props.model.openNodeMenu(anchorData)
}
},
dangerouslySetInnerHTML: {
__html: isConnect
? `<svg width="100%" height="100%" viewBox="0 0 42 42" fill="none" xmlns="http://www.w3.org/2000/svg">
<g filter="url(#filter0_d_5119_232585)">
<path d="M20.9998 29.8333C28.0875 29.8333 33.8332 24.0876 33.8332 17C33.8332 9.91231 28.0875 4.16663 20.9998 4.16663C13.9122 4.16663 8.1665 9.91231 8.1665 17C8.1665 24.0876 13.9122 29.8333 20.9998 29.8333Z" fill="white"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M20.9998 27.5C26.7988 27.5 31.4998 22.799 31.4998 17C31.4998 11.201 26.7988 6.49996 20.9998 6.49996C15.2008 6.49996 10.4998 11.201 10.4998 17C10.4998 22.799 15.2008 27.5 20.9998 27.5ZM33.8332 17C33.8332 24.0876 28.0875 29.8333 20.9998 29.8333C13.9122 29.8333 8.1665 24.0876 8.1665 17C8.1665 9.91231 13.9122 4.16663 20.9998 4.16663C28.0875 4.16663 33.8332 9.91231 33.8332 17Z" fill="#3370FF"/>
</g>
<defs>
<filter id="filter0_d_5119_232585" x="-1" y="-1" width="44" height="44" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
<feOffset dy="4"/>
<feGaussianBlur stdDeviation="4"/>
<feComposite in2="hardAlpha" operator="out"/>
<feColorMatrix type="matrix" values="0 0 0 0 0.2 0 0 0 0 0.439216 0 0 0 0 1 0 0 0 0.1 0"/>
<feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_5119_232585"/>
<feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow_5119_232585" result="shape"/>
</filter>
</defs>
</svg>
`
: `<svg width="100%" height="100%" viewBox="0 0 42 42" fill="none" xmlns="http://www.w3.org/2000/svg">
<g filter="url(#filter0_d_5199_166905)">
<path d="M20.9998 29.8333C28.0875 29.8333 33.8332 24.0876 33.8332 17C33.8332 9.91231 28.0875 4.16663 20.9998 4.16663C13.9122 4.16663 8.1665 9.91231 8.1665 17C8.1665 24.0876 13.9122 29.8333 20.9998 29.8333Z" fill="#3370FF"/>
<path d="M19.8332 11.75C19.8332 11.4278 20.0943 11.1666 20.4165 11.1666H21.5832C21.9053 11.1666 22.1665 11.4278 22.1665 11.75V15.8333H26.2498C26.572 15.8333 26.8332 16.0945 26.8332 16.4166V17.5833C26.8332 17.9055 26.572 18.1666 26.2498 18.1666H22.1665V22.25C22.1665 22.5721 21.9053 22.8333 21.5832 22.8333H20.4165C20.0943 22.8333 19.8332 22.5721 19.8332 22.25V18.1666H15.7498C15.4277 18.1666 15.1665 17.9055 15.1665 17.5833V16.4166C15.1665 16.0945 15.4277 15.8333 15.7498 15.8333H19.8332V11.75Z" fill="white"/>
</g>
<defs>
<filter id="filter0_d_5199_166905" x="-1" y="-1" width="44" height="44" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
<feOffset dy="4"/>
<feGaussianBlur stdDeviation="4"/>
<feComposite in2="hardAlpha" operator="out"/>
<feColorMatrix type="matrix" values="0 0 0 0 0.2 0 0 0 0 0.439216 0 0 0 0 1 0 0 0 0.1 0"/>
<feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_5199_166905"/>
<feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow_5199_166905" result="shape"/>
</filter>
</defs>
</svg>`,
},
}),
],
)
}
setHtml(rootEl: HTMLElement) {
if (!this.isMounted) {
this.isMounted = true
const node = document.createElement('div')
rootEl.appendChild(node)
this.renderVueComponent(node)
} else {
if (this.r && this.r.component) {
this.r.component.props.properties = this.props.model.getProperties()
}
}
}
componentWillUnmount() {
super.componentWillUnmount()
this.unmount()
}
getComponentContainer() {
return this.root
}
protected targetId() {
return `${this.props.graphModel.flowId}:${this.props.model.id}`
}
protected renderVueComponent(root: any) {
this.unmountVueComponent()
this.root = root
const { model, graphModel } = this.props
if (root) {
if (isActive()) {
connect(
this.targetId(),
this.component,
root,
model,
graphModel,
undefined,
this.props.graphModel.get_provide,
)
} else {
this.r = h(this.component, {
properties: this.props.model.properties,
nodeModel: this.props.model,
})
this.app = createApp({
render() {
return this.r
},
provide() {
return {
getNode: () => model,
getGraph: () => graphModel,
}
},
})
this.app.use(ElementPlus, {
locale: zhCn,
})
this.app.use(Components)
this.app.use(directives)
this.app.use(i18n)
for (const [key, component] of Object.entries(ElementPlusIcons)) {
this.app.component(key, component)
}
this.app?.mount(root)
}
}
}
protected unmountVueComponent() {
if (this.app) {
this.app.unmount()
this.app = null
}
if (this.root) {
this.root.innerHTML = ''
}
return this.root
}
unmount() {
if (isActive()) {
disconnect(this.targetId())
}
this.unmountVueComponent()
}
}
class AppNodeModel extends HtmlResize.model {
refreshDeges() {
// 更新节点连接边的path
this.incoming.edges.forEach((edge: any) => {
// 调用自定义的更新方案
edge.updatePathByAnchor()
})
this.outgoing.edges.forEach((edge: any) => {
edge.updatePathByAnchor()
})
}
set_position(position: { x?: number; y?: number }) {
const { x, y } = position
if (x) {
this.x = x
}
if (y) {
this.y = y
}
this.refreshDeges()
}
getResizeOutlineStyle() {
const style = super.getResizeOutlineStyle()
style.stroke = 'none'
return style
}
getControlPointStyle() {
const style = super.getControlPointStyle()
style.stroke = 'none'
style.fill = 'none'
return style
}
getNodeStyle() {
return {
overflow: 'visible',
}
}
getOutlineStyle() {
const style = super.getOutlineStyle()
style.stroke = 'none'
if (style.hover) {
style.hover.stroke = 'none'
}
return style
}
// 如果不用修改锚地形状,可以重写颜色相关样式
getAnchorStyle(anchorInfo: any) {
const style = super.getAnchorStyle(anchorInfo)
if (anchorInfo.type === 'left') {
style.fill = 'red'
style.hover.fill = 'transparent'
style.hover.stroke = 'transpanrent'
style.className = 'lf-hide-default'
} else {
style.fill = 'green'
}
return style
}
setHeight(height: number) {
const sourceHeight = this.height
const targetHeight = height + 100
this.height = targetHeight
this.properties['height'] = targetHeight
this.move(0, (targetHeight - sourceHeight) / 2)
this.outgoing.edges.forEach((edge: any) => {
// 调用自定义的更新方案
edge.updatePathByAnchor()
})
this.incoming.edges.forEach((edge: any) => {
// 调用自定义的更新方案
edge.updatePathByAnchor()
})
}
get_width() {
return this.properties?.width || 340
}
setAttributes() {
const { t } = i18n.global
this.width = this.get_width()
const isLoop = (node_id: string, target_node_id: string) => {
const up_node_list = this.graphModel.getNodeIncomingNode(node_id)
for (const index in up_node_list) {
const item = up_node_list[index]
if (item.id === target_node_id) {
return true
} else {
const result = isLoop(item.id, target_node_id)
if (result) {
return true
}
}
}
return false
}
const circleOnlyAsTarget = {
message: t('views.applicationWorkflow.tip.onlyRight'),
validate: (sourceNode: any, targetNode: any, sourceAnchor: any) => {
return sourceAnchor.type === 'right'
},
}
this.sourceRules.push({
message: t('views.applicationWorkflow.tip.notRecyclable'),
validate: (sourceNode: any, targetNode: any, sourceAnchor: any, targetAnchor: any) => {
if (targetNode.id == sourceNode.id) {
return false
}
const up_edge_list = this.graphModel.getNodeIncomingEdge(targetNode.id)
const is_c = up_edge_list.find(
(up_edge) =>
up_edge.targetAnchorId == targetAnchor.id && up_edge.sourceAnchorId == sourceAnchor.id,
)
return !is_c && !isLoop(sourceNode.id, targetNode.id)
},
})
this.sourceRules.push(circleOnlyAsTarget)
this.targetRules.push({
message: t('views.applicationWorkflow.tip.onlyLeft'),
validate: (sourceNode: any, targetNode: any, sourceAnchor: any, targetAnchor: any) => {
return targetAnchor.type === 'left'
},
})
}
getDefaultAnchor() {
const { id, x, y, width } = this
const showNode = this.properties.showNode === undefined ? true : this.properties.showNode
const anchors: any = []
if (this.type !== WorkflowType.Base) {
if (![WorkflowType.Start, WorkflowType.LoopStartNode.toString()].includes(this.type)) {
anchors.push({
x: x - width / 2 + 10,
y: showNode ? y : y - 15,
id: `${id}_left`,
edgeAddable: false,
type: 'left',
})
}
anchors.push({
x: x + width / 2 - 10,
y: showNode ? y : y - 15,
id: `${id}_right`,
type: 'right',
})
}
return anchors
}
}
export { AppNodeModel, AppNode }