edit.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Exception, queenApi } from "queenjs";
  2. import { EditorModule } from "..";
  3. import { ICompKeys } from "../../typings";
  4. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  5. export const editActions = EditorModule.action({
  6. // 添加组件到画布
  7. addCompToDesign(compKey: ICompKeys, index?: number) {
  8. let compId: string;
  9. if (index === undefined && this.store.currComp?.compKey === "Container") {
  10. compId = this.store.insertCompContainer(compKey, this.store.currComp);
  11. } else {
  12. compId = this.store.insertDesignContent(compKey, index);
  13. }
  14. this.actions.pickComp(compId);
  15. },
  16. // 切换当前组件
  17. pickComp(compId: string) {
  18. this.store.setCurrComp(compId);
  19. },
  20. // 切换到父组件
  21. pickParentComp(compId: string) {
  22. const parentComp = this.helper.findParentComp(compId);
  23. parentComp && this.store.setCurrComp(parentComp.id);
  24. },
  25. // 删除组件
  26. removeComp(compId: string) {
  27. if (compId === this.store.currCompId) {
  28. this.store.setCurrComp("");
  29. }
  30. this.store.deleteComp(compId);
  31. },
  32. // 移动组件
  33. moveComp(selIndex: number, targetIndex: number) {
  34. if (selIndex === targetIndex) return;
  35. this.store.moveComp(selIndex, targetIndex);
  36. },
  37. // 保存项目
  38. async saveDesign() {
  39. try {
  40. queenApi.showLoading("保存中");
  41. this.store.clearUnusedComps();
  42. await this.https.saveDesign(this.store.designData);
  43. queenApi.messageSuccess("保存成功");
  44. } catch (error: any) {
  45. throw Exception.error(error.toString());
  46. } finally {
  47. queenApi.hideLoading();
  48. }
  49. },
  50. // 设置组件显示隐藏
  51. setCompPosition(comp: DesignComp) {
  52. comp.layout.position =
  53. comp.layout.position === "absolute" ? undefined : "absolute";
  54. },
  55. // 设置组件显示隐藏
  56. setCompVisible(comp: DesignComp) {
  57. comp.layout.visible = comp.layout.visible === false ? true : false;
  58. },
  59. // 清除组件偏移
  60. clearCompOffset(comp: DesignComp) {
  61. comp.layout.margin = "";
  62. comp.layout.offset = {};
  63. },
  64. });