Browse Source

stateManager 测试用例2 通过;

stateManager.undo()
master
修宁 7 months ago
parent
commit
a9c0ab746a
  1. 144
      src/core/manager/StateManager.ts

144
src/core/manager/StateManager.ts

@ -4,20 +4,36 @@ import type EntityManager from './EntityManager'
import { markRaw, reactive, ref } from 'vue'
import type Viewport from '@/core/engine/Viewport.ts'
// 差异类型定义
interface DataDiff {
added: ItemJson[]
removed: string[]
updated: ItemJson[]
}
// 历史记录项
interface HistoryStep {
diff: DataDiff
snapshot: Map<string, ItemJson>
timestamp: number
}
/**
* ,
*
* , 稿, 稿,
* EntityManager
*
* 1. ,
* 2.
* 3. Interaction
* 3.
* - 1. beginStateUpdate
* - 2. vdata
* - 3. endStateUpdate
* 4. syncDataState() vdata viewport
* 5. syncDataState vdata viewport
* -
* 6. isLoading = true
*
*
* 4. syncDataState() vdata EntityManager
* 5. isLoading = true
*
* :
* - 10000 ,
*
* // 修改坐标点
* stateManager.beginStateUpdate();stateManager.vdata.items[1].tf[0] = [-10, 0, 4];stateManager.endStateUpdate();
@ -102,7 +118,6 @@ export default class StateManager {
*
*/
beginStateUpdate() {
// 创建当前状态快照(非深拷贝)
this.lastSnapshot = new Map(
this.vdata.items.map(item => [item.id, _.cloneDeep(item)])
)
@ -113,7 +128,6 @@ export default class StateManager {
*
*/
endStateUpdate() {
debugger
this.calculateDiff()
this.saveStep()
this.syncDataState()
@ -172,6 +186,7 @@ export default class StateManager {
const nextIndex = (this.historyIndex + 1) % this.maxHistorySteps
this.historySteps[nextIndex] = {
diff: _.cloneDeep(this.changeTracker),
snapshot: _.cloneDeep(this.lastSnapshot),
timestamp: Date.now()
}
@ -267,31 +282,9 @@ export default class StateManager {
}
/**
*
*/
async save(): Promise<Object> {
return _.cloneDeep(this.vdata)
}
/**
*
*/
async forceSave() {
try {
await this.saveToLocalstore()
this.pendingChanges = false
return true
} catch (error) {
console.error('[StateManager] 强制保存失败:', error)
return false
}
}
/**
*
*/
undo() {
debugger
if (!this.undoEnabled()) return
const step = this.historySteps[this.historyIndex]
@ -301,6 +294,11 @@ export default class StateManager {
this.historyIndex = (this.historyIndex - 1 + this.maxHistorySteps) % this.maxHistorySteps
this.isChanged.value = true
this.pendingChanges = true
this.lastSnapshot = new Map(
this.vdata.items.map(item => [item.id, _.cloneDeep(item)])
)
this.syncDataState()
}
@ -317,6 +315,11 @@ export default class StateManager {
this.applyDiff(step.diff)
this.isChanged.value = true
this.pendingChanges = true
this.lastSnapshot = new Map(
this.vdata.items.map(item => [item.id, _.cloneDeep(item)])
)
this.syncDataState()
}
@ -372,12 +375,32 @@ export default class StateManager {
.filter(([, item]) => !!item) as [string, ItemJson][]
)
this.vdata.items = this.vdata.items.map(item =>
restoreMap.has(item.id) ? restoreMap.get(item.id)! : item
)
for (const [key, itemJson] of restoreMap) {
const idx = _.findIndex(this.vdata.items, (item => item.id === key))
if (idx >= 0) {
Object.assign(this.vdata.items[idx], itemJson)
}
}
}
}
private fullSync() {
this.entityManager.beginEntityUpdate()
this.vdata.items.forEach(item => {
this.entityManager.createOrUpdateEntity(item)
})
this.entityManager.endEntityUpdate()
}
undoEnabled() {
return this.historyIndex >= 0 && this.historySteps[this.historyIndex]
}
redoEnabled() {
const nextIndex = (this.historyIndex + 1) % this.maxHistorySteps
return !!this.historySteps[nextIndex]
}
// /**
// * 保存到本地存储(防止数据丢失)
// */
@ -415,6 +438,27 @@ export default class StateManager {
// }
/**
*
*/
async save(): Promise<Object> {
return _.cloneDeep(this.vdata)
}
/**
*
*/
async forceSave() {
try {
await this.saveToLocalstore()
this.pendingChanges = false
return true
} catch (error) {
console.error('[StateManager] 强制保存失败:', error)
return false
}
}
/**
* indexDb()
*/
async saveToLocalstore() {
@ -443,23 +487,6 @@ export default class StateManager {
}
}
private fullSync() {
this.entityManager.beginEntityUpdate()
this.vdata.items.forEach(item => {
this.entityManager.createOrUpdateEntity(item)
})
this.entityManager.endEntityUpdate()
}
undoEnabled() {
return this.historyIndex >= 0 && this.historySteps[this.historyIndex]
}
redoEnabled() {
const nextIndex = (this.historyIndex + 1) % this.maxHistorySteps
return !!this.historySteps[nextIndex]
}
/**
*
*/
@ -533,16 +560,3 @@ export default class StateManager {
}
}
// 差异类型定义
interface DataDiff {
added: ItemJson[]
removed: string[]
updated: ItemJson[]
}
// 历史记录项
interface HistoryStep {
diff: DataDiff
timestamp: number
}
Loading…
Cancel
Save