useSaveAndRedo.ts
4.87 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
174
175
176
177
178
179
180
181
182
183
184
185
186
import type { VueFlowStore, Getters, Elements } from '@vue-flow/core';
import { ComputedRef, computed, ref, unref } from 'vue';
import { BasicNodeBindData, EdgeData, NodeData } from '../types/node';
import { EntryCategoryComponentEnum } from '../enum/category';
import { useBasicDataTransform } from './useBasicDataTransform';
import { getRuleChainData, saveRuleChainData } from '/@/api/ruleDesigner';
import { ConnectionItemType, RuleChainType } from '../types/ruleNode';
import { useInputNode } from './useInputNode';
import { buildUUID } from '/@/utils/uuid';
import { useRoute } from 'vue-router';
const ignoreNodeKeys: string[] = [EntryCategoryComponentEnum.INPUT];
export function useSaveAndRedo() {
const changeMarker = ref(false);
const loading = ref(false);
const redoDataRef = ref<Elements>([]);
const route = useRoute();
const getRuleChainId = computed(() => (route.params as Record<'id', string>).id);
const { mergeData, deconstructionData } = useBasicDataTransform();
const triggerChange = () => {
changeMarker.value = true;
};
const resetChange = () => {
changeMarker.value = false;
};
/**
* @description 保存连接信息
*/
function getConnections(
nodesRef: ComputedRef<Getters['getNodes']> | Getters['getNodes'],
edges: ComputedRef<Getters['getEdges']>
) {
const nodeIndexMap = new Map();
const connections: ConnectionItemType[] = [];
unref(nodesRef).forEach((item, index) => {
nodeIndexMap.set(item.id, index);
});
for (const item of unref(edges)) {
const { data, target, source } = item;
const { data: bindData } = data as EdgeData;
const { type } = bindData || {};
const fromIndex = nodeIndexMap.get(source);
const toIndex = nodeIndexMap.get(target);
type?.forEach((key) => {
connections.push({ fromIndex, toIndex, type: key });
});
}
return connections;
}
function getNodes(nodesRef: ComputedRef<Getters['getNodes']> | Getters['getNodes']) {
const nodes: BasicNodeBindData[] = [];
for (const node of unref(nodesRef)) {
const nodeData = node.data as NodeData;
if (ignoreNodeKeys.includes(nodeData.config?.key as string)) continue;
const data = nodeData.data;
nodes.push(mergeData(data, nodeData, node));
}
return nodes;
}
function getFirsetNodeIndex(
nodesRef: ComputedRef<Getters['getNodes']> | Getters['getNodes'],
edges: ComputedRef<Getters['getEdges']>
) {
const inputNode = unref(edges).find(
(item) => (item.sourceNode.data as NodeData).config?.key === EntryCategoryComponentEnum.INPUT
);
if (inputNode) {
const targetId = inputNode.target;
const index = unref(nodesRef).findIndex((item) => item.id === targetId);
return index;
}
}
const handleApplyChange = (flowActionType: VueFlowStore) => {
if (!unref(changeMarker)) return;
const edgesRef = flowActionType.getEdges;
const extraIgnoreNodeRef = unref(flowActionType.getNodes).filter(
(item) => !ignoreNodeKeys.includes((item.data as NodeData).config?.key as string)
);
const connections = getConnections(extraIgnoreNodeRef, edgesRef);
const nodes = getNodes(extraIgnoreNodeRef);
const firstNodeIndex = getFirsetNodeIndex(extraIgnoreNodeRef, edgesRef);
handleSaveRuleChain(connections, nodes, firstNodeIndex);
};
const handleRedoChange = (flowActionType: VueFlowStore) => {
if (!unref(changeMarker)) return;
flowActionType.setElements(unref(redoDataRef));
resetChange();
};
async function handleSaveRuleChain(
connections: ConnectionItemType[],
nodes: BasicNodeBindData[],
firstNodeIndex?: number
) {
try {
loading.value = true;
const data = await saveRuleChainData({
connections,
nodes,
firstNodeIndex,
ruleChainId: {
entityType: 'RULE_CHAIN',
id: unref(getRuleChainId),
},
});
parseRuleChain(data);
resetChange();
} finally {
loading.value = false;
}
}
async function getCurrentPageMetaData(flowActionType: VueFlowStore) {
try {
loading.value = true;
const data = await getRuleChainData(unref(getRuleChainId));
const elements = parseRuleChain(data);
flowActionType.setElements(elements);
resetChange();
} finally {
loading.value = false;
}
}
function parseRuleChain(ruleChain: RuleChainType) {
const inputId = buildUUID();
const { getInputNodeConfig } = useInputNode();
const value = deconstructionData(ruleChain, inputId);
const { nodes = [], edges = [] } = value || {};
const inputNode = getInputNodeConfig(inputId);
const elements = [inputNode, ...nodes, ...edges];
redoDataRef.value = elements;
return elements;
}
return {
loading,
changeMarker,
triggerChange,
handleApplyChange,
handleRedoChange,
getCurrentPageMetaData,
};
}