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
import { provide, inject, ref, isRef, Ref, reactive, toRaw } from 'vue'
import { BaseEdgeModel } from '@logicflow/core'
import mitt from 'mitt'
import type { Store, StoreKey } from '../type/index'
 
const selected = ref(null)
const lf = ref()
const theme = ref({})
const lastLines = ref<BaseEdgeModel[]>([])
const flowMap = new Map()
const xmlData = ref(null)
 
/**
 * 选择节点
 * @param node
 */
const onSelectNode = (node: any, event: Event) => {
  store.selected.value = node.id
}
 
const onCancelSelect = (event: Event) => {
  // @ts-ignore
  if (event.target?.nodeName === 'svg') {
    store.selected.value = null
    cancelEdgeAnimation()
  }
}
const cancelEdgeAnimation = () => {
  const lfRef = toRaw(lf?.value)
  lastLines.value.forEach((line) => {
    lfRef?.closeEdgeAnimation(line.id)
  })
  lastLines.value = []
}
/**
 * 边动画
 * @param data
 */
const showEdgeAnimation = (data: Record<string, any>) => {
  if (lastLines.value.length) {
    cancelEdgeAnimation()
  }
  const lines = lf.value.getEdgeModels({
    sourceNodeId: data.id,
  })
  lastLines.value = lines
  lines.forEach((line: BaseEdgeModel) => {
    lf.value.openEdgeAnimation(line.id)
  })
}
 
const store = {
  lf,
  theme,
  xmlData,
  flowMap,
  selected,
  lastLines,
  onSelectNode,
  onCancelSelect,
  showEdgeAnimation,
} as Store
 
export const createStore = () => {
  return store
}
 
export const injectStore = (key?: StoreKey) => {
  if (key) {
    return store[key]
  }
  return store
}
 
export const emitter = mitt()