useBasicDataTransform.ts
4.77 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Ref, toRaw, unref } from 'vue';
import { BasicNodeBindData, NodeData } from '../types/node';
import { Elements, GraphNode } from '@vue-flow/core';
import { RuleChainType } from '../types/ruleNode';
import { allComponents } from '../packages';
import { RuleNodeTypeEnum } from '../packages/index.type';
import { buildUUID } from '/@/utils/uuid';
import { isNullOrUnDef } from '/@/utils/is';
import { useAddNodes } from './useAddNodes';
import { useAddEdges } from './useAddEdges';
export function useBasicDataTransform() {
const nodeConfigMap = new Map<string, NodeData>();
function initNodeConfigMap() {
for (const key of Object.keys(allComponents)) {
const category = allComponents[key as RuleNodeTypeEnum];
for (const nodeConfig of category.components) {
const { clazz } = nodeConfig;
nodeConfigMap.set(clazz, { config: nodeConfig, category: category.category });
}
}
}
function mergeData(data: NodeData['data'], nodeData: NodeData, node: GraphNode) {
const { x: layoutX, y: layoutY } = node.computedPosition;
return {
debugMode: !!data?.debugMode,
name: data?.name,
type: nodeData.config?.clazz,
configuration: toRaw(unref(data?.configuration)),
additionalInfo: {
description: data?.description,
layoutX,
layoutY,
},
};
}
function deconstructionConnection(
ruleChain: Ref<RuleChainType> | RuleChainType,
inputNodeId: string
) {
const { connections, nodes, firstNodeIndex } = unref(ruleChain);
const indexMap = new Map<number, BasicNodeBindData>();
const groupByConnections = new Map<string, string[]>();
const SOURCE_HANDLE = '__handle-right';
const TARGET_HANDLE = '__handle-left';
const SEPARATOR = ',';
const edges: Elements = [];
const { getAddedgesParams } = useAddEdges();
nodes.forEach((item, index) => indexMap.set(index, item));
connections.forEach((item) => {
const { fromIndex, toIndex, type } = item;
const key = `${fromIndex}${SEPARATOR}${toIndex}`;
if (!groupByConnections.has(key)) groupByConnections.set(key, []);
const types = groupByConnections.get(key)!;
types.push(type);
});
for (const [key, types] of Array.from(groupByConnections.entries())) {
const [fromIndex, toIndex] = key.split(SEPARATOR);
const sourceNode = indexMap.get(Number(fromIndex));
const targetNode = indexMap.get(Number(toIndex));
const source = sourceNode!.id!.id;
const target = targetNode!.id!.id;
const sourceHandle = `${source}${SOURCE_HANDLE}`;
const targetHandle = `${target}${TARGET_HANDLE}`;
edges.push(
getAddedgesParams(
{
source,
target,
sourceHandle,
targetHandle,
},
{ type: types }
)
);
}
if (!isNullOrUnDef(firstNodeIndex)) {
const targetNode = indexMap.get(firstNodeIndex);
const source = inputNodeId;
const target = targetNode!.id!.id;
const sourceHandle = `${source}$${SOURCE_HANDLE}`;
const targetHandle = `${target}${TARGET_HANDLE}`;
edges.push(
getAddedgesParams(
{
source,
target,
sourceHandle,
targetHandle,
},
{}
)
);
}
return edges;
}
function genNewNodeByData(node: BasicNodeBindData, config: NodeData) {
const { additionalInfo, configuration, debugMode, name, id } = node;
const { layoutX, layoutY, description } = additionalInfo || {};
const { getAddNodesParams } = useAddNodes();
return getAddNodesParams(
{ x: layoutX!, y: layoutY! },
{
...config,
data: {
configuration,
debugMode,
description,
name,
},
},
{
id: id?.id || buildUUID(),
}
);
}
function deconstructionNode(nodes: RuleChainType['nodes']) {
const addNodes: Elements = [];
for (const node of unref(nodes)) {
const { type } = node;
if (!type) continue;
const nodeConfig = nodeConfigMap.get(type);
if (!nodeConfig) {
throw `No component configuration of type '${type}' was found`;
}
const newNode = genNewNodeByData(node, nodeConfig);
addNodes.push(newNode);
}
return addNodes;
}
function deconstructionData(
ruleChain: RuleChainType | Ref<RuleChainType | undefined>,
inputNodeId: string
) {
if (!ruleChain || !unref(ruleChain)) return;
ruleChain = toRaw(unref(ruleChain))!;
const nodes = deconstructionNode(ruleChain?.nodes || []);
const edges = deconstructionConnection(ruleChain!, inputNodeId);
return {
nodes,
edges,
};
}
initNodeConfigMap();
return {
mergeData,
deconstructionData,
};
}