mirror of
https://github.com/wanglin2/mind-map.git
synced 2026-02-21 18:37:43 +08:00
71 lines
1.5 KiB
JavaScript
71 lines
1.5 KiB
JavaScript
import { copyRenderTree, simpleDeepClone } from './utils';
|
|
|
|
/**
|
|
* @Author: 王林
|
|
* @Date: 2021-05-04 13:10:06
|
|
* @Desc: 命令类
|
|
*/
|
|
class Command {
|
|
/**
|
|
* @Author: 王林
|
|
* @Date: 2021-05-04 13:10:24
|
|
* @Desc: 构造函数
|
|
*/
|
|
constructor(opt = {}) {
|
|
this.opt = opt
|
|
this.mindMap = opt.mindMap
|
|
this.commands = {}
|
|
this.history = []
|
|
this.activeHistoryIndex = 0
|
|
}
|
|
|
|
/**
|
|
* @Author: 王林
|
|
* @Date: 2021-05-04 13:12:30
|
|
* @Desc: 执行命令
|
|
*/
|
|
exec(name, ...args) {
|
|
if (this.commands[name]) {
|
|
this.commands[name].forEach((fn) => {
|
|
fn(...args)
|
|
})
|
|
this.addHistory()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Author: 王林
|
|
* @Date: 2021-05-04 13:13:01
|
|
* @Desc: 添加命令
|
|
*/
|
|
add(name, fn) {
|
|
if (this.commands[name]) {
|
|
this.commands[name].push(fn)
|
|
} else[
|
|
this.commands[name] = [fn]
|
|
]
|
|
}
|
|
|
|
/**
|
|
* @Author: 王林
|
|
* @Date: 2021-05-04 14:35:43
|
|
* @Desc: 添加回退数据
|
|
*/
|
|
addHistory() {
|
|
let data = this.getCopyData()
|
|
this.history.push(simpleDeepClone(data))
|
|
this.activeHistoryIndex++
|
|
this.mindMap.emit('data_change', data)
|
|
}
|
|
|
|
/**
|
|
* @Author: 王林
|
|
* @Date: 2021-05-04 15:02:58
|
|
* @Desc: 获取渲染树数据副本
|
|
*/
|
|
getCopyData() {
|
|
return copyRenderTree({}, this.mindMap.renderer.renderTree)
|
|
}
|
|
}
|
|
|
|
export default Command |