AntvX6.vue 4.37 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
<script setup lang="ts">
import { onMounted, reactive } from 'vue';

// type
import { X6NodesType, X6EdgesType } from '../../type';
// antv/x6
import { Graph } from '@antv/x6';

const data = reactive({
  value: {
    // 节点
    nodes: [
      {
        id: 'node1', // String,可选,节点的唯一标识
        shape: 'rect', // 默认值 rect 矩形
        x: 0, // Number,必选,节点位置的 x 值
        y: 0, // Number,必选,节点位置的 y 值
        width: 80, // Number,可选,节点大小的 width 值
        height: 40, // Number,可选,节点大小的 height 值
        label: '节点1', // String,节点标签
      },
      {
        id: 'node2', // String,节点的唯一标识
        x: 160, // Number,必选,节点位置的 x 值
        y: 180, // Number,必选,节点位置的 y 值
        width: 80, // Number,可选,节点大小的 width 值
        height: 40, // Number,可选,节点大小的 height 值
        label: '节点2', // String,节点标签
      },
      {
        id: 'node3',
        x: 200,
        y: 200,
        width: 80,
        height: 40,
        label: '节点3',
      },
    ] as X6NodesType[],
    // 边
    edges: [
      {
        source: 'node1', // String,必须,起始节点 id
        target: 'node2', // String,必须,目标节点 id
      },
      {
        source: 'node2',
        target: 'node3',
      },
    ] as X6EdgesType[],
  },
});

const actDom = reactive({
  show: false,
  x: 0,
  y: 0,
});

const currentNode = reactive({
  value: {} as any,
});

const state = reactive({
  graph: null as any,
});

onMounted(() => {
  initGraph();
});

function initGraph() {
  let dom = document.getElementById('x6-container') as HTMLElement;
  const graph = new Graph({
    container: dom,
    background: {
      color: '#fffbe6', // 设置画布背景颜色
    },
    grid: {
      size: 10, // 网格大小 10px
      visible: true, // 渲染网格背景
    },
  });
  graph.fromJSON(data.value);
  state.graph = graph;

  cellClick(graph);
  nodeContextmenu(graph);
  blankClick(graph);
}

function cellClick(graph: any) {
  graph.on('cell:click', () => {
    resetActDom();
  });
}

// 右键点击节点
function nodeContextmenu(graph: any) {
  graph.on('node:contextmenu', ({ e, x, y, cell, view }: any) => {
    console.log('e >>>>', e);
    console.log('x >>>>', x);
    console.log('y >>>>', y);
    console.log('cell >>>>', cell);
    console.log('view >>>>', view);
    currentNode.value.e = e;
    currentNode.value.x = x;
    currentNode.value.y = y;
    currentNode.value.cell = cell;
    currentNode.value.view = view;

    actDom.x = x;
    actDom.y = y;
    actDom.show = true;
  });
}

// 点击画布空白区域
function blankClick(graph: any) {
  graph.on('blank:click', () => {
    resetActDom();
  });
}

// 添加子节点
function addChildNode() {
  console.log('添加子节点 >>>>', currentNode.value);
  console.log('data >>>>', data);
  resetActDom();

  // state.graph.fromJSON(state.graph.toJSON());
  // data.value.nodes.push({
  //   id: 'node4',
  //   x: 400,
  //   y: 400,
  //   width: 80,
  //   height: 40,
  //   label: '节点4',
  // });

  // data.value.edges.push({
  //   source: 'node3',
  //   target: 'node4',
  // });
  console.log('序列化 >>>>', state.graph.toJSON());
  // state.graph.fromJSON(data.value);
}

// 删除节点
function delNode() {
  console.log('删除节点 >>>>', currentNode.value);
  resetActDom();
}

function resetActDom() {
  actDom.show = false;
  actDom.x = 0;
  actDom.y = 0;
  currentNode.value = {};
}
</script>
<template>
  <div class="x6-box">
    <div id="x6-container"></div>
    <div
      v-if="actDom.show"
      class="action-box shadow-13"
      :style="{ transform: `translate(${actDom.x}px, ${actDom.y}px)` }"
    >
      <q-list dense separator>
        <q-item clickable @click="addChildNode">
          <q-item-section> 添加子节点 </q-item-section>
        </q-item>

        <q-item clickable>
          <q-item-section @click="delNode"> 删除节点 </q-item-section>
        </q-item>
      </q-list>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.x6-box {
  position: relative;
}
#x6-container {
  width: 800px;
  height: 400px;
}
.action-box {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100px;
  min-height: 64px;
  border: 1px solid rgba(8, 8, 8, 0.13);
  background-color: #fff;
  border-radius: 5px;
}
</style>