index.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { EditorModule } from "..";
  2. import { CompUI } from "../components/CompUI";
  3. import { DesignTemp } from "../defines/DesignTemp";
  4. import { DesignComp } from "../defines/DesignTemp/DesignComp";
  5. import { EditorMode, ICompKeys } from "../typings";
  6. export const store = EditorModule.store({
  7. state: () => ({
  8. mode: "edit" as EditorMode,
  9. currCompId: "",
  10. designData: new DesignTemp(),
  11. designCompMap: new Map<string, DesignComp>(),
  12. }),
  13. getters: {
  14. isEditMode(state) {
  15. return state.mode === "edit";
  16. },
  17. currComp(state) {
  18. return state.designCompMap.get(state.currCompId);
  19. },
  20. },
  21. actions: {
  22. setMode(v: EditorMode) {
  23. this.store.mode = v;
  24. },
  25. initDesignCompMap() {
  26. const { designData, designCompMap } = this.store;
  27. const arr = [...designData.content];
  28. designCompMap.clear();
  29. while (arr.length) {
  30. const item = arr[0];
  31. designCompMap.set(item.id, item);
  32. arr.shift();
  33. if (item.children) {
  34. arr.unshift(...Object.values(item.children));
  35. }
  36. }
  37. },
  38. initDesignData(data: Partial<DesignTemp>) {
  39. this.store.designData = new DesignTemp(data);
  40. this.store.initDesignCompMap();
  41. },
  42. insertDesignContent(compKey: ICompKeys, index?: number) {
  43. index === undefined && (index = this.store.designData.content.length);
  44. const options = CompUI[compKey].options;
  45. const comp = new DesignComp({
  46. compKey,
  47. value: options?.value,
  48. layout: options?.layout,
  49. background: options?.background,
  50. children: options?.children as any,
  51. });
  52. this.store.designData.content.splice(index, 0, comp);
  53. this.store.initDesignCompMap();
  54. return comp;
  55. },
  56. insertCompContainer(compKey: ICompKeys, container: DesignComp) {
  57. const options = CompUI[compKey].options;
  58. const comp = new DesignComp({
  59. compKey,
  60. value: options?.value,
  61. layout: options?.layout,
  62. background: options?.background,
  63. children: options?.children as any,
  64. });
  65. container.children || (container.children = {});
  66. container.children[comp.id] = comp;
  67. this.store.initDesignCompMap();
  68. return comp;
  69. },
  70. setCurrComp(compId: string) {
  71. this.store.currCompId = compId;
  72. },
  73. deleteComp(compId: string) {
  74. const parentComp = this.helper.findParentComp(compId);
  75. if (parentComp) {
  76. console.log("find parent comp=>", parentComp);
  77. compId = parentComp?.id;
  78. // delete parentComp.children[compId];
  79. }
  80. // else {
  81. const index = this.store.designData.content.findIndex(
  82. (d) => d.id === compId
  83. );
  84. if (index !== -1) {
  85. this.store.designData.content.splice(index, 1);
  86. }
  87. // }
  88. },
  89. moveComp(selIndex: number, targetIndex: number) {
  90. const { content } = this.store.designData;
  91. const [selComp] = content.splice(selIndex, 1);
  92. content.splice(targetIndex, 0, selComp);
  93. },
  94. },
  95. });