edit.tsx 33 KB

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