Browse Source

完成文档

master
修宁 7 months ago
parent
commit
5037be2e57
  1. 3
      src/core/base/BaseRenderer.ts
  2. 7
      src/core/manager/EntityManager.ts
  3. 147
      src/core/manager/StateManager.ts
  4. 4
      src/modules/measure/MeasureRenderer.ts

3
src/core/base/BaseRenderer.ts

@ -5,7 +5,8 @@ import { Line2 } from 'three/examples/jsm/lines/Line2'
/** /**
* *
* / 线 Three.js * / 线 Three.js .
* InstancePool
*/ */
export default abstract class BaseRenderer { export default abstract class BaseRenderer {

7
src/core/manager/EntityManager.ts

@ -4,6 +4,9 @@ import type BaseRenderer from '@/core/base/BaseRenderer'
import { getRenderer } from './ModuleManager' import { getRenderer } from './ModuleManager'
import { getLineId, parseLineId } from '@/core/ModelUtils' import { getLineId, parseLineId } from '@/core/ModelUtils'
/**
*
*/
export class Relation { export class Relation {
center = new Set<string>() center = new Set<string>()
input = new Set<string>() input = new Set<string>()
@ -179,7 +182,7 @@ export default class EntityManager {
// 添加存在性检查 // 添加存在性检查
if (!start || !end) { if (!start || !end) {
console.warn(`无法创建线 ${lineId}, 起点或终点不存在`) system.showErrorDialog(`无法创建线 ${lineId}, 起点或终点不存在`)
continue continue
} }
@ -197,7 +200,7 @@ export default class EntityManager {
// 添加存在性检查 // 添加存在性检查
if (!start || !end) { if (!start || !end) {
console.warn(`无法更新线 ${lineId}, 起点或终点不存在`) system.showErrorDialog(`无法更新线 ${lineId}, 起点或终点不存在`)
continue continue
} }

147
src/core/manager/StateManager.ts

@ -90,7 +90,10 @@ export default class StateManager {
*/ */
private historySteps: HistoryStep[] = [] private historySteps: HistoryStep[] = []
private historyIndex = -1 private historyIndex = -1
private readonly maxHistorySteps = 20 private maxHistorySteps = 20
// 标记是否有未保存的更改
private pendingChanges = false
// 变化追踪器 // 变化追踪器
private readonly changeTracker: DataDiff = { private readonly changeTracker: DataDiff = {
@ -104,20 +107,16 @@ export default class StateManager {
*/ */
private lastStateDict = new Map<string, ItemJson>() private lastStateDict = new Map<string, ItemJson>()
// 自动保存相关
private autoSaveInterval: number | null = null
// 是否有待保存的更改
private pendingChanges = false
/** /**
* @param id , key * @param id , key
* @param viewport , * @param viewport ,
* @param bufferSize 50 * @param maxHistorySteps 20
*/ */
constructor(id: string, viewport: Viewport, bufferSize = 50) { constructor(id: string, viewport: Viewport, maxHistorySteps = 20) {
this.id = id this.id = id
this.storeKey = `-tmp-yvan-lcc-${this.id}` this.storeKey = `-tmp-yvan-lcc-${this.id}`
this.entityManager = viewport.entityManager this.entityManager = viewport.entityManager
this.maxHistorySteps = maxHistorySteps
// this.historySteps = Array.from({ length: this.maxHistorySteps }, () => null) // this.historySteps = Array.from({ length: this.maxHistorySteps }, () => null)
this.historySteps = [] this.historySteps = []
@ -126,7 +125,7 @@ export default class StateManager {
/** /**
* *
*/ */
beginStateUpdate() { beginStateUpdate(): void {
this.lastStateDict = new Map(this.vdata.items.map(item => [item.id, _.cloneDeep(item)])) this.lastStateDict = new Map(this.vdata.items.map(item => [item.id, _.cloneDeep(item)]))
this.changeTracker.added.length = 0 this.changeTracker.added.length = 0
this.changeTracker.removed.length = 0 this.changeTracker.removed.length = 0
@ -136,7 +135,7 @@ export default class StateManager {
/** /**
* *
*/ */
endStateUpdate() { endStateUpdate(): void {
this.calculateDiff() this.calculateDiff()
this.saveStep() this.saveStep()
this.syncDataState(this.changeTracker) this.syncDataState(this.changeTracker)
@ -215,14 +214,13 @@ export default class StateManager {
/** /**
* viewport * entityManager
* - entityManager.beginEntityUpdate() * - entityManager.beginEntityUpdate()
* - entityManager.createEntity(vdataItem) * - entityManager.createOrUpdateEntity(vdataItem)
* - entityManager.updateEntity(vdataItem)
* - entityManager.deleteEntity(id) * - entityManager.deleteEntity(id)
* - entityManager.endEntityUpdate() * - entityManager.endEntityUpdate()
*/ */
syncDataState(diff: DataDiff) { syncDataState(diff: DataDiff): void {
// 没有变化时跳过同步 // 没有变化时跳过同步
if ( if (
diff.added.length === 0 && diff.added.length === 0 &&
@ -291,6 +289,14 @@ export default class StateManager {
system.msg('重做完成') system.msg('重做完成')
} }
stopAutoSave() {
// 停止自动保存逻辑
}
startAutoSave() {
// 实现自动保存逻辑, 只要调用过 endStateUpdate(), 固定 5秒后触发 saveToLocalstore 方法, 调用过程不用等回调完成
}
/** /**
* *
*/ */
@ -357,42 +363,6 @@ export default class StateManager {
} }
} }
// /**
// * 应用反向差异(用于撤销)
// */
// private applyReverseDiff(diff: DataDiff) {
// const { added, removed, updated } = diff
//
// // 恢复删除
// const restored = removed
// .map(id => {
// const found = this.vdata.items.find(i => i.id === id)
// return found ? _.cloneDeep(found) : null
// })
// .filter(Boolean) as ItemJson[]
//
// // 删除新增
// const addedIds = new Set(added.map(a => a.id))
// _.remove(this.vdata.items, (item => addedIds.has(item.id)))
//
// // 恢复更新
// const restoreMap = new Map(updated.map(u => [u.after.id, u.before]))
// 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)
// } else {
// this.vdata.items.push(itemJson)
// }
// }
//
// // 添加恢复的条目
// this.vdata.items.push(...restored)
//
// // 更新 lastStateDict
// this.lastStateDict = new Map(this.vdata.items.map(item => [item.id, _.cloneDeep(item)]))
// }
private fullSync() { private fullSync() {
this.entityManager.beginEntityUpdate() this.entityManager.beginEntityUpdate()
this.vdata.items.forEach(item => { this.vdata.items.forEach(item => {
@ -410,42 +380,6 @@ export default class StateManager {
return !!this.historySteps[nextIndex] return !!this.historySteps[nextIndex]
} }
// /**
// * 保存到本地存储(防止数据丢失)
// */
// async saveToLocalstore() {
// // 只保存变化部分和关键元数据
// const saveData = {
// diff: this.changeTracker,
// timestamp: Date.now(),
// itemsCount: this.vdata.items.length
// }
//
// await localforage.setItem(this.storeKey, saveData)
// }
//
// /**
// * 从本地存储加载数据
// */
// async loadFromLocalstore() {
// try {
// this.isLoading.value = true
// const saved: any = await localforage.getItem(this.storeKey)
// if (saved && saved.diff) {
// this.applyDiff(saved.diff)
// this.isChanged.value = true
// this.pendingChanges = true
// console.log('[StateManager] 从本地存储恢复 ', saved.itemsCount, '个对象')
// }
//
// } catch (error) {
// console.error('[StateManager] 从本地存储加载失败:', error)
//
// } finally {
// this.isLoading.value = false
// }
// }
/** /**
* *
*/ */
@ -454,20 +388,6 @@ export default class StateManager {
} }
/** /**
*
*/
async forceSave() {
try {
await this.saveToLocalstore()
this.pendingChanges = false
return true
} catch (error) {
console.error('[StateManager] 强制保存失败:', error)
return false
}
}
/**
* indexDb() * indexDb()
*/ */
async saveToLocalstore() { async saveToLocalstore() {
@ -482,7 +402,9 @@ export default class StateManager {
this.isLoading.value = true this.isLoading.value = true
const saved: VData = await localforage.getItem(this.storeKey) const saved: VData = await localforage.getItem(this.storeKey)
if (saved) { if (saved) {
this.vdata.items = saved.items || [] this.vdata = {
...saved
}
this.isChanged.value = saved.isChanged || false this.isChanged.value = saved.isChanged || false
this.pendingChanges = true this.pendingChanges = true
this.fullSync() // 同步到视口 this.fullSync() // 同步到视口
@ -491,6 +413,7 @@ export default class StateManager {
} catch (error) { } catch (error) {
console.error('[StateManager] 从本地存储加载失败:', error) console.error('[StateManager] 从本地存储加载失败:', error)
} finally { } finally {
this.isLoading.value = false this.isLoading.value = false
} }
@ -509,31 +432,9 @@ export default class StateManager {
} }
/** /**
*
*/
startAutoSave() {
// if (this.autoSaveInterval) return
//
// this.autoSaveInterval = window.setInterval(() => {
// this.autoSaveIfNeeded()
// }, this.autoSaveIntervalMs)
}
/**
*
*/
stopAutoSave() {
// if (this.autoSaveInterval) {
// clearInterval(this.autoSaveInterval)
// this.autoSaveInterval = null
// }
}
/**
* *
*/ */
destroy() { destroy() {
this.stopAutoSave()
// 清理引用 // 清理引用
delete this.vdata delete this.vdata
delete this.historySteps delete this.historySteps

4
src/modules/measure/MeasureRenderer.ts

@ -1,5 +1,5 @@
import BaseRenderer from '@/core/base/BaseRenderer.ts'
import * as THREE from 'three' import * as THREE from 'three'
import BaseRenderer from '@/core/base/BaseRenderer.ts'
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry' import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry'
import { Line2 } from 'three/examples/jsm/lines/Line2' import { Line2 } from 'three/examples/jsm/lines/Line2'
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial' import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial'
@ -14,7 +14,7 @@ export default class MeasureRenderer extends BaseRenderer {
*/ */
group: THREE.Group group: THREE.Group
static GROUP_NAME = 'measure-group' static GROUP_NAME = 'measure_group'
static LABEL_NAME = 'measure_label' static LABEL_NAME = 'measure_label'
static POINT_NAME = 'measure_point' static POINT_NAME = 'measure_point'
static LINE_NAME = 'measure_line' static LINE_NAME = 'measure_line'

Loading…
Cancel
Save