edit.ts 13 KB

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