index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { EditorModule } from "..";
  2. import { DesignTemp } from "../../objects/DesignTemp";
  3. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  4. import { EditorMode, ICompKeys } from "../../typings";
  5. export const store = EditorModule.store({
  6. state: () => ({
  7. textEditingState: false,
  8. mode: "editPage" as EditorMode,
  9. currCompId: "root",
  10. currStreamCardId: "",
  11. designData: new DesignTemp(),
  12. groupModeStatus: false,
  13. groupIds: [] as string[],
  14. compPids: {} as Record<string, string>,
  15. }),
  16. getters: {
  17. isEditMode(): boolean {
  18. return !this.store.isPreview;
  19. },
  20. isEditPage(state) {
  21. return state.mode === "editPage";
  22. },
  23. isEditComp(state) {
  24. return state.mode === "editComp";
  25. },
  26. isPreview(state) {
  27. return state.mode === "preview";
  28. },
  29. compMap(state) {
  30. return state.designData.compMap;
  31. },
  32. currComp(state) {
  33. return state.designData.compMap[state.currCompId];
  34. },
  35. currStreamCard(state) {
  36. return state.designData.compMap[state.currStreamCardId];
  37. },
  38. pageCompIds(state): string[] {
  39. return state.designData.compMap.root?.children.default || [];
  40. },
  41. streamCardIds(state): string[] {
  42. return state.designData.compMap.root?.children.default || [];
  43. },
  44. },
  45. actions: {
  46. setCompData(id: string, data: any) {
  47. this.store.designData.compMap[id] = data;
  48. },
  49. setMode(v: EditorMode) {
  50. this.store.mode = v;
  51. },
  52. setGroupMode(status: boolean) {
  53. this.store.groupModeStatus = status;
  54. },
  55. setGroupIds(ids: string[]) {
  56. this.store.groupIds = ids;
  57. },
  58. setDesignData(data: Partial<DesignTemp>) {
  59. this.store.designData = new DesignTemp(data);
  60. },
  61. setCompPid(compId: string, pid: string) {
  62. this.store.compPids[compId] = pid;
  63. },
  64. async insertDesignContent(compKey: ICompKeys, index?: number) {
  65. const { pageCompIds } = this.store;
  66. index === undefined && (index = pageCompIds.length);
  67. const compId = await this.controls.compUICtrl.createCompId(compKey);
  68. pageCompIds.splice(index, 0, compId);
  69. return compId;
  70. },
  71. async insertCompContainer(compKey: ICompKeys, container: DesignComp) {
  72. const compId = await this.controls.compUICtrl.createCompId(compKey);
  73. const childIds = [...(container.children.default || [])];
  74. childIds.push(compId);
  75. container.children.default = childIds;
  76. return compId;
  77. },
  78. setCurrComp(compId: string) {
  79. if (compId != "root") {
  80. this.store.currStreamCardId = "";
  81. }
  82. this.store.currCompId = compId;
  83. const comps = this.helper.getCompTrees(compId);
  84. this.store.currStreamCardId = comps[1]?.id || ''
  85. },
  86. deleteComp(compId: string) {
  87. const { compMap } = this.store.designData;
  88. const parentComp = this.helper.findParentComp(compId);
  89. let deleteOK = false;
  90. if (parentComp) {
  91. const ids = [...parentComp.children.default || []];
  92. // 只能删除children.default中的组件
  93. if (ids?.includes(compId)) {
  94. const index = ids.findIndex((id) => id === compId);
  95. if (index >= 0) {
  96. ids.splice(index, 1);
  97. parentComp.children.default = ids;
  98. deleteOK = true;
  99. }
  100. }
  101. }
  102. if (deleteOK) {
  103. const comp = this.helper.findComp(compId) as DesignComp;
  104. const ids = comp.getChildIds();
  105. [compId, ...ids].forEach((id) => {
  106. delete compMap[id];
  107. });
  108. delete this.store.compPids[compId];
  109. }
  110. },
  111. moveComp(selIndex: number, targetIndex: number) {
  112. const { pageCompIds } = this.store;
  113. const [selComp] = pageCompIds.splice(selIndex, 1);
  114. pageCompIds.splice(targetIndex, 0, selComp);
  115. },
  116. setTextEditingState(state: boolean) {
  117. this.store.textEditingState = state;
  118. },
  119. setDesignThumbnail(url: string) {
  120. this.store.designData.thumbnail = url;
  121. },
  122. },
  123. });