index.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { nanoid } from "nanoid";
  2. import { EditorModule } from "..";
  3. import { DesignTemp } from "../../objects/DesignTemp";
  4. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  5. import { EditorMode, ICompKeys } from "../../typings";
  6. import { eachValuesAndPathsDeep } from "@/utils";
  7. import { set } from "lodash";
  8. export const store = EditorModule.store({
  9. state: () => ({
  10. textEditingState: false,
  11. mode: "editPage" as EditorMode,
  12. isWk: false, //作品集内作品
  13. currCompId: "root",
  14. currStreamCardId: "",
  15. designData: new DesignTemp(),
  16. groupModeStatus: false,
  17. groupIds: [] as string[],
  18. compPids: {} as Record<string, string>,
  19. selected: [] as string[], //选中的组件
  20. selectId: "", //选中的id唯一标识一次选中
  21. croppImage: "", //裁剪图片
  22. compEditMode: false, //组件编辑模式
  23. compEditReslut: 0 // -1 取消, 1 确定
  24. }),
  25. getters: {
  26. isEditMode(): boolean {
  27. return !this.store.isPreview;
  28. },
  29. isEditPage(state) {
  30. return state.mode === "editPage";
  31. },
  32. isEditComp(state) {
  33. return state.mode === "editComp";
  34. },
  35. isPreview(state) {
  36. return state.mode === "preview";
  37. },
  38. compMap(state) {
  39. return state.designData.compMap;
  40. },
  41. currComp(state) {
  42. return state.designData.compMap[state.currCompId];
  43. },
  44. currStreamCard(state) {
  45. return state.designData.compMap[state.currStreamCardId];
  46. },
  47. pageCompIds(state): string[] {
  48. return state.designData.compMap.root?.children.default || [];
  49. },
  50. streamCardIds(state): string[] {
  51. return state.designData.compMap.root?.children.default || [];
  52. },
  53. previewImageList(state) {
  54. const res: string[] = [];
  55. function deepChild(item: any) {
  56. if (typeof item == "string") {
  57. const comp = state.designData.compMap[item];
  58. if (comp.compKey === "Image") {
  59. res.push(comp.value.url);
  60. } else if (comp.children) {
  61. deepChild(comp.children);
  62. }
  63. } else if (item instanceof Object) {
  64. if (item instanceof Array) {
  65. item.forEach((d) => {
  66. deepChild(d);
  67. });
  68. } else {
  69. Object.values(item).forEach((d) => {
  70. deepChild(d);
  71. });
  72. }
  73. }
  74. }
  75. deepChild(state.designData.compMap["root"].children);
  76. return res;
  77. },
  78. },
  79. actions: {
  80. setCompData(id: string, data: any) {
  81. this.store.designData.compMap[id] = data;
  82. },
  83. setMode(v: EditorMode) {
  84. this.store.mode = v;
  85. },
  86. setWk(v: boolean) {
  87. this.store.isWk = v;
  88. },
  89. setGroupMode(status: boolean) {
  90. this.store.groupModeStatus = status;
  91. },
  92. setGroupIds(ids: string[]) {
  93. this.store.groupIds = ids;
  94. },
  95. setDesignData(data: Partial<DesignTemp>) {
  96. this.store.designData = new DesignTemp(data);
  97. },
  98. setCompPid(compId: string, pid: string) {
  99. this.store.compPids[compId] = pid;
  100. },
  101. async insertDesignContent(compKey: ICompKeys, index?: number) {
  102. const compId = await this.controls.compUICtrl.createCompId(compKey);
  103. const childIds = [...this.store.pageCompIds];
  104. index === undefined && (index = childIds.length);
  105. childIds.splice(index, 0, compId);
  106. this.store.designData.compMap.root.children.default = childIds;
  107. return compId;
  108. },
  109. async insertCompContainer(compKey: ICompKeys, container: DesignComp) {
  110. const compId = this.controls.compUICtrl.createCompId(compKey);
  111. const childIds = [...(container.children.default || [])];
  112. childIds.push(compId);
  113. container.children.default = childIds;
  114. return compId;
  115. },
  116. addUserCard(detail: any) {
  117. const { compMap } = this.store.designData;
  118. const idMap = new Map<string, string>();
  119. const nextCompMap: Record<string, DesignComp> = {};
  120. Object.entries(detail.compMap as Record<string, DesignComp>).forEach(
  121. ([key, comp]) => {
  122. if (key === "root") {
  123. idMap.set(key, nanoid());
  124. comp.title = detail.title;
  125. comp.thumbnail = detail.thumbnail;
  126. }
  127. const id = idMap.get(key) || nanoid();
  128. idMap.set(key, id);
  129. comp.id = id;
  130. eachValuesAndPathsDeep(
  131. comp.children,
  132. (v) => typeof v === "string",
  133. (v, paths) => {
  134. const id = idMap.get(v) || nanoid();
  135. idMap.set(v, id);
  136. set(comp.children, paths, id);
  137. }
  138. );
  139. nextCompMap[id] = new DesignComp(comp);
  140. }
  141. );
  142. Object.assign(compMap, nextCompMap);
  143. return nextCompMap[idMap.get("root") as string];
  144. },
  145. setCurrComp(compId: string) {
  146. this.store.currCompId = compId;
  147. const comps = this.helper.getCompTrees(compId);
  148. if (compId == "root") {
  149. return;
  150. }
  151. this.store.currStreamCardId = comps[1]?.id || "";
  152. },
  153. deleteComp(compId: string) {
  154. const { compMap } = this.store.designData;
  155. const parentComp = this.helper.findParentComp(compId);
  156. let deleteOK = false;
  157. if (parentComp) {
  158. const ids = [...(parentComp.children.default || [])];
  159. // 只能删除children.default中的组件
  160. if (ids?.includes(compId)) {
  161. const index = ids.findIndex((id) => id === compId);
  162. if (index >= 0) {
  163. ids.splice(index, 1);
  164. parentComp.children.default = ids;
  165. deleteOK = true;
  166. }
  167. }
  168. }
  169. if (deleteOK) {
  170. const comp = this.helper.findComp(compId) as DesignComp;
  171. const ids = comp.getChildIds();
  172. [compId, ...ids].forEach((id) => {
  173. delete compMap[id];
  174. });
  175. delete this.store.compPids[compId];
  176. }
  177. },
  178. moveComp(selIndex: number, targetIndex: number) {
  179. const pageCompIds = [...this.store.pageCompIds];
  180. const [selComp] = pageCompIds.splice(selIndex, 1);
  181. pageCompIds.splice(targetIndex, 0, selComp);
  182. this.store.designData.compMap.root.children.default = pageCompIds;
  183. },
  184. setTextEditingState(state: boolean) {
  185. this.store.textEditingState = state;
  186. },
  187. setDesignThumbnail(url: string) {
  188. this.store.designData.thumbnail = url;
  189. },
  190. },
  191. });