类名 common/sketchEditor/base/UndoRedoManager.js
import { getGUID } from '../../util'

class UndoRedoManager {
  constructor() {
    this._id = getGUID()
    this._init()
  }

  /**
   * 初始化管理工具
   * @private
   */
  _init() {
    this.processCollection = []
    this.currentStateIndex = -1
  }

  /**
   * 撤销当前编辑操作<a id='undo'></a>
   * @returns {SketchStage}
   */
  undo() {
    if (this.canUndo()) {
      this.currentStateIndex--
      const preProcess = this.processCollection[this.currentStateIndex]
      return preProcess.sketchStage.clone()
    } else {
      return null
    }
  }

  /**
   * 恢复被撤销的草图<a id='redo'></a>
   * @returns {SketchStage}
   */
  redo() {
    if (this.canRedo()) {
      this.currentStateIndex++
      const nextProcess = this.processCollection[this.currentStateIndex]
      return nextProcess.sketchStage.clone()
    } else {
      return null
    }
  }

  /**
   * 草图是否可执行撤销操作<a id='canUndo'></a>
   * @returns {Boolean}
   */
  canUndo() {
    let canDo = false
    if (this.processCollection.length > 1 && this.currentStateIndex > 0) {
      canDo = true
    }
    return canDo
  }

  /**
   * 草图是否可执行恢复操作<a id='canRedo'></a>
   * @returns {Boolean}
   */
  canRedo() {
    let canDo = false
    if (
      this.processCollection.length > 1 &&
      this.currentStateIndex < this.processCollection.length - 1
    ) {
      canDo = true
    }
    return canDo
  }

  /**
   * 添加草图状态<a id='addProcess'></a>
   * @param {String} type  操作类型 type值为 "add"或"update"
   * @param {SketchStage} sketchStage 草图图形集合
   */
  addProcess(type, sketchStage) {
    sketchStage = sketchStage.clone()
    const id = getGUID()
    const process = {
      id,
      type,
      sketchStage,
      index: this.processCollection.length
    }
    if (this.currentStateIndex === this.processCollection.length - 1) {
      this.processCollection.push(process)
    } else {
      this.processCollection.splice(
        this.currentStateIndex + 1,
        this.processCollection.length - (this.currentStateIndex + 1),
        process
      )
    }
    this.currentStateIndex = this.processCollection.length - 1
  }

  /** 重置初始化管理工具
   */
  reset() {
    this._init()
  }

  removeProcessById(id) {
    const process = this.getProcessById(id)
    if (process) {
      this.processCollection.splice(process.index, 1)
    }
  }

  getProcessById(id) {
    const process = this.processCollection.filter((item) => item.id === id)
    if (process.length > 0) {
      return process[0]
    } else {
      return null
    }
  }

  _getNextProcess() {}
}
export default UndoRedoManager
构造函数
成员变量
方法
事件