index.ts 3.7 KB

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