You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WebCAD/__test__/FileSystemImmutable.test.ts

354 lines
5.7 KiB

/*
关于webCAD 文件系统的原型.
包括了对象的保存,撤销,还原.
对象的绘制 更新.
对象ID更新 引用
材质字典
关于对象的撤销和重做设计.
*/
import { List } from "immutable"
class CADObject
{
ApplyUndoData(data: any)
{
}
}
/**
* 表示了一个图纸该有的数据结构
*
* @class Database
*/
class Database extends CADObject
{
private curUndoIndex = -1;
//命令撤销数据列表
commandUndoData: { cmdName: string, undoData: Map<CADObject, UndoFile> }[] = [];
get UndoData(): Map<CADObject, UndoFile>
{
if (this.commandUndoData.length == 0)
{
this.startCmd("");
}
return this.commandUndoData[this.commandUndoData.length - 1].undoData;
}
startCmd(cmdName: string)
{
if (this.commandUndoData.length > 0 && this.curUndoIndex != this.commandUndoData.length - 1)
{
this.commandUndoData.splice(this.curUndoIndex, this.commandUndoData.length - this.curUndoIndex);
}
this.commandUndoData.push({ cmdName: "", undoData: new Map<CADObject, UndoFile>() });
this.curUndoIndex = this.commandUndoData.length - 1;
}
Undo(cout: number)
{
if (this.curUndoIndex == -1)
{
return;
}
for (let [obj, data] of this.commandUndoData[this.curUndoIndex].undoData)
{
for (let i = data.m_UndoDataList.length; i--;)
{
let d = data.m_UndoDataList[i];
obj.ApplyUndoData(d);
}
}
this.curUndoIndex--;
}
Redo(cout: number)
{
let undoFile = this.commandUndoData[this.curUndoIndex + 1];
if (undoFile)
{
for (let [obj, data] of undoFile.undoData)
{
for (let d of data.m_UndoDataList)
{
obj.ApplyUndoData(d);
}
}
this.curUndoIndex++;
}
}
}
let arr = [0, 1, 2, 3, 4, 5, 6];
arr.splice(3, arr.length - 3);
arr
/**
*
*
* @class UndoFile
*/
class UndoFile
{
m_UndoDataList = [];
}
enum EntityUndoType
{
}
//
interface EntityData
{
id: number;
isErase: boolean;
}
/**
*
* 所有图元的基类
*
* @class Entity
*/
class Entity extends CADObject
{
protected m_Data: EntityData;
m_Db: Database;
constructor()
{
super();
this.m_Data = {
id: -1,
isErase: false
}
}
get UndoFile(): UndoFile
{
if (this.m_Db)
{
let undoData = this.m_Db.UndoData;
if (!undoData.has(this))
{
undoData.set(this, new UndoFile());
}
return undoData.get(this);
}
}
WriteUndoData(data: any)
{
let undoFile = this.UndoFile;
if (undoFile)
{
undoFile.m_UndoDataList.push(data);
}
}
}
/**
* 直线撤销类型
*
* @enum {number}
*/
enum LineUndoType
{
Full = 0,
StartPoint = 1,
EndPoint = 2,
}
//
interface LineData extends EntityData
{
startPoint: List<number>,
endPoint: List<number>
}
/**
* 直线对象
*
* @class Line
*/
class Line extends Entity
{
protected m_Data: LineData;
constructor()
{
super();
this.m_Data.startPoint = List([0, 0, 0]);
this.m_Data.endPoint = List([0, 0, 0]);
}
get StartPoint(): number[]
{
return this.m_Data.startPoint.toJS();
}
set StartPoint(value: number[])
{
let newP = List(value);
this.WriteUndoData({ type: LineUndoType.StartPoint, data: { startPoint: this.m_Data.startPoint } });
this.m_Data.startPoint = newP;
}
ApplyUndoData(data: any)
{
switch (data.type)
{
case LineUndoType.Full:
break;
case LineUndoType.StartPoint:
this.m_Data.startPoint = data.data.startPoint;
break;
default:
break;
}
}
}
/**
* 图纸绘制适配器,使用适配器绘制对象图元.
*
* @class EentiyDrawAdapter
*/
class EentiyDrawAdapter
{
Draw()
{
return undefined;
}
}
/**
* 直线的绘制适配
*
* @class LineAdapter
* @extends {EentiyDrawAdapter}
*/
class LineAdapter extends EentiyDrawAdapter
{
Draw()
{
return undefined;
}
Update(type: number)
{
switch (type)
{
case LineUndoType.Full:
break;
case LineUndoType.StartPoint:
break;
case LineUndoType.EndPoint:
break;
default:
break;
}
}
}
let line = new Line();
console.log(line);
line.StartPoint = [1, 2, 3];
console.log(line);
let db = new Database();
line.m_Db = db;
line.StartPoint = [3, 3, 3];
console.log(line);
line.StartPoint = [3, 3, 5];
line.StartPoint = [12, 3, 5];
console.log(line);
console.log(db.commandUndoData);
db.Undo(1);
console.log(line.StartPoint);
db.Redo(1);
db.startCmd("");
console.log(line.StartPoint);
function per()
{
for (let i = 0; i < 5000; i++)
{
new Line();
}
}
function per2()
{
for (let i = 0; i < 5000; i++)
{
line.StartPoint = [3, 3, 3];
// console.log(line);
// line.StartPoint = [3, 3, 5];
line.StartPoint = [12, 3, 5];
// console.log(line);
// console.log(db.commandUndoData);
db.Undo(1);
// console.log(line);
db.Redo(1);
db.startCmd("");
// console.log(line.StartPoint);
}
}
per();/*?.*/
per2();/*?.*/
per2();/*?.*/
per2();/*?.*/
per2();/*?.*/
console.log(3);