index.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { EditorModule } from "..";
  2. import {
  3. addCacheToMap,
  4. createCompId,
  5. } from "../components/CompUI/defines/createCompId";
  6. import { DesignTemp } from "../defines/DesignTemp";
  7. import { DesignComp } from "../defines/DesignTemp/DesignComp";
  8. import { EditorMode, ICompKeys } from "../typings";
  9. export const store = EditorModule.store({
  10. state: () => ({
  11. mode: "edit" as EditorMode,
  12. currCompId: "",
  13. designData: new DesignTemp(),
  14. }),
  15. getters: {
  16. isEditMode(state) {
  17. return state.mode === "edit";
  18. },
  19. currComp(state) {
  20. return state.designData.compMap[state.currCompId];
  21. },
  22. },
  23. actions: {
  24. setCompData(id: string, data: any) {
  25. this.store.designData.compMap[id] = data;
  26. },
  27. setMode(v: EditorMode) {
  28. this.store.mode = v;
  29. },
  30. initDesignData(data: Partial<DesignTemp>) {
  31. this.store.designData = new DesignTemp(data);
  32. },
  33. insertDesignContent(compKey: ICompKeys, index?: number) {
  34. index === undefined && (index = this.store.designData.content.length);
  35. const compId = createCompId(compKey);
  36. addCacheToMap(this.store.designData.compMap);
  37. this.store.designData.content.splice(index, 0, compId);
  38. return compId;
  39. },
  40. insertCompContainer(compKey: ICompKeys, container: DesignComp) {
  41. const compId = createCompId(compKey);
  42. addCacheToMap(this.store.designData.compMap);
  43. container.children || (container.children = {});
  44. container.children[compId] = compId;
  45. return compId;
  46. },
  47. setCurrComp(compId: string) {
  48. this.store.currCompId = compId;
  49. },
  50. deleteComp(compId: string) {
  51. const { content, compMap } = this.store.designData;
  52. const parentComp = this.helper.findParentComp(compId);
  53. let deleteOK = false;
  54. if (parentComp) {
  55. // 只能删除children中以compId作为key的组件
  56. if (parentComp.children?.[compId]) {
  57. delete parentComp.children[compId];
  58. deleteOK = true;
  59. }
  60. } else {
  61. const index = content.findIndex((id) => id === compId);
  62. if (index >= 0) {
  63. content.splice(index, 1);
  64. deleteOK = true;
  65. }
  66. }
  67. if (deleteOK) {
  68. const comp = this.helper.findComp(compId);
  69. const ids = flatCompChildIds(comp?.children || {});
  70. [compId, ...ids].forEach((id) => {
  71. delete compMap[id];
  72. });
  73. }
  74. },
  75. moveComp(selIndex: number, targetIndex: number) {
  76. const { content } = this.store.designData;
  77. const [selComp] = content.splice(selIndex, 1);
  78. content.splice(targetIndex, 0, selComp);
  79. },
  80. },
  81. });
  82. function flatCompChildIds(obj: object, ids: string[] = []) {
  83. Object.values(obj).forEach((item) => {
  84. if (item instanceof Object) {
  85. flatCompChildIds(item, ids);
  86. } else if (typeof item === "string") {
  87. ids.push(item);
  88. }
  89. });
  90. return ids;
  91. }