index.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. lastSelected: "", //最后上传
  25. shortPage: {
  26. index: 0,
  27. offset: 0,
  28. isMoving: false,
  29. },
  30. }),
  31. getters: {
  32. rootPage(state) {
  33. return state.designData.compMap["root"] as DesignComp;
  34. },
  35. isEditMode(): boolean {
  36. return !this.store.isPreview && !this.store.isDisplay;
  37. },
  38. isEditPage(state) {
  39. return state.mode === "editPage";
  40. },
  41. isEditComp(state) {
  42. return state.mode === "editComp";
  43. },
  44. isPreview(state) {
  45. return state.mode === "preview";
  46. },
  47. isDisplay(state) {
  48. return state.mode === "display";
  49. },
  50. compMap(state) {
  51. return state.designData.compMap;
  52. },
  53. currComp(state) {
  54. return state.designData.compMap[state.currCompId];
  55. },
  56. currStreamCard(state) {
  57. return state.designData.compMap[state.currStreamCardId];
  58. },
  59. pageCompIds(state): string[] {
  60. return state.designData.compMap.root?.children.default || [];
  61. },
  62. streamCardIds(state): string[] {
  63. return state.designData.compMap.root?.children.default || [];
  64. },
  65. previewImageList(state) {
  66. const res: string[] = [];
  67. function deepChild(item: any) {
  68. if (typeof item == "string") {
  69. const comp = state.designData.compMap[item];
  70. if (comp.compKey === "Image") {
  71. res.push(comp.value.url);
  72. } else if (comp.children) {
  73. deepChild(comp.children);
  74. }
  75. } else if (item instanceof Object) {
  76. if (item instanceof Array) {
  77. item.forEach((d) => {
  78. deepChild(d);
  79. });
  80. } else {
  81. Object.values(item).forEach((d) => {
  82. deepChild(d);
  83. });
  84. }
  85. }
  86. }
  87. deepChild(state.designData.compMap["root"].children);
  88. return res;
  89. },
  90. },
  91. actions: {
  92. setCompData(id: string, data: any) {
  93. this.store.designData.compMap[id] = data;
  94. },
  95. setMode(v: EditorMode) {
  96. this.store.mode = v;
  97. },
  98. setWk(v: boolean) {
  99. this.store.isWk = v;
  100. },
  101. setGroupMode(status: boolean) {
  102. this.store.groupModeStatus = status;
  103. },
  104. setGroupIds(ids: string[]) {
  105. this.store.groupIds = ids;
  106. },
  107. setDesignData(data: Partial<DesignTemp>) {
  108. this.store.designData = new DesignTemp(data);
  109. },
  110. setCompPid(compId: string, pid: string) {
  111. this.store.compPids[compId] = pid;
  112. },
  113. async insertDesignContent(compKey: ICompKeys, index?: number) {
  114. const compId = await this.controls.compUICtrl.createCompId(compKey);
  115. const childIds = [...this.store.pageCompIds];
  116. index === undefined && (index = childIds.length);
  117. childIds.splice(index, 0, compId);
  118. this.store.designData.compMap.root.children.default = childIds;
  119. return compId;
  120. },
  121. async insertCompContainer(compKey: ICompKeys, container: DesignComp) {
  122. const compId = this.controls.compUICtrl.createCompId(compKey);
  123. const childIds = [...(container.children.default || [])];
  124. childIds.push(compId);
  125. container.children.default = childIds;
  126. return compId;
  127. },
  128. addUserCard(detail: any) {
  129. const { compMap } = this.store.designData;
  130. const idMap = new Map<string, string>();
  131. const nextCompMap: Record<string, DesignComp> = {};
  132. Object.entries(detail.compMap as Record<string, DesignComp>).forEach(
  133. ([key, comp]) => {
  134. if (key === "root") {
  135. idMap.set(key, nanoid());
  136. comp.title = detail.title;
  137. comp.thumbnail = detail.thumbnail;
  138. }
  139. const id = idMap.get(key) || nanoid();
  140. idMap.set(key, id);
  141. comp.id = id;
  142. eachValuesAndPathsDeep(
  143. comp.children,
  144. (v) => typeof v === "string",
  145. (v, paths) => {
  146. const id = idMap.get(v) || nanoid();
  147. idMap.set(v, id);
  148. set(comp.children, paths, id);
  149. }
  150. );
  151. nextCompMap[id] = new DesignComp(comp);
  152. }
  153. );
  154. Object.assign(compMap, nextCompMap);
  155. return nextCompMap[idMap.get("root") as string];
  156. },
  157. setCurrComp(compId: string) {
  158. this.store.currCompId = compId;
  159. const comps = this.helper.getCompTrees(compId);
  160. if (compId == "root") {
  161. return;
  162. }
  163. let cardId = comps[1]?.id || "";
  164. if (this.helper.isStreamCard(compId)) {
  165. cardId = compId;
  166. }
  167. this.store.currStreamCardId = cardId;
  168. },
  169. deleteComp(compId: string) {
  170. const parentComp = this.helper.findParentComp(compId);
  171. let deleteOK = false;
  172. if (parentComp) {
  173. const ids = [...(parentComp.children.default || [])];
  174. // 只能删除children.default中的组件
  175. if (ids?.includes(compId)) {
  176. const index = ids.findIndex((id) => id === compId);
  177. if (index >= 0) {
  178. ids.splice(index, 1);
  179. parentComp.children.default = ids;
  180. deleteOK = true;
  181. }
  182. }
  183. }
  184. if (deleteOK) {
  185. delete this.store.compPids[compId];
  186. }
  187. },
  188. moveComp(selIndex: number, targetIndex: number) {
  189. const pageCompIds = [...this.store.pageCompIds];
  190. const [selComp] = pageCompIds.splice(selIndex, 1);
  191. pageCompIds.splice(targetIndex, 0, selComp);
  192. this.store.designData.compMap.root.children.default = pageCompIds;
  193. },
  194. setTextEditingState(state: boolean) {
  195. this.store.textEditingState = state;
  196. },
  197. setDesignThumbnail(url: string) {
  198. this.store.designData.thumbnail = url;
  199. },
  200. },
  201. });