workflow.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { defineStore } from 'pinia'
  2. import { NodeType, NodeTypeObj, VarsSource } from '@/views/workflow/types'
  3. import { handleNode } from '@/views/workflow/handle'
  4. import { getNodeDefault } from '@/views/workflow/config'
  5. export const useWorkflowStore = defineStore('workflow', {
  6. state: () => ({
  7. graph: <any>null,
  8. panel: {
  9. node: <any>null,
  10. show: false,
  11. },
  12. envVars: {
  13. show: false,
  14. vars: [],
  15. },
  16. nodeSize: {},
  17. autoSaveFlag: false,
  18. }),
  19. getters: {},
  20. actions: {
  21. init(graph) {
  22. this.graph = graph
  23. return new Promise((resolve) => {
  24. const keys = Object.keys(NodeTypeObj)
  25. const arr: any = []
  26. keys.forEach((v, i) => {
  27. const d = handleNode(getNodeDefault(v as NodeType))
  28. delete d.portMarkup
  29. delete d.ports
  30. const node = this.graph.createNode(d)
  31. node.position(-99999, -99999)
  32. this.graph.addNode(node)
  33. arr.push({
  34. node: node,
  35. flag: false,
  36. })
  37. })
  38. let timer = setInterval(() => {
  39. arr.forEach((v, i) => {
  40. const s = v.node.size()
  41. if (s.width > 1) {
  42. this.nodeSize[keys[i]] = s
  43. v.flag = true
  44. this.graph.removeNode(v.node)
  45. }
  46. })
  47. if (arr.every((v) => v.flag)) {
  48. clearInterval(timer)
  49. resolve(null)
  50. }
  51. }, 100)
  52. })
  53. },
  54. nodePanelShow(node) {
  55. const f = () => {
  56. this.panel.node = node
  57. setTimeout(() => {
  58. this.panel.show = true
  59. }, 0)
  60. }
  61. if (this.panel.show) {
  62. this.nodePanelClose()
  63. setTimeout(() => {
  64. f()
  65. }, 0)
  66. } else {
  67. f()
  68. }
  69. },
  70. nodePanelClose() {
  71. this.panel.show = false
  72. setTimeout(() => {
  73. this.panel.node = null
  74. }, 0)
  75. },
  76. layoutPort(nodeId: string, portId: string) {
  77. const dom = document.getElementById(portId)
  78. const node = this.graph.getCellById(nodeId)
  79. if (dom && node) {
  80. node.portProp(portId, ['args', 'dy'], dom.offsetTop)
  81. }
  82. },
  83. getInVars(node) {
  84. const pathNodes = this.graph.getPredecessors(node).reverse()
  85. const options: any = [
  86. {
  87. label: '环境变量',
  88. key: VarsSource.Env,
  89. options: this.envVars.vars.map((v: any) => {
  90. const result = JSON.parse(JSON.stringify(v))
  91. delete result.value
  92. return result
  93. }),
  94. },
  95. ]
  96. pathNodes.forEach((n) => {
  97. const wd = n.data.workflowData
  98. const group = {
  99. label: wd.desc || wd.title,
  100. key: wd.id,
  101. options: [
  102. ...(wd.__outVars?.map((v) => {
  103. v.nodeId = wd.id
  104. return v
  105. }) || []),
  106. ...(wd.__sysVars?.map((v) => {
  107. v.nodeId = wd.id
  108. return v
  109. }) || []),
  110. ],
  111. }
  112. if (group.options.length > 0) {
  113. options.push(group)
  114. }
  115. })
  116. return options
  117. },
  118. autoSave() {
  119. console.log('自动保存触发')
  120. this.autoSaveFlag = !this.autoSaveFlag
  121. },
  122. },
  123. })