edit.tsx 32 KB

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