Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

use-interactions.ts 5.1KB

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