index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <script setup lang="ts">
  2. import "@vue-flow/core/dist/style.css";
  3. import "@vue-flow/core/dist/theme-default.css";
  4. import Icon from "./icon.vue";
  5. import { nextTick, ref } from "vue";
  6. import { useLayout } from "./useLayout";
  7. import { useShuffle } from "./useShuffle";
  8. import ProcessNode from "./processNode.vue";
  9. import { useRunProcess } from "./useRunProcess";
  10. import AnimationEdge from "./animationEdge.vue";
  11. import { Background } from "@vue-flow/background";
  12. import { Panel, VueFlow, useVueFlow } from "@vue-flow/core";
  13. import { initialEdges, initialNodes } from "./initialElements";
  14. const nodes = ref(initialNodes);
  15. const edges = ref(initialEdges);
  16. const cancelOnError = ref(true);
  17. const shuffle = useShuffle();
  18. const { graph, layout, previousDirection } = useLayout();
  19. // @ts-expect-error
  20. const { run, stop, reset, isRunning } = useRunProcess({ graph, cancelOnError });
  21. const { fitView } = useVueFlow();
  22. async function shuffleGraph() {
  23. await stop();
  24. reset(nodes.value);
  25. edges.value = shuffle(nodes.value);
  26. nextTick(() => {
  27. layoutGraph(previousDirection.value);
  28. });
  29. }
  30. async function layoutGraph(direction) {
  31. await stop();
  32. reset(nodes.value);
  33. nodes.value = layout(nodes.value, edges.value, direction);
  34. nextTick(() => {
  35. fitView();
  36. run(nodes.value);
  37. });
  38. }
  39. </script>
  40. <template>
  41. <div class="layout-flow">
  42. <VueFlow
  43. :nodes="nodes"
  44. :edges="edges"
  45. @nodes-initialized="layoutGraph('LR')"
  46. >
  47. <template #node-process="props">
  48. <ProcessNode
  49. :data="props.data"
  50. :source-position="props.sourcePosition"
  51. :target-position="props.targetPosition"
  52. />
  53. </template>
  54. <template #edge-animation="edgeProps">
  55. <AnimationEdge
  56. :id="edgeProps.id"
  57. :source="edgeProps.source"
  58. :target="edgeProps.target"
  59. :source-x="edgeProps.sourceX"
  60. :source-y="edgeProps.sourceY"
  61. :targetX="edgeProps.targetX"
  62. :targetY="edgeProps.targetY"
  63. :source-position="edgeProps.sourcePosition"
  64. :target-position="edgeProps.targetPosition"
  65. />
  66. </template>
  67. <Background />
  68. <Panel class="process-panel" position="top-left">
  69. <div class="layout-panel">
  70. <button v-if="isRunning" class="stop-btn" title="stop" @click="stop">
  71. <Icon name="stop" />
  72. <span class="spinner" />
  73. </button>
  74. <button v-else title="start" @click="run(nodes)">
  75. <Icon name="play" />
  76. </button>
  77. <button title="set horizontal layout" @click="layoutGraph('LR')">
  78. <Icon name="horizontal" />
  79. </button>
  80. <button title="set vertical layout" @click="layoutGraph('TB')">
  81. <Icon name="vertical" />
  82. </button>
  83. <button title="shuffle graph" @click="shuffleGraph">
  84. <Icon name="shuffle" />
  85. </button>
  86. </div>
  87. <!-- <div class="checkbox-panel">
  88. <label>Cancel on error</label>
  89. <input v-model="cancelOnError" type="checkbox" />
  90. </div> -->
  91. </Panel>
  92. </VueFlow>
  93. </div>
  94. </template>
  95. <style scoped>
  96. @keyframes spin {
  97. 0% {
  98. transform: rotate(0deg);
  99. }
  100. 100% {
  101. transform: rotate(360deg);
  102. }
  103. }
  104. *,
  105. ::before,
  106. ::after {
  107. box-sizing: content-box;
  108. }
  109. .main-content {
  110. margin: 0 !important;
  111. }
  112. .layout-flow {
  113. width: 100%;
  114. height: 100%;
  115. }
  116. .process-panel,
  117. .layout-panel {
  118. display: flex;
  119. gap: 10px;
  120. }
  121. .process-panel {
  122. display: flex;
  123. flex-direction: column;
  124. padding: 10px;
  125. background-color: #2d3748;
  126. border-radius: 8px;
  127. box-shadow: 0 0 10px rgb(0 0 0 / 50%);
  128. }
  129. .process-panel button {
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. width: 40px;
  134. height: 40px;
  135. font-size: 16px;
  136. color: white;
  137. cursor: pointer;
  138. background-color: #4a5568;
  139. border: none;
  140. border-radius: 8px;
  141. box-shadow: 0 0 10px rgb(0 0 0 / 50%);
  142. }
  143. /* .checkbox-panel {
  144. display: flex;
  145. align-items: center;
  146. gap: 10px;
  147. } */
  148. .process-panel button:hover,
  149. .layout-panel button:hover {
  150. background-color: #2563eb;
  151. transition: background-color 0.2s;
  152. }
  153. .process-panel label {
  154. font-size: 12px;
  155. color: white;
  156. }
  157. .stop-btn svg {
  158. display: none;
  159. }
  160. .stop-btn:hover svg {
  161. display: block;
  162. }
  163. .stop-btn:hover .spinner {
  164. display: none;
  165. }
  166. .spinner {
  167. width: 20px;
  168. height: 20px;
  169. border: 3px solid #f3f3f3;
  170. border-top: 3px solid #2563eb;
  171. border-radius: 50%;
  172. animation: spin 1s linear infinite;
  173. }
  174. </style>