-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelEditorGraph.vue
432 lines (361 loc) · 12.4 KB
/
ModelEditorGraph.vue
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<template>
<div ref="wrapper" class="model-editor-graph" :class="isDragging ? '' : 'non-drag'">
<div ref="container"></div>
<ul v-if="GRAPH.isLoaded" class="toolbar left-top">
<li>
<el-tooltip content="保存模型 [Ctrl + s]">
<el-button :icon="SaveIcon" @click="handleSaveModel"></el-button>
</el-tooltip>
</li>
<li>
<el-tooltip content="编辑模型">
<el-button :icon="EditPen" @click="EDIT.handleEdit"></el-button>
</el-tooltip>
</li>
<li>
<el-tooltip :disabled="!GRAPH.canUndo" content="撤回 [Ctrl + z]">
<el-button :disabled="!GRAPH.canUndo" :icon="UndoIcon" @click="HISTORY.undo()"></el-button>
</el-tooltip>
<el-tooltip :disabled="!GRAPH.canRedo" content="重做 [Ctrl + Shift + z]">
<el-button :disabled="!GRAPH.canRedo" :icon="RedoIcon" @click="HISTORY.redo()"></el-button>
</el-tooltip>
</li>
<li>
<el-tooltip content="整理布局">
<el-button :icon="LayoutIcon" class="cling-right" @click="() => {
VIEW.layout()
if (graph.isSelectionEmpty()) {
VIEW.fit()
}
}"></el-button>
</el-tooltip>
<el-select v-model="VIEW.layoutDirection" class="cling-left" size="small"
style="width: 4em"
@change="VIEW.layout()">
<el-option label="→" value="LR">左至右</el-option>
<el-option label="←" value="RL">右至左</el-option>
<el-option label="↓" value="TB">上至下</el-option>
<el-option label="↑" value="BT">下至上</el-option>
</el-select>
</li>
<li>
<el-tooltip content="适应画布">
<el-button :icon="FitIcon" @click="VIEW.fit()"></el-button>
</el-tooltip>
</li>
<li>
<el-tooltip content="居中">
<el-button :icon="CenterIcon" @click="VIEW.center()"></el-button>
</el-tooltip>
</li>
</ul>
<ul v-if="GRAPH.isLoaded" class="toolbar left-bottom">
<li>
<el-tooltip :content="GRAPH.isSelectionEmpty ? '清理画布' : '移除选中节点与关联[Delete]'">
<el-button :icon="EraserIcon"
@click="GRAPH.isSelectionEmpty ? REMOVE.removeAllCells() : REMOVE.removeSelectedCells()"></el-button>
</el-tooltip>
</li>
<li>
<el-tooltip :content="GRAPH.isSelectionEmpty ? '清除关联' : '移除选中关联[Shift + Delete]'">
<el-button :icon="AssociationOffIcon"
@click="GRAPH.isSelectionEmpty ? REMOVE.removeAllEdges() : REMOVE.removeSelectedEdges()"></el-button>
</el-tooltip>
</li>
</ul>
<ul v-if="GRAPH.isLoaded" class="toolbar right-top">
<li>
<el-tooltip content="预览 SQL">
<el-button :icon="SQLIcon" @click="handleSQLPreview"></el-button>
</el-tooltip>
<DragDialog v-model="sqlPreviewDialogOpenState"
:init-w="700" :init-x="5000"
can-drag can-resize
disabled-h disabled-y
limit-by-parent :modal="false">
<MultiCodePreview :code-files="sqlFiles"
height="calc(100vh - 5em - 30px)"
width="100%"
class="multi-code-preview"></MultiCodePreview>
<div class="code-download-button">
<el-button :icon="DownloadIcon" round size="large"
@click="handleSQLDownload"></el-button>
</div>
</DragDialog>
</li>
<li>
<el-tooltip content="预览实体">
<el-button :icon="CodeIcon" @click="handleEntityPreview"></el-button>
</el-tooltip>
<DragDialog v-model="entityPreviewDialogOpenState"
:init-w="700" :init-x="5000"
can-drag can-resize
disabled-h disabled-y
limit-by-parent :modal="false">
<MultiCodePreview :code-files="entityFiles"
height="calc(100vh - 5em - 30px)"
width="100%"
class="multi-code-preview"></MultiCodePreview>
<div class="code-download-button">
<el-button :icon="DownloadIcon" round size="large"
@click="handleEntityDownload"></el-button>
</div>
</DragDialog>
</li>
<li>
<el-tooltip content="导出模型">
<el-button :icon="ExportIcon" @click="handleModelExport"></el-button>
</el-tooltip>
</li>
<li>
<el-tooltip content="下载全部(获得 zip 压缩包)">
<el-button :icon="DownloadIcon" @click="handleModelDownload"></el-button>
</el-tooltip>
</li>
</ul>
<div v-if="GRAPH.isLoaded" class="toolbar right-bottom" style="width: max(15vw, 200px)">
<MiniMap :graph="graph"></MiniMap>
<ScaleBar :graph="graph"></ScaleBar>
</div>
<template v-if="GRAPH.isLoaded">
<GraphSearcher :graph="graph"></GraphSearcher>
</template>
</div>
</template>
<style scoped>
.multi-code-preview {
height: calc(100vh - 30px);
width: 100%;
}
.code-download-button {
position: absolute;
bottom: 2em;
right: 2em;
}
</style>
<script lang="ts" setup>
import {onMounted, onUnmounted, ref, watch} from "vue"
import {Graph, Node} from "@antv/x6"
import {initModelEditor} from "./init.ts"
import {useModelEditorStore} from "../store/ModelEditorStore.ts";
import {useGlobalLoadingStore} from "@/components/global/loading/GlobalLoadingStore.ts";
import {api} from "@/api";
import {sendMessage} from "@/message/message.ts";
import {GenTableModelInput, Pair} from "@/api/__generated/model/static";
import SaveIcon from "@/components/global/icons/toolbar/SaveIcon.vue";
import LayoutIcon from "@/components/global/icons/toolbar/LayoutIcon.vue";
import MultiCodePreview from "@/components/global/code/MultiCodePreview.vue";
import DragDialog from "@/components/global/dialog/DragDialog.vue";
import SQLIcon from "@/components/global/icons/toolbar/SQLIcon.vue";
import FitIcon from "@/components/global/icons/toolbar/FitIcon.vue";
import CenterIcon from "@/components/global/icons/toolbar/CenterIcon.vue";
import EraserIcon from "@/components/global/icons/toolbar/EraserIcon.vue";
import AssociationOffIcon from "@/components/global/icons/toolbar/AssociationOffIcon.vue";
import DownloadIcon from "@/components/global/icons/toolbar/DownloadIcon.vue";
import ScaleBar from "@/components/global/graphEditor/scale/ScaleBar.vue";
import GraphSearcher from "@/components/pages/ModelEditor/search/GraphSearcher.vue";
import CodeIcon from "@/components/global/icons/toolbar/CodeIcon.vue";
import {EditPen} from "@element-plus/icons-vue";
import {useClipBoard} from "@/components/pages/ModelEditor/graph/clipBoard/clipBoard.ts";
import {ModelEditorEventBus} from "@/components/pages/ModelEditor/store/ModelEditorEventBus.ts";
import RedoIcon from "@/components/global/icons/toolbar/RedoIcon.vue";
import UndoIcon from "@/components/global/icons/toolbar/UndoIcon.vue";
import ExportIcon from "@/components/global/icons/toolbar/ExportIcon.vue";
import {
convertModel,
downloadModel,
downloadModelEntity,
downloadModelSql,
exportModel,
previewModelEntity,
previewModelSql
} from "@/components/pages/ModelEditor/file/modelFileOperations.ts";
import {cloneDeep} from "lodash";
import {TABLE_NODE} from "@/components/pages/ModelEditor/constant.ts";
import {useDocumentEvent} from "@/utils/useDocumentEvent.ts";
import MiniMap from "@/components/pages/ModelEditor/minimap/MiniMap.vue";
import {useDebugStore} from "@/debug/debugStore.ts";
import {handleModelEditorKeyEvent} from "@/components/pages/ModelEditor/graph/keyEvent/keyEvent.ts";
import {validateModelForm} from "@/components/business/model/form/validateModel.ts";
const container = ref<HTMLElement>()
const wrapper = ref<HTMLElement>()
let graph: Graph
const {GRAPH, HISTORY, EDIT, VIEW, REMOVE, MODEL, GRAPH_DATA} = useModelEditorStore()
const loadingStore = useGlobalLoadingStore()
const debugStore = useDebugStore()
onMounted(async () => {
const flag = loadingStore.start('ModelEditorGraph onMounted')
graph = initModelEditor(container.value!, wrapper.value!)
GRAPH.load(graph)
graph.on('history:change', (args) => {
const message = args.options.name ?? 'history:change'
debugStore.log('HISTORY', message, args.cmds)
})
graph.on('blank:dblclick', () => {
ModelEditorEventBus.emit('createTable', {options: GRAPH.mousePosition})
})
graph.on('node:click', ({node}) => {
handleNodeClick(node)
})
handleModelEditorKeyEvent(graph)
useClipBoard(graph)
loadingStore.stop(flag)
})
onUnmounted(() => {
GRAPH.unload()
})
// 表编辑事件
const doubleClickWaitNodes = new Set<string>
const DOUBLE_CLICK_TIMEOUT = 300
const handleNodeClick = (node: Node) => {
if (node.shape === TABLE_NODE && node.getData()?.table !== undefined) {
const id = node.id
const table = node.getData()?.table as GenTableModelInput
if (doubleClickWaitNodes.has(id)) {
doubleClickWaitNodes.delete(id)
ModelEditorEventBus.emit('editTable', {id, table: cloneDeep(table)})
} else {
graph.select(node)
doubleClickWaitNodes.add(id)
setTimeout(() => {
doubleClickWaitNodes.delete(id)
}, DOUBLE_CLICK_TIMEOUT)
}
}
}
const handleSaveModel = async () => {
const flag = loadingStore.start('ModelEditorGraph handleSaveModel')
try {
let model = MODEL._model()
MODEL.isLoaded = false
if (model.graphData !== GRAPH_DATA.getGraphData()) {
graph.cleanSelection()
model.graphData = GRAPH_DATA.getGraphData()
}
const messageList = validateModelForm(model)
if (messageList.length > 0) {
messageList.forEach(it => sendMessage(it, 'warning'))
loadingStore.stop(flag)
return
}
await api.modelService.save({body: model})
MODEL.isLoaded = true
sendMessage("模型保存成功", "success")
} catch (e) {
sendMessage(`模型保存失败,原因:${e}`, 'error', e)
}
loadingStore.stop(flag)
}
const handleSaveEvent = (e: KeyboardEvent) => {
if (e.ctrlKey || e.metaKey) {
if (e.key === 's') {
e.preventDefault()
handleSaveModel()
}
}
}
useDocumentEvent('keydown', handleSaveEvent)
const isDragging = ref(false)
useDocumentEvent('mousedown', (e) => {
if (e.button === 2) {
isDragging.value = true
}
})
useDocumentEvent('mouseup', (e) => {
if (e.button === 2) {
isDragging.value = false
}
})
watch(() => MODEL.isLoaded, async (value) => {
if (value) {
if (sqlPreviewDialogOpenState.value) {
await handleSQLPreview()
}
if (entityPreviewDialogOpenState.value) {
await handleEntityPreview()
}
}
})
/**
* 代码预览与下载
*/
const judgeGraphDataIsChange = (): boolean => {
if (GRAPH.isLoaded && MODEL.isLoaded) {
if (
JSON.stringify(JSON.parse(GRAPH_DATA.getGraphData())["json"]) ===
JSON.stringify(JSON.parse(MODEL._model().graphData)["json"])
) {
return false
}
}
return true
}
const sendGraphDataChangeMessage = (): boolean => {
const judge = judgeGraphDataIsChange()
if (judge) {
sendMessage('模型有变更尚未保存', 'warning')
}
return judge
}
const entityPreviewDialogOpenState = ref(false)
const entityFiles = ref<Array<Pair<string, string>>>([])
watch(() => entityPreviewDialogOpenState.value, async (openState) => {
if (!openState) {
entityFiles.value = []
}
})
const handleEntityPreview = async () => {
sendGraphDataChangeMessage()
const flag = loadingStore.start('ModelEditorGraph handleEntityPreview')
const model = MODEL._model()
await convertModel(model.id)
entityFiles.value = await previewModelEntity(model.id)
entityPreviewDialogOpenState.value = true
loadingStore.stop(flag)
}
const sqlPreviewDialogOpenState = ref(false)
const sqlFiles = ref<Array<Pair<string, string>>>([])
watch(() => sqlPreviewDialogOpenState.value, async (openState) => {
if (!openState) {
sqlFiles.value = []
}
})
const handleSQLPreview = async () => {
sendGraphDataChangeMessage()
const flag = loadingStore.start('ModelEditorGraph handleSQLPreview')
const model = MODEL._model()
sqlPreviewDialogOpenState.value = true
sqlFiles.value = await previewModelSql(model.id)
loadingStore.stop(flag)
}
const handleEntityDownload = async () => {
if (sendGraphDataChangeMessage()) return
const flag = loadingStore.start('ModelEditorGraph handleEntityDownload')
const model = MODEL._model()
await convertModel(model.id)
await downloadModelEntity(model)
loadingStore.stop(flag)
}
const handleSQLDownload = async () => {
if (sendGraphDataChangeMessage()) return
const flag = loadingStore.start('ModelEditorGraph handleSQLDownload')
const model = MODEL._model()
await downloadModelSql(model)
loadingStore.stop(flag)
}
const handleModelExport = async () => {
if (sendGraphDataChangeMessage()) return
const flag = loadingStore.start('ModelEditorGraph handleModelExport')
const model = MODEL._model()
await exportModel(model)
loadingStore.stop(flag)
}
const handleModelDownload = async () => {
if (sendGraphDataChangeMessage()) return
const flag = loadingStore.start('ModelEditorGraph handleModelDownload')
const model = MODEL._model()
await convertModel(model.id)
await downloadModel(model)
loadingStore.stop(flag)
}
</script>