antv-x6在vue3中使用:Undo撤消和Redo还原

本文介绍了一个基于Vue3和AntV X6库的图形编辑器实现,重点展示了如何利用X6的撤销与重做历史记录功能。通过具体代码示例,展示了如何设置和监听这些功能的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、效果

2、vue3 demo

<template>
  <div class="x6-graph-wrap">
    <h1>Default Settings</h1>
    <div class="x6-graph-tools">
      <a-button-group>
        <a-button @click="onUndo" :disabled="!canUndo" >
          Undo 撤消
        </a-button>
        <a-button @click="onRedo" :disabled="!canRedo">
          Redo 还原
        </a-button>
      </a-button-group>
    </div>
    <div ref="container" class="x6-graph" />
  </div>
</template>

<script lang="ts">
import { Graph } from '@antv/x6'
import { defineComponent, ref} from 'vue' // reactive

export default defineComponent({
  name: "index",
  setup() {
    const canUndo = ref(true)
    const canRedo = ref(false)
    // eslint-disable-next-line
    const history = ref(undefined as any)

    const init: (any) => void =(that)=>{
      const graph = new Graph({
        container: (that.$refs.container) as HTMLDivElement,
        width: 800,
        height: 600,
        grid: true,
        history: true,
      })
      history.value = graph.history

      graph.history.on('change', () => {
        canUndo.value = graph.history.canUndo()
        canRedo.value = graph.history.canRedo()
      })

      const source = graph.addNode({
        x: 120,
        y: 120,
        width: 100,
        height: 40,
        attrs: {
          label: {
            text: 'Hello',
          },
          body: {
            strokeWidth: 1,
          },
        },
      })

      const target = graph.addNode({
        x: 300,
        y: 320,
        width: 100,
        height: 40,
        attrs: {
          label: {
            text: 'World',
          },
          body: {
            strokeWidth: 1,
          },
        },
      })

      graph.addEdge({ source, target, arrts: { line: { strokeWidth: 1 } } })
    }

    const onUndo: () => void = ()=> {
      history.value.undo()
    }
    const onRedo: () => void =()=> {
      history.value.redo()
    }

    return {
      canUndo,
      canRedo,
      history,
      init,
      onUndo,
      onRedo
    }
  },
  mounted()  {
    /* eslint-disable */
    const that = this as any
    this.$nextTick(function () {
      that.init(that)
    })
  },
})
</script>

<style scoped>

</style>

3、码云地址

地址:https://gitee.com/icefox1/x6-vue-demo

位置:x6-features/src/views/undo

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

<每天一点>

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值