useDnD.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 f11 from '@/assets/img/f11.png'
  5. import f32 from '@/assets/img/f32.png'
  6. import f33 from '@/assets/img/f33.png'
  7. import f12 from '@/assets/img/f12.png'
  8. import f21 from '@/assets/img/f21.png'
  9. import f22 from '@/assets/img/f22.png'
  10. import f41 from '@/assets/img/f41.png'
  11. import { E } from '@kitware/vtk.js/macros2'
  12. import { Right } from '@element-plus/icons'
  13. import { LEFT_CHECK_CHANGE_EVENT } from 'element-plus'
  14. let nid = 0;
  15. let id=0
  16. let datas={}
  17. /**
  18. * @returns {string} - A unique id.
  19. */
  20. function getId() {
  21. return `node_${id++}`
  22. }
  23. function imagefun(){
  24. // console.log(id);
  25. if(nid==1){
  26. return datas = {label: '开始', image:f11}
  27. }else if(nid==2){
  28. return datas = {label:'结束', image:f12}
  29. }else if(nid==3){
  30. return datas = {label:'优化器', image:f32}
  31. }else{
  32. // return datas = {label:'模版', image:f41}
  33. }
  34. }
  35. /**
  36. * 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.
  37. * @type {{draggedType: Ref<string|null>, isDragOver: Ref<boolean>, isDragging: Ref<boolean>}}
  38. */
  39. const state = {
  40. /**
  41. * The type of the node being dragged.
  42. */
  43. draggedType: ref(null),
  44. isDragOver: ref(false),
  45. isDragging: ref(false),
  46. }
  47. export default function useDragAndDrop() {
  48. const { draggedType, isDragOver, isDragging } = state
  49. const { addNodes, addEdges,screenToFlowCoordinate, onNodesInitialized, updateNode } = useVueFlow()
  50. watch(isDragging, (dragging) => {
  51. document.body.style.userSelect = dragging ? 'none' : ''
  52. })
  53. function onDragStart(event, type,id) {
  54. nid=id;
  55. console.log(type);
  56. console.log(1234667)
  57. if (event.dataTransfer) {
  58. event.dataTransfer.setData('application/vueflow', type)
  59. event.dataTransfer.effectAllowed = 'move'
  60. }
  61. draggedType.value = type
  62. // draggedType.value = 'smoothstep'
  63. isDragging.value = true
  64. document.addEventListener('drop', onDragEnd)
  65. }
  66. /**
  67. * Handles the drag over event.
  68. *
  69. * @param {DragEvent} event
  70. */
  71. function onDragOver(event) {
  72. event.preventDefault()
  73. if (draggedType.value) {
  74. isDragOver.value = true
  75. if (event.dataTransfer) {
  76. event.dataTransfer.dropEffect = 'move'
  77. }
  78. }
  79. }
  80. function handleNodeDrop(e){
  81. }
  82. function onDragLeave(e) {
  83. isDragOver.value = false
  84. }
  85. function onDragEnd() {
  86. isDragging.value = false
  87. isDragOver.value = false
  88. draggedType.value = null
  89. document.removeEventListener('drop', onDragEnd)
  90. }
  91. /**
  92. * Handles the drop event.
  93. *
  94. * @param {DragEvent} event
  95. */
  96. function onDrop(event) {
  97. const position = screenToFlowCoordinate({
  98. x: event.clientX,
  99. y: event.clientY,
  100. })
  101. const nodeId = getId()
  102. const image1=imagefun();
  103. let snodes=ref([]);
  104. let sedges=[];
  105. if(nid=='4'){
  106. const nodes = ref([
  107. {
  108. data:{label: '开始', image: f11},
  109. id: "node_01",
  110. position: {x:245, y: 317},
  111. // type: "default",
  112. sourcePosition: Position.Right,
  113. targetPosition: Position.Left,
  114. },
  115. {
  116. data:{label: '优化器', image: f32},
  117. id: "node_02",
  118. position: {x: 435, y: 317},
  119. // type: "default",
  120. sourcePosition: Position.Right,
  121. targetPosition: Position.Left,
  122. class: 'light',
  123. },
  124. {
  125. data:{label: 'Rosenbank', image: f33},
  126. id: "node_03",
  127. position: {x: 630, y: 317},
  128. // type: "default",
  129. // type: "default",
  130. sourcePosition: Position.Right,
  131. targetPosition: Position.Left,
  132. // sourcePosition: Position.Top,
  133. // targetPosition: Position.Bottom,
  134. },
  135. {
  136. data:{label: '结束', image: f12},
  137. id: "node_04",
  138. position: {x: 804, y: 317},
  139. // type: "default",
  140. sourcePosition: Position.Right,
  141. targetPosition: Position.Left,
  142. },
  143. {
  144. data:{label: '输入1', image: f21},
  145. id: "node_05",
  146. position: {x: 435, y: 146},
  147. // type: "default",
  148. draggable: true,
  149. sourcePosition: Position.Top,
  150. targetPosition: Position.Bottom,
  151. },
  152. {
  153. data:{label: '输入2', image: f21},
  154. id: "node_06",
  155. position: {x:804, y: 146},
  156. //type: "default",//default
  157. sourcePosition: Position.Top,
  158. targetPosition: Position.Bottom,
  159. },
  160. {
  161. data:{label: '输出', image: f22},
  162. id: "node_07",
  163. position: {x: 630, y: 519},
  164. //type: "default",
  165. sourceHandle: 'Top',
  166. sourcePosition: Position.Bottom,
  167. targetPosition: Position.Top,
  168. },
  169. ]);
  170. const edges = ref([
  171. {
  172. id: 'e1->2',
  173. source: 'node_01',
  174. target: 'node_02',
  175. label: '按照我的来',
  176. type: 'straight',
  177. markerEnd: {
  178. type: MarkerType.ArrowClosed,
  179. },
  180. // id: 'e1->2',
  181. // source: 'node_01',
  182. // target: 'node_02',
  183. // type: 'straight',
  184. // markerEnd: MarkerType.ArrowClosed,
  185. },
  186. {
  187. id: 'e2->3',
  188. source: 'node_02',
  189. target: 'node_03',
  190. type: 'straight',
  191. // markerEnd: MarkerType.ArrowClosed,
  192. // markerEnd: {
  193. // type: MarkerType.ArrowClosed,
  194. // color: 'red',
  195. // },
  196. markerEnd: {
  197. tagName: MarkerType.ArrowClosed,
  198. width: 20,
  199. height: 20,
  200. },
  201. //type: 'output',
  202. // style: {
  203. // stroke: '#FF0000',
  204. // markerEnd: MarkerType.ArrowClosed,
  205. // },
  206. },
  207. {
  208. id: 'e3->4',
  209. source: 'node_03',
  210. target: 'node_04',
  211. type: 'straight' ,
  212. markerEnd: "arrow" // 或者使用 " arrowhead"
  213. },
  214. {
  215. id: 'e4->5',
  216. source: 'node_05',
  217. target: 'node_03',
  218. type: 'straight',
  219. animated: true ,
  220. sourceHandle: 'Top',
  221. targetHandle:'Bottom',
  222. markerEnd: MarkerType.ArrowClosed,
  223. },
  224. {
  225. id: 'e5->6',
  226. source: 'node_06',
  227. target: 'node_03',
  228. type: 'straight',
  229. animated: true,
  230. // source: 'handle-bottom',
  231. // target: 'handle-top',
  232. sourceHandle: 'Top',
  233. targetHandle:'Bottom',
  234. tagName: MarkerType.ArrowClosed,
  235. },
  236. {
  237. id: 'e6->7',
  238. source: 'node_03',
  239. target: 'node_07',
  240. // type: 'straight',
  241. type: 'straight',
  242. animated: true,
  243. sourceHandle: 'Top',
  244. targetHandle:'Bottom',
  245. markerEnd: MarkerType.ArrowClosed,
  246. },
  247. ]);
  248. snodes=nodes;
  249. sedges=edges;
  250. }else{
  251. snodes.value=[];
  252. sedges.value=[];
  253. const newNode = {
  254. id: nodeId,
  255. type: draggedType.value,
  256. position,
  257. data: image1,
  258. }
  259. console.log(newNode);
  260. snodes.value.push(newNode)
  261. const newedges = {
  262. type: 'smoothstep',
  263. markerEnd: MarkerType.ArrowClosed,
  264. }
  265. sedges.value.push(newedges)
  266. //sedges=edges
  267. }
  268. // console.log(newNode);
  269. /**
  270. * Align node position after drop, so it's centered to the mouse
  271. *
  272. * We can hook into events even in a callback, and we can remove the event listener after it's been called.
  273. */
  274. const { off } = onNodesInitialized(() => {
  275. updateNode(nodeId, (node) => ({
  276. position: { x: node.position.x - node.dimensions.width / 2, y: node.position.y - node.dimensions.height / 2 },
  277. }))
  278. off()
  279. })
  280. console.log(snodes.value);
  281. addNodes(snodes.value)
  282. addEdges(sedges.value)
  283. }
  284. return {
  285. draggedType,
  286. isDragOver,
  287. isDragging,
  288. onDragStart,
  289. onDragLeave,
  290. onDragOver,
  291. onDrop,
  292. handleNodeDrop,
  293. }
  294. }