-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathshortcut.ts
More file actions
167 lines (160 loc) · 5.56 KB
/
shortcut.ts
File metadata and controls
167 lines (160 loc) · 5.56 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
import { cloneDeep } from 'lodash'
import type LogicFlow from '@logicflow/core'
import { type GraphModel } from '@logicflow/core'
import { MsgSuccess, MsgError, MsgConfirm } from '@/utils/message'
import { WorkflowType } from '@/enums/application'
import { t } from '@/locales'
import { getMenuNodes, workflowModelDict } from './data'
let selected: any | null = null
function translationNodeData(nodeData: any, distance: any) {
nodeData.x += distance
nodeData.y += distance
if (nodeData.text) {
nodeData.text.x += distance
nodeData.text.y += distance
}
return nodeData
}
function translationEdgeData(edgeData: any, distance: any) {
if (edgeData.startPoint) {
edgeData.startPoint.x += distance
edgeData.startPoint.y += distance
}
if (edgeData.endPoint) {
edgeData.endPoint.x += distance
edgeData.endPoint.y += distance
}
if (edgeData.pointsList && edgeData.pointsList.length > 0) {
edgeData.pointsList.forEach((point: any) => {
point.x += distance
point.y += distance
})
}
if (edgeData.text) {
edgeData.text.x += distance
edgeData.text.y += distance
}
return edgeData
}
const TRANSLATION_DISTANCE = 40
let CHILDREN_TRANSLATION_DISTANCE = 40
export function initDefaultShortcut(lf: LogicFlow, graph: GraphModel) {
const { keyboard } = lf
const {
options: { keyboard: keyboardOptions },
} = keyboard
const copy_node = () => {
CHILDREN_TRANSLATION_DISTANCE = TRANSLATION_DISTANCE
if (!keyboardOptions?.enabled) return true
if (graph.textEditElement) return true
const { guards } = lf.options
const elements = graph.getSelectElements(false)
const enabledClone = guards && guards.beforeClone ? guards.beforeClone(elements) : true
if (!enabledClone || (elements.nodes.length === 0 && elements.edges.length === 0)) {
selected = null
return true
}
const base_nodes = elements.nodes.filter(
(node: any) => node.type === WorkflowType.Start || node.type === WorkflowType.Base,
)
if (base_nodes.length > 0) {
MsgError(base_nodes[0]?.properties?.stepName + t('workflow.tip.cannotCopy'))
return
}
selected = cloneDeep(elements)
selected.nodes.forEach((node: any) => translationNodeData(node, TRANSLATION_DISTANCE))
selected.edges.forEach((edge: any) => translationEdgeData(edge, TRANSLATION_DISTANCE))
MsgSuccess(t('workflow.tip.copyError'))
return false
}
const paste_node = () => {
if (!keyboardOptions?.enabled) return true
if (graph.textEditElement) return true
const workflowMode = lf.graphModel.get_provide(null, null).workflowMode
const menus = getMenuNodes(workflowMode)
const nodes = menus?.flatMap((m: any) => m.list).map((n) => n.type)
selected.nodes = selected.nodes.filter(
(n: any) => nodes?.includes(n.type) || workflowModelDict[workflowMode](n),
)
if (selected && (selected.nodes || selected.edges)) {
lf.clearSelectElements()
const addElements = lf.addElements(selected, CHILDREN_TRANSLATION_DISTANCE)
if (!addElements) return true
addElements.nodes.forEach((node) => lf.selectElementById(node.id, true))
addElements.edges.forEach((edge) => lf.selectElementById(edge.id, true))
selected.nodes.forEach((node: any) => translationNodeData(node, TRANSLATION_DISTANCE))
selected.edges.forEach((edge: any) => translationEdgeData(edge, TRANSLATION_DISTANCE))
CHILDREN_TRANSLATION_DISTANCE = CHILDREN_TRANSLATION_DISTANCE + TRANSLATION_DISTANCE
}
return false
}
const delete_node = () => {
const elements = graph.getSelectElements(true)
lf.clearSelectElements()
if (elements.nodes.length == 0 && elements.edges.length == 0) {
return
}
if (elements.edges.length > 0 && elements.nodes.length == 0) {
elements.edges
.filter((edge) => !['loop-edge'].includes(edge.type || ''))
.forEach((edge: any) => lf.deleteEdge(edge.id))
return
}
const nodes = elements.nodes.filter((node) =>
[
'start-node',
'tool-start-node',
'tool-base-node',
'base-node',
'loop-body-node',
'loop-start-node',
'knowledge-base-node',
].includes(node.type),
)
if (nodes.length > 0) {
MsgError(`${nodes[0].properties?.stepName}${t('workflow.delete.deleteMessage')}`)
return
}
MsgConfirm(t('common.tip'), t('workflow.delete.confirmTitle'), {
confirmButtonText: t('common.confirm'),
confirmButtonClass: 'danger',
}).then(() => {
if (!keyboardOptions?.enabled) return true
if (graph.textEditElement) return true
elements.edges.forEach((edge: any) => lf.deleteEdge(edge.id))
elements.nodes.forEach((node: any) => {
if (node.type === 'loop-node') {
const next = lf.getNodeOutgoingNode(node.id)
next.forEach((n: any) => {
if (n.type === 'loop-body-node') {
lf.deleteNode(n.id)
}
})
}
lf.deleteNode(node.id)
})
})
return false
}
graph.eventCenter.on('copy_node', copy_node)
// 复制
keyboard.on(['cmd + c', 'ctrl + c'], copy_node)
// 粘贴
keyboard.on(['cmd + v', 'ctrl + v'], paste_node)
// undo
keyboard.on(['cmd + z', 'ctrl + z'], () => {
// if (!keyboardOptions?.enabled) return true
// if (graph.textEditElement) return true
// lf.undo()
// return false
})
// redo
keyboard.on(['cmd + y', 'ctrl + y'], () => {
if (!keyboardOptions?.enabled) return true
if (graph.textEditElement) return true
lf.redo()
return false
})
// delete
keyboard.on(['backspace'], delete_node)
}