edit.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { pick, set } from "lodash";
  2. import { Exception, queenApi } from "queenjs";
  3. import { EditorModule } from "..";
  4. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  5. import { ICompKeys, Layout } from "../../typings";
  6. export const editActions = EditorModule.action({
  7. // 添加组件到画布
  8. async addCompToDesign(compKey: ICompKeys, index?: number) {
  9. let compId: string;
  10. if (
  11. index === undefined &&
  12. this.store.currComp?.compKey === "Container"
  13. // this.store.pageCompIds.includes(this.store.currComp.id)
  14. ) {
  15. compId = await this.store.insertCompContainer(
  16. compKey,
  17. this.store.currComp
  18. );
  19. this.actions.pickComp(compId);
  20. this.actions.setCompPosition(this.store.currComp);
  21. } else {
  22. compId = await this.store.insertDesignContent(compKey, index);
  23. this.actions.pickComp(compId);
  24. }
  25. },
  26. // 切换当前组件
  27. pickComp(compId: string) {
  28. // let nextCompId = compId;
  29. // if (this.store.isEditPage) {
  30. // const comps = this.helper.getCompTrees(compId);
  31. // nextCompId = comps[1].id;
  32. // }
  33. // if (compId !== this.store.currCompId) {
  34. this.store.setCurrComp(compId);
  35. // }
  36. },
  37. // 切换到父组件
  38. pickParentComp(compId: string) {
  39. const parentComp = this.helper.findParentComp(compId);
  40. parentComp && this.store.setCurrComp(parentComp.id);
  41. },
  42. // 删除组件
  43. removeComp(compId: string) {
  44. if (this.helper.isCompCanDelete(compId)) {
  45. if (compId === this.store.currCompId) {
  46. this.store.setCurrComp("root");
  47. }
  48. this.store.deleteComp(compId);
  49. }
  50. },
  51. // 保存容器为组件
  52. saveAsComp(comp: DesignComp) {
  53. // todo: 保存为组件
  54. console.log("comp: ", comp);
  55. },
  56. // 移动组件顺序
  57. moveComp(selIndex: number, targetIndex: number) {
  58. if (selIndex === targetIndex) return;
  59. this.store.moveComp(selIndex, targetIndex);
  60. },
  61. // 保存项目
  62. async saveDesign() {
  63. // 清除无用组件
  64. this.store.clearUnusedComps();
  65. // 封面截屏
  66. if (!this.store.designData.thumbnail) {
  67. await this.actions.updateThumbnailByScreenshot();
  68. }
  69. try {
  70. queenApi.showLoading("保存中");
  71. await this.controls.uploader.uploadBlobs(this.store.designData);
  72. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  73. this.store.designData
  74. );
  75. queenApi.messageSuccess("保存成功");
  76. } catch (error: any) {
  77. throw Exception.error(error.toString());
  78. } finally {
  79. queenApi.hideLoading();
  80. }
  81. },
  82. // 截屏保存封面
  83. async updateThumbnailByScreenshot(autoSave?: boolean) {
  84. try {
  85. queenApi.showLoading("截屏中");
  86. const blob = await this.helper.screenshot({
  87. ratio: this.store.isEditComp ? 0 : 1,
  88. });
  89. const thumbnail = URL.createObjectURL(blob);
  90. this.store.updateDesignThumbnail(thumbnail);
  91. if (autoSave) {
  92. await this.controls.uploader.uploadBlobs(this.store.designData);
  93. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  94. pick(this.store.designData, ["_id", "thumbnail"])
  95. );
  96. queenApi.messageSuccess("保存成功");
  97. }
  98. } catch (error: any) {
  99. throw Exception.error(error.toString());
  100. } finally {
  101. queenApi.hideLoading();
  102. }
  103. },
  104. // 设置组件变换
  105. setCompTransform(comp: DesignComp, transform: Layout["transform"]) {
  106. comp.layout.transform = transform;
  107. console.log(comp);
  108. },
  109. updateCompData(comp: DesignComp, path: string, value: any) {
  110. set(comp, path, value);
  111. },
  112. // 设置组件显示隐藏
  113. setCompPosition(comp: DesignComp) {
  114. comp.layout.position =
  115. comp.layout.position === "absolute" ? undefined : "absolute";
  116. },
  117. // 设置组件显示隐藏
  118. setCompVisible(comp: DesignComp) {
  119. comp.layout.visible = comp.layout.visible === false ? true : false;
  120. },
  121. // 清除组件变换
  122. clearCompTransform(comp: DesignComp) {
  123. comp.layout.margin = "";
  124. comp.layout.transform = undefined;
  125. },
  126. // 设置组件锁定状态
  127. setCompLock(comp: DesignComp) {
  128. console.log(comp);
  129. },
  130. // 设置组件层级
  131. setCompLayer(comp: DesignComp, offset: number) {
  132. comp.layout.zIndex = Math.min(
  133. Math.max((comp.layout.zIndex || 0) + offset, 0),
  134. 99
  135. );
  136. },
  137. // 宽度铺满
  138. fullCompWidth(comp: DesignComp) {
  139. comp.layout.size || (comp.layout.size = []);
  140. comp.layout.size[0] = 750;
  141. },
  142. //
  143. setCompAlign(comp: DesignComp, align: string) {
  144. comp.layout.alignSelf = align;
  145. if (comp.layout.transform?.x) {
  146. comp.layout.transform.x = 0;
  147. }
  148. },
  149. });