use-custom-cell-builder.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import type { Node, Edge } from '@antv/x6'
  18. import { X6_NODE_NAME, X6_EDGE_NAME } from './dag-config'
  19. import utils from '@/utils'
  20. import { WorkflowDefinition, Coordinate } from './types'
  21. export function useCustomCellBuilder() {
  22. /**
  23. * Convert locationStr to JSON
  24. * @param {string} locationStr
  25. * @returns
  26. */
  27. function parseLocationStr(locationStr: string) {
  28. let locations = null
  29. if (!locationStr) return locations
  30. locations = JSON.parse(locationStr)
  31. return Array.isArray(locations) ? locations : null
  32. }
  33. /**
  34. * Build edge metadata
  35. * @param {string} sourceId
  36. * @param {string} targetId
  37. * @param {string} label
  38. */
  39. function buildEdge(
  40. sourceId: string,
  41. targetId: string,
  42. label = ''
  43. ): Edge.Metadata {
  44. return {
  45. shape: X6_EDGE_NAME,
  46. source: {
  47. cell: sourceId
  48. },
  49. target: {
  50. cell: targetId
  51. },
  52. labels: label ? [label] : undefined
  53. }
  54. }
  55. /**
  56. * Build node metadata
  57. * @param {string} id
  58. * @param {string} taskType
  59. * @param {Coordinate} coordinate Default is { x: 100, y: 100 }
  60. */
  61. function buildNode(
  62. id: string,
  63. type: string,
  64. taskName: string,
  65. flag: string,
  66. coordinate: Coordinate = { x: 100, y: 100 }
  67. ): Node.Metadata {
  68. const truncation = taskName ? utils.truncateText(taskName, 18) : id
  69. return {
  70. id: id,
  71. shape: X6_NODE_NAME,
  72. x: coordinate.x,
  73. y: coordinate.y,
  74. data: {
  75. taskType: type,
  76. taskName: taskName || id,
  77. flag: flag
  78. },
  79. attrs: {
  80. image: {
  81. // Use href instead of xlink:href, you may lose the icon when downloadPNG
  82. 'xlink:href': `${
  83. import.meta.env.BASE_URL
  84. }images/task-icons/${type.toLocaleLowerCase()}.png`
  85. },
  86. title: {
  87. text: truncation
  88. },
  89. rect: {
  90. fill: flag === 'NO' ? '#f3f3f5' : '#ffffff'
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Build graph JSON
  97. * @param {WorkflowDefinition} definition
  98. * @returns
  99. */
  100. function buildGraph(definition: WorkflowDefinition) {
  101. const nodes: Node.Metadata[] = []
  102. const edges: Edge.Metadata[] = []
  103. const locations =
  104. parseLocationStr(definition.processDefinition.locations) || []
  105. const tasks = definition.taskDefinitionList
  106. const connects = definition.processTaskRelationList
  107. tasks.forEach((task) => {
  108. const location = locations.find((l) => l.taskCode === task.code) || {}
  109. const node = buildNode(
  110. task.code + '',
  111. task.taskType,
  112. task.name,
  113. task.flag,
  114. {
  115. x: location.x,
  116. y: location.y
  117. }
  118. )
  119. nodes.push(node)
  120. })
  121. connects
  122. .filter((r) => !!r.preTaskCode)
  123. .forEach((c) => {
  124. const edge = buildEdge(c.preTaskCode + '', c.postTaskCode + '', c.name)
  125. edges.push(edge)
  126. })
  127. return {
  128. nodes,
  129. edges
  130. }
  131. }
  132. return {
  133. buildNode,
  134. buildEdge,
  135. buildGraph
  136. }
  137. }