edit.tsx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. import { cloneDeep, pick } from "lodash";
  2. import { nanoid } from "nanoid";
  3. import { Exception, queenApi } from "queenjs";
  4. import { EditorModule } from "..";
  5. import { ScreenshotCtrl } from "../../controllers/ScreenshotCtrl";
  6. import { ObjsContainer } from "../../controllers/SelectCtrl/ObjsContainer";
  7. import { CompObject } from "../../controllers/SelectCtrl/compObj";
  8. import { Matrix } from "../../controllers/SelectCtrl/matrix";
  9. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  10. import { ICompKeys, Layout } from "../../typings";
  11. const ctrlState = {
  12. selected: [] as string[],
  13. type: 0, // 1 ctrlc 2 ctrlx
  14. cardId: "", //当前的卡片Id
  15. screenId: "", //屏幕Id
  16. selWidth: 0,
  17. selX: 0,
  18. selY: 0,
  19. };
  20. export const editActions = EditorModule.action({
  21. pickComp(compId: string, selected = true) {
  22. if (compId == "") {
  23. //空的时候,就选择根页面
  24. compId = "root";
  25. }
  26. const selectCardChild = (id: string) => {
  27. const paths = this.helper.getCompTrees(id);
  28. const cardChilds = paths[2];
  29. if (cardChilds) {
  30. this.actions.selectObjs([cardChilds.id]);
  31. } else {
  32. this.actions.selectObjs([]);
  33. if (id != "root") {
  34. this.store.setCurrComp(this.store.currStreamCardId);
  35. }
  36. }
  37. };
  38. if (this.store.currCompId == compId) {
  39. return;
  40. }
  41. this.store.setCurrComp(compId);
  42. if (selected) {
  43. selectCardChild(compId);
  44. }
  45. },
  46. // 通过点击添加组件到画布
  47. async clickCompToDesign(compKey: ICompKeys, cb?: (comp: DesignComp) => void) {
  48. if (!this.store.currStreamCardId) {
  49. queenApi.messageError("请先选中一个卡片");
  50. return;
  51. }
  52. //点击默认都创建一个容器
  53. //创建容器
  54. const isCreatCard =
  55. compKey != "Text" &&
  56. compKey != "Image" &&
  57. compKey != "Rectage" &&
  58. compKey != "Line" &&
  59. compKey != "Arc" &&
  60. compKey != "Triangle" &&
  61. compKey != "Ellipse" &&
  62. compKey != "Polygon" &&
  63. compKey != "PolygonNormal" &&
  64. compKey != "Curve";
  65. let yOffset = 0;
  66. if (
  67. this.store.currCompId != this.store.currStreamCardId &&
  68. !isCreatCard &&
  69. this.store.currCompId != "root"
  70. ) {
  71. const bound = this.helper.getCardCompBound(this.store.currCompId);
  72. yOffset = bound.y + bound.h;
  73. }
  74. let currCard = this.store.currStreamCard;
  75. if (isCreatCard) {
  76. //先创建卡片
  77. const currCardIndex =
  78. this.store.streamCardIds.indexOf(this.store.currStreamCardId) + 1;
  79. const compId = await this.store.insertDesignContent(
  80. "Container",
  81. currCardIndex
  82. );
  83. currCard = this.helper.findComp(compId) as DesignComp;
  84. currCard.layout.size[0] = this.controls.screenCtrl.getCurrScreenWidth();
  85. }
  86. const compId = await this.store.insertCompContainer(compKey, currCard);
  87. const addedComp = this.store.compMap[compId];
  88. addedComp.layout.position = "absolute";
  89. const currComp = this.helper.findComp(compId) as DesignComp;
  90. cb?.(currComp);
  91. const w = this.controls.screenCtrl.getCurrScreenWidth();
  92. //添加组件到当前选中的组件下面
  93. let xOffset = this.helper.designSizeToPx(
  94. w / 2.0 - (currComp.layout.size?.[0] || w) / 2
  95. );
  96. const obj = new CompObject(currComp);
  97. //没有选中组件添加到当前卡片最后
  98. const children = currCard.children.default || [];
  99. if (yOffset == 0 && children.length >= 2) {
  100. const prevCompIndex = children.indexOf(compId) - 1;
  101. const bound = this.helper.getCardCompBound(children[prevCompIndex]);
  102. yOffset = bound.y + bound.h;
  103. }
  104. obj.worldTransform.translate(xOffset, yOffset);
  105. currComp.layout.transformMatrix = obj.worldTransform.getMatrixStr();
  106. this.actions.pickComp(compId);
  107. this.helper.extendStreamCard(currCard.id);
  108. if (compKey == "Text") {
  109. this.actions.textFocus(compId, true);
  110. }
  111. this.controls.cropCtrl.close();
  112. },
  113. // 通过点击添加组件到画布
  114. async clickCompUserToDesign(id: string, isSys) {
  115. if (!this.store.currStreamCardId) {
  116. queenApi.messageError("请先选中一个卡片");
  117. return;
  118. }
  119. const { result } = await this.https.getCompDetail(id, isSys);
  120. //先创建卡片
  121. // const currCardIndex = this.store.streamCardIds.indexOf(this.store.currStreamCardId) + 1;
  122. // const cardId = await this.store.insertDesignContent(
  123. // "Container",
  124. // currCardIndex
  125. // );
  126. // const currCard = this.helper.findComp(cardId) as DesignComp;
  127. const currCard = this.store.currStreamCard;
  128. const comp = this.store.addUserCard(result);
  129. const compId = comp.id;
  130. const childIds = [...(currCard.children.default || [])];
  131. childIds.push(compId);
  132. currCard.children.default = childIds;
  133. this.store.setCompPid(compId, currCard.id);
  134. const addedComp = this.store.compMap[compId];
  135. addedComp.layout.position = "absolute";
  136. this.actions.initAddedCompPos(this.store.currCompId, compId, currCard.id);
  137. this.actions.pickComp(compId);
  138. },
  139. initAddedCompPos(currId: string, addedId: string, cardId: string) {
  140. let yOffset = 0;
  141. if (currId != this.store.currStreamCardId && currId != "root") {
  142. const paths = this.helper.getCompTrees(currId);
  143. const bound = this.helper.getCardCompBound(paths[2].id);
  144. yOffset = bound.y + bound.h;
  145. }
  146. const currComp = this.helper.findComp(addedId) as DesignComp;
  147. const w = this.controls.screenCtrl.getCurrScreenWidth();
  148. //添加组件到当前选中的组件下面
  149. let xOffset = this.helper.designSizeToPx(
  150. w / 2.0 - (currComp.layout.size?.[0] || w) / 2
  151. );
  152. const obj = new CompObject(currComp);
  153. const currCard = this.helper.findComp(cardId) as DesignComp;
  154. //没有选中组件添加到当前卡片最后
  155. const children = currCard.children.default || [];
  156. if (yOffset == 0 && children.length >= 2) {
  157. const prevCompIndex = children.indexOf(addedId) - 1;
  158. const bound = this.helper.getCardCompBound(children[prevCompIndex]);
  159. yOffset = bound.y + bound.h;
  160. }
  161. obj.worldTransform.translate(xOffset, yOffset);
  162. currComp.layout.transformMatrix = obj.worldTransform.getMatrixStr();
  163. this.helper.extendStreamCard(currCard.id);
  164. this.controls.cropCtrl.close();
  165. },
  166. // 通过拖拽添加组件到画布
  167. async dragCompToDesign(event: MouseEvent, compKey: string, data: any) {
  168. const currCardDom = this.store.currStreamCard.$el;
  169. if (compKey == "CompCard") {
  170. const { result } = await this.https.getCompDetail(data.id, data.isSys);
  171. const comp = this.store.addUserCard(result);
  172. const currCard = this.store.currStreamCard;
  173. const compId = comp.id;
  174. const childIds = [...(currCard.children.default || [])];
  175. childIds.push(compId);
  176. currCard.children.default = childIds;
  177. const addedComp = this.store.compMap[compId];
  178. addedComp.layout.position = "absolute";
  179. this.store.setCompPid(compId, currCard.id);
  180. this.actions.pickComp(compId);
  181. } else {
  182. await this.actions.addCompToDesign(compKey as any);
  183. }
  184. setTimeout(() => {
  185. const cardPoints = this.helper.getPointOffsetWith(event, currCardDom);
  186. const { currComp } = this.store;
  187. let selCtrl = this.controls.selectCtrl;
  188. const w = this.controls.screenCtrl.getCurrScreenWidth();
  189. selCtrl.translate(
  190. this.helper.designSizeToPx(
  191. w / 2.0 - (currComp.layout.size?.[0] || w) / 2
  192. ),
  193. cardPoints.y
  194. );
  195. this.helper.extendStreamCard(this.store.currStreamCardId);
  196. if (compKey == "Text") {
  197. this.actions.selectObjs([]);
  198. this.actions.textFocus(currComp.id, true);
  199. }
  200. this.controls.cropCtrl.close();
  201. }, 100);
  202. },
  203. async selectObjs(ids: string[], last = "") {
  204. this.store.selected = ids;
  205. this.store.selectId = ids.length > 1 ? Date.now() + "" : "";
  206. this.store.lastSelected = last
  207. ? last
  208. : ids.length > 0
  209. ? ids[ids.length - 1]
  210. : "";
  211. },
  212. // 添加组件到画布
  213. async addCompToDesign(compKey: ICompKeys, index?: number) {
  214. if (!this.store.currStreamCardId) {
  215. //必须选中一个streamCard
  216. return;
  217. }
  218. if (compKey == "Container") {
  219. // index = this.store.streamCardIds.indexOf(this.store.currStreamCardId) + 1;
  220. const compId = await this.store.insertDesignContent(compKey, index);
  221. this.actions.pickComp(compId);
  222. const c = this.helper.findComp(compId) as DesignComp;
  223. c.layout.size[0] = this.controls.screenCtrl.getCurrScreenWidth();
  224. c.layout.size[1] = this.controls.screenCtrl.getCurrScreenHeight();
  225. this.helper.extendStreamCard(compId);
  226. this.actions.selectObjs([]);
  227. return;
  228. }
  229. const compId = await this.store.insertCompContainer(
  230. compKey,
  231. this.store.currStreamCard
  232. );
  233. const addedComp = this.store.compMap[compId];
  234. this.actions.setCompPositionFloat(addedComp);
  235. this.actions.pickComp(compId);
  236. },
  237. // 切换当前组件
  238. // pickComp(compId: string) {
  239. // const { store, helper } = this;
  240. // // 组合模式下,切换组件
  241. // // if (store.currCompId && store.groupModeStatus) {
  242. // // const enableGroupIds = helper
  243. // // .findParentComp(compId)
  244. // // ?.getChildIds() as string[];
  245. // // const comps = helper.getCompTrees(compId);
  246. // // while (comps.length) {
  247. // // const comp = comps.pop() as DesignComp;
  248. // // const index = store.groupIds.indexOf(comp.id);
  249. // // if (index >= 0) {
  250. // // const groupIds = [...store.groupIds];
  251. // // groupIds.splice(index, 1);
  252. // // store.setGroupIds(groupIds);
  253. // // } else if (enableGroupIds.includes(comp.id)) {
  254. // // store.groupIds.push(comp.id);
  255. // // return;
  256. // // }
  257. // // }
  258. // // return;
  259. // // }
  260. // // let nextCompId = compId;
  261. // // if (this.store.isEditPage) {
  262. // // const comps = this.helper.getCompTrees(compId);
  263. // // nextCompId = comps[1].id;
  264. // // }
  265. // if (this.store.currCompId == compId) {
  266. // return;
  267. // }
  268. // if (this.store.currComp.compKey == "Text") {
  269. // this.store.setTextEditingState(false);
  270. // }
  271. // this.store.setCurrComp(compId);
  272. // if (this.store.currCompId == this.store.currStreamCardId) {
  273. // this.controls.transferCtrl.destroy();
  274. // }
  275. // },
  276. // 切换到父组件
  277. pickParentComp(compId: string) {
  278. const parentComp = this.helper.findParentComp(compId);
  279. parentComp && this.store.setCurrComp(parentComp.id);
  280. },
  281. ctrlc() {
  282. ctrlState.selected = [];
  283. const children = this.store.currStreamCard.children.default || [];
  284. children.forEach((c) => {
  285. if (this.store.selected.indexOf(c) > -1) {
  286. ctrlState.selected.push(c);
  287. }
  288. });
  289. ctrlState.screenId = this.controls.screenCtrl.currScreenId;
  290. ctrlState.cardId = this.store.currStreamCardId;
  291. ctrlState.type = 1;
  292. const objc = this.controls.selectCtrl.objContainer as ObjsContainer;
  293. ctrlState.selWidth = this.helper.pxToDesignSize(objc.width);
  294. objc.setPivot(0);
  295. const currX = objc.parent.x,
  296. currY = objc.parent.y;
  297. ctrlState.selX = currX;
  298. ctrlState.selY = currY;
  299. objc.setPivot(4);
  300. },
  301. copyLastSelected() {
  302. if (this.store.currCompId && this.store.currCompId != "root") {
  303. ctrlState.selected = [this.store.currCompId];
  304. ctrlState.cardId = this.store.currStreamCardId;
  305. ctrlState.type = 1;
  306. }
  307. },
  308. setSameSize(isWidth: boolean) {
  309. const selectCtrl = this.controls.selectCtrl;
  310. const objc = selectCtrl.objContainer as ObjsContainer;
  311. const w = this.controls.screenCtrl.getCurrScreenWidth();
  312. if (this.store.selected.length == 1) {
  313. if (isWidth) this.store.currComp.layout.size[0] = w;
  314. else
  315. this.store.currComp.layout.size[1] =
  316. this.store.currStreamCard.layout.size[1];
  317. objc.updateSize();
  318. selectCtrl.upgateGizmoStyle();
  319. return;
  320. }
  321. const anchorBox = this.helper.findComp(this.store.lastSelected);
  322. if (!anchorBox) return;
  323. objc.parent.children.forEach((c) => {
  324. const child = c as CompObject;
  325. const r = child.getBox();
  326. if (isWidth) child.comp.layout.size[0] = anchorBox.layout.size[0];
  327. else child.comp.layout.size[1] = anchorBox.layout.size[1];
  328. });
  329. objc.updateSize();
  330. selectCtrl.upgateGizmoStyle();
  331. },
  332. toogleGroup() {
  333. if (this.store.selected.length > 1) {
  334. this.actions.createGroupComps();
  335. return;
  336. }
  337. if (this.store.selected.length == 1) {
  338. const c = this.helper.findComp(this.store.selected[0]) as DesignComp;
  339. if (c.compKey == "Group") {
  340. this.actions.cancelGroupComps(c);
  341. }
  342. }
  343. },
  344. ctrlx() {
  345. //console.log("ctrlv ", this.store.selected);
  346. //console.log("ctrlv ", this.store.selected);
  347. //保持图层顺序
  348. ctrlState.selected = [];
  349. const children = this.store.currStreamCard.children.default || [];
  350. children.forEach((c) => {
  351. if (this.store.selected.indexOf(c) > -1) {
  352. ctrlState.selected.push(c);
  353. }
  354. });
  355. ctrlState.cardId = this.store.currStreamCardId;
  356. ctrlState.type = 2;
  357. ctrlState.screenId = this.controls.screenCtrl.currScreenId;
  358. const objc = this.controls.selectCtrl.objContainer as ObjsContainer;
  359. ctrlState.selWidth = this.helper.pxToDesignSize(objc.width);
  360. objc.setPivot(0);
  361. const currX = objc.parent.x,
  362. currY = objc.parent.y;
  363. ctrlState.selX = currX;
  364. ctrlState.selY = currY;
  365. objc.setPivot(4);
  366. },
  367. ctrlv() {
  368. if (ctrlState.selected.length < 1) return;
  369. const news: string[] = [];
  370. const deepCopy = (c: DesignComp) => {
  371. const childs = c.children.default || [];
  372. if (childs.length > 0) {
  373. childs.forEach((id, index) => {
  374. const cp = this.helper.findComp(id) as DesignComp;
  375. const cp1 = cloneDeep(cp);
  376. cp1.id = nanoid();
  377. this.store.compMap[cp1.id] = cp1;
  378. this.store.setCompPid(cp1.id, c.id);
  379. childs[index] = cp1.id;
  380. deepCopy(cp);
  381. });
  382. }
  383. };
  384. ctrlState.selected.forEach((c) => {
  385. const cp = this.helper.findComp(c) as DesignComp;
  386. const cp1 = cloneDeep(cp);
  387. cp1.id = nanoid();
  388. news.push(cp1.id);
  389. this.store.compMap[cp1.id] = cp1;
  390. this.store.setCompPid(cp1.id, this.store.currStreamCardId);
  391. deepCopy(cp);
  392. });
  393. this.actions.addComps(news);
  394. this.actions.selectObjs(news);
  395. setTimeout(() => {
  396. if (ctrlState.type == 2) {
  397. //剪辑删除原来的组件
  398. const card = this.helper.findComp(ctrlState.cardId) as DesignComp;
  399. const childs = card.children.default?.slice(0) || [];
  400. ctrlState.selected.forEach((s) => {
  401. let i = childs.indexOf(s);
  402. if (i != -1) {
  403. childs.splice(i, 1);
  404. }
  405. });
  406. card.children.default = childs;
  407. this.helper.extendStreamCard(ctrlState.cardId);
  408. ctrlState.selected = [];
  409. }
  410. //获取当前选中的内容
  411. if (ctrlState.cardId != this.store.currStreamCardId) {
  412. //跨卡片拷贝
  413. let pox = this.helper.getCardNextPosY(
  414. this.store.currStreamCardId,
  415. ctrlState.selWidth
  416. );
  417. this.controls.selectCtrl.translate(0, pox.y - ctrlState.selY);
  418. return;
  419. }
  420. if (ctrlState.screenId == this.controls.screenCtrl.currScreenId) {
  421. this.controls.selectCtrl.translate(20, 20);
  422. }
  423. }, 100);
  424. },
  425. addComps(ids: string[]) {
  426. const childs = this.store.currStreamCard.children.default?.slice(0) || [];
  427. childs.push(...ids);
  428. this.store.currStreamCard.children.default = childs;
  429. },
  430. ctrlAndA() {
  431. const childrens = (this.store.currStreamCard.children.default || []).slice(
  432. 0
  433. );
  434. this.actions.selectObjs(childrens);
  435. // this.controls.selectCtrl.
  436. //objc.updateSize();
  437. //selectCtrl.upgateGizmoStyle();
  438. },
  439. // 删除组件
  440. removeSelectComps() {
  441. const selected = this.store.selected.slice(0);
  442. this.actions.selectObjs([]);
  443. let n = selected.length;
  444. while (n--) {
  445. this.actions.removeComp(selected[n]);
  446. }
  447. },
  448. // 删除组件
  449. removeComp(compId: string) {
  450. const paths = this.helper.getCompTrees(compId);
  451. if (!paths[2]) {
  452. if (this.helper.isStreamCard(compId)) {
  453. this.actions.removeStreamCard(compId);
  454. return;
  455. }
  456. return;
  457. }
  458. compId = paths[2].id;
  459. if (this.helper.isCompCanDelete(compId)) {
  460. if (this.helper.isStreamCard(compId)) {
  461. this.actions.removeStreamCard(compId);
  462. return;
  463. }
  464. const cardid = this.store.currStreamCardId;
  465. if (compId === this.store.currCompId) {
  466. this.store.setCurrComp(this.store.currStreamCardId);
  467. }
  468. this.store.deleteComp(compId);
  469. this.helper.extendStreamCard(cardid);
  470. this.actions.selectObjs([]);
  471. }
  472. },
  473. async removeStreamCard(compId: string) {
  474. await queenApi.showConfirm({ title: "删除", content: "确认删除当前页面?" });
  475. // if (this.store.streamCardIds.length < 2) {
  476. // queenApi.messageError("")
  477. // return;
  478. // }
  479. let nextCard = this.store.currStreamCardId;
  480. if (compId == this.store.currStreamCardId) {
  481. const i = this.store.streamCardIds.indexOf(compId);
  482. nextCard = this.store.streamCardIds[i + 1];
  483. if (!nextCard) {
  484. nextCard = this.store.streamCardIds[i - 1];
  485. }
  486. }
  487. this.controls.selectCtrl.selecteObjs([]);
  488. this.store.setCurrComp(nextCard);
  489. this.store.deleteComp(compId);
  490. },
  491. // 移动组件顺序
  492. moveComp(selIndex: number, targetIndex: number) {
  493. this.actions.selectObjs([]);
  494. if (selIndex === targetIndex) return;
  495. this.store.moveComp(selIndex, targetIndex);
  496. },
  497. // 保存容器为组件
  498. async saveAsComp(comp: DesignComp) {
  499. try {
  500. const CompSave = this.components.CompSave as any;
  501. let title = "";
  502. let type = "comp";
  503. try {
  504. const ret: any = await queenApi.dialog(<CompSave />, {
  505. width: "300px",
  506. title: "保存到我的",
  507. });
  508. if (!ret) {
  509. return;
  510. }
  511. title = ret.title;
  512. type = ret.type;
  513. } catch (error) {
  514. return;
  515. }
  516. console.log(title, type);
  517. // 组件封面
  518. const blob = await new ScreenshotCtrl().snap({
  519. element: comp.$el,
  520. });
  521. const thumbnail = URL.createObjectURL(blob);
  522. // const title = await queenApi.showInput({
  523. // title: "保存到我的",
  524. // defaultValue: this.controls.compUICtrl.state.components.get(
  525. // comp.compKey
  526. // )?.name,
  527. // });
  528. const data = {
  529. title,
  530. type,
  531. thumbnail,
  532. compMap: cloneDeep(this.store.designData.compMap),
  533. };
  534. this.helper.clearUnusedComps(data.compMap, comp.id);
  535. data.compMap.root = data.compMap[comp.id];
  536. data.compMap.root.id = "root";
  537. //清除平移距离
  538. const mtx = Matrix.createFromMatrixStr(
  539. data.compMap.root.layout.transformMatrix as string
  540. );
  541. mtx.tx = 0;
  542. mtx.ty = 0;
  543. data.compMap.root.layout.transformMatrix = mtx.getMatrixStr();
  544. delete data.compMap[comp.id];
  545. queenApi.showLoading("保存中");
  546. await this.controls.uploader.uploadBlobs(data);
  547. await this.https.createComp(data);
  548. queenApi.messageSuccess("保存成功");
  549. } catch (error: any) {
  550. throw Exception.error(error.toString());
  551. } finally {
  552. queenApi.hideLoading();
  553. }
  554. },
  555. // 保存项目
  556. async saveDesign() {
  557. try {
  558. // 清除无用组件
  559. this.helper.clearProjectUnusedComps(this.store.designData.compMap);
  560. const c = this.controls.screenCtrl;
  561. c.saveScreenPage();
  562. const root = this.helper.findRootComp() as DesignComp;
  563. root.value.useFor = c.state.screen.useFor;
  564. root.value.pageMode = c.state.screen.pageMode;
  565. root.value.pageSizeType = c.state.screen.pageSizeType;
  566. // 封面截屏
  567. if (!this.store.designData.thumbnail) {
  568. await this.actions.updateThumbnailByScreenshot();
  569. }
  570. queenApi.showLoading("保存中");
  571. await this.controls.uploader.uploadBlobs(this.store.designData);
  572. if (this.store.isWk) {
  573. await this.https[this.store.isEditPage ? "saveWkDesign" : "saveComp"](
  574. this.store.designData
  575. );
  576. } else {
  577. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  578. this.store.designData
  579. );
  580. }
  581. queenApi.messageSuccess("保存成功");
  582. } catch (error: any) {
  583. throw Exception.error(error.toString());
  584. } finally {
  585. queenApi.hideLoading();
  586. }
  587. },
  588. // 截屏保存封面
  589. async updateThumbnailByScreenshot(autoSave?: boolean) {
  590. try {
  591. const rootComp = this.helper.findRootComp();
  592. if (!rootComp) return;
  593. queenApi.showLoading("截屏中");
  594. const blob = await new ScreenshotCtrl().snap({
  595. element: rootComp.$el,
  596. ratio: this.store.isEditComp ? 0 : 1,
  597. });
  598. const thumbnail = URL.createObjectURL(blob);
  599. this.store.setDesignThumbnail(thumbnail);
  600. if (autoSave) {
  601. await this.controls.uploader.uploadBlobs(this.store.designData);
  602. await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
  603. pick(this.store.designData, ["_id", "thumbnail"])
  604. );
  605. queenApi.messageSuccess("保存成功");
  606. }
  607. } catch (error: any) {
  608. throw Exception.error(error.toString());
  609. } finally {
  610. queenApi.hideLoading();
  611. }
  612. },
  613. // 设置组件变换
  614. setCompTransform(comp: DesignComp, transform: Layout["transform"]) {
  615. if (!comp) return;
  616. comp.layout.transform = transform;
  617. console.log(comp);
  618. },
  619. // 设置组件变换
  620. setCompTransformMatrix(comp: DesignComp, transformMatrix: string) {
  621. if (!comp) return;
  622. comp.layout.transformMatrix = transformMatrix;
  623. },
  624. // 设置组件浮动
  625. setCompPositionFloat(comp: DesignComp) {
  626. comp.layout.position = "absolute";
  627. },
  628. // 设置组件浮动
  629. setCompPosition(comp: DesignComp) {
  630. comp.layout.position =
  631. comp.layout.position === "absolute" ? undefined : "absolute";
  632. },
  633. // 设置组件显示隐藏
  634. setCompVisible(comp: DesignComp) {
  635. comp.layout.visible = comp.layout.visible === false ? true : false;
  636. },
  637. // 组件重命名
  638. async resetCompName(comp: DesignComp) {
  639. const res = await queenApi.showInput({
  640. title: "重命名",
  641. defaultValue: comp.title,
  642. });
  643. if (!res) return;
  644. comp.title = res;
  645. },
  646. // 清除组件变换
  647. clearCompTransform(comp: DesignComp) {
  648. comp.layout.margin = "";
  649. comp.layout.transform = undefined;
  650. },
  651. // 设置组件锁定状态
  652. setCompLock(comp: DesignComp) {
  653. comp.layout.locked = !comp.layout.locked;
  654. },
  655. // 设置组件层级
  656. setCompLayer(comp: DesignComp, offset: number) {
  657. const cards = this.store.currStreamCard.children.default || [];
  658. const i = cards.indexOf(comp.id);
  659. if (i == -1) return;
  660. if (offset < 0) {
  661. //向下移动
  662. if (i == 0) return;
  663. const temp = cards[i - 1];
  664. cards[i - 1] = comp.id;
  665. cards[i] = temp;
  666. return;
  667. }
  668. if (i == cards.length - 1) return;
  669. const temp = cards[i + 1];
  670. cards[i + 1] = comp.id;
  671. cards[i] = temp;
  672. // comp.layout.zIndex = Math.min(
  673. // Math.max((comp.layout.zIndex || 0) + offset, 0),
  674. // 99
  675. // );
  676. },
  677. //横向对齐
  678. setAlignX(flag: 0 | 1 | 2 | 3, isGroup = false) {
  679. console.log("alignX");
  680. const selectCtrl = this.controls.selectCtrl;
  681. const w = this.helper.designSizeToPx(
  682. this.controls.screenCtrl.getCurrScreenWidth()
  683. );
  684. if (this.store.selected.length == 1 || isGroup) {
  685. const objc = selectCtrl.objContainer as ObjsContainer;
  686. const box = objc.getBound();
  687. switch (flag) {
  688. case 0:
  689. selectCtrl.translate(-box.left, 0);
  690. break;
  691. case 1:
  692. selectCtrl.translate(-(box.center.x - w / 2.0), 0);
  693. break;
  694. case 2:
  695. selectCtrl.translate(w - box.right, 0);
  696. break;
  697. }
  698. return;
  699. }
  700. //多选元素对齐模式
  701. const anchorBox = this.helper.findComp(this.store.lastSelected);
  702. if (!anchorBox) return;
  703. const anchor = new CompObject(anchorBox);
  704. const anchorRect = anchor.getBox();
  705. const objc = selectCtrl.objContainer as ObjsContainer;
  706. let min = 10000,
  707. max = -10000;
  708. let step = 0;
  709. if (flag == 3) {
  710. //Y轴均匀排布
  711. objc.parent.children.forEach((c) => {
  712. const child = c as CompObject;
  713. const r = child.getBox();
  714. const x = r.x + r.w / 2.0;
  715. if (x < min) min = x;
  716. if (x > max) max = x;
  717. });
  718. const stepCount = objc.parent.children.length;
  719. step = (max - min) / (stepCount - 1);
  720. const stepIndexMap: any = {};
  721. objc.parent.children.forEach((c) => {
  722. const child = c as CompObject;
  723. const r = child.getBox();
  724. const x = r.x + r.w / 2.0;
  725. let minIndex = -1;
  726. let minV = 10000;
  727. for (let i = 0; i < stepCount; i++) {
  728. const ty = i * step + min;
  729. if (Math.abs(x - ty) < minV && !stepIndexMap[i]) {
  730. minV = Math.abs(x - ty);
  731. minIndex = i;
  732. }
  733. }
  734. stepIndexMap[minIndex] = true;
  735. child.translate(minIndex * step + min - x, 0);
  736. });
  737. objc.updateSize();
  738. selectCtrl.upgateGizmoStyle();
  739. return;
  740. }
  741. objc.parent.children.forEach((c) => {
  742. const child = c as CompObject;
  743. if (child.comp.id == this.store.lastSelected) return;
  744. const r = child.getBox();
  745. switch (flag) {
  746. case 0:
  747. child.translate(anchorRect.x - r.x, 0);
  748. break;
  749. case 1:
  750. child.translate(
  751. anchorRect.x + anchorRect.w / 2.0 - (r.x + r.w / 2.0),
  752. 0
  753. );
  754. break;
  755. case 2:
  756. child.translate(anchorRect.x + anchorRect.w - (r.x + r.w), 0);
  757. break;
  758. }
  759. });
  760. objc.updateSize();
  761. selectCtrl.upgateGizmoStyle();
  762. },
  763. setAlignY(flag: 0 | 1 | 2 | 3, isGroup = false) {
  764. const selectCtrl = this.controls.selectCtrl;
  765. if (this.store.selected.length == 1 || isGroup) {
  766. const objc = selectCtrl.objContainer as ObjsContainer;
  767. const box = objc.getBound();
  768. const card = new CompObject(this.store.currStreamCard);
  769. const cardBox = card.getBox();
  770. switch (flag) {
  771. case 0:
  772. selectCtrl.translate(0, -box.top);
  773. break;
  774. case 1:
  775. selectCtrl.translate(0, -(box.center.y - cardBox.h / 2.0));
  776. break;
  777. case 2:
  778. selectCtrl.translate(0, cardBox.h - box.bottom);
  779. break;
  780. }
  781. return;
  782. }
  783. const anchorBox = this.helper.findComp(this.store.lastSelected);
  784. if (!anchorBox) return;
  785. const anchor = new CompObject(anchorBox);
  786. const anchorRect = anchor.getBox();
  787. const objc = selectCtrl.objContainer as ObjsContainer;
  788. let min = 10000;
  789. let max = -10000;
  790. let step = 0;
  791. if (flag == 3) {
  792. //Y轴均匀排布
  793. objc.parent.children.forEach((c) => {
  794. const child = c as CompObject;
  795. const r = child.getBox();
  796. const y = r.y + r.h / 2.0;
  797. if (y < min) min = y;
  798. if (y > max) max = y;
  799. });
  800. const stepCount = objc.parent.children.length;
  801. step = (max - min) / (stepCount - 1);
  802. const stepIndexMap: any = {};
  803. objc.parent.children.forEach((c) => {
  804. const child = c as CompObject;
  805. const r = child.getBox();
  806. const y = r.y + r.h / 2.0;
  807. let minIndex = -1;
  808. let minV = 10000;
  809. for (let i = 0; i < stepCount; i++) {
  810. const ty = i * step + min;
  811. if (Math.abs(y - ty) < minV && !stepIndexMap[i]) {
  812. minV = Math.abs(y - ty);
  813. minIndex = i;
  814. }
  815. }
  816. stepIndexMap[minIndex] = true;
  817. child.translate(0, minIndex * step + min - y);
  818. });
  819. objc.updateSize();
  820. selectCtrl.upgateGizmoStyle();
  821. return;
  822. }
  823. objc.parent.children.forEach((c) => {
  824. const child = c as CompObject;
  825. if (child.comp.id == this.store.lastSelected) return;
  826. const r = child.getBox();
  827. switch (flag) {
  828. case 0:
  829. child.translate(0, anchorRect.y - r.y);
  830. break;
  831. case 1:
  832. child.translate(
  833. 0,
  834. anchorRect.y + anchorRect.h / 2.0 - (r.y + r.h / 2.0)
  835. );
  836. break;
  837. case 2:
  838. child.translate(0, anchorRect.y + anchorRect.h - (r.y + r.h));
  839. break;
  840. }
  841. });
  842. objc.updateSize();
  843. selectCtrl.upgateGizmoStyle();
  844. },
  845. // 宽度铺满
  846. fullCompWidth(comp: DesignComp) {
  847. comp.layout.size[0] = this.controls.screenCtrl.getCurrScreenWidth();
  848. },
  849. //
  850. setCompAlign(comp: DesignComp, align: string) {
  851. comp.layout.alignSelf = align;
  852. if (comp.layout.transform?.x) {
  853. comp.layout.transform.x = 0;
  854. }
  855. },
  856. // 开启组合模式
  857. enableGroupMode() {
  858. this.store.setGroupIds(
  859. this.store.currCompId ? [this.store.currCompId] : []
  860. );
  861. this.store.setGroupMode(true);
  862. },
  863. // 关闭组合模式
  864. async disableGroupMode() {
  865. const groupId = await this.controls.transferCtrl.groupCtrl.combineGroup();
  866. if (groupId) {
  867. this.store.setCurrComp(groupId);
  868. }
  869. this.store.setGroupIds([]);
  870. this.store.setGroupMode(false);
  871. },
  872. // 取消打组
  873. cancelGroupComps(groupComp: DesignComp) {
  874. const childs = groupComp.children.default || [];
  875. const objC = this.controls.selectCtrl.objContainer as ObjsContainer;
  876. const parentMtrx = objC.parent.worldTransform.clone();
  877. childs.forEach((o) => {
  878. const obj = this.helper.findComp(o) as DesignComp;
  879. const mxt = Matrix.createFromMatrixStr(
  880. obj.layout.transformMatrix as string
  881. );
  882. mxt.prepend(parentMtrx);
  883. obj.layout.transformMatrix = mxt.getMatrixStr();
  884. });
  885. const paths = this.helper.getCompTrees(groupComp.id);
  886. const card = paths[1];
  887. const parentChilds = (card.children.default || []).filter(
  888. (item) => item != groupComp.id
  889. );
  890. parentChilds.push(...childs);
  891. card.children.default = parentChilds;
  892. this.actions.selectObjs([childs[0]]);
  893. },
  894. createGroupComps() {
  895. const selectCtrl = this.controls.selectCtrl;
  896. const Objc = this.controls.selectCtrl.objContainer as ObjsContainer;
  897. const id = this.controls.compUICtrl.createCompId("Group");
  898. const comp = this.helper.findComp(id) as DesignComp;
  899. comp.layout.position = "absolute";
  900. //先删除现有的节点
  901. const sels = this.store.selected.slice(0);
  902. const chils = this.store.currStreamCard.children.default || [];
  903. const newChilds: any = [];
  904. const groupChilds: any = [];
  905. chils.forEach((c) => {
  906. if (sels.indexOf(c) == -1) newChilds.push(c);
  907. else {
  908. groupChilds.push(c);
  909. }
  910. });
  911. newChilds.push(id);
  912. this.store.currStreamCard.children.default = newChilds;
  913. //更新节点的新位置
  914. Objc.parent.children.forEach((obj) => {
  915. const cobj = obj as CompObject;
  916. const comp = cobj.comp;
  917. comp.layout.transformMatrix = cobj.localTransform.getMatrixStr();
  918. });
  919. //再添加新的节点
  920. comp.layout.size = [
  921. this.helper.pxToDesignSize(Objc.width),
  922. this.helper.pxToDesignSize(Objc.height),
  923. ];
  924. comp.layout.transformMatrix = selectCtrl.transferStyle.matrix;
  925. comp.children.default = groupChilds;
  926. this.actions.selectObjs([]);
  927. setTimeout(() => {
  928. this.actions.pickComp(comp.id);
  929. }, 0);
  930. },
  931. handleSelectMoving(key: string) {
  932. if (this.store.selected.length < 1) return;
  933. let x = 0,
  934. y = 0;
  935. switch (key) {
  936. case "left":
  937. x = -1;
  938. break;
  939. case "right":
  940. x = 1;
  941. break;
  942. case "up":
  943. y = -1;
  944. break;
  945. case "down":
  946. y = 1;
  947. break;
  948. }
  949. this.controls.selectCtrl.translate(x * 0.5, y * 0.5);
  950. this.controls.selectCtrl.assistCtrl?.flashDrawCardDists();
  951. },
  952. // 点击模板
  953. async clickTplToDesign(record) {
  954. const res = await queenApi.showConfirm({
  955. title: "",
  956. content: "要替换正在编辑的内容?",
  957. });
  958. if (!res) return;
  959. const frameData = await this.https.getDesignDetail(record._id, {
  960. isSys: true,
  961. });
  962. const { compMap, content, desc, thumbnail, title } = frameData.result;
  963. const designData = {
  964. ...this.store.designData,
  965. compMap,
  966. content,
  967. desc,
  968. thumbnail,
  969. title,
  970. };
  971. this.actions.selectObjs([]);
  972. this.store.setCurrComp("root");
  973. this.store.setDesignData(designData);
  974. this.store.currStreamCardId = this.store.streamCardIds[0];
  975. },
  976. });