edit.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { cloneDeep, 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. moveComp(selIndex: number, targetIndex: number) {
  53. if (selIndex === targetIndex) return;
  54. this.store.moveComp(selIndex, targetIndex);
  55. },
  56. // 保存容器为组件
  57. async saveAsComp(comp: DesignComp) {
  58. try {
  59. // 组件封面
  60. const blob = await this.helper.screenshot({
  61. element: comp.$el,
  62. });
  63. const thumbnail = URL.createObjectURL(blob);
  64. const title = await queenApi.showInput({
  65. title: "保存到我的组件",
  66. defaultValue: this.controls.compUICtrl.state.components.get(
  67. comp.compKey
  68. )?.name,
  69. });
  70. const data = {
  71. title,
  72. thumbnail,
  73. compMap: cloneDeep(this.store.designData.compMap),
  74. };
  75. this.helper.clearUnusedComps(data.compMap, comp.id);
  76. data.compMap.root = data.compMap[comp.id];
  77. data.compMap.root.id = "root";
  78. delete data.compMap[comp.id];
  79. queenApi.showLoading("保存中");
  80. await this.controls.uploader.uploadBlobs(data);
  81. await this.https.createComp(data);
  82. queenApi.messageSuccess("保存成功");
  83. } catch (error: any) {
  84. throw Exception.error(error.toString());
  85. } finally {
  86. queenApi.hideLoading();
  87. }
  88. },
  89. // 保存项目
  90. async saveDesign() {
  91. try {
  92. // 清除无用组件
  93. this.helper.clearUnusedComps(this.store.designData.compMap);
  94. // 封面截屏
  95. if (!this.store.designData.thumbnail) {
  96. await this.actions.updateThumbnailByScreenshot();
  97. }
  98. queenApi.showLoading("保存中");
  99. await this.controls.uploader.uploadBlobs(this.store.designData);
  100. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  101. this.store.designData
  102. );
  103. queenApi.messageSuccess("保存成功");
  104. } catch (error: any) {
  105. throw Exception.error(error.toString());
  106. } finally {
  107. queenApi.hideLoading();
  108. }
  109. },
  110. // 截屏保存封面
  111. async updateThumbnailByScreenshot(autoSave?: boolean) {
  112. try {
  113. const rootComp = this.helper.findRootComp();
  114. if (!rootComp) return;
  115. queenApi.showLoading("截屏中");
  116. const blob = await this.helper.screenshot({
  117. element: rootComp.$el,
  118. ratio: this.store.isEditComp ? 0 : 1,
  119. });
  120. const thumbnail = URL.createObjectURL(blob);
  121. this.store.updateDesignThumbnail(thumbnail);
  122. if (autoSave) {
  123. await this.controls.uploader.uploadBlobs(this.store.designData);
  124. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  125. pick(this.store.designData, ["_id", "thumbnail"])
  126. );
  127. queenApi.messageSuccess("保存成功");
  128. }
  129. } catch (error: any) {
  130. throw Exception.error(error.toString());
  131. } finally {
  132. queenApi.hideLoading();
  133. }
  134. },
  135. // 设置组件变换
  136. setCompTransform(comp: DesignComp, transform: Layout["transform"]) {
  137. comp.layout.transform = transform;
  138. console.log(comp);
  139. },
  140. updateCompData(comp: DesignComp, path: string, value: any) {
  141. set(comp, path, value);
  142. },
  143. // 设置组件显示隐藏
  144. setCompPosition(comp: DesignComp) {
  145. comp.layout.position =
  146. comp.layout.position === "absolute" ? undefined : "absolute";
  147. },
  148. // 设置组件显示隐藏
  149. setCompVisible(comp: DesignComp) {
  150. comp.layout.visible = comp.layout.visible === false ? true : false;
  151. },
  152. // 清除组件变换
  153. clearCompTransform(comp: DesignComp) {
  154. comp.layout.margin = "";
  155. comp.layout.transform = undefined;
  156. },
  157. // 设置组件锁定状态
  158. setCompLock(comp: DesignComp) {
  159. console.log(comp);
  160. },
  161. // 设置组件层级
  162. setCompLayer(comp: DesignComp, offset: number) {
  163. comp.layout.zIndex = Math.min(
  164. Math.max((comp.layout.zIndex || 0) + offset, 0),
  165. 99
  166. );
  167. },
  168. // 宽度铺满
  169. fullCompWidth(comp: DesignComp) {
  170. comp.layout.size || (comp.layout.size = []);
  171. comp.layout.size[0] = 750;
  172. },
  173. //
  174. setCompAlign(comp: DesignComp, align: string) {
  175. comp.layout.alignSelf = align;
  176. if (comp.layout.transform?.x) {
  177. comp.layout.transform.x = 0;
  178. }
  179. },
  180. });