index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Dict_Imgs } from "@/dict";
  2. import { eachValuesAndPathsDeep, mapValuesDeep } from "@/utils";
  3. import { PageListController } from "@queenjs/controllers";
  4. import { set } from "lodash";
  5. import { nanoid } from "nanoid";
  6. import { Exception, ModuleControl } from "queenjs";
  7. import { reactive } from "vue";
  8. import * as basicUI from "../../components/CompUI/basicUI";
  9. import * as customUI from "../../components/CompUI/customUI";
  10. import {
  11. addCacheToMap,
  12. createCompId,
  13. setCompPid,
  14. } from "../../components/CompUI/defines/createCompId";
  15. import { EditorModule } from "../../module";
  16. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  17. import { ICompKeys } from "../../typings";
  18. interface CompItem {
  19. compKey: string;
  20. compType: "basic" | "senior" | "user";
  21. name: string;
  22. thumbnail: string;
  23. Component: any;
  24. Form: any;
  25. createComp: (...args: any) => DesignComp;
  26. }
  27. export class CompUICtrl extends ModuleControl<EditorModule> {
  28. state = reactive({
  29. components: new Map<string, CompItem>(),
  30. });
  31. init() {
  32. this.initDefaultUI();
  33. this.initUserUI();
  34. }
  35. private initDefaultUI() {
  36. Object.entries({ ...basicUI, ...customUI }).forEach(([key, value]) => {
  37. this.state.components.set(key, {
  38. compKey: key,
  39. compType: (basicUI as any)[key] ? "basic" : "senior",
  40. name: value.options.name,
  41. thumbnail: value.options.thumbnail,
  42. Component: value.Component,
  43. Form: value.Form,
  44. createComp: value.createComp,
  45. });
  46. });
  47. }
  48. private async initUserUI() {
  49. const listCtrl = new PageListController<
  50. { _id: string; title: string; thumbnail: string },
  51. any
  52. >(this.module.config.httpConfig);
  53. listCtrl.setCrudPrefix("/frame");
  54. listCtrl.state.size = 999;
  55. await listCtrl.loadPage(1);
  56. listCtrl.state.list.forEach((d) => {
  57. const compItem: CompItem = {
  58. compKey: d._id,
  59. compType: "user",
  60. name: d.title,
  61. thumbnail: d.thumbnail || Dict_Imgs.Default,
  62. Component: basicUI.Container.Component,
  63. Form: basicUI.Container.Form,
  64. createComp: basicUI.Container.createComp,
  65. };
  66. this.state.components.set(d._id, compItem);
  67. });
  68. }
  69. async initUserCompDetail(compKey: string) {
  70. const { compMap } = this.store.designData;
  71. const { result } = await this.https.getCompDetail(compKey);
  72. const idMap = new Map<string, string>();
  73. const nextCompMap: Record<string, DesignComp> = {};
  74. Object.entries(result.compMap as Record<string, DesignComp>).forEach(
  75. ([key, comp]) => {
  76. if (key === "root") {
  77. comp.compKey = compKey as any;
  78. idMap.set(key, compKey);
  79. }
  80. const id = idMap.get(key) || nanoid();
  81. idMap.set(key, id);
  82. comp.id = id;
  83. eachValuesAndPathsDeep(
  84. comp.children,
  85. (v) => typeof v === "string",
  86. (v, paths) => {
  87. const id = idMap.get(v) || nanoid();
  88. idMap.set(v, id);
  89. set(comp.children, paths, id);
  90. }
  91. );
  92. nextCompMap[id] = new DesignComp(comp);
  93. }
  94. );
  95. Object.assign(compMap, nextCompMap);
  96. Object.values(nextCompMap).forEach((comp) => {
  97. const childIds = mapValuesDeep(
  98. comp.children,
  99. (v) => typeof v === "string",
  100. (v: string) => v
  101. );
  102. childIds.forEach((id) => {
  103. const childComp = nextCompMap[id];
  104. if (!childComp) {
  105. throw Exception.error("无效的childCompId");
  106. }
  107. setCompPid(childComp, comp.id);
  108. });
  109. });
  110. return nextCompMap[idMap.get("root") as string];
  111. }
  112. async createCompId(compKey: ICompKeys, parentCompId: string) {
  113. let compId = "";
  114. const compItem = this.state.components.get(compKey);
  115. if (!compItem) throw Exception.error("无效的组件");
  116. if (compItem.compType === "user") {
  117. const comp = await this.initUserCompDetail(compKey);
  118. setCompPid(comp, parentCompId);
  119. compId = comp.id;
  120. } else {
  121. compId = createCompId(compKey);
  122. addCacheToMap(this.store.designData.compMap, parentCompId);
  123. }
  124. return compId;
  125. }
  126. }