|
| 1 | +import React from "react"; |
| 2 | +import {LogicFlow} from "@logicflow/core"; |
| 3 | +import {NodeProperties, NodeType} from "@/components/flow/types"; |
| 4 | +import {message} from "antd"; |
| 5 | +import {isEmpty} from "lodash-es"; |
| 6 | +import NodeData = LogicFlow.NodeData; |
| 7 | + |
| 8 | + |
| 9 | +const TRANSLATION_DISTANCE = 40 |
| 10 | + |
| 11 | +class FlowPanelContext { |
| 12 | + |
| 13 | + private readonly lfRef: React.RefObject<LogicFlow>; |
| 14 | + |
| 15 | + constructor(lfRef: React.RefObject<LogicFlow>) { |
| 16 | + this.lfRef = lfRef; |
| 17 | + } |
| 18 | + |
| 19 | + private generateUUID() { |
| 20 | + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
| 21 | + const r = (Math.random() * 16) | 0; |
| 22 | + const v = c === 'x' ? r : (r & 0x3) | 0x8; |
| 23 | + return v.toString(16); |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * 添加节点 |
| 29 | + * @param type 节点类型 |
| 30 | + * @param properties 节点属性 |
| 31 | + */ |
| 32 | + addNode(type: NodeType, properties: NodeProperties) { |
| 33 | + if (this.nodeVerify(type)) { |
| 34 | + const uid = this.generateUUID(); |
| 35 | + this.lfRef.current?.dnd.startDrag({ |
| 36 | + id: uid, |
| 37 | + type: type, |
| 38 | + properties: { |
| 39 | + ...properties, |
| 40 | + id: uid |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * 复制节点 控制位置的偏移 |
| 49 | + * @param nodeData |
| 50 | + * @param distance |
| 51 | + * @private |
| 52 | + */ |
| 53 | + private translateNodeData(nodeData: NodeData, distance: number) { |
| 54 | + nodeData.x += distance |
| 55 | + nodeData.y += distance |
| 56 | + |
| 57 | + if (!isEmpty(nodeData.text)) { |
| 58 | + nodeData.text.x += distance |
| 59 | + nodeData.text.y += distance |
| 60 | + } |
| 61 | + |
| 62 | + return nodeData |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * 从粘贴板中复制节点 |
| 68 | + */ |
| 69 | + copyNode = () => { |
| 70 | + const flow = this.lfRef.current; |
| 71 | + if (!flow) { |
| 72 | + return; |
| 73 | + } |
| 74 | + const selected = flow.getSelectElements(true); |
| 75 | + if (selected && (selected.nodes || selected.edges)) { |
| 76 | + flow.clearSelectElements(); |
| 77 | + if (selected.nodes) { |
| 78 | + const nodes = selected.nodes; |
| 79 | + for (const node of nodes) { |
| 80 | + if (node.type === 'start-node') { |
| 81 | + message.error('开始节点只能有一个').then(); |
| 82 | + return false; |
| 83 | + } |
| 84 | + if (node.type === 'over-node') { |
| 85 | + message.error('结束节点只能有一个').then(); |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + const addElements = flow.addElements( |
| 90 | + selected, |
| 91 | + TRANSLATION_DISTANCE |
| 92 | + ); |
| 93 | + if (!addElements) return true; |
| 94 | + addElements.nodes.forEach((node) => flow.selectElementById(node.id, true)); |
| 95 | + addElements.nodes.forEach((node) => { |
| 96 | + this.translateNodeData(node, TRANSLATION_DISTANCE); |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * 节点校验 |
| 106 | + * @param type |
| 107 | + */ |
| 108 | + private nodeVerify = (type: NodeType) => { |
| 109 | + // @ts-ignore |
| 110 | + const nodes = this.lfRef.current?.getGraphData().nodes; |
| 111 | + if (type === 'start-node') { |
| 112 | + for (let i = 0; i < nodes.length; i++) { |
| 113 | + if (nodes[i].type === type) { |
| 114 | + message.error('开始节点只能有一个').then(); |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + if (type === 'over-node') { |
| 120 | + for (let i = 0; i < nodes.length; i++) { |
| 121 | + if (nodes[i].type === type) { |
| 122 | + message.error('结束节点只能有一个').then(); |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + /** |
| 132 | + * 缩放 |
| 133 | + * @param flag true为放大 false为缩小 |
| 134 | + */ |
| 135 | + zoom = (flag:boolean) => { |
| 136 | + this.lfRef.current?.zoom(flag); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * 重置缩放 |
| 141 | + */ |
| 142 | + resetZoom = ()=>{ |
| 143 | + this.lfRef.current?.resetZoom(); |
| 144 | + this.lfRef.current?.resetTranslate(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * 恢复 下一步 |
| 149 | + */ |
| 150 | + redo = ()=>{ |
| 151 | + this.lfRef.current?.redo(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * 撤销 上一步 |
| 156 | + */ |
| 157 | + undo = ()=> { |
| 158 | + this.lfRef.current?.undo(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * 隐藏地图 |
| 163 | + */ |
| 164 | + hiddenMap = ()=>{ |
| 165 | + // @ts-ignore |
| 166 | + this.lfRef.current?.extension.miniMap.hide(); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 显示地图 |
| 171 | + */ |
| 172 | + showMap = ()=>{ |
| 173 | + const modelWidth = this.lfRef.current?.graphModel.width; |
| 174 | + // @ts-ignore |
| 175 | + this.lfRef.current?.extension.miniMap.show(modelWidth - 300, 200); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * 下载图片 |
| 180 | + */ |
| 181 | + download = ()=>{ |
| 182 | + this.lfRef.current?.getSnapshot(); |
| 183 | + } |
| 184 | + |
| 185 | +} |
| 186 | + |
| 187 | +export default FlowPanelContext; |
0 commit comments