DesignComp.ts 889 B

12345678910111213141516171819202122232425262728293031323334
  1. import { nanoid } from "nanoid";
  2. import { Background, ICompKeys, Layout } from "../../typings";
  3. import { cloneDeep } from "lodash";
  4. export class DesignComp {
  5. id = nanoid();
  6. compKey: ICompKeys = "Text";
  7. value: any = undefined;
  8. layout: Layout = {};
  9. background: Background = {};
  10. children: Record<string, DesignComp> = {};
  11. constructor(data?: Partial<DesignComp>) {
  12. if (!data) return;
  13. const newData = filterObj(data);
  14. if (newData.children) {
  15. Object.entries(newData.children).forEach(([key, value]) => {
  16. newData.children[key] = new DesignComp(value as any);
  17. });
  18. }
  19. Object.assign(this, cloneDeep(newData));
  20. }
  21. }
  22. function filterObj(obj: any) {
  23. const filteredObj: any = {};
  24. Object.keys(obj).forEach((key) => {
  25. if (obj[key] !== null && obj[key] !== undefined) {
  26. filteredObj[key] = obj[key];
  27. }
  28. });
  29. return filteredObj;
  30. }