index.ts 3.7 KB

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