edit.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { cloneDeep, pick, set } from "lodash";
  2. import { Exception, queenApi } from "queenjs";
  3. import { EditorModule } from "..";
  4. import { ScreenshotCtrl } from "../../controllers/ScreenshotCtrl";
  5. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  6. import { ICompKeys, Layout } from "../../typings";
  7. export const editActions = EditorModule.action({
  8. // 通过拖拽添加组件到画布
  9. async dragCompToDesign(event: MouseEvent, compKey: ICompKeys) {
  10. await this.actions.addCompToDesign(compKey);
  11. const cardPoints = this.helper.getPointOffsetWith(
  12. event,
  13. this.store.currStreamCard.$el
  14. );
  15. const { currComp } = this.store;
  16. currComp.translate(
  17. 375 - (currComp.layout.size?.[0] || 750) / 2,
  18. this.helper.pxToDesignSize(cardPoints.y)
  19. );
  20. },
  21. // 通过点击添加组件到画布
  22. async clickCompToDesign(compKey: ICompKeys) {
  23. await this.actions.addCompToDesign(compKey);
  24. const { currStreamCard, currComp } = this.store;
  25. const y = currStreamCard.getH();
  26. currStreamCard.setH(y + currComp.getH());
  27. currComp.translate(0, y);
  28. },
  29. // 添加组件到画布
  30. async addCompToDesign(compKey: ICompKeys, index?: number) {
  31. if (!this.store.currStreamCardId) {
  32. //必须选中一个streamCard
  33. return;
  34. }
  35. if (compKey == "Container") {
  36. index = this.store.streamCardIds.indexOf(this.store.currStreamCardId) + 1;
  37. const compId = await this.store.insertDesignContent(compKey, index);
  38. this.actions.pickComp(compId);
  39. return;
  40. }
  41. // if (
  42. // index === undefined &&
  43. // this.store.currComp?.compKey === "Container"
  44. // // this.store.pageCompIds.includes(this.store.currComp.id)
  45. // ) {
  46. const compId = await this.store.insertCompContainer(
  47. compKey,
  48. this.store.currStreamCard
  49. );
  50. this.actions.pickComp(compId);
  51. this.actions.setCompPosition(this.store.currComp);
  52. // } else {
  53. // compId = await this.store.insertDesignContent(compKey, index);
  54. // this.actions.pickComp(compId);
  55. // }
  56. },
  57. // 切换当前组件
  58. pickComp(compId: string) {
  59. const { store, helper } = this;
  60. // 组合模式下,切换组件
  61. if (store.currCompId && store.groupModeStatus) {
  62. const enableGroupIds = helper
  63. .findParentComp(compId)
  64. ?.getChildIds() as string[];
  65. const comps = helper.getCompTrees(compId);
  66. while (comps.length) {
  67. const comp = comps.pop() as DesignComp;
  68. const index = store.groupIds.indexOf(comp.id);
  69. if (index >= 0) {
  70. const groupIds = [...store.groupIds];
  71. groupIds.splice(index, 1);
  72. store.setGroupIds(groupIds);
  73. } else if (enableGroupIds.includes(comp.id)) {
  74. store.groupIds.push(comp.id);
  75. return;
  76. }
  77. }
  78. return;
  79. }
  80. // let nextCompId = compId;
  81. // if (this.store.isEditPage) {
  82. // const comps = this.helper.getCompTrees(compId);
  83. // nextCompId = comps[1].id;
  84. // }
  85. if (this.store.currCompId == compId) {
  86. return;
  87. }
  88. this.store.setCurrComp(compId);
  89. if (this.store.currCompId == this.store.currStreamCardId) {
  90. this.controls.transferCtrl.destroy();
  91. }
  92. },
  93. // 切换到父组件
  94. pickParentComp(compId: string) {
  95. const parentComp = this.helper.findParentComp(compId);
  96. parentComp && this.store.setCurrComp(parentComp.id);
  97. },
  98. // 删除组件
  99. removeComp(compId: string) {
  100. if (this.helper.isCompCanDelete(compId)) {
  101. if (compId === this.store.currCompId) {
  102. this.store.setCurrComp("root");
  103. }
  104. this.store.deleteComp(compId);
  105. }
  106. },
  107. async removeStreamCard(compId: string) {
  108. await queenApi.showConfirm({ title: "删除", content: "确认删除当前内容?" });
  109. if (this.store.streamCardIds.length < 2) {
  110. return;
  111. }
  112. let nextCard = this.store.currStreamCardId;
  113. if (compId == this.store.currStreamCardId) {
  114. const i = this.store.streamCardIds.indexOf(compId);
  115. nextCard = this.store.streamCardIds[i + 1];
  116. if (!nextCard) {
  117. nextCard = this.store.streamCardIds[i - 1];
  118. }
  119. }
  120. this.store.deleteComp(compId);
  121. this.store.setCurrComp(nextCard);
  122. },
  123. // 移动组件顺序
  124. moveComp(selIndex: number, targetIndex: number) {
  125. if (selIndex === targetIndex) return;
  126. this.store.moveComp(selIndex, targetIndex);
  127. },
  128. // 保存容器为组件
  129. async saveAsComp(comp: DesignComp) {
  130. try {
  131. // 组件封面
  132. const blob = await new ScreenshotCtrl().snap({
  133. element: comp.$el,
  134. });
  135. const thumbnail = URL.createObjectURL(blob);
  136. const title = await queenApi.showInput({
  137. title: "保存到我的组件",
  138. defaultValue: this.controls.compUICtrl.state.components.get(
  139. comp.compKey
  140. )?.name,
  141. });
  142. const data = {
  143. title,
  144. thumbnail,
  145. compMap: cloneDeep(this.store.designData.compMap),
  146. };
  147. this.helper.clearUnusedComps(data.compMap, comp.id);
  148. data.compMap.root = data.compMap[comp.id];
  149. data.compMap.root.id = "root";
  150. delete data.compMap[comp.id];
  151. queenApi.showLoading("保存中");
  152. await this.controls.uploader.uploadBlobs(data);
  153. await this.https.createComp(data);
  154. queenApi.messageSuccess("保存成功");
  155. } catch (error: any) {
  156. throw Exception.error(error.toString());
  157. } finally {
  158. queenApi.hideLoading();
  159. }
  160. },
  161. // 保存项目
  162. async saveDesign() {
  163. try {
  164. // 清除无用组件
  165. this.helper.clearUnusedComps(this.store.designData.compMap);
  166. // 封面截屏
  167. if (!this.store.designData.thumbnail) {
  168. await this.actions.updateThumbnailByScreenshot();
  169. }
  170. queenApi.showLoading("保存中");
  171. await this.controls.uploader.uploadBlobs(this.store.designData);
  172. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  173. this.store.designData
  174. );
  175. queenApi.messageSuccess("保存成功");
  176. } catch (error: any) {
  177. throw Exception.error(error.toString());
  178. } finally {
  179. queenApi.hideLoading();
  180. }
  181. },
  182. // 截屏保存封面
  183. async updateThumbnailByScreenshot(autoSave?: boolean) {
  184. try {
  185. const rootComp = this.helper.findRootComp();
  186. if (!rootComp) return;
  187. queenApi.showLoading("截屏中");
  188. const blob = await new ScreenshotCtrl().snap({
  189. element: rootComp.$el,
  190. ratio: this.store.isEditComp ? 0 : 1,
  191. });
  192. const thumbnail = URL.createObjectURL(blob);
  193. this.store.setDesignThumbnail(thumbnail);
  194. if (autoSave) {
  195. await this.controls.uploader.uploadBlobs(this.store.designData);
  196. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  197. pick(this.store.designData, ["_id", "thumbnail"])
  198. );
  199. queenApi.messageSuccess("保存成功");
  200. }
  201. } catch (error: any) {
  202. throw Exception.error(error.toString());
  203. } finally {
  204. queenApi.hideLoading();
  205. }
  206. },
  207. // 设置组件变换
  208. setCompTransform(comp: DesignComp, transform: Layout["transform"]) {
  209. if (!comp) return;
  210. comp.layout.transform = transform;
  211. console.log(comp);
  212. },
  213. updateCompData(comp: DesignComp, path: string, value: any) {
  214. set(comp, path, value);
  215. },
  216. // 设置组件浮动
  217. setCompPosition(comp: DesignComp) {
  218. comp.layout.position =
  219. comp.layout.position === "absolute" ? undefined : "absolute";
  220. },
  221. // 设置组件显示隐藏
  222. setCompVisible(comp: DesignComp) {
  223. comp.layout.visible = comp.layout.visible === false ? true : false;
  224. },
  225. // 清除组件变换
  226. clearCompTransform(comp: DesignComp) {
  227. comp.layout.margin = "";
  228. comp.layout.transform = undefined;
  229. },
  230. // 设置组件锁定状态
  231. setCompLock(comp: DesignComp) {
  232. console.log(comp);
  233. },
  234. // 设置组件层级
  235. setCompLayer(comp: DesignComp, offset: number) {
  236. comp.layout.zIndex = Math.min(
  237. Math.max((comp.layout.zIndex || 0) + offset, 0),
  238. 99
  239. );
  240. },
  241. // 宽度铺满
  242. fullCompWidth(comp: DesignComp) {
  243. comp.layout.size || (comp.layout.size = []);
  244. comp.layout.size[0] = 750;
  245. },
  246. //
  247. setCompAlign(comp: DesignComp, align: string) {
  248. comp.layout.alignSelf = align;
  249. if (comp.layout.transform?.x) {
  250. comp.layout.transform.x = 0;
  251. }
  252. },
  253. // 开启组合模式
  254. enableGroupMode() {
  255. this.store.setGroupIds(
  256. this.store.currCompId ? [this.store.currCompId] : []
  257. );
  258. this.store.setGroupMode(true);
  259. },
  260. // 关闭组合模式
  261. async disableGroupMode() {
  262. const groupId = await this.controls.transferCtrl.groupCtrl.combineGroup();
  263. if (groupId) {
  264. this.store.setCurrComp(groupId);
  265. }
  266. this.store.setGroupIds([]);
  267. this.store.setGroupMode(false);
  268. },
  269. // 取消打组
  270. cancelGroupComps(groupComp: DesignComp) {
  271. this.controls.transferCtrl.groupCtrl.cancelGroup(groupComp);
  272. this.store.setCurrComp(groupComp.children.default?.[0] as string);
  273. },
  274. });