import { defineStore } from 'pinia' import { NodeType, NodeTypeObj, VarsSource } from '@/views/workflow/types' import { handleNode } from '@/views/workflow/handle' import { getNodeDefault } from '@/views/workflow/config' export const useWorkflowStore = defineStore('workflow', { state: () => ({ graph: null, panel: { node: null, show: false, }, envVars: { show: false, vars: [], }, nodeSize: {}, autoSaveFlag: false, }), getters: {}, actions: { init(graph) { this.graph = graph return new Promise((resolve) => { const keys = Object.keys(NodeTypeObj) const arr: any = [] keys.forEach((v, i) => { const d = handleNode(getNodeDefault(v as NodeType)) delete d.portMarkup delete d.ports const node = this.graph.createNode(d) node.position(-99999, -99999) this.graph.addNode(node) arr.push({ node: node, flag: false, }) }) let timer = setInterval(() => { arr.forEach((v, i) => { const s = v.node.size() if (s.width > 1) { this.nodeSize[keys[i]] = s v.flag = true this.graph.removeNode(v.node) } }) if (arr.every((v) => v.flag)) { clearInterval(timer) resolve(null) } }, 100) }) }, nodePanelShow(node) { const f = () => { this.panel.node = node setTimeout(() => { this.panel.show = true }, 0) } if (this.panel.show) { this.nodePanelClose() setTimeout(() => { f() }, 0) } else { f() } }, nodePanelClose() { this.panel.show = false setTimeout(() => { this.panel.node = null }, 0) }, layoutPort(nodeId: string, portId: string) { const dom = document.getElementById(portId) const node = this.graph.getCellById(nodeId) if (dom && node) { node.portProp(portId, ['args', 'dy'], dom.offsetTop) } }, getInVars(node) { const pathNodes = this.graph.getPredecessors(node).reverse() const options: any = [ { label: '环境变量', key: VarsSource.Env, options: this.envVars.vars.map((v: any) => { const result = JSON.parse(JSON.stringify(v)) delete result.value return result }), }, ] pathNodes.forEach((n) => { const wd = n.data.workflowData const group = { label: wd.desc || wd.title, key: wd.id, options: [ ...(wd.__outVars?.map((v) => { v.nodeId = wd.id return v }) || []), ...(wd.__sysVars?.map((v) => { v.nodeId = wd.id return v }) || []), ], } if (group.options.length > 0) { options.push(group) } }) return options }, autoSave() { console.log('自动保存触发') this.autoSaveFlag = !this.autoSaveFlag }, }, })