use-helpline.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { useCallback } from 'react'
  2. import { useStoreApi } from 'reactflow'
  3. import type { Node } from '../types'
  4. import { useWorkflowStore } from '../store'
  5. export const useHelpline = () => {
  6. const store = useStoreApi()
  7. const workflowStore = useWorkflowStore()
  8. const handleSetHelpline = useCallback((node: Node) => {
  9. const { getNodes } = store.getState()
  10. const nodes = getNodes()
  11. const {
  12. setHelpLineHorizontal,
  13. setHelpLineVertical,
  14. } = workflowStore.getState()
  15. if (node.data.isInIteration) {
  16. return {
  17. showHorizontalHelpLineNodes: [],
  18. showVerticalHelpLineNodes: [],
  19. }
  20. }
  21. if (node.data.isInLoop) {
  22. return {
  23. showHorizontalHelpLineNodes: [],
  24. showVerticalHelpLineNodes: [],
  25. }
  26. }
  27. const showHorizontalHelpLineNodes = nodes.filter((n) => {
  28. if (n.id === node.id)
  29. return false
  30. if (n.data.isInIteration)
  31. return false
  32. if (n.data.isInLoop)
  33. return false
  34. const nY = Math.ceil(n.position.y)
  35. const nodeY = Math.ceil(node.position.y)
  36. if (nY - nodeY < 5 && nY - nodeY > -5)
  37. return true
  38. return false
  39. }).sort((a, b) => a.position.x - b.position.x)
  40. const showHorizontalHelpLineNodesLength = showHorizontalHelpLineNodes.length
  41. if (showHorizontalHelpLineNodesLength > 0) {
  42. const first = showHorizontalHelpLineNodes[0]
  43. const last = showHorizontalHelpLineNodes[showHorizontalHelpLineNodesLength - 1]
  44. const helpLine = {
  45. top: first.position.y,
  46. left: first.position.x,
  47. width: last.position.x + last.width! - first.position.x,
  48. }
  49. if (node.position.x < first.position.x) {
  50. helpLine.left = node.position.x
  51. helpLine.width = first.position.x + first.width! - node.position.x
  52. }
  53. if (node.position.x > last.position.x)
  54. helpLine.width = node.position.x + node.width! - first.position.x
  55. setHelpLineHorizontal(helpLine)
  56. }
  57. else {
  58. setHelpLineHorizontal()
  59. }
  60. const showVerticalHelpLineNodes = nodes.filter((n) => {
  61. if (n.id === node.id)
  62. return false
  63. if (n.data.isInIteration)
  64. return false
  65. if (n.data.isInLoop)
  66. return false
  67. const nX = Math.ceil(n.position.x)
  68. const nodeX = Math.ceil(node.position.x)
  69. if (nX - nodeX < 5 && nX - nodeX > -5)
  70. return true
  71. return false
  72. }).sort((a, b) => a.position.x - b.position.x)
  73. const showVerticalHelpLineNodesLength = showVerticalHelpLineNodes.length
  74. if (showVerticalHelpLineNodesLength > 0) {
  75. const first = showVerticalHelpLineNodes[0]
  76. const last = showVerticalHelpLineNodes[showVerticalHelpLineNodesLength - 1]
  77. const helpLine = {
  78. top: first.position.y,
  79. left: first.position.x,
  80. height: last.position.y + last.height! - first.position.y,
  81. }
  82. if (node.position.y < first.position.y) {
  83. helpLine.top = node.position.y
  84. helpLine.height = first.position.y + first.height! - node.position.y
  85. }
  86. if (node.position.y > last.position.y)
  87. helpLine.height = node.position.y + node.height! - first.position.y
  88. setHelpLineVertical(helpLine)
  89. }
  90. else {
  91. setHelpLineVertical()
  92. }
  93. return {
  94. showHorizontalHelpLineNodes,
  95. showVerticalHelpLineNodes,
  96. }
  97. }, [store, workflowStore])
  98. return {
  99. handleSetHelpline,
  100. }
  101. }