Files
FiDA_Front/src/components/Canvas/DepthCanvas/manager/StateManager.ts

116 lines
3.1 KiB
TypeScript
Raw Normal View History

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-16 16:51:12 +08:00
class Event {
list: any[]
constructor() {
this.list = []
}
add(name, call) {
this.list.push({ name, call })
}
remove(name, call) {
this.list = this.list.filter(item => item.name != name && item.call != call)
}
emit(name, data) {
this.list.forEach(item => {
if (item.name == name) item.call(data)
})
}
}
2026-03-06 15:50:05 +08:00
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-16 16:51:12 +08:00
event: Event
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-16 16:51:12 +08:00
objectManager: 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-16 16:51:12 +08:00
options.objectManager && (this.objectManager = options.objectManager)
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-16 16:51:12 +08:00
this.event = new Event()
2026-03-06 15:50:05 +08:00
}
2026-03-16 16:51:12 +08:00
onMounted() { }
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
2026-03-23 16:43:08 +08:00
console.log("recordState")
2026-03-12 11:40:48 +08:00
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
2026-03-19 13:17:03 +08:00
this.canvasManager.loadJSON(state.canvas, false).then(() => {
2026-03-16 16:51:12 +08:00
this.event.emit('canvas:undo', state)
2026-03-12 11:40:48 +08:00
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
2026-03-19 13:17:03 +08:00
this.canvasManager.loadJSON(state.canvas, false).then(() => {
2026-03-16 16:51:12 +08:00
this.event.emit('canvas:redo', state)
2026-03-12 11:40:48 +08:00
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
}