index.ts 6.4 KB

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