index.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { ModuleControl } from "queenjs";
  2. import { onMounted, ref } from "vue";
  3. import { EditorModule } from "../../module";
  4. import { RxValue } from "../ReactCtrl/rxValue";
  5. import { ObjsContainer } from "../SelectCtrl/ObjsContainer";
  6. import { DesignTemp } from "../../objects/DesignTemp";
  7. import { CompPageObj } from "../../components/CompUI/basicUI/Page";
  8. import { createObj, history } from "../../objects/DesignTemp/factory";
  9. import { ICompKeys } from "../../typings";
  10. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  11. import { nanoid } from "nanoid";
  12. import { eachValuesAndPathsDeep } from "@/utils";
  13. import { set } from "lodash";
  14. export class PageCtrl extends ModuleControl<EditorModule> {
  15. state = RxValue.create({
  16. currCompId: "root",
  17. currStreamCardId: "",
  18. rootId: "",
  19. title: ''
  20. }, history)
  21. initEvent() {
  22. this.rootPage.children.default.forEach(cid=>{
  23. const card = this.compMap[cid];
  24. let first = true;
  25. this.setCompPid(cid, this.rootPage.id);
  26. card.children.onDefaultChanged((childs)=>{
  27. if (first) {
  28. first = false;
  29. return;
  30. }
  31. childs.forEach(c=>{
  32. this.setCompPid(c, card.id);
  33. })
  34. this.helper.extendStreamCard(cid);
  35. })
  36. })
  37. }
  38. designData = {} as DesignTemp
  39. compPids = {} as Record<string, string>;
  40. toJson() {
  41. const out:any = {
  42. _id: this.designData._id,
  43. version: this.designData.version,
  44. title: this.designData.title,
  45. desc: this.designData.desc,
  46. pageStyle: this.designData.pageStyle,
  47. content: this.designData.content,
  48. thumbnail: this.designData.thumbnail,
  49. compScreenMap: this.designData.compScreenMap,
  50. }
  51. const keys = Object.keys(this.designData.compMap);
  52. const compMap :any = {};
  53. keys.forEach(k=>{
  54. compMap[k] = this.designData.compMap[k].toJson();
  55. })
  56. out.compMap = compMap;
  57. console.log(out);
  58. return out;
  59. }
  60. setCompPid(compId: string, pid: string) {
  61. this.compPids[compId] = pid;
  62. }
  63. get compMap() {
  64. return this.designData.compMap || {};
  65. }
  66. get currComp() {
  67. return this.compMap[this.state.currCompId];
  68. }
  69. get currStreamCard() {
  70. return this.compMap[this.state.currStreamCardId];
  71. }
  72. get streamCardIds(): string[] {
  73. return this.rootPage?.children.default || [];
  74. }
  75. setDesignData(data: Partial<DesignTemp>) {
  76. this.designData = new DesignTemp(data);
  77. this.state.title = data.title || '';
  78. //设置组件父子关系
  79. const ite = (root:any)=> {
  80. const cards = root.children?.default || [];
  81. cards.forEach((c:string)=>{
  82. this.setCompPid(c, root.id);
  83. const r = this.compMap[c];
  84. if (r) {
  85. ite(r);
  86. }
  87. })
  88. }
  89. ite(this.designData.compMap.root);
  90. this.state.rootId = "root";
  91. this.state.currStreamCardId = this.streamCardIds[0];
  92. this.initEvent();
  93. this.controls.propsCtrl.state.propId = "root";
  94. setTimeout(() => {
  95. history.enable = true;
  96. }, 1000);
  97. }
  98. get rootPage() {
  99. return this.compMap[this.state.rootId] as CompPageObj;
  100. }
  101. setCurrComp(compId: string) {
  102. if (compId == "") compId = "root";
  103. this.state.setCurrCompId(compId);
  104. if (compId == "root") {
  105. return;
  106. }
  107. const comps = this.helper.getCompTrees(compId);
  108. let cardId = comps[1]?.id || "";
  109. if (this.helper.isStreamCard(compId)) {
  110. cardId = compId;
  111. }
  112. this.state.setCurrStreamCardId(cardId);
  113. }
  114. findParentComp(compId: string): DesignComp | undefined {
  115. const comp = this.compMap[compId];
  116. if (comp) return this.compMap[this.compPids[compId]];
  117. }
  118. deleteComp(compId: string) {
  119. const parentComp = this.findParentComp(compId);
  120. let deleteOK = false;
  121. if (parentComp) {
  122. const ids = [...(parentComp.children.default || [])];
  123. // 只能删除children.default中的组件
  124. if (ids?.includes(compId)) {
  125. const index = ids.findIndex((id) => id === compId);
  126. if (index >= 0) {
  127. ids.splice(index, 1);
  128. parentComp.children.setDefault(ids);
  129. deleteOK = true;
  130. }
  131. }
  132. }
  133. if (deleteOK) {
  134. delete this.compPids[compId];
  135. }
  136. }
  137. insertDesignCard(index?: number) {
  138. const card = createObj({compKey: "Container"}, false);
  139. this.setCompPid(card.id, this.rootPage.id);
  140. this.compMap[card.id] = card;
  141. const childIds = [...this.rootPage.children.default];
  142. index === undefined && (index = childIds.length);
  143. childIds.splice(index, 0, card.id);
  144. this.rootPage.children.setDefault(childIds);
  145. card.children.onDefaultChanged(()=>{
  146. this.helper.extendStreamCard(card.id);
  147. })
  148. return card.id;
  149. }
  150. insertCompContainer(compKey: ICompKeys, container: DesignComp) {
  151. const compId = this.controls.compUICtrl.createCompId(compKey);
  152. this.setCompPid(compId, container.id);
  153. const childIds = [...(container.children.default || [])];
  154. childIds.push(compId);
  155. container.children.setDefault(childIds);
  156. return compId;
  157. }
  158. moveComp(selIndex: number, targetIndex: number) {
  159. const pageCompIds = [...this.streamCardIds];
  160. const [selComp] = pageCompIds.splice(selIndex, 1);
  161. pageCompIds.splice(targetIndex, 0, selComp);
  162. this.rootPage.children.setDefault(pageCompIds );
  163. history.submit();
  164. }
  165. addUserCard(detail: any) {
  166. const { compMap } = this.controls.pageCtrl.designData;
  167. const idMap = new Map<string, string>();
  168. const nextCompMap: Record<string, DesignComp> = {};
  169. Object.entries(detail.compMap as Record<string, DesignComp>).forEach(
  170. ([key, comp]) => {
  171. if (key === "root") {
  172. idMap.set(key, nanoid());
  173. comp.title = detail.title;
  174. comp.thumbnail = detail.thumbnail;
  175. }
  176. const newPid = idMap.get(key) || nanoid();
  177. idMap.set(key, newPid);
  178. comp.id = newPid;
  179. eachValuesAndPathsDeep(
  180. comp.children,
  181. (v) => typeof v === "string",
  182. (v, paths) => {
  183. const id = idMap.get(v) || nanoid();
  184. idMap.set(v, id);
  185. this.setCompPid(id, newPid)
  186. set(comp.children, paths, id);
  187. }
  188. );
  189. nextCompMap[newPid] = createObj(comp);
  190. }
  191. );
  192. Object.assign(compMap, nextCompMap);
  193. return nextCompMap[idMap.get("root") as string];
  194. }
  195. setDesignThumbnail(url: string) {
  196. this.designData.thumbnail = url;
  197. }
  198. }