edit.ts 11 KB

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