feat: Build the positions of the Switch handle #1739 (#2018)

### What problem does this PR solve?

feat: Build the positions of the Switch handle #1739

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2024-08-20 16:08:53 +08:00
committed by GitHub
parent 6f438e0a49
commit 02985fc905
5 changed files with 119 additions and 36 deletions

View File

@@ -1,7 +1,7 @@
import { DSLComponents } from '@/interfaces/database/flow';
import { removeUselessFieldsFromValues } from '@/utils/form';
import { humanId } from 'human-id';
import { curry, sample } from 'lodash';
import { curry, intersectionWith, isEqual, sample } from 'lodash';
import pipe from 'lodash/fp/pipe';
import isObject from 'lodash/isObject';
import { Edge, Node, Position } from 'reactflow';
@@ -172,10 +172,24 @@ export const isEdgeEqual = (previous: Edge, current: Edge) =>
previous.sourceHandle === current.sourceHandle;
export const buildNewPositionMap = (
categoryDataKeys: string[],
indexesInUse: number[],
currentKeys: string[],
previousPositionMap: Record<string, IPosition>,
) => {
return categoryDataKeys.reduce<Record<string, IPosition>>((pre, cur) => {
// index in use
const indexesInUse = Object.values(previousPositionMap).map((x) => x.idx);
const previousKeys = Object.keys(previousPositionMap);
const intersectionKeys = intersectionWith(
previousKeys,
currentKeys,
(categoryDataKey, positionMapKey) => categoryDataKey === positionMapKey,
);
// difference set
const currentDifferenceKeys = currentKeys.filter(
(x) => !intersectionKeys.some((y) => y === x),
);
const newPositionMap = currentDifferenceKeys.reduce<
Record<string, IPosition>
>((pre, cur) => {
// take a coordinate
const effectiveIdxes = CategorizeAnchorPointPositions.map(
(x, idx) => idx,
@@ -188,4 +202,10 @@ export const buildNewPositionMap = (
return pre;
}, {});
return { intersectionKeys, newPositionMap };
};
export const isKeysEqual = (currentKeys: string[], previousKeys: string[]) => {
return isEqual(currentKeys.sort(), previousKeys.sort());
};