StructV2/src/Common/util.ts
2021-05-17 14:02:19 +08:00

126 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ConstructList } from "../Model/modelConstructor";
import { G6EdgeModel, G6NodeModel, Link, Model } from "../Model/modelData";
import { SV } from "../StructV";
import { G6Data } from "../View/renderer";
/**
* 工具函数
*/
export const Util = {
/**
* 生成唯一id
*/
generateId(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
},
/**
* 乞丐版对象克隆
* @param obj
*/
objectClone<T extends Object>(obj: T): T {
return obj? JSON.parse(JSON.stringify(obj)): { };
},
/**
* 从列表中移除元素
* @param list 移除列表
* @param fn 移除判断规则
*/
removeFromList<T>(list: T[], fn: (item: T) => boolean) {
for(let i = 0; i < list.length; i++) {
fn(list[i]) && list.splice(i, 1) && i--;
}
},
/**
* 断言函数
* @param assertFn
* @param errorText
*/
assert(condition: boolean, errorText: string): void | never {
if(condition) {
throw errorText;
}
},
/**
* 文本解析
* @param text
*/
textParser(text: string): string[] | string {
let fieldReg = /\[[^\]]*\]/g;
if(fieldReg.test(text)) {
let contents = text.match(fieldReg),
values = contents.map(item => item.replace(/\[|\]/g, ''));
return values;
}
else {
return text;
}
},
/**
* 牵制某个值
* @param value
*/
clamp(value: number, max: number, min: number): number {
if(value <= max && value >= min) return value;
if(value > max) return max;
if(value < min) return min;
},
/**
*
* @param constructListType
* @returns
*/
converterList(modelContainer: { [key: string]: ConstructList[keyof ConstructList]}) {
return [].concat(...Object.keys(modelContainer).map(item => modelContainer[item]));
},
/**
* G6 data 转换器
* @param constructList
* @returns
*/
convertG6Data(constructList: ConstructList): G6Data {
let nodes = [...constructList.element, ...constructList.pointer],
edges = constructList.link;
return {
nodes: nodes.map(item => item.cloneProps()) as G6NodeModel[],
edges: edges.map(item => item.cloneProps()) as G6EdgeModel[]
};
},
/**
* 将 modelList 转换到 G6Data
* @param modelList
*/
convertModelList2G6Data(modelList: Model[]): G6Data {
return {
nodes: <G6NodeModel[]>(modelList.filter(item => !(item instanceof Link)).map(item => item.cloneProps())),
edges: <G6EdgeModel[]>(modelList.filter(item => item instanceof Link).map(item => item.cloneProps()))
}
},
/**
* 计算旋转矩阵
* @param matrix
* @param rotation
*/
calcRotateMatrix(matrix: number[], rotation: number): number[] {
const Mat3 = SV.G6.Util.mat3;
Mat3.rotate(matrix, matrix, rotation);
return matrix;
}
};