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

use-interactions.ts 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. LOOP_PADDING,
  15. NODES_INITIAL_DATA,
  16. } from '../../constants'
  17. import { CUSTOM_LOOP_START_NODE } from '../loop-start/constants'
  18. export const useNodeLoopInteractions = () => {
  19. const { t } = useTranslation()
  20. const store = useStoreApi()
  21. const handleNodeLoopRerender = 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! + LOOP_PADDING.right
  55. n.width = rightNode.position.x + rightNode.width! + LOOP_PADDING.right
  56. }
  57. if (heightShouldExtend) {
  58. n.data.height = bottomNode.position.y + bottomNode.height! + LOOP_PADDING.bottom
  59. n.height = bottomNode.position.y + bottomNode.height! + LOOP_PADDING.bottom
  60. }
  61. }
  62. })
  63. })
  64. setNodes(newNodes)
  65. }
  66. }, [store])
  67. const handleNodeLoopChildDrag = 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.isInLoop) {
  72. const parentNode = nodes.find(n => n.id === node.parentId)
  73. if (parentNode) {
  74. if (node.position.y < LOOP_PADDING.top)
  75. restrictPosition.y = LOOP_PADDING.top
  76. if (node.position.x < LOOP_PADDING.left)
  77. restrictPosition.x = LOOP_PADDING.left
  78. if (node.position.x + node.width! > parentNode!.width! - LOOP_PADDING.right)
  79. restrictPosition.x = parentNode!.width! - LOOP_PADDING.right - node.width!
  80. if (node.position.y + node.height! > parentNode!.height! - LOOP_PADDING.bottom)
  81. restrictPosition.y = parentNode!.height! - LOOP_PADDING.bottom - node.height!
  82. }
  83. }
  84. return {
  85. restrictPosition,
  86. }
  87. }, [store])
  88. const handleNodeLoopChildSizeChange = 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. handleNodeLoopRerender(parentId)
  95. }, [store, handleNodeLoopRerender])
  96. const handleNodeLoopChildrenCopy = useCallback((nodeId: string, newNodeId: string) => {
  97. const { getNodes } = store.getState()
  98. const nodes = getNodes()
  99. const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_LOOP_START_NODE)
  100. return childrenNodes.map((child, index) => {
  101. const childNodeType = child.data.type as BlockEnum
  102. const nodesWithSameType = nodes.filter(node => node.data.type === childNodeType)
  103. const { newNode } = generateNewNode({
  104. type: getNodeCustomTypeByNodeDataType(childNodeType),
  105. data: {
  106. ...NODES_INITIAL_DATA[childNodeType],
  107. ...child.data,
  108. selected: false,
  109. _isBundled: false,
  110. _connectedSourceHandleIds: [],
  111. _connectedTargetHandleIds: [],
  112. title: nodesWithSameType.length > 0 ? `${t(`workflow.blocks.${childNodeType}`)} ${nodesWithSameType.length + 1}` : t(`workflow.blocks.${childNodeType}`),
  113. loop_id: newNodeId,
  114. },
  115. position: child.position,
  116. positionAbsolute: child.positionAbsolute,
  117. parentId: newNodeId,
  118. extent: child.extent,
  119. zIndex: child.zIndex,
  120. })
  121. newNode.id = `${newNodeId}${newNode.id + index}`
  122. return newNode
  123. })
  124. }, [store, t])
  125. return {
  126. handleNodeLoopRerender,
  127. handleNodeLoopChildDrag,
  128. handleNodeLoopChildSizeChange,
  129. handleNodeLoopChildrenCopy,
  130. }
  131. }