logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • XFlow
  • 所有产品antv logo arrow
  • 2.x
  • 简介
  • 快速上手
  • 基础
    • 画布
    • 节点
    • 边
    • 连接桩
    • 交互
    • 事件
    • 数据
  • 进阶
    • 连接点
    • 工具
    • 群组
    • React 节点
    • Vue 节点
    • Angular 节点
    • HTML 节点
  • 插件
    • 图形变换
    • 对齐线
    • 复制粘贴
    • 快捷键
    • 撤销重做
    • 框选
    • 滚动画布
    • 小地图
    • Dnd
    • Stencil
    • 导出
  • 升级到 2.x 版本
  • 开发者工具

撤销重做

上一篇
快捷键
下一篇
框选

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

在本章节中主要介绍撤销重做相关的知识,通过阅读你可以了解到

  • 如何实现元素操作的撤销与重做

使用

我们提供了一个独立的插件包 @antv/x6-plugin-history 来使用撤销重做功能。

# npm
$ npm install @antv/x6-plugin-history --save
# yarn
$ yarn add @antv/x6-plugin-history

然后我们在代码中这样使用:

import { History } from '@antv/x6-plugin-history'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
graph.use(
new History({
enabled: true,
}),
)

演示

  • 随意移动节点后,Undo 按钮变得可用。
  • 点击 Undo 按钮,节点位置被还原,然后 Redo 按钮变得可用。
  • 点击 Redo 按钮,节点位置被更新。

配置

属性名类型默认值必选描述
stackSizenumber0stackSize 为 0 表示不限制历史记录栈的长度,如果设置为其他数字表示最多只会记录该数字长度的历史记录
ignoreAddbooleanfalseignoreAdd 如果为 true,添加元素不会被记录到历史记录
ignoreRemovebooleanfalseignoreRemove 如果为 true,删除元素不会被记录到历史记录
ignoreChangebooleanfalseignoreChange 如果为 true,元素属性变化是否被记录到历史记录
beforeAddCommand(event, args) => any-当一个命令被添加到 Undo 队列前被调用,如果该方法返回 false,那么这个命令将不会被添加到 Undo 队列中
afterAddCommand(event, args, cmd) => any-当一个命令被添加到 Undo 队列后被调用
executeCommand(cmd, revert, options) => any-当命令被撤销或重做时被调用,revert 为 true 表示命令被撤销,否则表示命令被重做

提示

在实际项目中,我们经常会需要将多个改变一次性撤销或者重做,X6 中提供 batch 的概念,可以将多个改变合并成一个历史记录。使用方式如下:

// 方式一
graph.startBatch('custom-batch-name')
// 节点改变边框颜色以及修改位置会合并成一条记录,可以一次性撤销
node.attr('body/stroke', 'red')
node.position(30, 30)
graph.stopBatch('custom-batch-name')
// 方式二
graph.batchUpdate(() => {
node.prop('zIndex', 10)
node.attr('label/text', 'hello')
node.attr('label/fill', '#ff0000')
})

API

graph.undo(...)

undo(options?: KeyValue): this

撤销。options 将被传递到事件回调中。

graph.undoAndCancel(...)

undoAndCancel(options?: KeyValue): this

撤销,并且不添加到重做队列中,所以这个被撤销的命令不能被重做。options 将被传递到事件回调中。

graph.redo(...)

redo(options?: KeyValue): this

重做。options 将被传递到事件回调中。

graph.canUndo()

canUndo(): boolean

是否可以撤销。

graph.canRedo()

canRedo(): boolean

是否可以重做。

graph.cleanHistory(...)

cleanHistory(options?: KeyValue): this

清空历史队列。options 将被传递到事件回调中。

graph.getHistoryStackSize(...)

getHistoryStackSize(): number

获取 history 栈的尺寸。

graph.getUndoRemainSize(...)

getUndoRemainSize(): number

获取 history undo 栈的剩余尺寸。

graph.getUndoStackSize(...)

getUndoStackSize(): number

获取 history undo 栈的尺寸。

graph.getRedoStackSize(...)

getRedoStackSize(): number

获取 history redo 栈的尺寸。

graph.isHistoryEnabled()

isHistoryEnabled(): boolean

是否启用了历史状态。

graph.enableHistory()

enableHistory(): this

启用历史状态。

graph.disableHistory()

disableHistory(): this

禁用历史状态。

graph.toggleHistory(...)

toggleHistory(enabled?: boolean): this

切换历史的启用状态。参数如下:

名称类型必选默认值描述
enabledboolean-是否启用历史状态,缺省时切换历史的启用状态。

事件

事件名称参数类型描述
history:undo{ cmds: Command[], options: KeyValue }当命令被撤销时触发
history:redo{ cmds: Command[], options: KeyValue }当命令被重做时触发
history:cancel{ cmds: Command[], options: KeyValue }当命令被取消时触发
history:add{ cmds: Command[], options: KeyValue }当命令被添加到队列时触发
history:clean{ cmds: Command[] | null, options: KeyValue }当历史队列被清空时触发
history:change{ cmds: Command[] | null, options: KeyValue }当历史队列改变时触发
history:batch{ cmds: Command, options: KeyValue }当接收到 batch 命令时触发
graph.on('history:undo', ({ cmds }) => {
console.log(cmds)
})
// 我们也可以在插件实例上监听事件
history.on('undo', ({ cmds }) => {
console.log(cmds)
})