edit.ts 9.1 KB

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