import { Dict_Imgs } from "@/dict"; import { eachValuesAndPathsDeep, mapValuesDeep } from "@/utils"; import { PageListController } from "@queenjs/controllers"; import { set } from "lodash"; import { nanoid } from "nanoid"; import { Exception, ModuleControl } from "queenjs"; import { reactive } from "vue"; import * as basicUI from "../../components/CompUI/basicUI"; import * as customUI from "../../components/CompUI/customUI"; import { addCacheToMap, createCompId, setCompPid, } from "../../components/CompUI/defines/createCompId"; import { EditorModule } from "../../module"; import { DesignComp } from "../../objects/DesignTemp/DesignComp"; import { ICompKeys } from "../../typings"; interface CompItem { compKey: string; compType: "basic" | "senior" | "user"; name: string; thumbnail: string; Component: any; Form: any; createComp: (...args: any) => DesignComp; } export class CompUICtrl extends ModuleControl { state = reactive({ components: new Map(), }); init() { this.initDefaultUI(); this.initUserUI(); } private initDefaultUI() { Object.entries({ ...basicUI, ...customUI }).forEach(([key, value]) => { this.state.components.set(key, { compKey: key, compType: (basicUI as any)[key] ? "basic" : "senior", name: value.options.name, thumbnail: value.options.thumbnail, Component: value.Component, Form: value.Form, createComp: value.createComp, }); }); } private async initUserUI() { const listCtrl = new PageListController< { _id: string; title: string; thumbnail: string }, any >(this.module.config.httpConfig); listCtrl.setCrudPrefix("/frame"); listCtrl.state.size = 999; await listCtrl.loadPage(1); listCtrl.state.list.forEach((d) => { const compItem: CompItem = { compKey: d._id, compType: "user", name: d.title, thumbnail: d.thumbnail || Dict_Imgs.Default, Component: basicUI.Container.Component, Form: basicUI.Container.Form, createComp: basicUI.Container.createComp, }; this.state.components.set(d._id, compItem); }); } async initUserCompDetail(compKey: string) { const { compMap } = this.store.designData; const { result } = await this.https.getCompDetail(compKey); const idMap = new Map(); const nextCompMap: Record = {}; Object.entries(result.compMap as Record).forEach( ([key, comp]) => { if (key === "root") { comp.compKey = compKey as any; idMap.set(key, compKey); } const id = idMap.get(key) || nanoid(); idMap.set(key, id); comp.id = id; eachValuesAndPathsDeep( comp.children, (v) => typeof v === "string", (v, paths) => { const id = idMap.get(v) || nanoid(); idMap.set(v, id); set(comp.children, paths, id); } ); nextCompMap[id] = new DesignComp(comp); } ); Object.assign(compMap, nextCompMap); Object.values(nextCompMap).forEach((comp) => { const childIds = mapValuesDeep( comp.children, (v) => typeof v === "string", (v: string) => v ); childIds.forEach((id) => { const childComp = nextCompMap[id]; if (!childComp) { throw Exception.error("无效的childCompId"); } setCompPid(childComp, comp.id); }); }); return nextCompMap[idMap.get("root") as string]; } async createCompId(compKey: ICompKeys, parentCompId: string) { let compId = ""; const compItem = this.state.components.get(compKey); if (!compItem) throw Exception.error("无效的组件"); if (compItem.compType === "user") { const comp = await this.initUserCompDetail(compKey); setCompPid(comp, parentCompId); compId = comp.id; } else { compId = createCompId(compKey); addCacheToMap(this.store.designData.compMap, parentCompId); } return compId; } }