gbflowengine 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/README.md +271 -271
  2. package/dist/gbflowengine.css +1 -1
  3. package/dist/gbflowengine.js +8232 -6913
  4. package/dist/gbflowengine.js.map +1 -1
  5. package/dist/gbflowengine.umd.cjs +2 -2
  6. package/dist/gbflowengine.umd.cjs.map +1 -1
  7. package/package.json +85 -84
  8. package/src/center/DefinitionInspect.vue +291 -291
  9. package/src/center/FlowCenter.vue +33 -33
  10. package/src/center/index.js +29 -29
  11. package/src/center/storage.js +748 -748
  12. package/src/core/flow/condition.js +160 -160
  13. package/src/core/flow/customNode.js +385 -385
  14. package/src/core/flow/defaults.js +124 -124
  15. package/src/core/flow/flowConstant.js +265 -265
  16. package/src/core/flow/flowNodeUtils.js +29 -29
  17. package/src/core/flow/index.js +47 -47
  18. package/src/core/flow/node.js +44 -44
  19. package/src/core/flow/nodeFactory.js +58 -58
  20. package/src/core/flow/prediction.js +325 -325
  21. package/src/core/flow/schema.js +864 -864
  22. package/src/definition/FlowDefinitionDesigner.vue +338 -338
  23. package/src/definition/index.js +9 -9
  24. package/src/index.js +39 -39
  25. package/src/storage.js +200 -200
  26. package/src/testing/FlowTestPanel.vue +2502 -2502
  27. package/src/testing/UserConfigDrawer.vue +376 -376
  28. package/src/testing/diagram/FlowProgressDiagram.vue +318 -318
  29. package/src/testing/diagramModel.js +517 -517
  30. package/src/testing/index.js +23 -23
  31. package/src/testing/mock.js +246 -246
  32. package/src/testing/oaExecutors.js +51 -51
  33. package/src/testing/oaSeed.js +467 -467
  34. package/src/testing/sample.js +412 -412
  35. package/src/testing/seed.js +122 -122
  36. package/src/utils/format.js +27 -27
  37. package/src/visual/FlowCanvas.vue +440 -440
  38. package/src/visual/NodeProgressDetail.vue +43 -43
  39. package/src/visual/NodeStatusBadge.vue +17 -17
  40. package/src/visual/NodeWrap.vue +46 -46
  41. package/src/visual/ScWorkflow.vue +49 -49
  42. package/src/visual/SelectDialog.vue +459 -459
  43. package/src/visual/conditionHelpers.js +116 -116
  44. package/src/visual/index.js +6 -6
  45. package/src/visual/nodes/AddNode.vue +222 -222
  46. package/src/visual/nodes/ApproverNode.vue +347 -347
  47. package/src/visual/nodes/BranchNode.vue +441 -441
  48. package/src/visual/nodes/CustomNode.vue +339 -339
  49. package/src/visual/nodes/EndNode.vue +21 -21
  50. package/src/visual/nodes/PassNode.vue +100 -100
  51. package/src/visual/nodes/PromoterNode.vue +134 -134
  52. package/src/visual/nodes/RejectNode.vue +100 -100
  53. package/src/visual/nodes/SendNode.vue +151 -151
  54. package/src/visual/styles.scss +879 -879
  55. package/src/visual/useScNodeStatus.js +18 -18
  56. package/src/workbench/OaWorkbench.vue +1341 -1341
  57. package/src/workbench/components/ApprovalWorkspace.vue +2090 -2090
  58. package/src/workbench/components/CategoryIcon.vue +74 -74
  59. package/src/workbench/components/NotificationIcon.vue +73 -73
  60. package/src/workbench/components/ProcessSteps.vue +557 -557
  61. package/src/workbench/components/ResubmitDialog.vue +227 -227
  62. package/src/workbench/components/SidebarNav.vue +138 -138
  63. package/src/workbench/components/StartFlowDialog.vue +1058 -1058
  64. package/src/workbench/components/UserAvatar.vue +68 -68
  65. package/src/workbench/model.js +1080 -1080
  66. package/src/workbench/pages/DesignPage.vue +798 -798
  67. package/src/workbench/pages/HomePage.vue +1050 -1050
  68. package/src/workbench/pages/MonitorPage.vue +1029 -1029
  69. package/src/workbench/pages/NotificationsPage.vue +554 -554
  70. package/src/workbench/pages/RequestsPage.vue +644 -644
  71. package/src/workbench/pages/TaskCenterPage.vue +1155 -1155
  72. package/src/workbench/pages/TemplatesPage.vue +680 -680
  73. package/src/workbench/styles.scss +555 -555
  74. package/types/center.d.ts +5 -5
  75. package/types/core.d.ts +479 -479
  76. package/types/definition.d.ts +4 -4
  77. package/types/index.d.ts +34 -34
  78. package/types/testing.d.ts +20 -20
  79. package/types/visual.d.ts +20 -20
