edit.ts 10 KB

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