dataease/frontend/src/components/canvas/store/snapshot.js

48 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-03-30 15:38:32 +08:00
import store from '@/store/index'
import { deepCopy } from '@/components/canvas/utils/utils'
2021-03-25 19:16:32 +08:00
export default {
state: {
snapshotData: [], // 编辑器快照数据
snapshotStyleData: [], // 样式改变也记录快照
snapshotIndex: -1 // 快照索引
},
mutations: {
undo(state) {
2021-06-17 11:56:05 +08:00
store.commit('setCurComponent', { component: null, index: null })
2021-05-27 17:06:38 +08:00
if (state.snapshotIndex > 0) {
state.snapshotIndex--
store.commit('setComponentData', deepCopy(state.snapshotData[state.snapshotIndex]))
store.commit('setCanvasStyle', deepCopy(state.snapshotStyleData[state.snapshotIndex]))
}
2021-03-25 19:16:32 +08:00
},
redo(state) {
2021-06-17 11:56:05 +08:00
store.commit('setCurComponent', { component: null, index: null })
if (state.snapshotIndex < state.snapshotData.length - 1) {
state.snapshotIndex++
store.commit('setComponentData', deepCopy(state.snapshotData[state.snapshotIndex]))
store.commit('setCanvasStyle', deepCopy(state.snapshotStyleData[state.snapshotIndex]))
}
},
2021-03-25 19:16:32 +08:00
recordSnapshot(state) {
2021-06-10 17:46:16 +08:00
// console.log('recordSnapshot')
// 添加新的快照
state.snapshotData[++state.snapshotIndex] = deepCopy(state.componentData)
state.snapshotStyleData[state.snapshotIndex] = deepCopy(state.canvasStyleData)
// 在 undo 过程中,添加新的快照时,要将它后面的快照清理掉
if (state.snapshotIndex < state.snapshotData.length - 1) {
state.snapshotData = state.snapshotData.slice(0, state.snapshotIndex + 1)
state.snapshotStyleData = state.snapshotStyleData.slice(0, state.snapshotIndex + 1)
}
2021-03-25 19:16:32 +08:00
},
refreshSnapshot(state) {
// 刷新快照
state.snapshotData = []
state.snapshotStyleData = []
state.snapshotIndex = -1
}
}
2021-03-30 15:38:32 +08:00
}