Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

use-interactions.ts 4.9KB

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