edit.tsx 14 KB

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