2026-03-06 15:50:05 +08:00
|
|
|
import { ref, computed } from "vue";
|
|
|
|
|
import { ElMessageBox } from 'element-plus'
|
|
|
|
|
import i18n from '@/lang'
|
|
|
|
|
const t = i18n.global.t
|
|
|
|
|
|
|
|
|
|
|
2026-03-09 13:44:32 +08:00
|
|
|
export class StateManager {
|
2026-03-06 15:50:05 +08:00
|
|
|
// 历史记录-撤回/重做
|
|
|
|
|
mxHistory: any
|
|
|
|
|
historyList: any
|
|
|
|
|
historyIndex: any
|
2026-03-12 11:40:48 +08:00
|
|
|
running: any
|
2026-03-06 15:50:05 +08:00
|
|
|
|
|
|
|
|
// 管理器
|
2026-03-09 13:44:32 +08:00
|
|
|
canvasManager: any
|
2026-03-09 16:45:30 +08:00
|
|
|
layerManager: any
|
|
|
|
|
eventManager: any
|
2026-03-06 15:50:05 +08:00
|
|
|
toolManager: any
|
2026-03-11 15:34:56 +08:00
|
|
|
brushManager: any
|
2026-03-12 11:40:48 +08:00
|
|
|
keyEventManager: any
|
2026-03-06 15:50:05 +08:00
|
|
|
// 设置管理器
|
|
|
|
|
setManager(options) {
|
|
|
|
|
options.eventManager && (this.eventManager = options.eventManager)
|
2026-03-09 13:44:32 +08:00
|
|
|
options.canvasManager && (this.canvasManager = options.canvasManager)
|
2026-03-09 16:45:30 +08:00
|
|
|
options.layerManager && (this.layerManager = options.layerManager)
|
2026-03-06 15:50:05 +08:00
|
|
|
options.toolManager && (this.toolManager = options.toolManager)
|
2026-03-11 15:34:56 +08:00
|
|
|
options.brushManager && (this.brushManager = options.brushManager)
|
2026-03-12 11:40:48 +08:00
|
|
|
options.keyEventManager && (this.keyEventManager = options.keyEventManager)
|
2026-03-06 15:50:05 +08:00
|
|
|
}
|
|
|
|
|
constructor(options) {
|
|
|
|
|
this.mxHistory = ref(50)
|
|
|
|
|
this.historyList = ref([])
|
|
|
|
|
this.historyIndex = ref(0)
|
2026-03-12 11:40:48 +08:00
|
|
|
this.running = ref(false)
|
2026-03-06 15:50:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2026-03-09 13:44:32 +08:00
|
|
|
|
|
|
|
|
|
2026-03-13 14:08:40 +08:00
|
|
|
/** 设置是否开始记录状态 */
|
|
|
|
|
setIsRecord(isRecord: boolean) {
|
|
|
|
|
this.running.value = !isRecord
|
|
|
|
|
}
|
2026-03-12 11:40:48 +08:00
|
|
|
/** 清空状态 */
|
|
|
|
|
clearState(isRecordCurrentState?: boolean) {
|
|
|
|
|
this.historyList.value = []
|
|
|
|
|
this.historyIndex.value = 0
|
|
|
|
|
this.running.value = false
|
|
|
|
|
if (isRecordCurrentState) this.recordState()
|
|
|
|
|
}
|
2026-03-06 15:50:05 +08:00
|
|
|
/** 记录状态 */
|
|
|
|
|
recordState() {
|
2026-03-12 11:40:48 +08:00
|
|
|
if (this.running.value) return
|
|
|
|
|
this.running.value = true
|
|
|
|
|
if (this.historyIndex.value < this.historyList.value.length - 1) {
|
|
|
|
|
this.historyList.value.splice(this.historyIndex.value + 1)
|
|
|
|
|
}
|
|
|
|
|
const state = {
|
|
|
|
|
canvas: this.canvasManager.getCanvasJSON(),
|
|
|
|
|
}
|
|
|
|
|
this.historyList.value.push(state)
|
|
|
|
|
const size = this.historyList.value.length - this.mxHistory.value
|
|
|
|
|
if (size > 0) this.historyList.value.splice(0, size)
|
|
|
|
|
this.historyIndex.value = this.historyList.value.length - 1
|
|
|
|
|
this.running.value = false
|
2026-03-06 15:50:05 +08:00
|
|
|
}
|
|
|
|
|
/** 撤回状态 */
|
|
|
|
|
undoState() {
|
2026-03-12 11:40:48 +08:00
|
|
|
if (this.running.value) return
|
|
|
|
|
var index = this.historyIndex.value - 1
|
|
|
|
|
const state = this.historyList.value[index]
|
|
|
|
|
if (!state) return
|
|
|
|
|
this.running.value = true
|
|
|
|
|
this.historyIndex.value = index
|
|
|
|
|
this.canvasManager.loadJSON(state.canvas, () => {
|
|
|
|
|
this.running.value = false
|
|
|
|
|
})
|
2026-03-06 15:50:05 +08:00
|
|
|
}
|
|
|
|
|
/** 重做状态 */
|
|
|
|
|
redoState() {
|
2026-03-12 11:40:48 +08:00
|
|
|
if (this.running.value) return
|
|
|
|
|
var index = this.historyIndex.value + 1
|
|
|
|
|
const state = this.historyList.value[index]
|
|
|
|
|
if (!state) return
|
|
|
|
|
this.running.value = true
|
|
|
|
|
this.historyIndex.value = index
|
|
|
|
|
this.canvasManager.loadJSON(state.canvas, () => {
|
|
|
|
|
this.running.value = false
|
|
|
|
|
})
|
2026-03-06 15:50:05 +08:00
|
|
|
}
|
2026-03-12 11:40:48 +08:00
|
|
|
dispose() { }
|
2026-03-06 15:50:05 +08:00
|
|
|
}
|