您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

use-interactions.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { useTranslation } from 'react-i18next'
  4. import { useStoreApi } from 'reactflow'
  5. import type {
  6. BlockEnum,
  7. ChildNodeTypeCount,
  8. Node,
  9. } from '../../types'
  10. import {
  11. generateNewNode,
  12. getNodeCustomTypeByNodeDataType,
  13. } from '../../utils'
  14. import {
  15. ITERATION_PADDING,
  16. NODES_INITIAL_DATA,
  17. } from '../../constants'
  18. import { CUSTOM_ITERATION_START_NODE } from '../iteration-start/constants'
  19. export const useNodeIterationInteractions = () => {
  20. const { t } = useTranslation()
  21. const store = useStoreApi()
  22. const handleNodeIterationRerender = useCallback((nodeId: string) => {
  23. const {
  24. getNodes,
  25. setNodes,
  26. } = store.getState()
  27. const nodes = getNodes()
  28. const currentNode = nodes.find(n => n.id === nodeId)!
  29. const childrenNodes = nodes.filter(n => n.parentId === nodeId)
  30. let rightNode: Node
  31. let bottomNode: Node
  32. childrenNodes.forEach((n) => {
  33. if (rightNode) {
  34. if (n.position.x + n.width! > rightNode.position.x + rightNode.width!)
  35. rightNode = n
  36. }
  37. else {
  38. rightNode = n
  39. }
  40. if (bottomNode) {
  41. if (n.position.y + n.height! > bottomNode.position.y + bottomNode.height!)
  42. bottomNode = n
  43. }
  44. else {
  45. bottomNode = n
  46. }
  47. })
  48. const widthShouldExtend = rightNode! && currentNode.width! < rightNode.position.x + rightNode.width!
  49. const heightShouldExtend = bottomNode! && currentNode.height! < bottomNode.position.y + bottomNode.height!
  50. if (widthShouldExtend || heightShouldExtend) {
  51. const newNodes = produce(nodes, (draft) => {
  52. draft.forEach((n) => {
  53. if (n.id === nodeId) {
  54. if (widthShouldExtend) {
  55. n.data.width = rightNode.position.x + rightNode.width! + ITERATION_PADDING.right
  56. n.width = rightNode.position.x + rightNode.width! + ITERATION_PADDING.right
  57. }
  58. if (heightShouldExtend) {
  59. n.data.height = bottomNode.position.y + bottomNode.height! + ITERATION_PADDING.bottom
  60. n.height = bottomNode.position.y + bottomNode.height! + ITERATION_PADDING.bottom
  61. }
  62. }
  63. })
  64. })
  65. setNodes(newNodes)
  66. }
  67. }, [store])
  68. const handleNodeIterationChildDrag = useCallback((node: Node) => {
  69. const { getNodes } = store.getState()
  70. const nodes = getNodes()
  71. const restrictPosition: { x?: number; y?: number } = { x: undefined, y: undefined }
  72. if (node.data.isInIteration) {
  73. const parentNode = nodes.find(n => n.id === node.parentId)
  74. if (parentNode) {
  75. if (node.position.y < ITERATION_PADDING.top)
  76. restrictPosition.y = ITERATION_PADDING.top
  77. if (node.position.x < ITERATION_PADDING.left)
  78. restrictPosition.x = ITERATION_PADDING.left
  79. if (node.position.x + node.width! > parentNode!.width! - ITERATION_PADDING.right)
  80. restrictPosition.x = parentNode!.width! - ITERATION_PADDING.right - node.width!
  81. if (node.position.y + node.height! > parentNode!.height! - ITERATION_PADDING.bottom)
  82. restrictPosition.y = parentNode!.height! - ITERATION_PADDING.bottom - node.height!
  83. }
  84. }
  85. return {
  86. restrictPosition,
  87. }
  88. }, [store])
  89. const handleNodeIterationChildSizeChange = useCallback((nodeId: string) => {
  90. const { getNodes } = store.getState()
  91. const nodes = getNodes()
  92. const currentNode = nodes.find(n => n.id === nodeId)!
  93. const parentId = currentNode.parentId
  94. if (parentId)
  95. handleNodeIterationRerender(parentId)
  96. }, [store, handleNodeIterationRerender])
  97. const handleNodeIterationChildrenCopy = useCallback((nodeId: string, newNodeId: string, idMapping: Record<string, string>) => {
  98. const { getNodes } = store.getState()
  99. const nodes = getNodes()
  100. const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_ITERATION_START_NODE)
  101. const newIdMapping = { ...idMapping }
  102. const childNodeTypeCount: ChildNodeTypeCount = {}
  103. const copyChildren = childrenNodes.map((child, index) => {
  104. const childNodeType = child.data.type as BlockEnum
  105. const nodesWithSameType = nodes.filter(node => node.data.type === childNodeType)
  106. if(!childNodeTypeCount[childNodeType])
  107. childNodeTypeCount[childNodeType] = nodesWithSameType.length + 1
  108. else
  109. childNodeTypeCount[childNodeType] = childNodeTypeCount[childNodeType] + 1
  110. const { newNode } = generateNewNode({
  111. type: getNodeCustomTypeByNodeDataType(childNodeType),
  112. data: {
  113. ...NODES_INITIAL_DATA[childNodeType],
  114. ...child.data,
  115. selected: false,
  116. _isBundled: false,
  117. _connectedSourceHandleIds: [],
  118. _connectedTargetHandleIds: [],
  119. title: nodesWithSameType.length > 0 ? `${t(`workflow.blocks.${childNodeType}`)} ${childNodeTypeCount[childNodeType]}` : t(`workflow.blocks.${childNodeType}`),
  120. iteration_id: newNodeId,
  121. },
  122. position: child.position,
  123. positionAbsolute: child.positionAbsolute,
  124. parentId: newNodeId,
  125. extent: child.extent,
  126. zIndex: child.zIndex,
  127. })
  128. newNode.id = `${newNodeId}${newNode.id + index}`
  129. newIdMapping[child.id] = newNode.id
  130. return newNode
  131. })
  132. return {
  133. copyChildren,
  134. newIdMapping,
  135. }
  136. }, [store, t])
  137. return {
  138. handleNodeIterationRerender,
  139. handleNodeIterationChildDrag,
  140. handleNodeIterationChildSizeChange,
  141. handleNodeIterationChildrenCopy,
  142. }
  143. }