schangxiang@126.com
2025-05-21 aab29f4290c968665312bfc98c5598a25a4debf9
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
import { defineComponent, SetupContext, onMounted, reactive, ref } from 'vue'
import styles from './LogicFlow.module.scss'
import {
  getFlowDataToXml,
  getJsonByXml,
  getLogicFLowStruct,
} from './core/transformHelp'
import { getFlowXml } from '@/api/logic-flow'
import LogicRenderer from './components/Renderer/Renderer'
import Canvas from './components/Canvas/Canvas'
import Theme from './components/Theme/Theme'
import NodeDrawer from './components/NodeDrawer/NodeDrawer'
import EdgeDrawer from './components/EdgeDrawer/EdgeDrawer'
import { injectStore } from './core/store'
import Empty from '../Empty/Empty'
import { FlowType } from './type'
interface NodeDrawerConfig {
  visible: boolean
  title: string
  node: Record<string, any>
}
export default defineComponent({
  name: 'LogicFlow',
  props: {
    flowType: {
      type: [String, Number],
      default: '',
    },
  },
  setup(props, { slots, attrs }: SetupContext) {
    const { lf } = injectStore()
    const graphData = ref<FlowType | null>(null)
    const logicRenderRef = ref<any>()
 
    const nodeDrawerConfig = reactive<NodeDrawerConfig>({
      visible: false,
      title: '节点配置',
      node: {},
    })
    const edgeDrawerConfig = reactive<NodeDrawerConfig>({
      visible: false,
      title: '条件配置',
      node: {},
    })
    const initData = async () => {
      const res = await getFlowXml(props.flowType)
      const json = getJsonByXml(res)
      graphData.value = getLogicFLowStruct(json)
    }
 
    const onAutoLayout = () => {
      logicRenderRef.value?.autoLayout()
    }
    /**
     * 节点双击
     * @param data
     */
    const onNodeDbClick = ({ data }: Record<string, any>) => {
      nodeDrawerConfig.visible = true
      nodeDrawerConfig.node = data
      nodeDrawerConfig.title = data.properties?.name || data.id || '节点配置'
    }
 
    const onEdgeDbClick = ({ data }: Record<string, any>) => {
      edgeDrawerConfig.visible = true
      edgeDrawerConfig.node = data
      edgeDrawerConfig.title = data.properties?.name || data.id || '条件配置'
    }
 
    const onView = (properties: Record<string, any>) => {
      onNodeDbClick({
        data: {
          id: properties.id,
          properties,
        },
      })
    }
 
    const onTransformXml = () => {
      if (graphData.value !== null) {
        getFlowDataToXml(graphData.value)
      }
    }
 
    onMounted(initData)
 
    return () => {
      if (!graphData.value) return <el-empty description="暂无数据" />
 
      return (
        <div class={styles.logicFlow}>
          <NodeDrawer
            lf={lf}
            title={nodeDrawerConfig.title}
            v-model={nodeDrawerConfig.visible}
            node={nodeDrawerConfig.node}
          ></NodeDrawer>
          <EdgeDrawer
            title={edgeDrawerConfig.title}
            v-model={edgeDrawerConfig.visible}
            node={edgeDrawerConfig.node}
          ></EdgeDrawer>
          <el-button
            onClick={onTransformXml}
            class={styles.xmlbeautify}
            size="small"
            type="primary"
          >
            xml
          </el-button>
          <el-button
            onClick={onAutoLayout}
            class={styles.beautify}
            size="small"
            type="primary"
          >
            一键美化
          </el-button>
          <Canvas grid={{ visible: true }} minimap={true}>
            <Theme
              snapline={{
                stroke: '#ff0000', // 对齐线颜色
                strokeWidth: 0.5, // 对齐线宽度
              }}
            />
            <LogicRenderer
              ref={logicRenderRef}
              graphData={graphData.value}
              onNodeDbClick={onNodeDbClick}
              onEdgeDbClick={onEdgeDbClick}
              onView={onView}
              // adjustEdge={true}
              isEdgeAnimation={true}
              // isSilentMode={true}
              // adjustNodePosition={true}
              // hideAnchors={true}
              // edgeTextEdit={false}
              // nodeTextEdit={false}
            ></LogicRenderer>
          </Canvas>
        </div>
      )
    }
  },
})