factory.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { nanoid } from "nanoid";
  2. import { CompArcObj } from "../../components/CompUI/basicUI/Arc";
  3. import { CompCardObj } from "../../components/CompUI/basicUI/Container";
  4. import { CompCurveObj } from "../../components/CompUI/basicUI/Curve";
  5. import { CompEllipseObj } from "../../components/CompUI/basicUI/Ellipse";
  6. import { CompGroupObj } from "../../components/CompUI/basicUI/Group";
  7. import { CompImageObj } from "../../components/CompUI/basicUI/Image2";
  8. import { CompLineObj } from "../../components/CompUI/basicUI/Line";
  9. import { CompMapObj } from "../../components/CompUI/basicUI/Map";
  10. import { CompPageObj } from "../../components/CompUI/basicUI/Page";
  11. import { CompPolygonObj } from "../../components/CompUI/basicUI/Polygon";
  12. import { CompPolygonNormalObj } from "../../components/CompUI/basicUI/PolygonNormal";
  13. import { CompRectObj } from "../../components/CompUI/basicUI/Rectage";
  14. import { CompTextObj } from "../../components/CompUI/basicUI/Text";
  15. import { CompTriangleObj } from "../../components/CompUI/basicUI/Triangle";
  16. import { CompVideoObj } from "../../components/CompUI/basicUI/Video";
  17. import { CompWeb3DObj } from "../../components/CompUI/basicUI/Web3D";
  18. import { HistoryController } from "../../controllers/ReactCtrl/history";
  19. import { ICompKeys } from "../../typings";
  20. import { DesignComp, DesignCompObj } from "./DesignComp";
  21. const history = new HistoryController();
  22. export function createObj( data:any, init = true) {
  23. const compKey = data.compKey as ICompKeys
  24. let obj = {} as DesignComp;
  25. switch(compKey) {
  26. case "Container":
  27. obj = new CompCardObj();
  28. break;
  29. case "Image":
  30. obj = new CompImageObj();
  31. break;
  32. case "Text":
  33. obj = new CompTextObj();
  34. break;
  35. case "Group":
  36. obj = new CompGroupObj();
  37. break;
  38. case "Page":
  39. obj = new CompPageObj();
  40. break;
  41. case "Video":
  42. obj = new CompVideoObj();
  43. break;
  44. case "Web3D":
  45. obj = new CompWeb3DObj();
  46. break;
  47. case "Rectage":
  48. obj = new CompRectObj();
  49. break;
  50. case "Line":
  51. obj = new CompLineObj();
  52. break;
  53. case "Arc":
  54. obj = new CompArcObj();
  55. break;
  56. case "Ellipse":
  57. obj = new CompEllipseObj();
  58. break;
  59. case "Triangle":
  60. obj = new CompTriangleObj();
  61. break;
  62. case "Polygon":
  63. obj = new CompPolygonObj();
  64. break;
  65. case "PolygonNormal":
  66. obj = new CompPolygonNormalObj();
  67. break;
  68. case "Curve":
  69. obj = new CompCurveObj();
  70. break;
  71. case "Map":
  72. obj = new CompMapObj()
  73. break;
  74. }
  75. obj.compKey = compKey;
  76. obj.value.setHistory( history );
  77. obj.layout.setHistory(history);
  78. obj.layout.background.setHistory(history);
  79. obj.layout.border.setHistory(history);
  80. obj.children.setHistory(history);
  81. if (init) obj.fromJson(data);
  82. return obj;
  83. }
  84. export function cloneObj(o: DesignComp ) {
  85. const data = o.toJson();
  86. data.id = nanoid();
  87. const obj = createObj({compKey: o.compKey}, false)
  88. obj.fromJson(data);
  89. return obj;
  90. }
  91. export {history};