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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import get from 'lodash/get';
  2. import { useEffect, useMemo } from 'react';
  3. import { useUpdateNodeInternals } from 'reactflow';
  4. import { SwitchElseTo } from '../../constant';
  5. import {
  6. ICategorizeItemResult,
  7. ISwitchCondition,
  8. NodeData,
  9. } from '../../interface';
  10. import { generateSwitchHandleText } from '../../utils';
  11. export const useBuildCategorizeHandlePositions = ({
  12. data,
  13. id,
  14. }: {
  15. id: string;
  16. data: NodeData;
  17. }) => {
  18. const updateNodeInternals = useUpdateNodeInternals();
  19. const categoryData: ICategorizeItemResult = useMemo(() => {
  20. return get(data, `form.category_description`, {});
  21. }, [data]);
  22. const positions = useMemo(() => {
  23. const list: Array<{
  24. text: string;
  25. top: number;
  26. idx: number;
  27. }> = [];
  28. Object.keys(categoryData)
  29. .sort((a, b) => categoryData[a].index - categoryData[b].index)
  30. .forEach((x, idx) => {
  31. list.push({
  32. text: x,
  33. idx,
  34. top: idx === 0 ? 98 : list[idx - 1].top + 8 + 26,
  35. });
  36. });
  37. return list;
  38. }, [categoryData]);
  39. useEffect(() => {
  40. updateNodeInternals(id);
  41. }, [id, updateNodeInternals, categoryData]);
  42. return { positions };
  43. };
  44. export const useBuildSwitchHandlePositions = ({
  45. data,
  46. id,
  47. }: {
  48. id: string;
  49. data: NodeData;
  50. }) => {
  51. const updateNodeInternals = useUpdateNodeInternals();
  52. const conditions: ISwitchCondition[] = useMemo(() => {
  53. return get(data, 'form.conditions', []);
  54. }, [data]);
  55. const positions = useMemo(() => {
  56. const list: Array<{
  57. text: string;
  58. top: number;
  59. idx: number;
  60. condition?: ISwitchCondition;
  61. }> = [];
  62. [...conditions, ''].forEach((x, idx) => {
  63. let top = idx === 0 ? 58 : list[idx - 1].top + 32; // case number (Case 1) height + flex gap
  64. if (idx - 1 >= 0) {
  65. const previousItems = conditions[idx - 1]?.items ?? [];
  66. if (previousItems.length > 0) {
  67. top += 12; // ConditionBlock padding
  68. top += previousItems.length * 22; // condition variable height
  69. top += (previousItems.length - 1) * 25; // operator height
  70. }
  71. }
  72. list.push({
  73. text:
  74. idx < conditions.length
  75. ? generateSwitchHandleText(idx)
  76. : SwitchElseTo,
  77. idx,
  78. top,
  79. condition: typeof x === 'string' ? undefined : x,
  80. });
  81. });
  82. return list;
  83. }, [conditions]);
  84. useEffect(() => {
  85. updateNodeInternals(id);
  86. }, [id, updateNodeInternals, conditions]);
  87. return { positions };
  88. };