handle.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {Markup} from "@antv/x6";
  2. import {merge} from "lodash";
  3. import {lineStyle, portStyle} from "@/views/workflow/config";
  4. import { v4 } from "uuid";
  5. import {useWorkflowStore} from "@/stores";
  6. import {GraphHistoryStep, NodeType} from "@/views/workflow/types";
  7. const WorkflowStore = useWorkflowStore()
  8. export const handleNode = (no) => {
  9. const id = v4()
  10. if (!no.id) {
  11. no.id = id
  12. }
  13. const node: any = {
  14. id: no.id,
  15. x: no.x,
  16. y: no.y,
  17. data: {
  18. workflowData: {
  19. ...no.data,
  20. id: no.id
  21. }
  22. },
  23. shape: 'workflow-node',
  24. portMarkup: [Markup.getForeignObjectMarkup()],
  25. ports: {
  26. groups: {
  27. start: merge(JSON.parse(JSON.stringify(portStyle)), {position: {name: 'start'}}),
  28. end: merge(JSON.parse(JSON.stringify(portStyle)), {position: {name: 'end'}}),
  29. more: merge(JSON.parse(JSON.stringify(portStyle)), {position: {name: 'more'}}),
  30. },
  31. items: []
  32. },
  33. tools: [
  34. {
  35. name: 'contextmenu',
  36. args: {
  37. delFlag: GraphHistoryStep.NodeDel,
  38. data: no,
  39. }
  40. }
  41. ]
  42. }
  43. if (node.data.workflowData.type === NodeType.Root) {
  44. node.data.sysVars = [
  45. ...WorkflowStore.systemVars
  46. ]
  47. } else {
  48. node.ports.items.push({
  49. id: `${node.id}_start`,
  50. group: 'start',
  51. args: {
  52. type: 'start',
  53. nodeId: node.id,
  54. }
  55. })
  56. }
  57. if (node.data.workflowData.ports?.length > 0) {
  58. node.data.workflowData.ports.forEach((p, pI) => {
  59. node.ports.items.push({
  60. id: p.id,
  61. group: 'more',
  62. args: {
  63. type: 'more',
  64. portId: p.id,
  65. nodeId: node.id,
  66. dy: 0
  67. }
  68. })
  69. })
  70. } else if (node.data.workflowData.type !== NodeType.Answer) {
  71. node.ports.items.push({
  72. id: `${node.id}_end`,
  73. group: 'end',
  74. args: {
  75. type: 'end',
  76. nodeId: node.id,
  77. }
  78. })
  79. }
  80. return node
  81. }
  82. export const handleEdge = (ed) => {
  83. const id = v4()
  84. if (!ed.id) {
  85. ed.id = id
  86. }
  87. const edge = {
  88. id: ed.id,
  89. source: {
  90. cell: ed.source,
  91. port: ed.port || `${ed.source}_end`
  92. },
  93. target: {
  94. cell: ed.target,
  95. port: `${ed.target}_start`
  96. },
  97. data: {
  98. workflowData: {
  99. ...ed.data,
  100. id: ed.id
  101. }
  102. },
  103. shape: 'edge',
  104. attrs: lineStyle,
  105. zIndex: -1,
  106. // router: {
  107. // name: 'manhattan',
  108. // args: {
  109. // startDirections: ['right'],
  110. // endDirections: ['left'],
  111. // },
  112. // },
  113. tools: [
  114. {
  115. name: 'contextmenu',
  116. args: {
  117. delFlag: GraphHistoryStep.EdgeDel,
  118. data: ed,
  119. }
  120. }
  121. ]
  122. }
  123. return edge
  124. }