@@ -1,441 +1,441 @@
1
- <template>
2
- <div class="gb-flow-canvas">
3
- <div v-if="showToolbar" class="gb-flow-canvas__toolbar btn-container">
4
- <el-button type="primary" @click="openJson">查看 JSON</el-button>
5
- <el-button type="primary" @click="saveAsPng">保存图片</el-button>
6
- <el-button v-if="!readonly" type="primary" @click="openImport">导入 JSON</el-button>
7
- <el-button type="primary" @click="downloadWorkflow">导出 JSON</el-button>
8
- <div class="slider">
9
- <el-button type="primary" :icon="Minus" style="margin-right: 16px; width: 32px" @click="zoom -= 0.1" />
10
- <el-slider v-model="zoom" :marks="marks" :min="0.1" :max="5" :step="0.1" />
11
- <el-button type="primary" :icon="Plus" style="margin-left: 16px; width: 32px" @click="zoom += 0.1" />
12
- </div>
13
- <span class="gb-flow-canvas__count">{{ nodeCount }} 个节点</span>
14
- </div>
15
-
16
- <div class="gb-flow-canvas__viewport">
17
- <div class="gb-flow-canvas__stage" :style="{ transform: `scale(${zoom})` }">
18
- <ScWorkflow
19
- id="content-to-capture"
20
- v-model="data"
21
- :form-variables="formVariables"
22
- :readonly="readonly"
23
- />
24
- </div>
25
- </div>
26
-
27
- <el-drawer v-model="jsonVisible" size="500px" class="drawer" :append-to-body="true" :modal="false" :with-header="false">
28
- <div style="height: 100%">
29
- <div style="padding: 1px; background-color: #3883fa">
30
- <el-button type="primary" plain @click="copyJson(true)">复制格式化后的 JSON</el-button>
31
- <el-button type="primary" plain @click="copyJson(false)">复制压缩后的 JSON</el-button>
32
- <el-button type="primary" plain @click="applyJson">应用修改</el-button>
33
- <el-button type="primary" plain @click="jsonVisible = false">关闭弹窗</el-button>
34
- </div>
35
- <div v-if="jsonError || jsonValidation" class="gb-flow-canvas__feedback">
36
- <span v-if="jsonError" class="is-error">{{ jsonError }}</span>
37
- <span v-else-if="jsonValidation.valid">结构校验通过</span>
38
- <span v-else class="is-error">
39
- {{ jsonValidation.errors[0]?.message }}{{ jsonValidation.errors.length > 1 ? ` 等 ${jsonValidation.errors.length} 项问题` : '' }}
40
- </span>
41
- </div>
42
- <textarea v-model="jsonText" class="editor" spellcheck="false"></textarea>
43
- </div>
44
- </el-drawer>
45
-
46
- <el-dialog v-model="importVisible" title="导入 JSON" width="720px" append-to-body destroy-on-close>
47
- <div class="gb-flow-canvas__import-bar">
48
- <el-button :icon="FolderOpened" @click="chooseImportFile">选择 JSON 文件</el-button>
49
- <span v-if="importFileName" class="gb-flow-canvas__file-name">{{ importFileName }}</span>
50
- </div>
51
- <el-input v-model="importText" type="textarea" :rows="12" placeholder="粘贴流程 JSON" @input="previewImport" />
52
- <div v-if="importError" class="gb-flow-canvas__feedback"><span class="is-error">{{ importError }}</span></div>
53
- <div v-else-if="importValidation" class="gb-flow-canvas__feedback">
54
- <span :class="{ 'is-error': !importValidation.valid }">
55
- {{ importValidation.valid ? '结构校验通过' : importValidation.errors[0]?.message }}
56
- </span>
57
- </div>
58
- <template #footer>
59
- <el-button @click="importVisible = false">取消</el-button>
60
- <el-button type="primary" :disabled="!importDocument" @click="applyImport">导入</el-button>
61
- </template>
62
- </el-dialog>
63
- </div>
64
- </template>
65
-
66
- <script setup>
67
- import { computed, onMounted, ref, watch } from 'vue'
68
- import { ElMessage } from 'element-plus'
69
- import { FolderOpened, Minus, Plus } from '@element-plus/icons-vue'
70
- import ScWorkflow from './ScWorkflow.vue'
71
- import {
72
- countNodes,
73
- createDefinition,
74
- downloadJSON,
75
- ensureEndNode,
76
- exportWorkflow,
77
- FlowNodeType,
78
- importDefinition,
79
- pickJSONFile,
80
- validateWorkflow
81
- } from '../core/flow/index.js'
82
-
83
- defineOptions({ name: 'GbFlowCanvas' })
84
-
85
- const props = defineProps({
86
- modelValue: { type: Object, default: null },
87
- formVariables: { type: Array, default: () => [] },
88
- readonly: { type: Boolean, default: false },
89
- meta: { type: Object, default: () => ({}) },
90
- fileName: { type: String, default: 'workflow' },
91
- showToolbar: { type: Boolean, default: true },
92
- autoEnsureEnd: { type: Boolean, default: true },
93
- exportBuilder: { type: Function, default: null }
94
- })
95
-
96
- const emit = defineEmits(['update:modelValue', 'import', 'export', 'validate', 'change'])
97
-
98
- const clone = (value) => JSON.parse(JSON.stringify(value))
99
-
100
- function createDefaultWorkflow() {
101
- const workflow = {
102
- nodeId: 0,
103
- nodeName: '发起人',
104
- type: FlowNodeType.START,
105
- childNode: null
106
- }
107
- ensureEndNode(workflow)
108
- return workflow
109
- }
110
-
111
- function normalizeInput(value) {
112
- if (!value || typeof value !== 'object') return createDefaultWorkflow()
113
- const next = clone(value)
114
- if (props.autoEnsureEnd) ensureEndNode(next)
115
- return next
116
- }
117
-
118
- const data = ref(normalizeInput(props.modelValue))
119
- const zoom = ref(1)
120
- const marks = { 0.1: 'min', 1: '1', 2: '2', 3: '3', 4: '4', 5: 'max' }
121
- const nodeCount = computed(() => countNodes(data.value))
122
-
123
- let syncing = false
124
-
125
- watch(
126
- () => props.modelValue,
127
- (value) => {
128
- if (!value || syncing) return
129
- if (JSON.stringify(value) === JSON.stringify(data.value)) return
130
- syncing = true
131
- data.value = normalizeInput(value)
132
- requestAnimationFrame(() => {
133
- syncing = false
134
- })
135
- },
136
- { deep: true }
137
- )
138
-
139
- watch(
140
- data,
141
- (value) => {
142
- if (syncing) return
143
- if (props.autoEnsureEnd) ensureEndNode(value)
144
- const next = clone(value)
145
- emit('update:modelValue', next)
146
- emit('change', next)
147
- },
148
- { deep: true }
149
- )
150
-
151
- onMounted(() => {
152
- if (props.autoEnsureEnd) ensureEndNode(data.value)
153
- })
154
-
155
- // ==================== JSON drawer ====================
156
-
157
- const jsonVisible = ref(false)
158
- const jsonText = ref('')
159
- const jsonError = ref('')
160
- const jsonValidation = ref(null)
161
-
162
- const openJson = () => {
163
- jsonText.value = JSON.stringify(data.value, null, ' ')
164
- jsonError.value = ''
165
- jsonValidation.value = validateWorkflow(data.value)
166
- jsonVisible.value = true
167
- }
168
-
169
- watch(jsonText, (text) => {
170
- if (!jsonVisible.value) return
171
- jsonError.value = ''
172
- jsonValidation.value = null
173
- if (!text.trim()) return
174
- try {
175
- const parsed = JSON.parse(text)
176
- jsonValidation.value = validateWorkflow(parsed.workflow || parsed)
177
- } catch (error) {
178
- jsonError.value = `JSON 解析失败:${error.message}`
179
- }
180
- })
181
-
182
- const applyJson = () => {
183
- try {
184
- const result = importDefinition(jsonText.value)
185
- data.value = result.document.workflow
186
- jsonValidation.value = result.validation
187
- ElMessage.success('流程 JSON 已应用')
188
- } catch (error) {
189
- jsonError.value = error.message || String(error)
190
- }
191
- }
192
-
193
- const copyJson = async (pretty) => {
194
- const text = pretty ? JSON.stringify(data.value, null, ' ') : JSON.stringify(data.value)
195
- try {
196
- await navigator.clipboard.writeText(text)
197
- ElMessage.success('已复制到剪贴板')
198
- } catch {
199
- ElMessage.warning('复制失败,请手动复制')
200
- }
201
- }
202
-
203
- // ==================== Import / export ====================
204
-
205
- const importVisible = ref(false)
206
- const importText = ref('')
207
- const importFileName = ref('')
208
- const importError = ref('')
209
- const importValidation = ref(null)
210
- const importDocument = ref(null)
211
-
212
- const openImport = () => {
213
- importVisible.value = true
214
- importText.value = ''
215
- importFileName.value = ''
216
- importError.value = ''
217
- importValidation.value = null
218
- importDocument.value = null
219
- }
220
-
221
- const previewImport = () => {
222
- importError.value = ''
223
- importValidation.value = null
224
- importDocument.value = null
225
- if (!importText.value.trim()) return
226
- try {
227
- const result = importDefinition(importText.value)
228
- importDocument.value = result.document
229
- importValidation.value = result.validation
230
- } catch (error) {
231
- importError.value = error.message || String(error)
232
- }
233
- }
234
-
235
- const chooseImportFile = async () => {
236
- const picked = await pickJSONFile()
237
- if (!picked) return
238
- importFileName.value = picked.file.name
239
- importText.value = picked.text
240
- if (picked.error) {
241
- importError.value = picked.error.message || '读取文件失败'
242
- return
243
- }
244
- previewImport()
245
- }
246
-
247
- const applyImport = () => {
248
- if (!importDocument.value) return
249
- data.value = importDocument.value.workflow
250
- importVisible.value = false
251
- emit('import', importDocument.value)
252
- ElMessage.success('流程导入成功')
253
- }
254
-
255
- const buildExportPayload = () => {
256
- if (typeof props.exportBuilder === 'function') {
257
- const payload = props.exportBuilder()
258
- if (payload) return payload
259
- }
260
- return exportWorkflow(data.value, { ...props.meta, name: props.meta?.name || props.fileName }, { raw: true })
261
- }
262
-
263
- const exportJSON = (space = 2) => {
264
- const payload = buildExportPayload()
265
- emit('export', payload)
266
- return JSON.stringify(payload, null, space)
267
- }
268
-
269
- const downloadWorkflow = () => {
270
- const payload = buildExportPayload()
271
- const isDefinition = typeof props.exportBuilder === 'function'
272
- const name = isDefinition
273
- ? `${props.meta?.name || props.fileName}.gbflow.json`
274
- : `${props.meta?.name || props.fileName}-workflow.json`
275
- downloadJSON(payload, name)
276
- emit('export', payload)
277
- ElMessage.success('JSON 已导出')
278
- }
279
-
280
- const saveAsPng = async () => {
281
- const element = document.getElementById('content-to-capture')
282
- if (!element) return
283
- const html2canvas = window.html2canvas
284
- if (typeof html2canvas !== 'function') {
285
- ElMessage.warning('保存图片需要宿主应用提供 html2canvas')
286
- return
287
- }
288
- const stage = element.parentElement
289
- const scale = stage.style.transform
290
- stage.style.transform = 'scale(1)'
291
- try {
292
- const canvas = await html2canvas(element, { backgroundColor: '#efefef' })
293
- const image = canvas.toDataURL('image/png')
294
- const link = document.createElement('a')
295
- link.download = `gbflow-${Date.now()}.png`
296
- link.href = image
297
- link.click()
298
- } finally {
299
- stage.style.transform = scale
300
- }
301
- }
302
-
303
- // ==================== Public API ====================
304
-
305
- const validate = () => {
306
- const result = validateWorkflow(data.value)
307
- emit('validate', result)
308
- return result
309
- }
310
-
311
- const getWorkflow = () => clone(data.value)
312
- const getDefinition = () =>
313
- createDefinition({
314
- workflow: data.value,
315
- form: [],
316
- meta: { ...props.meta, name: props.meta?.name || props.fileName }
317
- })
318
-
319
- const importJSON = (input) => {
320
- const result = importDefinition(input)
321
- data.value = result.document.workflow
322
- emit('import', result.document)
323
- return result
324
- }
325
-
326
- defineExpose({
327
- getWorkflow,
328
- getDefinition,
329
- importJSON,
330
- importDefinition,
331
- exportJSON,
332
- downloadWorkflow,
333
- validate,
334
- zoomIn: () => (zoom.value = Math.min(zoom.value + 0.1, 5)),
335
- zoomOut: () => (zoom.value = Math.max(zoom.value - 0.1, 0.1)),
336
- resetZoom: () => (zoom.value = 1),
337
- saveAsPng,
338
- data
339
- })
340
- </script>
341
-
342
- <style lang="scss">
343
- .gb-flow-canvas {
344
- display: flex;
345
- flex-direction: column;
346
- width: 100%;
347
- height: 100%;
348
- min-height: 360px;
349
- overflow: hidden;
350
- background-color: #efefef;
351
- box-sizing: border-box;
352
-
353
- &__toolbar {
354
- flex-shrink: 0;
355
- display: flex;
356
- align-items: center;
357
- gap: 4px;
358
- padding: 8px 16px;
359
- background: #fff;
360
- border-bottom: 1px solid var(--el-border-color-light);
361
- z-index: 5;
362
- }
363
-
364
- &__viewport {
365
- flex: 1;
366
- min-height: 0;
367
- overflow: auto;
368
- display: flex;
369
- justify-content: center;
370
- }
371
-
372
- &__stage {
373
- transform-origin: top center;
374
- width: fit-content;
375
- min-width: 100%;
376
- padding: 10px 0 60px;
377
- }
378
-
379
- #content-to-capture {
380
- padding: 10px;
381
- }
382
-
383
- .btn-container {
384
- display: flex;
385
- align-items: center;
386
- }
387
-
388
- .slider {
389
- margin-left: 16px;
390
- width: 300px;
391
- display: flex;
392
- align-items: center;
393
- }
394
-
395
- &__count {
396
- margin-left: 16px;
397
- font-size: 12px;
398
- color: #909399;
399
- }
400
-
401
- &__feedback {
402
- padding: 8px 12px;
403
- font-size: 12px;
404
- color: #606266;
405
- background: #f5f7fa;
406
-
407
- .is-error {
408
- color: var(--el-color-danger);
409
- }
410
- }
411
-
412
- &__import-bar {
413
- display: flex;
414
- align-items: center;
415
- gap: 10px;
416
- margin-bottom: 10px;
417
- }
418
-
419
- &__file-name {
420
- font-size: 12px;
421
- color: var(--el-color-primary);
422
- }
423
-
424
- .editor {
425
- width: 100%;
426
- height: calc(100vh - 76px);
427
- box-sizing: border-box;
428
- border: none;
429
- outline: none;
430
- resize: none;
431
- padding: 12px;
432
- font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
433
- font-size: 13px;
434
- line-height: 1.6;
435
- }
436
-
437
- .el-drawer__body {
438
- padding: 0 !important;
439
- }
440
- }
1
+ <template>
2
+ <div class="gb-flow-canvas">
3
+ <div v-if="showToolbar" class="gb-flow-canvas__toolbar btn-container">
4
+ <el-button type="primary" @click="openJson">查看 JSON</el-button>
5
+ <el-button type="primary" @click="saveAsPng">保存图片</el-button>
6
+ <el-button v-if="!readonly" type="primary" @click="openImport">导入 JSON</el-button>
7
+ <el-button type="primary" @click="downloadWorkflow">导出 JSON</el-button>
8
+ <div class="slider">
9
+ <el-button type="primary" :icon="Minus" style="margin-right: 16px; width: 32px" @click="zoom -= 0.1" />
10
+ <el-slider v-model="zoom" :marks="marks" :min="0.1" :max="5" :step="0.1" />
11
+ <el-button type="primary" :icon="Plus" style="margin-left: 16px; width: 32px" @click="zoom += 0.1" />
12
+ </div>
13
+ <span class="gb-flow-canvas__count">{{ nodeCount }} 个节点</span>
14
+ </div>
15
+
16
+ <div class="gb-flow-canvas__viewport">
17
+ <div class="gb-flow-canvas__stage" :style="{ transform: `scale(${zoom})` }">
18
+ <ScWorkflow
19
+ id="content-to-capture"
20
+ v-model="data"
21
+ :form-variables="formVariables"
22
+ :readonly="readonly"
23
+ />
24
+ </div>
25
+ </div>
26
+
27
+ <el-drawer v-model="jsonVisible" size="500px" class="drawer" :append-to-body="true" :modal="false" :with-header="false">
28
+ <div style="height: 100%">
29
+ <div style="padding: 1px; background-color: #3883fa">
30
+ <el-button type="primary" plain @click="copyJson(true)">复制格式化后的 JSON</el-button>
31
+ <el-button type="primary" plain @click="copyJson(false)">复制压缩后的 JSON</el-button>
32
+ <el-button type="primary" plain @click="applyJson">应用修改</el-button>
33
+ <el-button type="primary" plain @click="jsonVisible = false">关闭弹窗</el-button>
34
+ </div>
35
+ <div v-if="jsonError || jsonValidation" class="gb-flow-canvas__feedback">
36
+ <span v-if="jsonError" class="is-error">{{ jsonError }}</span>
37
+ <span v-else-if="jsonValidation.valid">结构校验通过</span>
38
+ <span v-else class="is-error">
39
+ {{ jsonValidation.errors[0]?.message }}{{ jsonValidation.errors.length > 1 ? ` 等 ${jsonValidation.errors.length} 项问题` : '' }}
40
+ </span>
41
+ </div>
42
+ <textarea v-model="jsonText" class="editor" spellcheck="false"></textarea>
43
+ </div>
44
+ </el-drawer>
45
+
46
+ <el-dialog v-model="importVisible" title="导入 JSON" width="720px" append-to-body destroy-on-close>
47
+ <div class="gb-flow-canvas__import-bar">
48
+ <el-button :icon="FolderOpened" @click="chooseImportFile">选择 JSON 文件</el-button>
49
+ <span v-if="importFileName" class="gb-flow-canvas__file-name">{{ importFileName }}</span>
50
+ </div>
51
+ <el-input v-model="importText" type="textarea" :rows="12" placeholder="粘贴流程 JSON" @input="previewImport" />
52
+ <div v-if="importError" class="gb-flow-canvas__feedback"><span class="is-error">{{ importError }}</span></div>
53
+ <div v-else-if="importValidation" class="gb-flow-canvas__feedback">
54
+ <span :class="{ 'is-error': !importValidation.valid }">
55
+ {{ importValidation.valid ? '结构校验通过' : importValidation.errors[0]?.message }}
56
+ </span>
57
+ </div>
58
+ <template #footer>
59
+ <el-button @click="importVisible = false">取消</el-button>
60
+ <el-button type="primary" :disabled="!importDocument" @click="applyImport">导入</el-button>
61
+ </template>
62
+ </el-dialog>
63
+ </div>
64
+ </template>
65
+
66
+ <script setup>
67
+ import { computed, onMounted, ref, watch } from 'vue'
68
+ import { ElMessage } from 'element-plus'
69
+ import { FolderOpened, Minus, Plus } from 'gbicons'
70
+ import ScWorkflow from './ScWorkflow.vue'
71
+ import {
72
+ countNodes,
73
+ createDefinition,
74
+ downloadJSON,
75
+ ensureEndNode,
76
+ exportWorkflow,
77
+ FlowNodeType,
78
+ importDefinition,
79
+ pickJSONFile,
80
+ validateWorkflow
81
+ } from '../core/flow/index.js'
82
+
83
+ defineOptions({ name: 'GbFlowCanvas' })
84
+
85
+ const props = defineProps({
86
+ modelValue: { type: Object, default: null },
87
+ formVariables: { type: Array, default: () => [] },
88
+ readonly: { type: Boolean, default: false },
89
+ meta: { type: Object, default: () => ({}) },
90
+ fileName: { type: String, default: 'workflow' },
91
+ showToolbar: { type: Boolean, default: true },
92
+ autoEnsureEnd: { type: Boolean, default: true },
93
+ exportBuilder: { type: Function, default: null }
94
+ })
95
+
96
+ const emit = defineEmits(['update:modelValue', 'import', 'export', 'validate', 'change'])
97
+
98
+ const clone = (value) => JSON.parse(JSON.stringify(value))
99
+
100
+ function createDefaultWorkflow() {
101
+ const workflow = {
102
+ nodeId: 0,
103
+ nodeName: '发起人',
104
+ type: FlowNodeType.START,
105
+ childNode: null
106
+ }
107
+ ensureEndNode(workflow)
108
+ return workflow
109
+ }
110
+
111
+ function normalizeInput(value) {
112
+ if (!value || typeof value !== 'object') return createDefaultWorkflow()
113
+ const next = clone(value)
114
+ if (props.autoEnsureEnd) ensureEndNode(next)
115
+ return next
116
+ }
117
+
118
+ const data = ref(normalizeInput(props.modelValue))
119
+ const zoom = ref(1)
120
+ const marks = { 0.1: 'min', 1: '1', 2: '2', 3: '3', 4: '4', 5: 'max' }
121
+ const nodeCount = computed(() => countNodes(data.value))
122
+
123
+ let syncing = false
124
+
125
+ watch(
126
+ () => props.modelValue,
127
+ (value) => {
128
+ if (!value || syncing) return
129
+ if (JSON.stringify(value) === JSON.stringify(data.value)) return
130
+ syncing = true
131
+ data.value = normalizeInput(value)
132
+ requestAnimationFrame(() => {
133
+ syncing = false
134
+ })
135
+ },
136
+ { deep: true }
137
+ )
138
+
139
+ watch(
140
+ data,
141
+ (value) => {
142
+ if (syncing) return
143
+ if (props.autoEnsureEnd) ensureEndNode(value)
144
+ const next = clone(value)
145
+ emit('update:modelValue', next)
146
+ emit('change', next)
147
+ },
148
+ { deep: true }
149
+ )
150
+
151
+ onMounted(() => {
152
+ if (props.autoEnsureEnd) ensureEndNode(data.value)
153
+ })
154
+
155
+ // ==================== JSON drawer ====================
156
+
157
+ const jsonVisible = ref(false)
158
+ const jsonText = ref('')
159
+ const jsonError = ref('')
160
+ const jsonValidation = ref(null)
161
+
162
+ const openJson = () => {
163
+ jsonText.value = JSON.stringify(data.value, null, ' ')
164
+ jsonError.value = ''
165
+ jsonValidation.value = validateWorkflow(data.value)
166
+ jsonVisible.value = true
167
+ }
168
+
169
+ watch(jsonText, (text) => {
170
+ if (!jsonVisible.value) return
171
+ jsonError.value = ''
172
+ jsonValidation.value = null
173
+ if (!text.trim()) return
174
+ try {
175
+ const parsed = JSON.parse(text)
176
+ jsonValidation.value = validateWorkflow(parsed.workflow || parsed)
177
+ } catch (error) {
178
+ jsonError.value = `JSON 解析失败:${error.message}`
179
+ }
180
+ })
181
+
182
+ const applyJson = () => {
183
+ try {
184
+ const result = importDefinition(jsonText.value)
185
+ data.value = result.document.workflow
186
+ jsonValidation.value = result.validation
187
+ ElMessage.success('流程 JSON 已应用')
188
+ } catch (error) {
189
+ jsonError.value = error.message || String(error)
190
+ }
191
+ }
192
+
193
+ const copyJson = async (pretty) => {
194
+ const text = pretty ? JSON.stringify(data.value, null, ' ') : JSON.stringify(data.value)
195
+ try {
196
+ await navigator.clipboard.writeText(text)
197
+ ElMessage.success('已复制到剪贴板')
198
+ } catch {
199
+ ElMessage.warning('复制失败,请手动复制')
200
+ }
201
+ }
202
+
203
+ // ==================== Import / export ====================
204
+
205
+ const importVisible = ref(false)
206
+ const importText = ref('')
207
+ const importFileName = ref('')
208
+ const importError = ref('')
209
+ const importValidation = ref(null)
210
+ const importDocument = ref(null)
211
+
212
+ const openImport = () => {
213
+ importVisible.value = true
214
+ importText.value = ''
215
+ importFileName.value = ''
216
+ importError.value = ''
217
+ importValidation.value = null
218
+ importDocument.value = null
219
+ }
220
+
221
+ const previewImport = () => {
222
+ importError.value = ''
223
+ importValidation.value = null
224
+ importDocument.value = null
225
+ if (!importText.value.trim()) return
226
+ try {
227
+ const result = importDefinition(importText.value)
228
+ importDocument.value = result.document
229
+ importValidation.value = result.validation
230
+ } catch (error) {
231
+ importError.value = error.message || String(error)
232
+ }
233
+ }
234
+
235
+ const chooseImportFile = async () => {
236
+ const picked = await pickJSONFile()
237
+ if (!picked) return
238
+ importFileName.value = picked.file.name
239
+ importText.value = picked.text
240
+ if (picked.error) {
241
+ importError.value = picked.error.message || '读取文件失败'
242
+ return
243
+ }
244
+ previewImport()
245
+ }
246
+
247
+ const applyImport = () => {
248
+ if (!importDocument.value) return
249
+ data.value = importDocument.value.workflow
250
+ importVisible.value = false
251
+ emit('import', importDocument.value)
252
+ ElMessage.success('流程导入成功')
253
+ }
254
+
255
+ const buildExportPayload = () => {
256
+ if (typeof props.exportBuilder === 'function') {
257
+ const payload = props.exportBuilder()
258
+ if (payload) return payload
259
+ }
260
+ return exportWorkflow(data.value, { ...props.meta, name: props.meta?.name || props.fileName }, { raw: true })
261
+ }
262
+
263
+ const exportJSON = (space = 2) => {
264
+ const payload = buildExportPayload()
265
+ emit('export', payload)
266
+ return JSON.stringify(payload, null, space)
267
+ }
268
+
269
+ const downloadWorkflow = () => {
270
+ const payload = buildExportPayload()
271
+ const isDefinition = typeof props.exportBuilder === 'function'
272
+ const name = isDefinition
273
+ ? `${props.meta?.name || props.fileName}.gbflow.json`
274
+ : `${props.meta?.name || props.fileName}-workflow.json`
275
+ downloadJSON(payload, name)
276
+ emit('export', payload)
277
+ ElMessage.success('JSON 已导出')
278
+ }
279
+
280
+ const saveAsPng = async () => {
281
+ const element = document.getElementById('content-to-capture')
282
+ if (!element) return
283
+ const html2canvas = window.html2canvas
284
+ if (typeof html2canvas !== 'function') {
285
+ ElMessage.warning('保存图片需要宿主应用提供 html2canvas')
286
+ return
287
+ }
288
+ const stage = element.parentElement
289
+ const scale = stage.style.transform
290
+ stage.style.transform = 'scale(1)'
291
+ try {
292
+ const canvas = await html2canvas(element, { backgroundColor: '#efefef' })
293
+ const image = canvas.toDataURL('image/png')
294
+ const link = document.createElement('a')
295
+ link.download = `gbflow-${Date.now()}.png`
296
+ link.href = image
297
+ link.click()
298
+ } finally {
299
+ stage.style.transform = scale
300
+ }
301
+ }
302
+
303
+ // ==================== Public API ====================
304
+
305
+ const validate = () => {
306
+ const result = validateWorkflow(data.value)
307
+ emit('validate', result)
308
+ return result
309
+ }
310
+
311
+ const getWorkflow = () => clone(data.value)
312
+ const getDefinition = () =>
313
+ createDefinition({
314
+ workflow: data.value,
315
+ form: [],
316
+ meta: { ...props.meta, name: props.meta?.name || props.fileName }
317
+ })
318
+
319
+ const importJSON = (input) => {
320
+ const result = importDefinition(input)
321
+ data.value = result.document.workflow
322
+ emit('import', result.document)
323
+ return result
324
+ }
325
+
326
+ defineExpose({
327
+ getWorkflow,
328
+ getDefinition,
329
+ importJSON,
330
+ importDefinition,
331
+ exportJSON,
332
+ downloadWorkflow,
333
+ validate,
334
+ zoomIn: () => (zoom.value = Math.min(zoom.value + 0.1, 5)),
335
+ zoomOut: () => (zoom.value = Math.max(zoom.value - 0.1, 0.1)),
336
+ resetZoom: () => (zoom.value = 1),
337
+ saveAsPng,
338
+ data
339
+ })
340
+ </script>
341
+
342
+ <style lang="scss">
343
+ .gb-flow-canvas {
344
+ display: flex;
345
+ flex-direction: column;
346
+ width: 100%;
347
+ height: 100%;
348
+ min-height: 360px;
349
+ overflow: hidden;
350
+ background-color: #efefef;
351
+ box-sizing: border-box;
352
+
353
+ &__toolbar {
354
+ flex-shrink: 0;
355
+ display: flex;
356
+ align-items: center;
357
+ gap: 4px;
358
+ padding: 8px 16px;
359
+ background: #fff;
360
+ border-bottom: 1px solid var(--el-border-color-light);
361
+ z-index: 5;
362
+ }
363
+
364
+ &__viewport {
365
+ flex: 1;
366
+ min-height: 0;
367
+ overflow: auto;
368
+ display: flex;
369
+ justify-content: center;
370
+ }
371
+
372
+ &__stage {
373
+ transform-origin: top center;
374
+ width: fit-content;
375
+ min-width: 100%;
376
+ padding: 10px 0 60px;
377
+ }
378
+
379
+ #content-to-capture {
380
+ padding: 10px;
381
+ }
382
+
383
+ .btn-container {
384
+ display: flex;
385
+ align-items: center;
386
+ }
387
+
388
+ .slider {
389
+ margin-left: 16px;
390
+ width: 300px;
391
+ display: flex;
392
+ align-items: center;
393
+ }
394
+
395
+ &__count {
396
+ margin-left: 16px;
397
+ font-size: 12px;
398
+ color: #909399;
399
+ }
400
+
401
+ &__feedback {
402
+ padding: 8px 12px;
403
+ font-size: 12px;
404
+ color: #606266;
405
+ background: #f5f7fa;
406
+
407
+ .is-error {
408
+ color: var(--el-color-danger);
409
+ }
410
+ }
411
+
412
+ &__import-bar {
413
+ display: flex;
414
+ align-items: center;
415
+ gap: 10px;
416
+ margin-bottom: 10px;
417
+ }
418
+
419
+ &__file-name {
420
+ font-size: 12px;
421
+ color: var(--el-color-primary);
422
+ }
423
+
424
+ .editor {
425
+ width: 100%;
426
+ height: calc(100vh - 76px);
427
+ box-sizing: border-box;
428
+ border: none;
429
+ outline: none;
430
+ resize: none;
431
+ padding: 12px;
432
+ font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
433
+ font-size: 13px;
434
+ line-height: 1.6;
435
+ }
436
+
437
+ .el-drawer__body {
438
+ padding: 0 !important;
439
+ }
440
+ }
441
441
  </style>