use-helpline.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. const showHorizontalHelpLineNodes = nodes.filter((n) => {
  22. if (n.id === node.id)
  23. return false
  24. if (n.data.isInIteration)
  25. return false
  26. const nY = Math.ceil(n.position.y)
  27. const nodeY = Math.ceil(node.position.y)
  28. if (nY - nodeY < 5 && nY - nodeY > -5)
  29. return true
  30. return false
  31. }).sort((a, b) => a.position.x - b.position.x)
  32. const showHorizontalHelpLineNodesLength = showHorizontalHelpLineNodes.length
  33. if (showHorizontalHelpLineNodesLength > 0) {
  34. const first = showHorizontalHelpLineNodes[0]
  35. const last = showHorizontalHelpLineNodes[showHorizontalHelpLineNodesLength - 1]
  36. const helpLine = {
  37. top: first.position.y,
  38. left: first.position.x,
  39. width: last.position.x + last.width! - first.position.x,
  40. }
  41. if (node.position.x < first.position.x) {
  42. helpLine.left = node.position.x
  43. helpLine.width = first.position.x + first.width! - node.position.x
  44. }
  45. if (node.position.x > last.position.x)
  46. helpLine.width = node.position.x + node.width! - first.position.x
  47. setHelpLineHorizontal(helpLine)
  48. }
  49. else {
  50. setHelpLineHorizontal()
  51. }
  52. const showVerticalHelpLineNodes = nodes.filter((n) => {
  53. if (n.id === node.id)
  54. return false
  55. if (n.data.isInIteration)
  56. return false
  57. const nX = Math.ceil(n.position.x)
  58. const nodeX = Math.ceil(node.position.x)
  59. if (nX - nodeX < 5 && nX - nodeX > -5)
  60. return true
  61. return false
  62. }).sort((a, b) => a.position.x - b.position.x)
  63. const showVerticalHelpLineNodesLength = showVerticalHelpLineNodes.length
  64. if (showVerticalHelpLineNodesLength > 0) {
  65. const first = showVerticalHelpLineNodes[0]
  66. const last = showVerticalHelpLineNodes[showVerticalHelpLineNodesLength - 1]
  67. const helpLine = {
  68. top: first.position.y,
  69. left: first.position.x,
  70. height: last.position.y + last.height! - first.position.y,
  71. }
  72. if (node.position.y < first.position.y) {
  73. helpLine.top = node.position.y
  74. helpLine.height = first.position.y + first.height! - node.position.y
  75. }
  76. if (node.position.y > last.position.y)
  77. helpLine.height = node.position.y + node.height! - first.position.y
  78. setHelpLineVertical(helpLine)
  79. }
  80. else {
  81. setHelpLineVertical()
  82. }
  83. return {
  84. showHorizontalHelpLineNodes,
  85. showVerticalHelpLineNodes,
  86. }
  87. }, [store, workflowStore])
  88. return {
  89. handleSetHelpline,
  90. }
  91. }