use-interactions.ts 4.7 KB

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