edit.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if (this.store.currComp.compKey == "Text") {
  85. this.store.setTextEditingState(false);
  86. }
  87. this.store.setCurrComp(compId);
  88. if (this.store.currCompId == this.store.currStreamCardId) {
  89. this.controls.transferCtrl.destroy();
  90. }
  91. },
  92. // 切换到父组件
  93. pickParentComp(compId: string) {
  94. const parentComp = this.helper.findParentComp(compId);
  95. parentComp && this.store.setCurrComp(parentComp.id);
  96. },
  97. // 删除组件
  98. removeComp(compId: string) {
  99. if (this.helper.isCompCanDelete(compId)) {
  100. if (compId === this.store.currCompId) {
  101. this.store.setCurrComp("root");
  102. }
  103. this.store.deleteComp(compId);
  104. }
  105. },
  106. async removeStreamCard(compId: string) {
  107. await queenApi.showConfirm({ title: "删除", content: "确认删除当前内容?" });
  108. if (this.store.streamCardIds.length < 2) {
  109. return;
  110. }
  111. let nextCard = this.store.currStreamCardId;
  112. if (compId == this.store.currStreamCardId) {
  113. const i = this.store.streamCardIds.indexOf(compId);
  114. nextCard = this.store.streamCardIds[i + 1];
  115. if (!nextCard) {
  116. nextCard = this.store.streamCardIds[i - 1];
  117. }
  118. }
  119. this.store.deleteComp(compId);
  120. this.store.setCurrComp(nextCard);
  121. },
  122. // 移动组件顺序
  123. moveComp(selIndex: number, targetIndex: number) {
  124. if (selIndex === targetIndex) return;
  125. this.store.moveComp(selIndex, targetIndex);
  126. },
  127. // 保存容器为组件
  128. async saveAsComp(comp: DesignComp) {
  129. try {
  130. // 组件封面
  131. const blob = await new ScreenshotCtrl().snap({
  132. element: comp.$el,
  133. });
  134. const thumbnail = URL.createObjectURL(blob);
  135. const title = await queenApi.showInput({
  136. title: "保存到我的组件",
  137. defaultValue: this.controls.compUICtrl.state.components.get(
  138. comp.compKey
  139. )?.name,
  140. });
  141. const data = {
  142. title,
  143. thumbnail,
  144. compMap: cloneDeep(this.store.designData.compMap),
  145. };
  146. this.helper.clearUnusedComps(data.compMap, comp.id);
  147. data.compMap.root = data.compMap[comp.id];
  148. data.compMap.root.id = "root";
  149. delete data.compMap[comp.id];
  150. queenApi.showLoading("保存中");
  151. await this.controls.uploader.uploadBlobs(data);
  152. await this.https.createComp(data);
  153. queenApi.messageSuccess("保存成功");
  154. this.controls.compUICtrl.initUserUI();
  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. setCompPositionFloat(comp: DesignComp) {
  218. comp.layout.position = "absolute"
  219. },
  220. // 设置组件浮动
  221. setCompPosition(comp: DesignComp) {
  222. comp.layout.position =
  223. comp.layout.position === "absolute" ? undefined : "absolute";
  224. },
  225. // 设置组件显示隐藏
  226. setCompVisible(comp: DesignComp) {
  227. comp.layout.visible = comp.layout.visible === false ? true : false;
  228. },
  229. // 清除组件变换
  230. clearCompTransform(comp: DesignComp) {
  231. comp.layout.margin = "";
  232. comp.layout.transform = undefined;
  233. },
  234. // 设置组件锁定状态
  235. setCompLock(comp: DesignComp) {
  236. console.log(comp);
  237. },
  238. // 设置组件层级
  239. setCompLayer(comp: DesignComp, offset: number) {
  240. comp.layout.zIndex = Math.min(
  241. Math.max((comp.layout.zIndex || 0) + offset, 0),
  242. 99
  243. );
  244. },
  245. // 宽度铺满
  246. fullCompWidth(comp: DesignComp) {
  247. comp.layout.size || (comp.layout.size = []);
  248. comp.layout.size[0] = 750;
  249. },
  250. //
  251. setCompAlign(comp: DesignComp, align: string) {
  252. comp.layout.alignSelf = align;
  253. if (comp.layout.transform?.x) {
  254. comp.layout.transform.x = 0;
  255. }
  256. },
  257. // 开启组合模式
  258. enableGroupMode() {
  259. this.store.setGroupIds(
  260. this.store.currCompId ? [this.store.currCompId] : []
  261. );
  262. this.store.setGroupMode(true);
  263. },
  264. // 关闭组合模式
  265. async disableGroupMode() {
  266. const groupId = await this.controls.transferCtrl.groupCtrl.combineGroup();
  267. if (groupId) {
  268. this.store.setCurrComp(groupId);
  269. }
  270. this.store.setGroupIds([]);
  271. this.store.setGroupMode(false);
  272. },
  273. // 取消打组
  274. cancelGroupComps(groupComp: DesignComp) {
  275. this.controls.transferCtrl.groupCtrl.cancelGroup(groupComp);
  276. this.store.setCurrComp(groupComp.children.default?.[0] as string);
  277. },
  278. });