schangxiang@126.com
2025-05-07 cace264ad9d86a7831099810b079da1141957add
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
import {
  defineComponent,
  onMounted,
  ref,
  nextTick,
  watch,
  computed,
  getCurrentInstance,
  SetupContext,
} from 'vue'
import LogicFlow, { BaseEdgeModel } from '@logicflow/core'
import '@logicflow/core/dist/style/index.css'
import Dagre from '../../core/dagre'
import { createStore } from '../../core/store'
import { MiniMap } from '@logicflow/extension'
import Curve from '../Edges/Curve'
import StartNode from '../Nodes/StartNode'
import EndNode from '../Nodes/EndNode'
import OrdinaryNode from '../Nodes/OrdinaryNode'
import { eventMap } from '../../core/event'
import {
  toLowerCaseFirstLetter,
  getNodeTargetLines,
} from '../../core/transformHelp'
import { isFunction } from 'lodash'
import styles from './Renderer.module.scss'
import { emitter } from '../../core/store'
import Empty from '@/components/Empty/Empty'
 
interface PropsType {
  graphData: Record<string, any>
  [key: string]: any
  style: Record<string, any>
}
 
export default defineComponent<PropsType>({
  // @ts-ignore
  name: 'LogicFlowRenderer',
  props: {
    graphData: {
      type: Object,
      required: true,
    },
    style: { type: Object, default: () => ({}) },
    minimap: { type: [Boolean, Object], default: false },
    isEdgeAnimation: { type: Boolean, default: false },
  },
  // emits: Object.keys(eventMap),
  setup(props: PropsType, { expose, attrs, slots, emit }: SetupContext) {
    const lfRef = ref()
    const lf = ref()
    const store = createStore()
    const { onCancelSelect, showEdgeAnimation } = store
    const logicFlowConfig = computed(() => {
      return {
        ...attrs,
      }
    })
    /**
     * 注册边与节点
     */
    const batchRegister = () => {
      lf.value.batchRegister([Curve, StartNode, EndNode, OrdinaryNode])
    }
    /**
     * 主题设置
     */
    const setTheme = () => {
      const theme = store.theme
      lf.value.setTheme(theme.value)
    }
    /**
     * 自动布局
     */
    const autoLayout = () => {
      if (lf.value?.extension?.dagre) {
        lf.value.extension.dagre.layout({
          nodesep: 40,
          ranksep: 30,
          // radial: true,
          // controlPoints: true,
        })
      }
    }
 
    const showMiniMap = () => {
      if (!props.minimap) return
 
      let params: {
        leftPosition?: number | string
        topPosition?: number | string
      } = {
        leftPosition: 20,
        topPosition: 20,
      }
      if (typeof props.minimap === 'object') {
        params = props.minimap
      }
      lf.value?.extension.miniMap.show(params.leftPosition, params.topPosition)
    }
    /**
     * 渲染逻辑流
     * @param graphData
     */
    const renderLogicFlow = () => {
      if (!Object.keys(props.graphData).length) return
      lf.value.render(props.graphData)
 
      return nextTick(autoLayout)
    }
    /**
     * 初始化渲染
     */
    const initializeRenderer = async () => {
      batchRegister()
      setTheme()
      await renderLogicFlow()
      showMiniMap()
 
      initializeEvent()
    }
    /**
     * 实例化LogicFlow
     */
    const instanceLogicFlow = () => {
      if (!Object.keys(props.graphData).length) return
      lf.value = new LogicFlow({
        container: lfRef.value,
        plugins: [Dagre, MiniMap],
        ...logicFlowConfig.value,
      })
      store.lf.value = lf.value
      initializeRenderer()
    }
 
    /**
     * 注册事件
     */
    const initializeEvent = () => {
      const eventBox: string[] = []
      const eventNameMap: Record<string, any> = {}
      // 注册节点事件
      emitter.on('view', (node: any) => emit('view', node))
      // click事件单独做处理
      lf.value?.on(eventMap.nodeClick, (...arg: any) => {
        const { data } = arg[0]
        emit(eventMap.nodeClick, ...arg)
        if (props.isEdgeAnimation) {
          showEdgeAnimation(data)
        }
      })
      Object.entries(attrs).forEach(([eventName, fn]) => {
        if (eventName.includes('on')) {
          const name = toLowerCaseFirstLetter(eventName.replace('on', ''))
          eventNameMap[name] = fn
          if (eventMap.nodeClick !== name) {
            eventBox.push(name)
          }
        }
      })
 
      Object.entries(eventMap).forEach(([key, eventName]: string[]) => {
        if (eventBox.includes(key)) {
          lf.value?.on(eventName, (...arg: any) => {
            emit(key, ...arg)
          })
        }
      })
    }
    /**
     * 获取当前LogicFlow实例
     * @returns
     */
    const getCurrentInstance = () => {
      return lf.value
    }
 
    watch(
      () => props.graphData,
      (v, oldV) => {
        if (v !== oldV && v) {
          instanceLogicFlow()
        }
      }
    )
 
    onMounted(() => {
      instanceLogicFlow()
    })
 
    expose({
      autoLayout,
      getCurrentInstance,
    })
    return () => {
      return (
        <div
          class={styles.renderer}
          onClick={(event: Event) => onCancelSelect(event)}
          ref={lfRef}
          style={{ width: '100%', height: '100%', ...props.style }}
        >
          {slots.default?.()}
        </div>
      )
    }
  },
})