Forráskód Böngészése

自动排序后期再议

CzRger 4 hónapja%!(EXTRA string=óta)
szülő
commit
f8736d7d5e
3 módosított fájl, 88 hozzáadás és 2 törlés
  1. 1 0
      package.json
  2. 71 1
      src/views/workflow/chart/index.vue
  3. 16 1
      yarn.lock

+ 1 - 0
package.json

@@ -20,6 +20,7 @@
     "@types/node": "^20.17.11",
     "ant-design-vue": "4.x",
     "axios": "^1.7.9",
+    "dagre": "^0.8.5",
     "default-passive-events": "^2.0.0",
     "echarts": "^5.6.0",
     "element-plus": "^2.9.7",

+ 71 - 1
src/views/workflow/chart/index.vue

@@ -123,6 +123,11 @@
               </div>
             </div>
           </el-popover>
+<!--          <el-tooltip content="自动布局" effect="light" placement="top" :show-arrow="false">-->
+<!--            <div class="__hover-bg" @click="onLayoutAuto">-->
+<!--              <SvgIcon name="zoom+"/>-->
+<!--            </div>-->
+<!--          </el-tooltip>-->
         </div>
       </div>
     </div>
@@ -146,7 +151,7 @@ import { History } from '@antv/x6-plugin-history'
 import {v4} from "uuid";
 import { Dnd } from '@antv/x6-plugin-dnd'
 import nodeAdd from './node-add.vue'
-import {delay} from "@/utils/czr-util";
+import dagre from 'dagre';
 
 register({
   shape: 'workflow-node',
@@ -574,6 +579,71 @@ const onAddNode = ({type, e}) => {
   const node = state.graph.createNode(handleNode(getNodeDefault(type)))
   state.dnd.start(node, e)
 }
+const onLayoutAuto = () => {
+  // 布局方向
+  const dir = 'LR' // LR RL TB BT
+  const nodes = state.graph.getNodes()
+  const edges = state.graph.getEdges()
+  const g = new dagre.graphlib.Graph()
+  g.setGraph({ rankdir: dir, nodesep: 100, ranksep: 16 })
+  g.setDefaultEdgeLabel(() => ({}))
+
+  nodes.forEach((node) => {
+    g.setNode(node.id, node.size())
+  })
+
+  edges.forEach((edge) => {
+    const source = edge.getSource()
+    const target = edge.getTarget()
+    g.setEdge(source.cell, target.cell)
+  })
+
+  dagre.layout(g)
+
+  g.nodes().forEach((id) => {
+    const node = state.graph.getCellById(id) as Node
+    if (node) {
+      const pos = g.node(id)
+      node.position(pos.x, pos.y)
+    }
+  })
+
+  // edges.forEach((edge) => {
+  //   const source = edge.getSourceNode()!
+  //   const target = edge.getTargetNode()!
+  //   const sourceBBox = source.getBBox()
+  //   const targetBBox = target.getBBox()
+  //
+  //   if ((dir === 'LR' || dir === 'RL') && sourceBBox.y !== targetBBox.y) {
+  //     const gap =
+  //       dir === 'LR'
+  //         ? targetBBox.x - sourceBBox.x - sourceBBox.width
+  //         : -sourceBBox.x + targetBBox.x + targetBBox.width
+  //     const fix = dir === 'LR' ? sourceBBox.width : 0
+  //     const x = sourceBBox.x + fix + gap / 2
+  //     edge.setVertices([
+  //       { x, y: sourceBBox.center.y },
+  //       { x, y: targetBBox.center.y },
+  //     ])
+  //   } else if (
+  //     (dir === 'TB' || dir === 'BT') &&
+  //     sourceBBox.x !== targetBBox.x
+  //   ) {
+  //     const gap =
+  //       dir === 'TB'
+  //         ? targetBBox.y - sourceBBox.y - sourceBBox.height
+  //         : -sourceBBox.y + targetBBox.y + targetBBox.height
+  //     const fix = dir === 'TB' ? sourceBBox.height : 0
+  //     const y = sourceBBox.y + fix + gap / 2
+  //     edge.setVertices([
+  //       { x: sourceBBox.center.x, y },
+  //       { x: targetBBox.center.x, y },
+  //     ])
+  //   } else {
+  //     edge.setVertices([])
+  //   }
+  // })
+}
 watch(() => props.data, (n) => {
   if (n) {
     initChart()

+ 16 - 1
yarn.lock

@@ -1782,6 +1782,14 @@ csstype@^3.1.1, csstype@^3.1.3:
   resolved "https://registry.npmmirror.com/csstype/-/csstype-3.1.3.tgz"
   integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==
 
+dagre@^0.8.5:
+  version "0.8.5"
+  resolved "https://registry.npmmirror.com/dagre/-/dagre-0.8.5.tgz#ba30b0055dac12b6c1fcc247817442777d06afee"
+  integrity sha512-/aTqmnRta7x7MCCpExk7HQL2O4owCT2h8NT//9I1OQ9vt29Pa0BzSAkR5lwFUcQ7491yVi/3CXU9jQ5o0Mn2Sw==
+  dependencies:
+    graphlib "^2.1.8"
+    lodash "^4.17.15"
+
 data-view-buffer@^1.0.2:
   version "1.0.2"
   resolved "https://registry.npmmirror.com/data-view-buffer/-/data-view-buffer-1.0.2.tgz"
@@ -2487,6 +2495,13 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.11, graceful-fs@^4.2.4,
   resolved "https://registry.npmmirror.com/graceful-fs/-/graceful-fs-4.2.11.tgz"
   integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==
 
+graphlib@^2.1.8:
+  version "2.1.8"
+  resolved "https://registry.npmmirror.com/graphlib/-/graphlib-2.1.8.tgz#5761d414737870084c92ec7b5dbcb0592c9d35da"
+  integrity sha512-jcLLfkpoVGmH7/InMC/1hIvOPSUh38oJtGhvrOFGzioE1DZ+0YW16RgmOJhHiuWTvGiJQ9Z1Ik43JvkRPRvE+A==
+  dependencies:
+    lodash "^4.17.15"
+
 has-ansi@^2.0.0:
   version "2.0.0"
   resolved "https://registry.npmmirror.com/has-ansi/-/has-ansi-2.0.0.tgz"
@@ -3301,7 +3316,7 @@ lodash-unified@^1.0.2:
   resolved "https://registry.npmmirror.com/lodash-unified/-/lodash-unified-1.0.3.tgz"
   integrity sha512-WK9qSozxXOD7ZJQlpSqOT+om2ZfcT4yO+03FuzAHD0wF6S0l0090LRPDx3vhTTLZ8cFKpBn+IOcVXK6qOcIlfQ==
 
-lodash@^4.17.21:
+lodash@^4.17.15, lodash@^4.17.21:
   version "4.17.21"
   resolved "https://registry.npmmirror.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
   integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==