edit.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Exception, queenApi } from "queenjs";
  2. import { EditorModule } from "..";
  3. import { ICompKeys } from "../typings";
  4. export const editActions = EditorModule.action({
  5. // 添加组件到画布
  6. addCompToDesign(compKey: ICompKeys, index?: number) {
  7. let compId: string;
  8. if (index === undefined && this.store.currComp?.compKey === "Container") {
  9. compId = this.store.insertCompContainer(compKey, this.store.currComp);
  10. } else {
  11. compId = this.store.insertDesignContent(compKey, index);
  12. }
  13. this.actions.pickComp(compId);
  14. },
  15. // 切换当前组件
  16. pickComp(compId: string) {
  17. this.store.setCurrComp(compId);
  18. },
  19. // 切换到父组件
  20. pickParentComp(compId: string) {
  21. const parentComp = this.helper.findParentComp(compId);
  22. parentComp && this.store.setCurrComp(parentComp.id);
  23. },
  24. // 删除组件
  25. removeComp(compId: string) {
  26. if (compId === this.store.currCompId) {
  27. this.store.setCurrComp("");
  28. }
  29. this.store.deleteComp(compId);
  30. },
  31. // 移动组件
  32. moveComp(selIndex: number, targetIndex: number) {
  33. if (selIndex === targetIndex) return;
  34. this.store.moveComp(selIndex, targetIndex);
  35. },
  36. // 保存项目
  37. async saveDesign() {
  38. try {
  39. queenApi.showLoading("保存中");
  40. this.store.clearUnusedComps();
  41. await this.https.saveDesign(this.store.designData);
  42. queenApi.messageSuccess("保存成功");
  43. } catch (error: any) {
  44. throw Exception.error(error.toString());
  45. } finally {
  46. queenApi.hideLoading();
  47. }
  48. },
  49. });