edit.ts 9.1 KB

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