use-interactions.ts 4.9 KB

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