chartHistoryStore.ts
6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { defineStore } from 'pinia'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
import { EditCanvasType } from '@/store/modules/chartEditStore/chartEditStore.d'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
import { editHistoryMax } from '@/settings/designSetting'
import {
HistoryStackItemEnum,
HistoryActionTypeEnum,
HistoryTargetTypeEnum,
HistoryItemType,
ChartHistoryStoreType
} from './chartHistoryStore.d'
export const useChartHistoryStore = defineStore({
id: 'useChartHistoryStore',
state: (): ChartHistoryStoreType => ({
// 后退栈
backStack: [],
// 前进栈
forwardStack: []
}),
getters: {
getBackStack(): Array<HistoryItemType> {
return this.backStack
},
getForwardStack(): Array<HistoryItemType> {
return this.forwardStack
}
},
actions: {
/**
* * 新增记录并插入栈
* @param item 图表实例
* @param actionType 动作类型
* @param targetType 对象类型(默认图表)
*/
createStackItem(
item: CreateComponentType[] | CreateComponentGroupType[] | EditCanvasType[],
actionType: HistoryActionTypeEnum,
targetType: HistoryTargetTypeEnum = HistoryTargetTypeEnum.CHART
) {
// 优化性能转为 freeze
this.pushBackStackItem(
Object.freeze({
[HistoryStackItemEnum.ID]: new Date().getTime().toString(),
[HistoryStackItemEnum.HISTORY_DATA]: item,
[HistoryStackItemEnum.ACTION_TYPE]: actionType,
[HistoryStackItemEnum.TARGET_TYPE]: targetType
} as const)
)
},
// * 画布初始化
canvasInit(canvas: EditCanvasType) {
this.createStackItem([canvas], HistoryActionTypeEnum.ADD, HistoryTargetTypeEnum.CANVAS)
},
// * 推入后退栈
pushBackStackItem(item: HistoryItemType | Array<HistoryItemType>, notClear = false): void {
if (item instanceof Array) this.backStack = [...this.backStack, ...item]
else this.backStack.push(item)
this.backStack.splice(0, this.backStack.length - editHistoryMax)
// 新动作需清空前进栈
if (notClear) return
this.clearForwardStack()
},
// * 推入前进栈
pushForwardStack(item: HistoryItemType | Array<HistoryItemType>): void {
if (item instanceof Array) this.forwardStack = [...this.forwardStack, ...item]
else this.forwardStack.push(item)
},
// * 移出后退栈
popBackStackItem(): HistoryItemType | undefined {
if (this.backStack.length > 0) {
return this.backStack.pop()
}
},
// * 移出前进栈
popForwardStack(): HistoryItemType | undefined {
if (this.forwardStack.length > 0) {
return this.forwardStack.pop()
}
},
// * 清空前进栈
clearForwardStack() {
this.forwardStack = []
},
// * 清空后退栈(保留初始化)
clearBackStack() {
const canvasHistory = this.getBackStack[0]
this.backStack = [canvasHistory]
},
// * 撤回
backAction() {
try {
loadingStart()
// 排除画布初始化
if (this.getBackStack.length > 1) {
const targetData = this.popBackStackItem()
if (!targetData) {
loadingFinish()
return
}
// 移除记录到前进堆
this.pushForwardStack(targetData)
loadingFinish()
return targetData
}
loadingFinish()
} catch (error) {
loadingError()
}
},
// * 前进
forwardAction() {
try {
loadingStart()
if (this.getForwardStack.length) {
const targetData = this.popForwardStack()
if (!targetData) {
loadingFinish()
return
}
// 放入后退栈
this.pushBackStackItem(targetData, true)
loadingFinish()
return targetData
}
loadingFinish()
} catch (error) {
loadingError()
}
},
// * 新增组件记录
createAddHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.ADD, HistoryTargetTypeEnum.CHART)
},
// * 更新属性记录(大小、图表属性)
createUpdateHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.UPDATE, HistoryTargetTypeEnum.CHART)
},
// * 删除组件记录
createDeleteHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.DELETE, HistoryTargetTypeEnum.CHART)
},
// * 移动组件记录
createMoveHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.MOVE, HistoryTargetTypeEnum.CHART)
},
// * 改变层级组件记录
createLayerHistory(
item: Array<CreateComponentType | CreateComponentGroupType>,
type:
| HistoryActionTypeEnum.TOP
| HistoryActionTypeEnum.DOWN
| HistoryActionTypeEnum.UP
| HistoryActionTypeEnum.BOTTOM
) {
this.createStackItem(item, type, HistoryTargetTypeEnum.CHART)
},
// * 剪切组件记录
createPasteHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.CUT, HistoryTargetTypeEnum.CHART)
},
// * 创建分组
createGroupHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.GROUP, HistoryTargetTypeEnum.CHART)
},
// * 解除分组
createUnGroupHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.UN_GROUP, HistoryTargetTypeEnum.CHART)
},
// * 锁定记录
createLockHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.LOCK, HistoryTargetTypeEnum.CHART)
},
// * 解锁记录
createUnLockHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.UNLOCK, HistoryTargetTypeEnum.CHART)
},
// * 隐藏记录
createHideHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.HIDE, HistoryTargetTypeEnum.CHART)
},
// * 展示记录
createShowHistory(item: Array<CreateComponentType | CreateComponentGroupType>) {
this.createStackItem(item, HistoryActionTypeEnum.SHOW, HistoryTargetTypeEnum.CHART)
}
}
})