File size: 3,952 Bytes
68ed806 f43283a 457b4e6 970e973 457b4e6 970e973 38eebf9 970e973 457b4e6 970e973 457b4e6 e3b65ea 457b4e6 970e973 f43283a 38eebf9 970e973 457b4e6 e3b65ea 95ccaf9 e3b65ea 970e973 e3b65ea 970e973 e3b65ea 457b4e6 e3b65ea 457b4e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import { useTranslate } from '@/hooks/common-hooks';
import { Flex } from 'antd';
import classNames from 'classnames';
import { pick } from 'lodash';
import get from 'lodash/get';
import intersectionWith from 'lodash/intersectionWith';
import isEqual from 'lodash/isEqual';
import lowerFirst from 'lodash/lowerFirst';
import { useEffect, useMemo, useState } from 'react';
import { Handle, NodeProps, Position, useUpdateNodeInternals } from 'reactflow';
import { Operator, operatorMap } from '../../constant';
import { IPosition, NodeData } from '../../interface';
import OperatorIcon from '../../operator-icon';
import { buildNewPositionMap } from '../../utils';
import CategorizeHandle from './categorize-handle';
import NodeDropdown from './dropdown';
import styles from './index.less';
import NodePopover from './popover';
export function CategorizeNode({ id, data, selected }: NodeProps<NodeData>) {
const updateNodeInternals = useUpdateNodeInternals();
const [postionMap, setPositionMap] = useState<Record<string, IPosition>>({});
const categoryData = useMemo(
() => get(data, 'form.category_description') ?? {},
[data],
);
const style = operatorMap[data.label as Operator];
const { t } = useTranslate('flow');
useEffect(() => {
// Cache used coordinates
setPositionMap((state) => {
// index in use
const indexesInUse = Object.values(state).map((x) => x.idx);
const categoryDataKeys = Object.keys(categoryData);
const stateKeys = Object.keys(state);
if (!isEqual(categoryDataKeys.sort(), stateKeys.sort())) {
const intersectionKeys = intersectionWith(
stateKeys,
categoryDataKeys,
(categoryDataKey, postionMapKey) => categoryDataKey === postionMapKey,
);
const newPositionMap = buildNewPositionMap(
categoryDataKeys.filter(
(x) => !intersectionKeys.some((y) => y === x),
),
indexesInUse,
);
const nextPostionMap = {
...pick(state, intersectionKeys),
...newPositionMap,
};
return nextPostionMap;
}
return state;
});
}, [categoryData]);
useEffect(() => {
updateNodeInternals(id);
}, [id, updateNodeInternals, postionMap]);
return (
<NodePopover nodeId={id}>
<section
className={classNames(styles.logicNode, {
[styles.selectedNode]: selected,
})}
style={{
backgroundColor: style.backgroundColor,
color: style.color,
}}
>
<Handle
type="target"
position={Position.Left}
isConnectable
className={styles.handle}
id={'a'}
></Handle>
<Handle
type="target"
position={Position.Top}
isConnectable
className={styles.handle}
id={'b'}
></Handle>
<Handle
type="target"
position={Position.Bottom}
isConnectable
className={styles.handle}
id={'c'}
></Handle>
{Object.keys(categoryData).map((x, idx) => {
const position = postionMap[x];
return (
position && (
<CategorizeHandle
top={position.top}
right={position.right}
key={idx}
text={x}
idx={idx}
></CategorizeHandle>
)
);
})}
<Flex vertical align="center" justify="center" gap={6}>
<OperatorIcon
name={data.label as Operator}
fontSize={24}
></OperatorIcon>
<span className={styles.type}>{t(lowerFirst(data.label))}</span>
<NodeDropdown id={id}></NodeDropdown>
</Flex>
<section className={styles.bottomBox}>
<div className={styles.nodeName}>{data.name}</div>
</section>
</section>
</NodePopover>
);
}
|