useDnD.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // import { useVueFlow,Position,MarkerType } from '@vue-flow/core';
  2. import { useVueFlow,Position,MarkerType} from '@vue-flow/core';
  3. import { ref, watch } from 'vue'
  4. import { ElMessage, ElButton, ElDialog, ElSelect } from "element-plus"
  5. import { request, uploadFile } from "@/utils/request"
  6. import gc1 from '@/assets/flowimg/gc1.png'
  7. import gc2 from '@/assets/flowimg/gc2.png'
  8. import gc3 from '@/assets/flowimg/gc3.png'
  9. import wen from '@/assets/flowimg/wen.png'
  10. import xuek1 from '@/assets/flowimg/xuek1.png'
  11. import xuek0 from '@/assets/flowimg/xuek0.png'
  12. import xuek2 from '@/assets/flowimg/xuek2.png'
  13. import xuek3 from '@/assets/flowimg/xuek3.png'
  14. import xuek4 from '@/assets/flowimg/xuek4.png'
  15. import xuek5 from '@/assets/flowimg/xuek5.png'
  16. import xuek6 from '@/assets/flowimg/xuek6.png'
  17. import xuek7 from '@/assets/flowimg/xuek7.png'
  18. import xuek8 from '@/assets/flowimg/xuek8.png'
  19. import xuek9 from '@/assets/flowimg/xuek9.png'
  20. import xuek10 from '@/assets/flowimg/xuek10.png'
  21. import xuek11 from '@/assets/flowimg/xuek11.png'
  22. import xuek12 from '@/assets/flowimg/xuek12.png'
  23. import r2 from '@/assets/flowimg/mob.png'
  24. import aero from '@/assets/flowimg/aero.png'
  25. import csh from '@/assets/flowimg/csh.png'
  26. import ffd from '@/assets/flowimg/ffd.png'
  27. import ADflow from '@/assets/flowimg/ADflow.png'
  28. import tacs from '@/assets/flowimg/TACS.png'
  29. import fsi from '@/assets/flowimg/fsi.png'
  30. import mathfunc from '@/assets/flowimg/MathFunc.png'
  31. import flight from '@/assets/flowimg/flight.png'
  32. let nid = 0;
  33. let id=0
  34. let treeobj=ref([]);
  35. let datas={}
  36. /**
  37. * @returns {string} - A unique id.
  38. */
  39. function getId() {
  40. //return `node_${id++}`
  41. return `${nid}${id++}`;
  42. }
  43. function imagefun(){
  44. console.log(nid);
  45. // if(nid=='1-1'){
  46. // return datas = {label: '优化问题', image:gc1}
  47. // }else if(nid=='1-2'){
  48. // return datas = {label:'分析流程', image:gc2}
  49. // }else if(nid=='1-3'){
  50. // return datas = {label:'优化器', image:gc3}
  51. // }else
  52. if(nid=='2-1'){
  53. return datas = {label:'优化问题', image:wen,name:'Project'}
  54. }else if(nid=='3-1'){
  55. return datas = {label:'CATIA', image:xuek1,name:'CATIA'}
  56. }else if(nid=='3-0'){
  57. return datas = {label:'CST', image:xuek0,name:'CST'}
  58. }else if(nid=='3-10'){
  59. return datas = {label:'FFD', image:ffd,name:'FFD'}
  60. }else if(nid=='3-11'){
  61. return datas = {label:'ADflow', image:ADflow,name:'ADflow'}
  62. }else if(nid=='3-12'){
  63. return datas = {label:'TACS', image:tacs,name:'TACS'}
  64. }else if(nid=='3-13'){
  65. return datas = {label:'FUM to FEM', image:fsi,name:'FSI'}
  66. }else if(nid=='3-14'){
  67. return datas = {label:'MathFunc', image:mathfunc,name:'MathFunc'}
  68. }else if(nid=='3-15'){
  69. return datas = {label:'Flight', image:flight,name:'Flight'}
  70. }
  71. else if(nid=='3-2'){
  72. return datas = {label:'Excel', image:xuek2,name:'Excel'}
  73. }else if(nid=='3-3'){
  74. return datas = {label:'Feko', image:xuek3,name:'Feko'}
  75. }else if(nid=='3-4'){
  76. return datas = {label:'Fluent', image:xuek4,name:'Fluent'}
  77. }else if(nid=='3-5'){
  78. return datas = {label:'HCFD', image:xuek5,name:'HCFD'}
  79. }else if(nid=='3-6'){
  80. return datas = {label:'Matlab', image:xuek6,name:'Matlab'}
  81. }else if(nid=='3-7'){
  82. return datas = {label:'Nastran', image:xuek7,name:'Nastran'}
  83. }else if(nid=='3-8'){
  84. return datas = {label:'Python', image:xuek8,name:'Python'}
  85. }else if(nid=='3-9'){
  86. return datas = {label:'Xfoil', image:xuek9,name:'Xfoil'}
  87. }else if(nid=='4-1'){
  88. return datas = {label:'进化优化器', image:xuek10,name:'optimizer1'}
  89. }else if(nid=='4-2'){
  90. return datas = {label:'代理优化器', image:xuek11,name:'optimizer3'}
  91. }else if(nid=='4-3'){
  92. return datas = {label:'梯度优化器', image:xuek12,name:'optimizer2'}
  93. }else if(nid=='3-01'){
  94. return datas = {label:'优化器', image:r2,name:'optimizer'}
  95. }else if(nid=='3-02'){
  96. return datas = {label:'参数化', image:csh,name:'参数化'}
  97. }else if(nid=='3-03'){
  98. return datas = {label:'气动分析', image:aero,name:'气动分析'}
  99. }
  100. else{
  101. return null;
  102. }
  103. }
  104. /**
  105. * In a real world scenario you'd want to avoid creating refs in a global scope like this as they might not be cleaned up properly.
  106. * @type {{draggedType: Ref<string|null>, isDragOver: Ref<boolean>, isDragging: Ref<boolean>}}
  107. */
  108. const state = {
  109. /**
  110. * The type of the node being dragged.
  111. */
  112. draggedType: ref(null),
  113. isDragOver: ref(false),
  114. isDragging: ref(false),
  115. }
  116. // 保存流
  117. const saveflow = async (pid, wid, uid, type, fromuid, touid) => {
  118. const params = {
  119. transCode: 'MDO0058',
  120. pid: pid || '',
  121. wid: wid || '', // 流ID
  122. uid: uid || '',
  123. type: type || '',
  124. fromuid: fromuid || '',
  125. touid: touid || '',
  126. };
  127. try {
  128. const res = await request(params);
  129. return res.wid;
  130. } catch (err) {
  131. ElMessage.error(err.returnMsg);
  132. }
  133. };
  134. // 用来存储每种组件名称出现次数的对象
  135. const nameCount = {}
  136. export default function useDragAndDrop() {
  137. const { draggedType, isDragOver, isDragging } = state
  138. const { addNodes, addEdges,screenToFlowCoordinate, onNodesInitialized, updateNode } = useVueFlow()
  139. watch(isDragging, (dragging) => {
  140. document.body.style.userSelect = dragging ? 'none' : ''
  141. })
  142. function onDragStart(event, type,id) {
  143. if (event.dataTransfer) {
  144. nid=id;
  145. event.dataTransfer.setData('application/vueflow', type)
  146. event.dataTransfer.effectAllowed = 'move'
  147. }
  148. draggedType.value =type;
  149. //draggedType.value = 'smoothstep'
  150. isDragging.value = true
  151. document.addEventListener('drop', onDragEnd)
  152. }
  153. /**
  154. * Handles the drag over event.
  155. *
  156. * @param {DragEvent} event
  157. */
  158. function onDragOver(event) {
  159. event.preventDefault()
  160. if (draggedType.value) {
  161. isDragOver.value = true
  162. if (event.dataTransfer) {
  163. event.dataTransfer.dropEffect = 'move'
  164. }
  165. }
  166. }
  167. function handleNodeDrop(e){
  168. }
  169. function onDragLeave(e) {
  170. isDragOver.value = false
  171. }
  172. function onDragEnd() {
  173. isDragging.value = false
  174. isDragOver.value = false
  175. draggedType.value = null
  176. nid=''
  177. document.removeEventListener('drop', onDragEnd)
  178. }
  179. /**
  180. * Handles the drop event.
  181. *
  182. * @param {DragEvent} event
  183. */
  184. async function onDrop(event) {
  185. const position = screenToFlowCoordinate({
  186. x: event.clientX,
  187. y: event.clientY,
  188. })
  189. const savedObj = JSON.parse(sessionStorage.getItem("objlist"));
  190. // console.log('objlist',savedObj);
  191. const pid = savedObj.pid;
  192. const newwid = '';
  193. const nodeId = getId()
  194. const image1=imagefun();
  195. // 获取当前组件的名称
  196. const componentName = image1.name;
  197. // 如果该组件的名称已存在,给名称添加序号
  198. if (!nameCount[componentName]) {
  199. nameCount[componentName] = 0;
  200. }
  201. nameCount[componentName]++; // 增加计数
  202. const newName = `${componentName}${nameCount[componentName]}`; // 新的名称加上序号
  203. let snodes=ref([]);
  204. let sedges=ref([]);
  205. if(nid=='4'){
  206. // const nodes = ref([])
  207. // const edges = ref([])
  208. }else{
  209. // 创建初始的节点数据,不包含 wid
  210. const newNode = {
  211. id: nodeId,
  212. type: draggedType.value,
  213. position,
  214. data: { ...image1, uid: newName },
  215. };
  216. snodes.value.push(newNode)
  217. // 异步保存数据并更新 wid
  218. saveflow(pid, newwid, newName, 'com', '', '').then((wid) => {
  219. // 更新节点数据中的 wid
  220. newNode.data.wid = wid;
  221. // 使用 onNodesInitialized 来更新节点数据
  222. onNodesInitialized(() => {
  223. // 更新节点的其他数据
  224. updateNode(nodeId, (node) => ({
  225. ...node,
  226. data: {
  227. ...node.data,
  228. wid: newNode.data.wid, // 更新节点中的 wid
  229. },
  230. }));
  231. });
  232. }).catch((err) => {
  233. console.error('保存流程失败:', err.message);
  234. ElMessage.error('保存流程失败');
  235. });
  236. // saveflow(pid , savewid, newName, 'com', '', '')
  237. // console.log('返回的 wid:', widtemp.value)
  238. // const wid = widtemp.value
  239. // const uid = newName
  240. // // 创建新节点,包含 wid 数据
  241. // const newNode = {
  242. // id: nodeId,
  243. // type: draggedType.value,
  244. // position,
  245. // data: { ...image1, wid , uid}, // 将 wid 存入节点数据中
  246. // }
  247. // snodes.value.push(newNode)
  248. }
  249. const { off } = onNodesInitialized(() => {
  250. updateNode(nodeId, (node) => ({
  251. position: { x: node.position.x - node.dimensions.width / 2, y: node.position.y - node.dimensions.height / 2 },
  252. dimensions:{ height: 58, width: 60}
  253. }))
  254. off()
  255. })
  256. addNodes(snodes.value)
  257. treeobj.value=snodes.value
  258. // addEdges(sedges.value)
  259. }
  260. return {
  261. treeobj,
  262. draggedType,
  263. isDragOver,
  264. isDragging,
  265. onDragStart,
  266. onDragLeave,
  267. onDragOver,
  268. onDrop,
  269. handleNodeDrop,
  270. }
  271. }